Files
lcbp3/frontend/lib/stores/draft-store.ts
T
admin 11984bfa29
CI Pipeline / build (push) Failing after 12m41s
Build and Deploy / deploy (push) Failing after 2m44s
260322:1648 Correct Coresspondence / Doing RFA / Correct CI
2026-03-22 16:48:12 +07:00

34 lines
1015 B
TypeScript

// File: lib/stores/draft-store.ts
import { create } from 'zustand';
import { persist, createJSONStorage } from 'zustand/middleware';
/** A draft can hold any serializable form data — typed as unknown for strictness */
type DraftValue = Record<string, unknown>;
interface DraftState {
drafts: Record<string, DraftValue>;
saveDraft: (key: string, data: DraftValue) => void;
getDraft: (key: string) => DraftValue | undefined;
clearDraft: (key: string) => void;
}
export const useDraftStore = create<DraftState>()(
persist(
(set, get) => ({
drafts: {},
saveDraft: (key, data) => set((state) => ({ drafts: { ...state.drafts, [key]: data } })),
getDraft: (key) => get().drafts[key],
clearDraft: (key) =>
set((state) => {
const newDrafts = { ...state.drafts };
delete newDrafts[key];
return { drafts: newDrafts };
}),
}),
{
name: 'lcbp3-form-drafts',
storage: createJSONStorage(() => localStorage),
}
)
);