From f2a65cb0fb42f5c8d06d970593b9205bbdad4dc1 Mon Sep 17 00:00:00 2001 From: ahaapple Date: Tue, 7 Jan 2025 18:05:34 +0800 Subject: [PATCH] Improve history search index status logic --- frontend/components/modal/search-model.tsx | 17 +++++++++-------- frontend/lib/store/local-store.ts | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/frontend/components/modal/search-model.tsx b/frontend/components/modal/search-model.tsx index 0e97543f..615696dd 100644 --- a/frontend/components/modal/search-model.tsx +++ b/frontend/components/modal/search-model.tsx @@ -13,6 +13,7 @@ import { Alert, AlertDescription } from '@/components/ui/alert'; import { resolveTime } from '@/lib/utils'; import { toast } from 'sonner'; import { Skeleton } from '@/components/ui/skeleton'; +import { useIndexStore } from '@/lib/store/local-store'; interface SearchResult { id: string; @@ -39,19 +40,20 @@ export function SearchDialog({ openSearch: open, onOpenModelChange: onOpenChange const [results, setResults] = useState([]); const [isLoading, setIsLoading] = useState(false); - const [isIndexed, setIsIndexed] = useState(false); + const { isIndexed, isIndexing, setIsIndexed, setIsIndexing } = useIndexStore(); useEffect(() => { const checkIndexStatus = async () => { - if (open && user?.id) { + if (open && user?.id && !isIndexed) { const indexed = await isUserFullIndexed(user?.id); - console.log('isIndexed:', indexed); - setIsIndexed(indexed); + if (indexed) { + setIsIndexed(indexed); + setIsIndexing(false); + } } }; checkIndexStatus(); - }, [open, user?.id]); + }, [open, user?.id, isIndexed]); - const [isIndexing, setIsIndexing] = useState(false); const handleFullIndex = async () => { if (isIndexing) return; setIsIndexing(true); @@ -76,7 +78,6 @@ export function SearchDialog({ openSearch: open, onOpenModelChange: onOpenChange } } catch (error) { console.error('Failed to trigger full index:', error); - } finally { setIsIndexing(false); } }; @@ -122,7 +123,7 @@ export function SearchDialog({ openSearch: open, onOpenModelChange: onOpenChange {isIndexing ? ( <> - Indexing... + We are indexing your search history, please wait ... ) : ( 'Index All Search History' diff --git a/frontend/lib/store/local-store.ts b/frontend/lib/store/local-store.ts index 0290e26e..d291f01d 100644 --- a/frontend/lib/store/local-store.ts +++ b/frontend/lib/store/local-store.ts @@ -95,3 +95,24 @@ export const useUserStore = create((set) => ({ user: null, setUser: (user: User) => set({ user }), })); + +interface IndexState { + isIndexed: boolean; + isIndexing: boolean; + setIsIndexed: (status: boolean) => void; + setIsIndexing: (status: boolean) => void; +} + +export const useIndexStore = create()( + persist( + (set) => ({ + isIndexed: false, + isIndexing: false, + setIsIndexed: (status: boolean) => set({ isIndexed: status }), + setIsIndexing: (status: boolean) => set({ isIndexing: status }), + }), + { + name: 'his-index-storage', + }, + ), +);