|
| 1 | +import * as z from "zod"; |
| 2 | +import { useSyncExternalStore } from "react"; |
| 3 | + |
| 4 | +export const Entry = z.object({ |
| 5 | + createdAt: z.coerce.date(), |
| 6 | + finishedAt: z.coerce.date().optional(), |
| 7 | + displayName: z.string(), |
| 8 | + id: z.uuid(), |
| 9 | +}); |
| 10 | +export type Entry = z.infer<typeof Entry>; |
| 11 | +type EntryWithIndex = Entry & { index: number }; |
| 12 | +type ContextType = { |
| 13 | + entries: EntryWithIndex[]; |
| 14 | + nextIndex: number; |
| 15 | +}; |
| 16 | + |
| 17 | +const getFromStorage = (storage?: Record<string, string>): ContextType => { |
| 18 | + storage ??= localStorage; |
| 19 | + |
| 20 | + const entries: EntryWithIndex[] = []; |
| 21 | + let nextIndex = 0; |
| 22 | + for (const key in storage) { |
| 23 | + const keyMatch = key.match(/^simulation\[(\d+)]$/); |
| 24 | + if (keyMatch === null) continue; |
| 25 | + |
| 26 | + const index = +keyMatch[1]; |
| 27 | + if (index >= nextIndex) nextIndex = index + 1; |
| 28 | + |
| 29 | + const value = storage[key]!; |
| 30 | + const entry = Entry.safeParse(JSON.parse(value)); |
| 31 | + if (entry.success) { |
| 32 | + entries.push({ ...entry.data, index }); |
| 33 | + } else { |
| 34 | + console.warn(`Couldn't parse saved simulation '${key}'`, value, entry.error); |
| 35 | + delete storage[key]; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + // Sort from oldest to newest |
| 40 | + entries.sort((lhs, rhs) => +lhs.createdAt - +rhs.createdAt); |
| 41 | + |
| 42 | + return { |
| 43 | + entries, |
| 44 | + nextIndex, |
| 45 | + }; |
| 46 | +}; |
| 47 | + |
| 48 | +class ContextStore { |
| 49 | + entries: EntryWithIndex[]; |
| 50 | + nextIndex: number; |
| 51 | + listeners: Set<() => void>; |
| 52 | + storage: Record<string, string>; |
| 53 | + |
| 54 | + constructor(ctx: ContextType, storage: Record<string, string>) { |
| 55 | + this.entries = ctx.entries; |
| 56 | + this.nextIndex = ctx.nextIndex; |
| 57 | + this.storage = storage; |
| 58 | + this.listeners = new Set(); |
| 59 | + } |
| 60 | + |
| 61 | + static fromStorage(storage: Record<string, string>): ContextStore { |
| 62 | + return new ContextStore(getFromStorage(storage), storage); |
| 63 | + } |
| 64 | + |
| 65 | + addEntry(entry: Entry): void { |
| 66 | + this.entries.push({ ...entry, index: this.nextIndex }); |
| 67 | + |
| 68 | + // Assign to itself so that React knows this value was updated |
| 69 | + // eslint-disable-next-line no-self-assign |
| 70 | + this.entries = this.entries; |
| 71 | + |
| 72 | + this.storage[`simulation[${this.nextIndex}]`] = JSON.stringify(entry); |
| 73 | + this.nextIndex += 1; |
| 74 | + |
| 75 | + this.listeners.forEach((x) => x()); |
| 76 | + } |
| 77 | + |
| 78 | + finalizeEntry(id: string) { |
| 79 | + for (const entry of this.entries) { |
| 80 | + if (entry.id !== id) continue; |
| 81 | + |
| 82 | + if (entry.finishedAt !== undefined) continue; |
| 83 | + |
| 84 | + entry.finishedAt = new Date(); |
| 85 | + this.storage[`simulation[${entry.index}]`] = JSON.stringify(entry); |
| 86 | + this.listeners.forEach((x) => x()); |
| 87 | + return; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + subscribe(callable: () => void): () => void { |
| 92 | + this.listeners.add(callable); |
| 93 | + return () => this.listeners.delete(callable); |
| 94 | + } |
| 95 | + |
| 96 | + getSnapshot(): Entry[] { |
| 97 | + return this.entries; |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +export const simulationHistory = ContextStore.fromStorage(localStorage); |
| 102 | + |
| 103 | +export const useSimulationHistory = () => |
| 104 | + useSyncExternalStore( |
| 105 | + (listener) => simulationHistory.subscribe(listener), |
| 106 | + () => simulationHistory.getSnapshot() |
| 107 | + ); |
| 108 | + |
| 109 | +export const __testing__ = { |
| 110 | + getFromStorage, |
| 111 | + ContextStore, |
| 112 | +}; |
0 commit comments