forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculatorTemplateStore.ts
More file actions
109 lines (91 loc) · 3.09 KB
/
Copy pathcalculatorTemplateStore.ts
File metadata and controls
109 lines (91 loc) · 3.09 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
import type { CalculatorState } from '../components/SplitCalculator/SplitCalculator';
const STORAGE_KEY = 'splitcalculator_templates_v1';
export interface CalculatorTemplate {
name: string;
state: CalculatorState;
createdAt: string;
updatedAt: string;
}
export class DuplicateTemplateNameError extends Error {
constructor(name: string) {
super(`A template named '${name}' already exists.`);
this.name = 'DuplicateTemplateNameError';
}
}
export class CalculatorTemplateStoreError extends Error {
constructor(message: string) {
super(message);
this.name = 'CalculatorTemplateStoreError';
}
}
function safelyParseTemplates(raw: string | null): Record<string, CalculatorTemplate> {
if (!raw) {
return {};
}
try {
const parsed = JSON.parse(raw);
if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
return {};
}
return Object.entries(parsed).reduce((acc, [key, value]) => {
if (
typeof key === 'string' &&
typeof value === 'object' &&
value !== null &&
typeof (value as any).name === 'string' &&
typeof (value as any).createdAt === 'string' &&
typeof (value as any).updatedAt === 'string' &&
typeof (value as any).state === 'object'
) {
acc[key] = value as CalculatorTemplate;
}
return acc;
}, {} as Record<string, CalculatorTemplate>);
} catch {
return {};
}
}
function persistTemplates(templates: Record<string, CalculatorTemplate>) {
localStorage.setItem(STORAGE_KEY, JSON.stringify(templates));
}
function cloneState(state: CalculatorState): CalculatorState {
return JSON.parse(JSON.stringify(state));
}
export function listCalculatorTemplates(): CalculatorTemplate[] {
const storage = safelyParseTemplates(localStorage.getItem(STORAGE_KEY));
return Object.values(storage);
}
export function getCalculatorTemplate(name: string): CalculatorTemplate | null {
const storage = safelyParseTemplates(localStorage.getItem(STORAGE_KEY));
const candidate = storage[name];
return candidate ? { ...candidate, state: cloneState(candidate.state) } : null;
}
export function saveCalculatorTemplate(name: string, state: CalculatorState): CalculatorTemplate {
const normalizedName = name.trim();
if (!normalizedName) {
throw new CalculatorTemplateStoreError('Template name cannot be empty.');
}
const storage = safelyParseTemplates(localStorage.getItem(STORAGE_KEY));
if (storage[normalizedName]) {
throw new DuplicateTemplateNameError(normalizedName);
}
const template: CalculatorTemplate = {
name: normalizedName,
state: cloneState(state),
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
};
storage[normalizedName] = template;
persistTemplates(storage);
return template;
}
export function deleteCalculatorTemplate(name: string): void {
const storage = safelyParseTemplates(localStorage.getItem(STORAGE_KEY));
if (storage[name]) {
delete storage[name];
persistTemplates(storage);
}
}
export function clearCalculatorTemplates(): void {
localStorage.removeItem(STORAGE_KEY);
}