forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.ts
More file actions
111 lines (90 loc) · 2.47 KB
/
Copy pathsession.ts
File metadata and controls
111 lines (90 loc) · 2.47 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
const ACTIVE_USER_ID_KEY = 'stellarsplit.active-user-id';
const AUTH_TOKEN_KEY = 'stellarsplit.auth-token';
const PARTICIPANT_DIRECTORY_KEY = 'stellarsplit.participant-directory';
export interface StoredParticipantIdentity {
name: string;
email?: string;
walletAddress?: string;
}
type StoredParticipantDirectory = Record<
string,
Record<string, StoredParticipantIdentity>
>;
function isBrowser(): boolean {
return typeof window !== 'undefined' && typeof window.localStorage !== 'undefined';
}
export function getStoredActiveUserId(): string | null {
if (!isBrowser()) {
return null;
}
return window.localStorage.getItem(ACTIVE_USER_ID_KEY);
}
export function setStoredActiveUserId(userId: string | null): void {
if (!isBrowser()) {
return;
}
if (!userId) {
window.localStorage.removeItem(ACTIVE_USER_ID_KEY);
return;
}
window.localStorage.setItem(ACTIVE_USER_ID_KEY, userId);
}
export function getStoredAuthToken(): string | null {
if (!isBrowser()) {
return null;
}
return window.localStorage.getItem(AUTH_TOKEN_KEY);
}
export function setStoredAuthToken(token: string | null): void {
if (!isBrowser()) {
return;
}
if (!token) {
window.localStorage.removeItem(AUTH_TOKEN_KEY);
return;
}
window.localStorage.setItem(AUTH_TOKEN_KEY, token);
}
function readParticipantDirectory(): StoredParticipantDirectory {
if (!isBrowser()) {
return {};
}
try {
const raw = window.localStorage.getItem(PARTICIPANT_DIRECTORY_KEY);
if (!raw) {
return {};
}
const parsed = JSON.parse(raw) as StoredParticipantDirectory;
return parsed && typeof parsed === 'object' ? parsed : {};
} catch {
return {};
}
}
function writeParticipantDirectory(directory: StoredParticipantDirectory): void {
if (!isBrowser()) {
return;
}
window.localStorage.setItem(
PARTICIPANT_DIRECTORY_KEY,
JSON.stringify(directory),
);
}
export function storeSplitParticipantDirectory(
splitId: string,
identities: Record<string, StoredParticipantIdentity>,
): void {
if (!splitId || Object.keys(identities).length === 0) {
return;
}
const currentDirectory = readParticipantDirectory();
currentDirectory[splitId] = {
...(currentDirectory[splitId] ?? {}),
...identities,
};
writeParticipantDirectory(currentDirectory);
}
export function getStoredSplitParticipantDirectory(
splitId: string,
): Record<string, StoredParticipantIdentity> {
return readParticipantDirectory()[splitId] ?? {};
}