Skip to content

Commit f8d674e

Browse files
committed
feat: refactor DevtronLicenseCard component and introduce types for license management
1 parent 65c4d01 commit f8d674e

File tree

6 files changed

+61
-60
lines changed

6 files changed

+61
-60
lines changed

src/Shared/Components/DevtronLicenseCard/DevtronLicenseCard.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { ClipboardButton, getTTLInHumanReadableFormat } from '@Common/index'
22
import { ReactComponent as ICChatSupport } from '@IconsV2/ic-chat-circle-dots.svg'
3-
import { DevtronLicenseCardProps, ENTERPRISE_SUPPORT_LINK, LicenseStatus } from '@Shared/index'
3+
import { ENTERPRISE_SUPPORT_LINK } from '@Shared/index'
44
import { Button, ButtonVariantType } from '../Button'
55
import { Icon } from '../Icon'
66
import { getLicenseColorsAccordingToStatus } from './utils'
7+
import { DevtronLicenseCardProps, LicenseStatus } from './types'
78
import './licenseCard.scss'
89

910
export const DevtronLicenseCard = ({
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
export { default as DevtronLicenseCard } from './DevtronLicenseCard'
2+
export { parseDevtronLicenseDTOIntoLicenseCardData } from './utils'
3+
export * from './types'
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export enum LicenseStatus {
2+
ACTIVE = 'ACTIVE',
3+
EXPIRED = 'EXPIRED',
4+
REMINDER_THRESHOLD_REACHED = 'REMINDER_THRESHOLD_REACHED',
5+
}
6+
7+
export type DevtronLicenseCardProps = {
8+
enterpriseName: string
9+
expiryDate: string
10+
ttl: number
11+
licenseStatus: LicenseStatus
12+
isTrial: boolean
13+
} & (
14+
| {
15+
licenseKey: string
16+
licenseSuffix?: never
17+
}
18+
| {
19+
licenseKey?: never
20+
licenseSuffix: string
21+
}
22+
)

src/Shared/Components/DevtronLicenseCard/utils.tsx

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { LicenseStatus } from '@Shared/index'
1+
import moment from 'moment'
2+
import { DATE_TIME_FORMATS } from '@Common/Constants'
3+
import { DevtronLicenseCardProps, DevtronLicenseDTO, LicenseStatus } from '@Shared/index'
24

35
export const getLicenseColorsAccordingToStatus = (
46
licenseStatus: LicenseStatus,
@@ -12,3 +14,35 @@ export const getLicenseColorsAccordingToStatus = (
1214
return { bgColor: 'var(--R100)', textColor: 'var(--R500)' }
1315
}
1416
}
17+
18+
const getDevtronLicenseStatus = ({
19+
ttl,
20+
reminderThreshold,
21+
}: Pick<DevtronLicenseDTO, 'ttl' | 'reminderThreshold'>): LicenseStatus => {
22+
if (ttl < 0) {
23+
return LicenseStatus.EXPIRED
24+
}
25+
26+
if (ttl < reminderThreshold * 24 * 60 * 60) {
27+
return LicenseStatus.REMINDER_THRESHOLD_REACHED
28+
}
29+
30+
return LicenseStatus.ACTIVE
31+
}
32+
33+
export const parseDevtronLicenseDTOIntoLicenseCardData = <isCentralDashboard extends boolean = false>(
34+
licenseDTO: DevtronLicenseDTO<isCentralDashboard>,
35+
currentUserEmail?: isCentralDashboard extends true ? string : never,
36+
): DevtronLicenseCardProps => {
37+
const { isTrial, expiry, ttl, reminderThreshold, organisationMetadata, license, claimedByUserDetails } =
38+
licenseDTO || {}
39+
40+
return {
41+
enterpriseName: organisationMetadata?.name || '',
42+
expiryDate: expiry ? moment(expiry).format(DATE_TIME_FORMATS['DD/MM/YYYY']) : '',
43+
ttl,
44+
licenseStatus: getDevtronLicenseStatus({ ttl, reminderThreshold }),
45+
isTrial,
46+
...(currentUserEmail === claimedByUserDetails?.email ? { licenseKey: license } : { licenseSuffix: license }),
47+
}
48+
}

src/Shared/Helpers.tsx

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,9 @@ import {
5151
import {
5252
AggregationKeys,
5353
BorderConfigType,
54-
DevtronLicenseCardProps,
55-
DevtronLicenseDTO,
5654
GitTriggers,
5755
IntersectionChangeHandler,
5856
IntersectionOptions,
59-
LicenseStatus,
6057
Nodes,
6158
PreventOutsideFocusProps,
6259
TargetPlatformItemDTO,
@@ -1070,38 +1067,6 @@ export const deriveBorderRadiusAndBorderClassFromConfig = ({
10701067
export const getClassNameForStickyHeaderWithShadow = (isStuck: boolean, topClassName = 'dc__top-0') =>
10711068
`dc__position-sticky ${topClassName} dc__transition--box-shadow ${isStuck ? 'dc__box-shadow--header' : ''}`
10721069

1073-
const getDevtronLicenseStatus = ({
1074-
ttl,
1075-
reminderThreshold,
1076-
}: Pick<DevtronLicenseDTO, 'ttl' | 'reminderThreshold'>): LicenseStatus => {
1077-
if (ttl < 0) {
1078-
return LicenseStatus.EXPIRED
1079-
}
1080-
1081-
if (ttl < reminderThreshold * 24 * 60 * 60) {
1082-
return LicenseStatus.REMINDER_THRESHOLD_REACHED
1083-
}
1084-
1085-
return LicenseStatus.ACTIVE
1086-
}
1087-
1088-
export const parseDevtronLicenseDTOIntoLicenseCardData = <isCentralDashboard extends boolean = false>(
1089-
licenseDTO: DevtronLicenseDTO<isCentralDashboard>,
1090-
currentUserEmail?: isCentralDashboard extends true ? string : never,
1091-
): DevtronLicenseCardProps => {
1092-
const { isTrial, expiry, ttl, reminderThreshold, organisationMetadata, license, claimedByUserDetails } =
1093-
licenseDTO || {}
1094-
1095-
return {
1096-
enterpriseName: organisationMetadata?.name || '',
1097-
expiryDate: expiry ? moment(expiry).format(DATE_TIME_FORMATS['DD/MM/YYYY']) : '',
1098-
ttl,
1099-
licenseStatus: getDevtronLicenseStatus({ ttl, reminderThreshold }),
1100-
isTrial,
1101-
...(currentUserEmail === claimedByUserDetails?.email ? { licenseKey: license } : { licenseSuffix: license }),
1102-
}
1103-
}
1104-
11051070
export const clearCookieOnLogout = () => {
11061071
document.cookie = `${TOKEN_COOKIE_NAME}=; expires=Thu, 01-Jan-1970 00:00:01 GMT;path=/`
11071072
}

src/Shared/types.ts

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,26 +1070,3 @@ export type DevtronLicenseDTO<isCentralDashboard extends boolean = false> = Devt
10701070
})
10711071

10721072
export type CountryISO2Type = ParsedCountry['iso2']
1073-
1074-
export enum LicenseStatus {
1075-
ACTIVE = 'ACTIVE',
1076-
EXPIRED = 'EXPIRED',
1077-
REMINDER_THRESHOLD_REACHED = 'REMINDER_THRESHOLD_REACHED',
1078-
}
1079-
1080-
export type DevtronLicenseCardProps = {
1081-
enterpriseName: string
1082-
expiryDate: string
1083-
ttl: number
1084-
licenseStatus: LicenseStatus
1085-
isTrial: boolean
1086-
} & (
1087-
| {
1088-
licenseKey: string
1089-
licenseSuffix?: never
1090-
}
1091-
| {
1092-
licenseKey?: never
1093-
licenseSuffix: string
1094-
}
1095-
)

0 commit comments

Comments
 (0)