From 9fa2d93c54fb6f37315796bb09782f5273b487a6 Mon Sep 17 00:00:00 2001 From: Dion Low Date: Mon, 7 Apr 2025 12:17:25 -0700 Subject: [PATCH] feat: useInstallationLocalStorage --- src/hooks/useInstallationLocalStorage.ts | 77 ++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/hooks/useInstallationLocalStorage.ts diff --git a/src/hooks/useInstallationLocalStorage.ts b/src/hooks/useInstallationLocalStorage.ts new file mode 100644 index 00000000..15e70ec0 --- /dev/null +++ b/src/hooks/useInstallationLocalStorage.ts @@ -0,0 +1,77 @@ +import { useCallback, useEffect, useState } from 'react'; + +type DraftKey = `${string}|${string}`; + +const STORAGE_KEY = 'installationDrafts'; + +type DraftStorage = Record; + +export function useInstallationLocalStorage( + projectId: string, + integrationId: string, + groupRef: string, + consumerRef: string, + initialData: T, +) { + const key: DraftKey = `${projectId}|${integrationId}|${groupRef}|${consumerRef}`; + const [formData, setFormData] = useState(() => { + const raw = localStorage.getItem(STORAGE_KEY); + if (!raw) return initialData; + + try { + const parsed: DraftStorage = JSON.parse(raw); + return parsed[key]?.formData || initialData; + } catch { + return initialData; + } + }); + + const clear = () => { + const raw = localStorage.getItem(STORAGE_KEY); + if (!raw) return; + + const parsed: DraftStorage = JSON.parse(raw); + delete parsed[key]; + localStorage.setItem(STORAGE_KEY, JSON.stringify(parsed)); + setFormData(initialData); + }; + + const clearExpired = useCallback(() => { + const raw = localStorage.getItem(STORAGE_KEY); + if (!raw) return; + + const parsed: DraftStorage = JSON.parse(raw); + const now = Date.now(); + + const draftKeys = Object.keys(parsed) as DraftKey[]; // re-assigned parsed type + + draftKeys.forEach((storageKey) => { + if (parsed[storageKey].expiryDate < now) { + delete parsed[storageKey]; + } + }); + + localStorage.setItem(STORAGE_KEY, JSON.stringify(parsed)); + }, []); + + useEffect(() => { + const raw = localStorage.getItem(STORAGE_KEY); + const parsed: DraftStorage = raw ? JSON.parse(raw) : {}; + parsed[key] = { + formData, + expiryDate: Date.now() + 1000 * 60 * 60 * 24, // 24 hours + }; + localStorage.setItem(STORAGE_KEY, JSON.stringify(parsed)); + }, [clearExpired, formData, key]); + + return { + formData, + setFormData, + clear, + key, + clearExpired, + }; +}