diff --git a/store/index.ts b/store/index.ts index 03e02e1..9115f45 100644 --- a/store/index.ts +++ b/store/index.ts @@ -12,3 +12,4 @@ export * from './ui-store'; export * from './toast-store'; export * from './context-menu-store'; export * from './guides-store'; +export * from './recent-files-store'; diff --git a/store/recent-files-store.ts b/store/recent-files-store.ts new file mode 100644 index 0000000..2273bb7 --- /dev/null +++ b/store/recent-files-store.ts @@ -0,0 +1,78 @@ +import { create } from 'zustand'; +import { persist } from 'zustand/middleware'; + +export interface RecentFile { + id: string; + name: string; + path: string; + timestamp: number; + thumbnail?: string; +} + +interface RecentFilesStore { + /** List of recent files */ + recentFiles: RecentFile[]; + /** Maximum number of recent files to keep */ + maxRecent: number; + + /** Add file to recent list */ + addRecentFile: (name: string, path: string, thumbnail?: string) => void; + /** Remove file from recent list */ + removeRecentFile: (id: string) => void; + /** Clear all recent files */ + clearRecentFiles: () => void; + /** Update file thumbnail */ + updateThumbnail: (id: string, thumbnail: string) => void; +} + +export const useRecentFilesStore = create()( + persist( + (set, get) => ({ + recentFiles: [], + maxRecent: 10, + + addRecentFile: (name, path, thumbnail) => { + const id = `${path}-${Date.now()}`; + const newFile: RecentFile = { + id, + name, + path, + timestamp: Date.now(), + thumbnail, + }; + + set((state) => { + // Remove duplicate if exists (same path) + const filtered = state.recentFiles.filter((f) => f.path !== path); + // Add new file at the beginning + const updated = [newFile, ...filtered]; + // Keep only maxRecent files + return { + recentFiles: updated.slice(0, state.maxRecent), + }; + }); + }, + + removeRecentFile: (id) => { + set((state) => ({ + recentFiles: state.recentFiles.filter((f) => f.id !== id), + })); + }, + + clearRecentFiles: () => { + set({ recentFiles: [] }); + }, + + updateThumbnail: (id, thumbnail) => { + set((state) => ({ + recentFiles: state.recentFiles.map((f) => + f.id === id ? { ...f, thumbnail } : f + ), + })); + }, + }), + { + name: 'recent-files-storage', + } + ) +);