refactor(Index.tsx): remove unused AbortController state and logic from text correction

This commit is contained in:
2025-08-29 18:22:18 +02:00
parent f27f897358
commit bed316c17a

View File

@@ -124,7 +124,6 @@ $current`);
const [correctedContent, setCorrectedContent] = useState(''); const [correctedContent, setCorrectedContent] = useState('');
const [previousNoteCorrectedContent, setPreviousNoteCorrectedContent] = useState(''); const [previousNoteCorrectedContent, setPreviousNoteCorrectedContent] = useState('');
const [correctionTimeout, setCorrectionTimeout] = useState<NodeJS.Timeout>(); const [correctionTimeout, setCorrectionTimeout] = useState<NodeJS.Timeout>();
const [correctionAbortController, setCorrectionAbortController] = useState<AbortController | null>(null);
const { resolvedTheme, setTheme } = useTheme(); const { resolvedTheme, setTheme } = useTheme();
@@ -339,37 +338,22 @@ $current`);
// Debounced text correction for previous note // Debounced text correction for previous note
const debouncedCorrectPreviousNote = (content: string) => { const debouncedCorrectPreviousNote = (content: string) => {
// Cancel any pending correction request
if (correctionAbortController) {
try {
correctionAbortController.abort();
} catch (error) {
// Ignore abort errors - the controller might already be aborted
}
}
if (correctionTimeout) { if (correctionTimeout) {
clearTimeout(correctionTimeout); clearTimeout(correctionTimeout);
} }
// Create new AbortController for this request
const abortController = new AbortController();
setCorrectionAbortController(abortController);
const timeout = setTimeout(async () => { const timeout = setTimeout(async () => {
if (content.trim()) { if (content.trim()) {
try { try {
addDebugInfo('Correcting previous note text...'); addDebugInfo('Correcting previous note text...');
const corrected = await correctText(content, abortController); const corrected = await correctText(content);
addDebugInfo(`Received correction: ${corrected.substring(0, 50)}...`);
setPreviousNoteCorrectedContent(corrected); setPreviousNoteCorrectedContent(corrected);
addDebugInfo('Previous note corrected content set');
} catch (error) { } catch (error) {
if (error instanceof Error && error.name === 'AbortError') { console.error('Previous note text correction failed:', error);
addDebugInfo('Previous note correction cancelled'); addDebugInfo(`Correction error: ${error instanceof Error ? error.message : 'Unknown error'}`);
return; // Don't set empty content for aborted requests setPreviousNoteCorrectedContent('');
} else {
console.error('Previous note text correction failed:', error);
setPreviousNoteCorrectedContent('');
}
} }
} else { } else {
setPreviousNoteCorrectedContent(''); setPreviousNoteCorrectedContent('');
@@ -380,7 +364,7 @@ $current`);
}; };
// Correct text using Ollama // Correct text using Ollama
const correctText = async (content: string, abortController?: AbortController): Promise<string> => { const correctText = async (content: string): Promise<string> => {
try { try {
const response = await fetchWithTiming(`${ollamaEndpoint}/api/generate`, { const response = await fetchWithTiming(`${ollamaEndpoint}/api/generate`, {
method: 'POST', method: 'POST',
@@ -406,7 +390,6 @@ ${content}`,
keep_alive: ollamaKeepAlive, keep_alive: ollamaKeepAlive,
temperature: 0.1, temperature: 0.1,
}), }),
signal: abortController?.signal,
}, 'Correct Text'); }, 'Correct Text');
if (!response.ok) { if (!response.ok) {
@@ -421,7 +404,7 @@ ${content}`,
throw new Error('Empty response from Ollama - check if model is loaded'); throw new Error('Empty response from Ollama - check if model is loaded');
} }
addDebugInfo(`Text corrected successfully`); addDebugInfo(`Text corrected successfully: ${correctedText.substring(0, 50)}...`);
return correctedText; return correctedText;
} catch (error) { } catch (error) {
console.error('Error correcting text:', error); console.error('Error correcting text:', error);
@@ -1189,16 +1172,6 @@ ${content}`,
const reversedDelta = -delta; const reversedDelta = -delta;
const newIndex = Math.max(0, Math.min(currentNoteIndex + reversedDelta, noteCache.length - 1)); const newIndex = Math.max(0, Math.min(currentNoteIndex + reversedDelta, noteCache.length - 1));
if (newIndex !== currentNoteIndex && noteCache[newIndex]) { if (newIndex !== currentNoteIndex && noteCache[newIndex]) {
// Cancel any pending correction request before changing notes
if (correctionAbortController) {
try {
correctionAbortController.abort();
} catch (error) {
// Ignore abort errors - the controller might already be aborted
}
setCorrectionAbortController(null);
}
setCurrentNoteIndex(newIndex); setCurrentNoteIndex(newIndex);
setPreviousNote(noteCache[newIndex]); setPreviousNote(noteCache[newIndex]);
setIsPreviousNoteModified(false); setIsPreviousNoteModified(false);
@@ -1219,9 +1192,15 @@ ${content}`,
// Handle wheel event for previous note navigation // Handle wheel event for previous note navigation
useEffect(() => { useEffect(() => {
const handleWheel = (e: WheelEvent) => { const handleWheel = (e: WheelEvent) => {
if (previousNoteRef.current?.contains(e.target as Node)) { // Check if we're in the previous note area (either left or right pane)
const target = e.target as HTMLElement;
const isInPreviousNoteArea = previousNoteRef.current?.contains(target) ||
target.closest('[data-previous-note-area]');
if (isInPreviousNoteArea) {
e.preventDefault(); e.preventDefault();
const delta = e.deltaY > 0 ? 1 : -1; const delta = e.deltaY > 0 ? 1 : -1;
addDebugInfo(`Previous note scroll triggered: delta=${delta}`);
handlePreviousNoteScroll(delta); handlePreviousNoteScroll(delta);
} }
}; };
@@ -1385,14 +1364,7 @@ ${content}`,
if (correctionTimeout) { if (correctionTimeout) {
clearTimeout(correctionTimeout); clearTimeout(correctionTimeout);
} }
// Cancel any pending correction request on cleanup // Cleanup complete
if (correctionAbortController) {
try {
correctionAbortController.abort();
} catch (error) {
// Ignore abort errors - the controller might already be aborted
}
}
}; };
}, [currentNote, previousNote, scratchPad, isPreviousNoteModified, isSearchOpen, isGotoOpen, isCleanupOpen]); }, [currentNote, previousNote, scratchPad, isPreviousNoteModified, isSearchOpen, isGotoOpen, isCleanupOpen]);
@@ -2516,6 +2488,7 @@ ${content}`,
{/* Previous Note - Top Half - Split Layout */} {/* Previous Note - Top Half - Split Layout */}
<div <div
ref={previousNoteRef} ref={previousNoteRef}
data-previous-note-area
className="flex-1 flex gap-4 overflow-hidden cursor-pointer select-none" className="flex-1 flex gap-4 overflow-hidden cursor-pointer select-none"
> >
{/* Left Side - Original Previous Note */} {/* Left Side - Original Previous Note */}
@@ -2565,19 +2538,21 @@ ${content}`,
</div> </div>
<Textarea <Textarea
value={previousNote.content} value={previousNote.content}
onChange={(e) => { onChange={(e) => {
const newContent = e.target.value; const newContent = e.target.value;
const updatedNote = { ...previousNote, content: newContent }; const updatedNote = { ...previousNote, content: newContent };
setPreviousNote(updatedNote); setPreviousNote(updatedNote);
setIsPreviousNoteModified(true); setIsPreviousNoteModified(true);
// Debounced tag generation for previous notes // Debounced tag generation for previous notes
if (autoGenerateTags) { if (autoGenerateTags) {
debouncedGenerateTags(newContent, currentNoteIndex); debouncedGenerateTags(newContent, currentNoteIndex);
} }
if (newContent.trim() === '') { // Debounced text correction for previous notes
deleteNote(previousNote.id); debouncedCorrectPreviousNote(newContent);
} if (newContent.trim() === '') {
}} deleteNote(previousNote.id);
}
}}
onBlur={handlePreviousNoteBlur} onBlur={handlePreviousNoteBlur}
className={`h-[calc(100%-3rem)] border-0 resize-none focus:ring-0 bg-transparent ${getTextClass('2xl')}`} className={`h-[calc(100%-3rem)] border-0 resize-none focus:ring-0 bg-transparent ${getTextClass('2xl')}`}
placeholder="Previous entry content..." placeholder="Previous entry content..."