Skip to content

Commit

Permalink
Improve history search index status logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ahaapple committed Jan 7, 2025
1 parent 5aa3f97 commit f2a65cb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
17 changes: 9 additions & 8 deletions frontend/components/modal/search-model.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,19 +40,20 @@ export function SearchDialog({ openSearch: open, onOpenModelChange: onOpenChange
const [results, setResults] = useState<SearchResult[]>([]);
const [isLoading, setIsLoading] = useState(false);

const [isIndexed, setIsIndexed] = useState<boolean>(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);
Expand All @@ -76,7 +78,6 @@ export function SearchDialog({ openSearch: open, onOpenModelChange: onOpenChange
}
} catch (error) {
console.error('Failed to trigger full index:', error);
} finally {
setIsIndexing(false);
}
};
Expand Down Expand Up @@ -122,7 +123,7 @@ export function SearchDialog({ openSearch: open, onOpenModelChange: onOpenChange
{isIndexing ? (
<>
<Loader2 className="h-4 w-4 animate-spin mr-2" />
Indexing...
We are indexing your search history, please wait ...
</>
) : (
'Index All Search History'
Expand Down
21 changes: 21 additions & 0 deletions frontend/lib/store/local-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,24 @@ export const useUserStore = create<UserState>((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<IndexState>()(
persist(
(set) => ({
isIndexed: false,
isIndexing: false,
setIsIndexed: (status: boolean) => set({ isIndexed: status }),
setIsIndexing: (status: boolean) => set({ isIndexing: status }),
}),
{
name: 'his-index-storage',
},
),
);

0 comments on commit f2a65cb

Please sign in to comment.