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