Repository navigation
Expand file tree
/
Copy pathcreate-form-draft.ts
More file actions
67 lines (57 loc) · 1.53 KB
/
Copy pathcreate-form-draft.ts
File metadata and controls
67 lines (57 loc) · 1.53 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
import type { FormDraft } from "@/types/form";
export const FORM_DRAFT_STORAGE_KEY = "tricklepay-create-form-draft";
export const EMPTY_FORM_DRAFT: FormDraft = {
recipient: "",
token: "",
amount: "",
start: "",
end: "",
cliff: "",
durationAmount: "",
durationUnit: "days",
};
/** Restores the create-stream form draft saved in localStorage, if any. */
export function readFormDraft(): FormDraft | null {
if (typeof window === "undefined") {
return null;
}
try {
const raw = window.localStorage.getItem(FORM_DRAFT_STORAGE_KEY);
if (!raw) {
return null;
}
const parsed = JSON.parse(raw) as Partial<FormDraft>;
return {
recipient: parsed.recipient ?? "",
token: parsed.token ?? "",
amount: parsed.amount ?? "",
start: parsed.start ?? "",
end: parsed.end ?? "",
cliff: parsed.cliff ?? "",
durationAmount: parsed.durationAmount ?? "",
durationUnit: parsed.durationUnit ?? "days",
};
} catch {
return null;
}
}
export function writeFormDraft(values: FormDraft): void {
if (typeof window === "undefined") {
return;
}
try {
window.localStorage.setItem(FORM_DRAFT_STORAGE_KEY, JSON.stringify(values));
} catch {
// Ignore storage failures from private browsing or quota limits.
}
}
export function clearFormDraft(): void {
if (typeof window === "undefined") {
return;
}
try {
window.localStorage.removeItem(FORM_DRAFT_STORAGE_KEY);
} catch {
// Ignore storage failures from private browsing or quota limits.
}
}