From c7dbc751739c146104774c34ca4ae5d3aa937ed4 Mon Sep 17 00:00:00 2001 From: arsam Date: Fri, 18 Sep 2026 23:17:38 +0330 Subject: [PATCH] Import source: src/context/HistoryContext Import the existing local project snapshot. This commit organizes existing files; it does not represent newly developed functionality. --- src/context/HistoryContext.tsx | 65 ++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/context/HistoryContext.tsx diff --git a/src/context/HistoryContext.tsx b/src/context/HistoryContext.tsx new file mode 100644 index 0000000..4dc019b --- /dev/null +++ b/src/context/HistoryContext.tsx @@ -0,0 +1,65 @@ +import { createContext, useContext, useState, useCallback, type ReactNode } from 'react'; +import type { QRState } from '../types'; + +export interface HistoryEntry { + id: number; + type: QRState['selectedType']; + label: string; + snapshot: Pick; + formData: QRState['formData']; + savedAt: number; +} + +interface HistoryCtx { + history: HistoryEntry[]; + push: (state: QRState, label: string) => void; + remove: (id: number) => void; + clear: () => void; +} + +const KEY = 'cqrc_history'; +const MAX = 20; + +function load(): HistoryEntry[] { + try { return JSON.parse(localStorage.getItem(KEY) ?? '[]'); } catch { return []; } +} +function save(h: HistoryEntry[]) { + try { localStorage.setItem(KEY, JSON.stringify(h)); } catch { /* storage full */ } +} + +const HistoryContext = createContext({ history: [], push: () => {}, remove: () => {}, clear: () => {} }); + +export function HistoryProvider({ children }: { children: ReactNode }) { + const [history, setHistory] = useState(load); + + const push = useCallback((state: QRState, label: string) => { + setHistory(prev => { + const entry: HistoryEntry = { + id: Date.now(), + type: state.selectedType, + label: label.slice(0, 60), + snapshot: { + qrColor: state.qrColor, bgColor: state.bgColor, pattern: state.pattern, + theme: state.theme, gradientEnabled: state.gradientEnabled, + gradientColor2: state.gradientColor2, cornerSquareStyle: state.cornerSquareStyle, + cornerDotStyle: state.cornerDotStyle, logoDataUrl: state.logoDataUrl, + }, + formData: state.formData, + savedAt: Date.now(), + }; + const next = [entry, ...prev.filter(e => e.label !== label)].slice(0, MAX); + save(next); + return next; + }); + }, []); + + const remove = useCallback((id: number) => { + setHistory(prev => { const next = prev.filter(e => e.id !== id); save(next); return next; }); + }, []); + + const clear = useCallback(() => { setHistory([]); localStorage.removeItem(KEY); }, []); + + return {children}; +} + +export const useHistory = () => useContext(HistoryContext);