refactor(Index.tsx): improve tag generation logic and Ollama status handling

This commit is contained in:
2025-08-29 13:21:59 +02:00
parent 48f3545454
commit d4dfe11cef

View File

@@ -94,7 +94,7 @@ const Index = () => {
const [debugInfo, setDebugInfo] = useState<string[]>([]); const [debugInfo, setDebugInfo] = useState<string[]>([]);
const [showDebugPanel, setShowDebugPanel] = useState(false); const [showDebugPanel, setShowDebugPanel] = useState(false);
const [autoGenerateTags, setAutoGenerateTags] = useState(true); const [autoGenerateTags, setAutoGenerateTags] = useState(true);
const [ollamaStatus, setOllamaStatus] = useState<'unknown' | 'online' | 'offline'>('unknown'); const [ollamaStatus, setOllamaStatus] = useState<'unknown' | 'online' | 'offline'>('unknown');
const [includeTagsInSearch, setIncludeTagsInSearch] = useState(true); const [includeTagsInSearch, setIncludeTagsInSearch] = useState(true);
const [tagGenerationTimeout, setTagGenerationTimeout] = useState<NodeJS.Timeout>(); const [tagGenerationTimeout, setTagGenerationTimeout] = useState<NodeJS.Timeout>();
const [isSettingsOpen, setIsSettingsOpen] = useState(false); const [isSettingsOpen, setIsSettingsOpen] = useState(false);
@@ -312,7 +312,7 @@ $current`);
// Generate tags using Ollama // Generate tags using Ollama
const generateTags = async (content: string, noteIndex?: number): Promise<string[]> => { const generateTags = async (content: string, noteIndex?: number): Promise<string[]> => {
try { try {
// Get context from previous notes only // Get context from previous notes only
let previousNotes = ''; let previousNotes = '';
if (noteIndex !== undefined && noteCache.length > 0) { if (noteIndex !== undefined && noteCache.length > 0) {
const contextNotes = []; const contextNotes = [];
@@ -368,8 +368,17 @@ $current`);
.map((tag: string) => tag.trim()) .map((tag: string) => tag.trim())
.filter((tag: string) => tag.length > 0); .filter((tag: string) => tag.length > 0);
addDebugInfo(`Generated ${tags.length} tags: ${tags.join(', ')}`); // Filter out tags that already exist in the content
return tags; const filteredTags = tags.filter((tag: string) => {
const tagExists = content.toLowerCase().includes(tag.toLowerCase());
if (tagExists) {
addDebugInfo(`Removing tag "${tag}" - already exists in content`);
}
return !tagExists;
});
addDebugInfo(`Generated ${tags.length} tags, filtered to ${filteredTags.length}: ${filteredTags.join(', ')}`);
return filteredTags;
// Last resort: return empty array // Last resort: return empty array
addDebugInfo('Could not extract tags from Ollama response'); addDebugInfo('Could not extract tags from Ollama response');
@@ -716,31 +725,31 @@ $current`);
const data = await response.json(); const data = await response.json();
const notes: Note[] = data.hits.map((hit: any) => mapHitToNote(hit)); const notes: Note[] = data.hits.map((hit: any) => mapHitToNote(hit));
if (offset === 0) { if (offset === 0) {
setNoteCache(notes); setNoteCache(notes);
if (notes.length > 0) { if (notes.length > 0) {
setPreviousNote(notes[0]); setPreviousNote(notes[0]);
setCurrentNoteIndex(0); setCurrentNoteIndex(0);
}
const loadTime = Date.now() - loadStartTime;
addDebugInfo(`Loaded ${notes.length} notes in ${loadTime}ms`);
} else {
setNoteCache(prev => [...prev, ...notes]);
const loadTime = Date.now() - loadStartTime;
addDebugInfo(`Loaded ${notes.length} additional notes in ${loadTime}ms`);
} }
} catch (error) {
console.error('Error loading notes:', error);
const loadTime = Date.now() - loadStartTime; const loadTime = Date.now() - loadStartTime;
addDebugInfo(`Failed to load notes after ${loadTime}ms`); addDebugInfo(`Loaded ${notes.length} notes in ${loadTime}ms`);
toast({ } else {
title: "Error", setNoteCache(prev => [...prev, ...notes]);
description: "Failed to load notes. Please check your connection.", const loadTime = Date.now() - loadStartTime;
variant: "destructive", addDebugInfo(`Loaded ${notes.length} additional notes in ${loadTime}ms`);
});
} finally {
setIsLoading(false);
} }
} catch (error) {
console.error('Error loading notes:', error);
const loadTime = Date.now() - loadStartTime;
addDebugInfo(`Failed to load notes after ${loadTime}ms`);
toast({
title: "Error",
description: "Failed to load notes. Please check your connection.",
variant: "destructive",
});
} finally {
setIsLoading(false);
}
}; };
// Load notes around a specific note // Load notes around a specific note
@@ -2358,16 +2367,16 @@ $current`);
className={`flex-1 border-0 bg-transparent ${getTextClass('base')} p-0 outline-none`} className={`flex-1 border-0 bg-transparent ${getTextClass('base')} p-0 outline-none`}
/> />
{!autoGenerateTags && ( {!autoGenerateTags && (
<Button <Button
onClick={async () => { onClick={async () => {
if (previousNote) { if (previousNote) {
addDebugInfo('Manually generating tags...'); addDebugInfo('Manually generating tags...');
const tags = await generateTags(previousNote.content, currentNoteIndex); const tags = await generateTags(previousNote.content, currentNoteIndex);
const updatedNote = { ...previousNote, tags }; const updatedNote = { ...previousNote, tags };
setPreviousNote(updatedNote); setPreviousNote(updatedNote);
setIsPreviousNoteModified(true); setIsPreviousNoteModified(true);
} }
}} }}
size="sm" size="sm"
variant="outline" variant="outline"
className={`${getTextClass('base')} px-2 py-1 h-6`} className={`${getTextClass('base')} px-2 py-1 h-6`}
@@ -2414,17 +2423,17 @@ $current`);
<div className="flex-1 bg-card rounded-lg border border-border shadow-sm p-6 overflow-hidden"> <div className="flex-1 bg-card rounded-lg border border-border shadow-sm p-6 overflow-hidden">
<div className={`${getTextClass('2xl')} text-muted-foreground mb-3 flex items-center gap-4`}> <div className={`${getTextClass('2xl')} text-muted-foreground mb-3 flex items-center gap-4`}>
<div>Current Entry</div> <div>Current Entry</div>
<div className="flex-1 flex items-center gap-2"> <div className="flex-1 flex items-center gap-2">
<span className={`${getTextClass('base')} text-muted-foreground`}>Tags:</span> <span className={`${getTextClass('base')} text-muted-foreground`}>Tags:</span>
<input <input
type="text" type="text"
value={currentNoteTags.join(', ')} value={currentNoteTags.join(', ')}
onChange={(e) => { onChange={(e) => {
setCurrentNoteTags([e.target.value]); setCurrentNoteTags([e.target.value]);
}} }}
className={`flex-1 border-0 bg-transparent ${getTextClass('base')} p-0 outline-none`} className={`flex-1 border-0 bg-transparent ${getTextClass('base')} p-0 outline-none`}
/> />
</div> </div>
</div> </div>
<Textarea <Textarea
ref={currentNoteRef} ref={currentNoteRef}