|
| 1 | +import React, { |
| 2 | + createContext, |
| 3 | + useContext, |
| 4 | + useEffect, |
| 5 | + useState, |
| 6 | + useRef, |
| 7 | +} from 'react'; |
| 8 | +import { useVectorIndexing } from '../hooks/useVectorIndexing'; |
| 9 | +import { trpcProxyClient } from '@shared/config'; |
| 10 | + |
| 11 | +// Static flag to ensure indexing is only initialized once across all instances |
| 12 | +// This is outside the component to persist across component mounts/unmounts |
| 13 | +let hasInitializedIndexing = false; |
| 14 | + |
| 15 | +interface IndexingContextType { |
| 16 | + isIndexingComplete: boolean; |
| 17 | + isIndexing: boolean; |
| 18 | + indexingProgress: { |
| 19 | + total: number; |
| 20 | + processed: number; |
| 21 | + }; |
| 22 | + triggerManualIndexing: (forceReindex?: boolean) => Promise<void>; |
| 23 | +} |
| 24 | + |
| 25 | +const IndexingContext = createContext<IndexingContextType | undefined>( |
| 26 | + undefined |
| 27 | +); |
| 28 | + |
| 29 | +/** |
| 30 | + * Provider component that handles note vector indexing once per session |
| 31 | + * and provides indexing state to the application |
| 32 | + */ |
| 33 | +export function NoteIndexingProvider({ |
| 34 | + children, |
| 35 | +}: { |
| 36 | + children: React.ReactNode; |
| 37 | +}) { |
| 38 | + const { checkIndexingStatus, startIndexing } = useVectorIndexing(); |
| 39 | + const [isIndexingComplete, setIsIndexingComplete] = useState(false); |
| 40 | + const [isIndexing, setIsIndexing] = useState(false); |
| 41 | + const [indexingProgress, setIndexingProgress] = useState({ |
| 42 | + total: 0, |
| 43 | + processed: 0, |
| 44 | + }); |
| 45 | + |
| 46 | + // Function to manually trigger indexing |
| 47 | + const triggerManualIndexing = async (forceReindex = false) => { |
| 48 | + try { |
| 49 | + const result = await startIndexing(forceReindex); |
| 50 | + console.log('Manual indexing triggered:', result); |
| 51 | + } catch (error) { |
| 52 | + console.error('Error triggering manual indexing:', error); |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + // Run indexing check once on app startup |
| 57 | + useEffect(() => { |
| 58 | + // Set up subscription to indexing status updates first |
| 59 | + const subscription = trpcProxyClient.notes.onIndexingStatus.subscribe( |
| 60 | + undefined, |
| 61 | + { |
| 62 | + onData: (data) => { |
| 63 | + setIsIndexing( |
| 64 | + data.status === 'started' || data.status === 'progress' |
| 65 | + ); |
| 66 | + setIsIndexingComplete(data.status === 'completed'); |
| 67 | + |
| 68 | + if (data.total !== undefined && data.processed !== undefined) { |
| 69 | + setIndexingProgress({ |
| 70 | + total: data.total, |
| 71 | + processed: data.processed, |
| 72 | + }); |
| 73 | + } |
| 74 | + }, |
| 75 | + onError: (error) => { |
| 76 | + console.error('Error in indexing status subscription:', error); |
| 77 | + setIsIndexing(false); |
| 78 | + }, |
| 79 | + } |
| 80 | + ); |
| 81 | + |
| 82 | + // Only run the initialization logic once per app session |
| 83 | + if (!hasInitializedIndexing) { |
| 84 | + hasInitializedIndexing = true; |
| 85 | + console.log('Initializing indexing check (first time)'); |
| 86 | + |
| 87 | + const checkIndexingNeeded = async () => { |
| 88 | + try { |
| 89 | + // First check if we're already indexing |
| 90 | + const status = await checkIndexingStatus(); |
| 91 | + setIsIndexing(status.isIndexing); |
| 92 | + |
| 93 | + if (status.isIndexing) { |
| 94 | + console.log('Indexing already in progress'); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + // Check if notes are already indexed |
| 99 | + const notesNeedingIndexing = |
| 100 | + await trpcProxyClient.notes.getNotesNeedingIndexing.query(); |
| 101 | + |
| 102 | + console.log( |
| 103 | + 'INDEXING DEBUG: Notes needing indexing:', |
| 104 | + notesNeedingIndexing |
| 105 | + ); |
| 106 | + |
| 107 | + const needsIndexing = notesNeedingIndexing.needsIndexing.length > 0; |
| 108 | + |
| 109 | + // If indexing is needed, start it with a delay |
| 110 | + if (needsIndexing) { |
| 111 | + console.log('Notes not indexed, will trigger background indexing'); |
| 112 | + setTimeout(() => { |
| 113 | + startIndexing(false) |
| 114 | + .then((result) => { |
| 115 | + console.log('Indexing started in background:', result); |
| 116 | + }) |
| 117 | + .catch((error) => { |
| 118 | + console.error('Error starting background indexing:', error); |
| 119 | + }); |
| 120 | + }, 5000); // 5 second delay to allow app to initialize |
| 121 | + } else { |
| 122 | + console.log('Notes already indexed, no need to start indexing'); |
| 123 | + setIsIndexingComplete(true); |
| 124 | + } |
| 125 | + } catch (error) { |
| 126 | + console.error('Error checking if indexing is needed:', error); |
| 127 | + } |
| 128 | + }; |
| 129 | + |
| 130 | + // Start the check with a small delay to allow other initialization to complete |
| 131 | + setTimeout(() => { |
| 132 | + checkIndexingNeeded(); |
| 133 | + }, 2000); |
| 134 | + } else { |
| 135 | + console.log('Skipping indexing initialization (already done)'); |
| 136 | + // Still check status to update the UI correctly |
| 137 | + checkIndexingStatus().then((status) => { |
| 138 | + setIsIndexing(status.isIndexing); |
| 139 | + }); |
| 140 | + } |
| 141 | + |
| 142 | + // Cleanup subscription |
| 143 | + return () => { |
| 144 | + subscription.unsubscribe(); |
| 145 | + }; |
| 146 | + }, [checkIndexingStatus, startIndexing]); |
| 147 | + |
| 148 | + const value = { |
| 149 | + isIndexingComplete, |
| 150 | + isIndexing, |
| 151 | + indexingProgress, |
| 152 | + triggerManualIndexing, |
| 153 | + }; |
| 154 | + |
| 155 | + return ( |
| 156 | + <IndexingContext.Provider value={value}> |
| 157 | + {children} |
| 158 | + </IndexingContext.Provider> |
| 159 | + ); |
| 160 | +} |
| 161 | + |
| 162 | +/** |
| 163 | + * Hook to access the indexing context |
| 164 | + */ |
| 165 | +export function useNoteIndexing() { |
| 166 | + const context = useContext(IndexingContext); |
| 167 | + if (context === undefined) { |
| 168 | + throw new Error( |
| 169 | + 'useNoteIndexing must be used within a NoteIndexingProvider' |
| 170 | + ); |
| 171 | + } |
| 172 | + return context; |
| 173 | +} |
0 commit comments