Repository navigation
Expand file tree
/
Copy pathutils.ts
More file actions
111 lines (91 loc) · 2.48 KB
/
Copy pathutils.ts
File metadata and controls
111 lines (91 loc) · 2.48 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
import jwt_decode from 'jwt-decode';
import { NotificationsState } from '../../telemetry/state';
import { getLSItem, LS_KEYS, setLSItem } from '../../utils/localStorage';
import { Nullable } from '../Common/utils/tsUtils';
import { ConsoleScope } from './ConsoleNotification';
const defaultProClickState = {
isProClicked: false,
};
const defaultState = {
isDismissed: false,
};
const setLoveConsentState = (stateData: { isDismissed: boolean }) => {
setLSItem(LS_KEYS.loveConsent, JSON.stringify(stateData));
};
const getLoveConsentState = () => {
const s = getLSItem(LS_KEYS.loveConsent);
if (s) {
return JSON.parse(s);
}
setLSItem(LS_KEYS.loveConsent, JSON.stringify(defaultState));
return defaultState;
};
const setProClickState = (proStateData: { isProClicked: boolean }) => {
setLSItem(LS_KEYS.proClick, JSON.stringify(proStateData));
};
const getProClickState = () => {
try {
const proState = getLSItem(LS_KEYS.proClick);
if (proState) {
return JSON.parse(proState);
}
setLSItem(LS_KEYS.proClick, JSON.stringify(defaultProClickState));
return defaultProClickState;
} catch (err) {
console.error(err);
return defaultProClickState;
}
};
const getReadAllNotificationsState = (): NotificationsState => {
return {
read: 'all',
date: new Date().toISOString(),
showBadge: false,
};
};
const getConsoleScope = (
serverVersion: string,
consoleID: Nullable<string>
): ConsoleScope => {
if (!consoleID) {
return 'OSS';
}
if (serverVersion.includes('cloud')) {
return 'CLOUD';
}
if (serverVersion.includes('pro')) {
return 'PRO';
}
return 'OSS';
};
// added these, so that it can be repurposed if needed
type JWTKeys = 'sub' | 'iat' | 'aud' | 'exp' | 'iss';
type JWTType = Record<JWTKeys, string>;
type DecodedJWT = Partial<JWTType>;
// This function is specifically to help identify the multiple users on cloud.
// This is a temporary solution atm. but improvements will be added soon
const getUserType = (token: string) => {
const IDToken = 'IDToken ';
if (!token.includes(IDToken)) {
return 'admin';
}
const jwtToken = token.split(IDToken)[1];
try {
const decodedToken: DecodedJWT = jwt_decode(jwtToken);
if (!decodedToken.sub) {
return 'admin';
}
return decodedToken.sub;
} catch {
return 'admin';
}
};
export {
getConsoleScope,
getLoveConsentState,
getProClickState,
getReadAllNotificationsState,
getUserType,
setLoveConsentState,
setProClickState,
};