| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import { create } from 'zustand'; |
| import { devtools } from 'zustand/middleware'; |
| import { immer } from 'zustand/middleware/immer'; |
| import { HistoryEntry, MdxGroupIndex } from '../types'; |
| import * as historyApi from '../api/history'; |
| import { withAsyncHandler } from '../utils/storeUtils'; |
|
|
| interface HistoryState { |
| |
| history: HistoryEntry[]; |
| currentIndex: number; |
| maxHistorySize: number; |
| |
| |
| loading: boolean; |
| error: string | null; |
| |
| |
| canGoBack: () => boolean; |
| canGoForward: () => boolean; |
| getCurrentEntry: () => HistoryEntry | null; |
| |
| |
| addToHistory: ( |
| keyword: string, |
| groupIndex: MdxGroupIndex[], |
| profileId: number, |
| profileName: string |
| ) => Promise<void>; |
| goBack: () => HistoryEntry | null; |
| goForward: () => HistoryEntry | null; |
| goToIndex: (index: number) => HistoryEntry | null; |
| removeFromHistory: (id: string) => Promise<void>; |
| |
| |
| setMaxHistorySize: (size: number) => Promise<void>; |
| |
| |
| clearHistory: () => Promise<void>; |
| importHistory: (history: HistoryEntry[]) => Promise<void>; |
| } |
|
|
| export const useHistoryStore = create<HistoryState>()( |
| devtools( |
| immer((set, get) => { |
| |
| const handleAsync = <T extends any[], R>( |
| fn: (...args: T) => Promise<R>, |
| errorMessage: string |
| ) => withAsyncHandler(set, fn, errorMessage); |
|
|
| return { |
| |
| history: [], |
| currentIndex: -1, |
| maxHistorySize: 1000, |
| loading: false, |
| error: null, |
|
|
| |
| canGoBack: () => { |
| const { currentIndex } = get(); |
| return currentIndex > 0; |
| }, |
|
|
| |
| canGoForward: () => { |
| const { currentIndex, history } = get(); |
| return currentIndex < history.length - 1; |
| }, |
|
|
| |
| getCurrentEntry: () => { |
| const { history, currentIndex } = get(); |
| if (currentIndex >= 0 && currentIndex < history.length) { |
| return history[currentIndex]; |
| } |
| return null; |
| }, |
|
|
| |
| addToHistory: handleAsync( |
| async ( |
| keyword: string, |
| groupIndex: MdxGroupIndex[], |
| profileId: number, |
| profileName: string |
| ) => { |
| |
| const newEntry = await historyApi.addToHistory(keyword, groupIndex, profileId, profileName); |
| |
| |
| set((state) => { |
| |
| if (state.currentIndex < state.history.length - 1) { |
| state.history = state.history.slice(0, state.currentIndex + 1); |
| } |
|
|
| |
| state.history.push(newEntry); |
| |
| |
| state.currentIndex = state.history.length - 1; |
| }); |
| }, |
| 'Failed to add to history' |
| ), |
|
|
| |
| goBack: () => { |
| const { currentIndex, history } = get(); |
| if (currentIndex > 0) { |
| const newIndex = currentIndex - 1; |
| set({ currentIndex: newIndex }); |
| return history[newIndex]; |
| } |
| return null; |
| }, |
|
|
| |
| goForward: () => { |
| const { currentIndex, history } = get(); |
| if (currentIndex < history.length - 1) { |
| const newIndex = currentIndex + 1; |
| set({ currentIndex: newIndex }); |
| return history[newIndex]; |
| } |
| return null; |
| }, |
|
|
| |
| goToIndex: (index: number) => { |
| const { history } = get(); |
| if (index >= 0 && index < history.length) { |
| set({ currentIndex: index }); |
| return history[index]; |
| } |
| return null; |
| }, |
|
|
| |
| removeFromHistory: handleAsync( |
| async (id: string) => { |
| |
| await historyApi.removeFromHistory(id); |
| |
| |
| set((state) => { |
| const index = state.history.findIndex((entry) => entry.id === id); |
| if (index !== -1) { |
| state.history.splice(index, 1); |
| |
| |
| if (state.currentIndex >= state.history.length) { |
| state.currentIndex = state.history.length - 1; |
| } else if (state.currentIndex >= index) { |
| state.currentIndex = Math.max(-1, state.currentIndex - 1); |
| } |
| } |
| }); |
| }, |
| 'Failed to remove from history' |
| ), |
|
|
| |
| setMaxHistorySize: handleAsync( |
| async (size: number) => { |
| |
| await historyApi.setMaxHistorySize(size); |
| |
| |
| set((state) => { |
| state.maxHistorySize = size; |
| }); |
| |
| |
| const history = await historyApi.getAllHistory(); |
| set({ |
| history, |
| currentIndex: history.length > 0 ? history.length - 1 : -1, |
| }); |
| }, |
| 'Failed to set max history size' |
| ), |
|
|
| |
| clearHistory: handleAsync( |
| async () => { |
| |
| await historyApi.clearHistory(); |
| |
| |
| set({ history: [], currentIndex: -1 }); |
| }, |
| 'Failed to clear history' |
| ), |
|
|
| |
| importHistory: handleAsync( |
| async (history: HistoryEntry[]) => { |
| |
| await historyApi.importHistory(history); |
| |
| |
| set({ |
| history, |
| currentIndex: history.length > 0 ? history.length - 1 : -1, |
| }); |
| }, |
| 'Failed to import history' |
| ), |
| }; |
| }), |
| { name: 'HistoryStore' } |
| ) |
| ); |
|
|
| |
| historyApi.getAllHistory() |
| .then((history) => { |
| useHistoryStore.setState({ |
| history, |
| currentIndex: history.length > 0 ? history.length - 1 : -1, |
| }); |
| }) |
| .catch((error) => { |
| console.error('Failed to load history:', error); |
| }); |
|
|
| |
| historyApi.getMaxHistorySize() |
| .then((size) => { |
| useHistoryStore.setState({ maxHistorySize: size }); |
| }) |
| .catch((error) => { |
| console.error('Failed to load max history size:', error); |
| }); |
|
|
|
|