Repository navigation
Expand file tree
/
Copy pathlocalStorage.ts
More file actions
107 lines (87 loc) · 2.65 KB
/
Copy pathlocalStorage.ts
File metadata and controls
107 lines (87 loc) · 2.65 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
import globals from '../Globals';
export const setLSItem = (key: string, data: string) => {
window.localStorage.setItem(key, data);
};
export const getLSItem = (key: string) => {
if (!key) {
return null;
}
return window.localStorage.getItem(key);
};
export const getParsedLSItem = (key: string, defaultVal: any = null) => {
const value = getLSItem(key);
if (!value) {
return defaultVal;
}
try {
const jsonValue = JSON.parse(value);
return jsonValue || defaultVal;
} catch {
return defaultVal;
}
};
export const removeLSItem = (key: string) => {
const value = getLSItem(key);
if (!value) {
return null;
}
window.localStorage.removeItem(key);
};
type expiryValue = {
value: string;
expiry: number;
};
export const setLSItemWithExpiry = (key: string, data: string, ttl: number) => {
const now = new Date();
const item: expiryValue = {
value: data,
expiry: now.getTime() + ttl,
};
setLSItem(key, JSON.stringify(item));
};
export const getItemWithExpiry = (key: string) => {
const lsValue = getLSItem(key);
if (!lsValue) {
return null;
}
const item: expiryValue = JSON.parse(lsValue);
const now = new Date();
if (now.getTime() > item.expiry) {
window.localStorage.removeItem(key);
return null;
}
return item.value;
};
export const listLSKeys = () => {
return Object.keys(window.localStorage);
};
export const LS_KEYS = {
apiExplorerAdminSecretWasAdded: 'apiExplorer:adminSecretHeaderWasAdded',
apiExplorerConsoleGraphQLHeaders: 'apiExplorer:graphiqlHeaders',
apiExplorerEndpointSectionIsOpen: 'apiExplorer:endpointSectionIsOpen',
apiExplorerGraphiqlMode: 'apiExplorer:graphiQLMode',
apiExplorerHeaderSectionIsOpen: 'apiExplorer:headersSectionIsOpen',
consoleAdminSecret: 'console:adminSecret',
consoleLocalInfo: `console:localInfo:${globals.dataApiUrl}`,
dataColumnsCollapsedKey: 'data:collapsed',
dataColumnsOrderKey: 'data:order',
dataPageSizeKey: 'data:pageSize',
derivedActions: 'actions:derivedActions',
graphiqlQuery: 'graphiql:query',
loveConsent: 'console:loveIcon',
oneGraphExplorerCodeExporterOpen: 'graphiql:codeExporterOpen',
oneGraphExplorerOpen: 'graphiql:explorerOpen',
oneGraphExplorerWidth: 'graphiql:explorerWidth',
proClick: 'console:pro',
rawSQLKey: 'rawSql:sql',
rawSqlStatementTimeout: 'rawSql:rawSqlStatementTimeout',
showConsoleOnboarding: 'console:showConsoleOnboarding',
versionUpdateCheckLastClosed: 'console:versionUpdateCheckLastClosed',
};
export const clearGraphiqlLS = () => {
Object.values(LS_KEYS).forEach(lsKey => {
if (lsKey.startsWith('graphiql:')) {
window.localStorage.removeItem(lsKey);
}
});
};