-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathutils.tsx
More file actions
125 lines (111 loc) · 4.57 KB
/
utils.tsx
File metadata and controls
125 lines (111 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* Copyright (c) 2024. Devtron Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import moment from 'moment'
import { DATE_TIME_FORMATS } from '@Common/Constants'
import { getUrlWithSearchParams } from '@Common/index'
import { DevtronLicenseDTO, LicensingErrorCodes } from '@Shared/types'
import { ALLOWED_CLUSTER_IN_FREEMIUM } from './constants'
import { DevtronLicenseCardProps, DevtronLicenseInfo, LicenseStatus } from './types'
export const getLicenseColorsAccordingToStatus = ({
isFreemium,
licenseStatus,
licenseStatusError,
isSaasInstance,
}: Pick<DevtronLicenseCardProps, 'licenseStatus' | 'isFreemium' | 'licenseStatusError' | 'isSaasInstance'>): {
bgColor: string
textColor: string
} => {
if (isFreemium) {
const freemiumLimitReached = licenseStatusError?.code === LicensingErrorCodes.ClusterLimitExceeded
if (freemiumLimitReached) {
return { bgColor: 'var(--R100)', textColor: 'var(--R500)' }
}
if (!isSaasInstance) {
return { bgColor: 'var(--G100)', textColor: 'var(--G500)' }
}
}
switch (licenseStatus) {
case LicenseStatus.ACTIVE:
return { bgColor: 'var(--G100)', textColor: 'var(--G500)' }
case LicenseStatus.REMINDER_THRESHOLD_REACHED:
return { bgColor: 'var(--Y100)', textColor: 'var(--Y700)' }
default:
return { bgColor: 'var(--R100)', textColor: 'var(--R500)' }
}
}
const getDevtronLicenseStatus = ({
ttl,
reminderThreshold,
}: Pick<DevtronLicenseDTO, 'ttl' | 'reminderThreshold'>): LicenseStatus => {
if (ttl <= 0) {
return LicenseStatus.EXPIRED
}
if (ttl < reminderThreshold * 24 * 60 * 60) {
return LicenseStatus.REMINDER_THRESHOLD_REACHED
}
return LicenseStatus.ACTIVE
}
export const parseDevtronLicenseDTOIntoLicenseCardData = <isCentralDashboard extends boolean = false>(
licenseDTO: DevtronLicenseDTO<isCentralDashboard>,
currentUserEmail?: isCentralDashboard extends true ? string : never,
): Omit<DevtronLicenseCardProps, 'appTheme'> => {
const {
isTrial,
expiry: onPremExpiry,
ttl: onPremTTL,
reminderThreshold,
organisationMetadata,
license,
claimedByUserDetails,
isFreemium,
licenseStatusError,
isSaasInstance,
timeElapsedSinceCreation,
creationTime,
} = licenseDTO || {}
// In case of Saas expiry date is 30 days from creation time
const expiry = isSaasInstance && creationTime ? moment(creationTime).add(30, 'days').toISOString() : onPremExpiry
// For TTL will use timeElapsedSinceCreation to calculate remaining time for Saas license with 30 days validity, since browser time may differ from server time
const ttl = isSaasInstance && timeElapsedSinceCreation ? 30 * 24 * 60 * 60 - timeElapsedSinceCreation : onPremTTL
return {
enterpriseName: organisationMetadata?.name || 'Devtron Enterprise',
expiryDate: expiry ? moment(expiry).format(DATE_TIME_FORMATS['DD/MM/YYYY']) : '',
ttl,
licenseStatus: getDevtronLicenseStatus({ ttl, reminderThreshold }),
isTrial,
isFreemium,
licenseStatusError,
isSaasInstance: !!isSaasInstance,
...(currentUserEmail && currentUserEmail === claimedByUserDetails?.email
? { licenseKey: license }
: { licenseSuffix: license }),
}
}
export const parseDevtronLicenseData = (result: DevtronLicenseDTO): DevtronLicenseInfo => {
const parsedResponse = parseDevtronLicenseDTOIntoLicenseCardData(result)
return {
...parsedResponse,
fingerprint: result?.fingerprint || '',
showLicenseData: result?.showLicenseData,
licenseStatusError: result?.licenseStatusError,
moduleLimits: {
allAllowed: result?.moduleLimits?.allAllowed || false,
maxAllowedClusters: result?.moduleLimits?.maxAllowedClusters || ALLOWED_CLUSTER_IN_FREEMIUM,
},
}
}
export const getGateKeeperUrl = (fingerprint: string) =>
getUrlWithSearchParams(window._env_.GATEKEEPER_URL, { fingerprint })