Repository navigation
Expand file tree
/
Copy pathstorage.ts
More file actions
103 lines (93 loc) · 2.8 KB
/
Copy pathstorage.ts
File metadata and controls
103 lines (93 loc) · 2.8 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
import {
PersistedCrossfireDraftSchema,
type CrossfireState,
type PersistedCrossfireDraft,
} from './schemas';
export const CROSSFIRE_STORAGE_KEY_PREFIX = 'debatelab-crossfire-draft';
export const MAX_CROSSFIRE_STORAGE_CHARS = 80_000;
/** The slice of a key-value store the draft needs; the web app passes browser storage. */
export interface StorageLike {
getItem(key: string): string | null;
setItem(key: string, value: string): void;
removeItem(key: string): void;
}
export function crossfireStorageKey(ownerId: string): string {
return `${CROSSFIRE_STORAGE_KEY_PREFIX}:${ownerId}`;
}
export function loadCrossfireDraft(
ownerId: string,
storage: StorageLike | null,
): PersistedCrossfireDraft['state'] | null {
if (!storage || !ownerId) return null;
const key = crossfireStorageKey(ownerId);
let serialized: string | null;
try {
serialized = storage.getItem(key);
} catch {
return null;
}
if (!serialized) return null;
if (serialized.length > MAX_CROSSFIRE_STORAGE_CHARS) {
try { storage.removeItem(key); } catch { return null; }
return null;
}
try {
const parsed = PersistedCrossfireDraftSchema.safeParse(JSON.parse(serialized));
if (!parsed.success || parsed.data.ownerId !== ownerId) {
try { storage.removeItem(key); } catch { return null; }
return null;
}
return parsed.data.state;
} catch {
try { storage.removeItem(key); } catch { return null; }
return null;
}
}
export function saveCrossfireDraft(
ownerId: string,
state: CrossfireState,
storage: StorageLike | null,
now = Date.now(),
): boolean {
if (!storage || !ownerId) return false;
const key = crossfireStorageKey(ownerId);
if (state.phase === 'complete') {
try {
storage.removeItem(key);
return true;
} catch {
return false;
}
}
const draft: PersistedCrossfireDraft = {
version: 1,
ownerId,
savedAt: now,
state,
};
const parsed = PersistedCrossfireDraftSchema.safeParse(draft);
if (!parsed.success) return false;
const serialized = JSON.stringify(parsed.data);
if (serialized.length > MAX_CROSSFIRE_STORAGE_CHARS) return false;
try {
storage.setItem(key, serialized);
return true;
} catch {
return false;
}
}
export function clearCrossfireDraft(ownerId: string, storage: StorageLike | null): void {
if (!ownerId || !storage) return;
try { storage.removeItem(crossfireStorageKey(ownerId)); } catch { /* storage unavailable */ }
}
export function clearCrossfireDraftOnAuthChange(
previousUserId: string | undefined,
nextUserId: string | undefined,
storage: StorageLike | null,
): boolean {
if (previousUserId === nextUserId) return false;
if (previousUserId) {
try { storage?.removeItem(crossfireStorageKey(previousUserId)); } catch { /* storage unavailable */ }
}
return true;
}