/** * Purpose: DraftStorageMode supports form-draft.ts by holding or executing the draft storage mode page or API role. * Usage: import, call, read, or update this type from the owning page/module while preserving the surrounding loading and permission flow. */ export type DraftStorageMode = 'local' | 'session' /** * Purpose: DraftRecord supports form-draft.ts by holding or executing the draft record page or API role. * Usage: import, call, read, or update this interface from the owning page/module while preserving the surrounding loading and permission flow. */ export interface DraftRecord { savedAt: number payload: T } /** * Purpose: getStorage supports form-draft.ts by holding or executing the get storage page or API role. * Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow. */ const getStorage = (mode: DraftStorageMode) => (mode === 'session' ? window.sessionStorage : window.localStorage) /** * Purpose: buildDraftStorageKey supports form-draft.ts by holding or executing the build draft storage key page or API role. * Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow. */ export const buildDraftStorageKey = (...parts: Array) => { const normalizedParts = parts .filter((part): part is string | number => part !== undefined && part !== null && String(part).trim().length > 0) .map(part => String(part).trim()) return `fingertipdesigner:draft:${normalizedParts.join(':')}` } /** * Purpose: saveDraftRecord supports form-draft.ts by holding or executing the save draft record page or API role. * Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow. */ export const saveDraftRecord = (key: string, payload: T, mode: DraftStorageMode = 'local') => { const record: DraftRecord = { savedAt: Date.now(), payload } getStorage(mode).setItem(key, JSON.stringify(record)) return record } /** * Purpose: loadDraftRecord supports form-draft.ts by holding or executing the load draft record page or API role. * Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow. */ export const loadDraftRecord = (key: string, mode: DraftStorageMode = 'local') => { const rawValue = getStorage(mode).getItem(key) if (!rawValue) { return null } try { return JSON.parse(rawValue) as DraftRecord } catch { getStorage(mode).removeItem(key) return null } } /** * Purpose: clearDraftRecord supports form-draft.ts by holding or executing the clear draft record page or API role. * Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow. */ export const clearDraftRecord = (key: string, mode: DraftStorageMode = 'local') => { getStorage(mode).removeItem(key) }