Fix Ollama AI menu visibility and cancel stale stream listeners on menu close

This commit is contained in:
Yuri Karamian committed 2026-02-25 02:05:30 +01:00
1 parent 1440bdcd90
commit 61fa9cf1d8
1 file changed
+11 -2
+11 -2
View File
@@ -110,6 +110,7 @@
let aiEmptyNote = $state(false); let aiEmptyNote = $state(false);
let aiOriginalMarkdown = $state(''); let aiOriginalMarkdown = $state('');
let aiMediaPlaceholders = $state<Map<string, string>>(new Map()); let aiMediaPlaceholders = $state<Map<string, string>>(new Map());
let aiStreamUnlisten: (() => void) | null = null;
// View mode (read-only) — state managed by $readOnly store // View mode (read-only) — state managed by $readOnly store
@@ -2516,6 +2517,7 @@
} }
function closeAiMenu() { function closeAiMenu() {
if (aiStreamUnlisten) { aiStreamUnlisten(); aiStreamUnlisten = null; }
aiMenu = null; aiMenu = null;
aiLoading = false; aiLoading = false;
aiResult = null; aiResult = null;
@@ -2543,6 +2545,9 @@
aiShowCustom = false; aiShowCustom = false;
aiTranslateMenu = false; aiTranslateMenu = false;
// Cancel any previous stream listener
if (aiStreamUnlisten) { aiStreamUnlisten(); aiStreamUnlisten = null; }
const requestId = crypto.randomUUID(); const requestId = crypto.randomUUID();
const unlisten = await listen<AiStreamEvent>('ai-stream', (event) => { const unlisten = await listen<AiStreamEvent>('ai-stream', (event) => {
const data = event.payload; const data = event.payload;
@@ -2550,13 +2555,16 @@
aiResult = (aiResult ?? '') + data.text; aiResult = (aiResult ?? '') + data.text;
} else if (data.event_type === 'done') { } else if (data.event_type === 'done') {
aiLoading = false; aiLoading = false;
aiStreamUnlisten = null;
unlisten(); unlisten();
} else if (data.event_type === 'error') { } else if (data.event_type === 'error') {
aiError = data.error ?? 'Unknown error'; aiError = data.error ?? 'Unknown error';
aiLoading = false; aiLoading = false;
aiStreamUnlisten = null;
unlisten(); unlisten();
} }
}); });
aiStreamUnlisten = unlisten;
try { try {
await aiAsk(action, aiSelectedText, customPrompt ?? null, requestId); await aiAsk(action, aiSelectedText, customPrompt ?? null, requestId);
@@ -2564,6 +2572,7 @@
aiError = String(e); aiError = String(e);
aiLoading = false; aiLoading = false;
unlisten(); unlisten();
aiStreamUnlisten = null;
} }
} }
@@ -3125,7 +3134,7 @@
</svg> </svg>
</button> </button>
{/if} {/if}
{#if $appConfig?.ai_provider && ($appConfig?.ai_api_key || $appConfig?.openai_api_key)} {#if $appConfig?.ai_provider && ($appConfig?.ai_provider === 'ollama' || $appConfig?.ai_api_key || $appConfig?.openai_api_key)}
<button <button
class="icon-btn" class="icon-btn"
onclick={openAiMenu} onclick={openAiMenu}
@@ -3959,7 +3968,7 @@
<svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="7" height="7" rx="1.5"/><polyline points="4.5 6.5 6 8 8.5 4.5"/><line x1="13" y1="6.5" x2="21" y2="6.5"/><rect x="3" y="14" width="7" height="7" rx="1.5"/><line x1="13" y1="17.5" x2="21" y2="17.5"/></svg> <svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="7" height="7" rx="1.5"/><polyline points="4.5 6.5 6 8 8.5 4.5"/><line x1="13" y1="6.5" x2="21" y2="6.5"/><rect x="3" y="14" width="7" height="7" rx="1.5"/><line x1="13" y1="17.5" x2="21" y2="17.5"/></svg>
Task List Task List
</button> </button>
{#if $appConfig?.ai_provider && ($appConfig?.ai_api_key || $appConfig?.openai_api_key)} {#if $appConfig?.ai_provider && ($appConfig?.ai_provider === 'ollama' || $appConfig?.ai_api_key || $appConfig?.openai_api_key)}
<div class="text-ctx-sep"></div> <div class="text-ctx-sep"></div>
<button onclick={openAiMenu}> <button onclick={openAiMenu}>
<svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">