Scrapboard.org client
1import { create } from "zustand";
2import { persist } from "zustand/middleware";
3
4interface RecentBoardsState {
5 recentBoards: string[]; // Array of board IDs
6 addRecentBoard: (boardId: string) => void;
7 removeRecentBoard: (boardId: string) => void;
8 clearRecentBoards: () => void;
9}
10
11export const useRecentBoardsStore = create<RecentBoardsState>()(
12 persist(
13 (set) => ({
14 recentBoards: [],
15
16 addRecentBoard: (boardId: string) =>
17 set((state) => {
18 // Remove board if it already exists (to reorder)
19 const filtered = state.recentBoards.filter((id) => id !== boardId);
20 // Add board to the beginning of the array (most recent first)
21 return { recentBoards: [boardId, ...filtered] };
22 }),
23
24 removeRecentBoard: (boardId: string) =>
25 set((state) => ({
26 recentBoards: state.recentBoards.filter((id) => id !== boardId),
27 })),
28
29 clearRecentBoards: () => set({ recentBoards: [] }),
30 }),
31 {
32 name: "recent-boards-storage", // name for localStorage
33 }
34 )
35);