Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

feat: add model validation on delete/load and download cancellation for whisper pickers

Gabriel Stein365edd4ddd35
diff --git a/src/components/LocalWhisperPicker.tsx b/src/components/LocalWhisperPicker.tsx
index 274391df2..f9dc57826 100644
--- a/src/components/LocalWhisperPicker.tsx
+++ b/src/components/LocalWhisperPicker.tsx
@@ -33,24 +33,47 @@ export default function LocalWhisperPicker({
const [models, setModels] = useState<WhisperModel[]>([]);
const hasLoadedRef = useRef(false);
const downloadingModelRef = useRef<string | null>(null);
+ const selectedModelRef = useRef(selectedModel);
+ const onModelSelectRef = useRef(onModelSelect);
const { confirmDialog, showConfirmDialog, hideConfirmDialog } = useDialogs();
const colorScheme: ColorScheme = variant === "settings" ? "purple" : "blue";
const styles = useMemo(() => MODEL_PICKER_COLORS[colorScheme], [colorScheme]);
+ useEffect(() => {
+ selectedModelRef.current = selectedModel;
+ }, [selectedModel]);
+ useEffect(() => {
+ onModelSelectRef.current = onModelSelect;
+ }, [onModelSelect]);
+
+ const validateAndSelectModel = useCallback((loadedModels: WhisperModel[]) => {
+ const current = selectedModelRef.current;
+ if (!current) return;
+
+ const downloaded = loadedModels.filter((m) => m.downloaded);
+ const isCurrentDownloaded = loadedModels.find((m) => m.model === current)?.downloaded;
+
+ if (!isCurrentDownloaded && downloaded.length > 0) {
+ onModelSelectRef.current(downloaded[0].model);
+ } else if (!isCurrentDownloaded && downloaded.length === 0) {
+ onModelSelectRef.current("");
+ }
+ }, []);
+
const loadModels = useCallback(async () => {
try {
const result = await window.electronAPI?.listWhisperModels();
if (result?.success) {
setModels(result.models);
+ validateAndSelectModel(result.models);
}
} catch (error) {
console.error("[LocalWhisperPicker] Failed to load models:", error);
setModels([]);
}
- }, []);
+ }, [validateAndSelectModel]);
- // Only load models once on mount to prevent re-renders
useEffect(() => {
if (!hasLoadedRef.current) {
hasLoadedRef.current = true;
@@ -70,7 +93,6 @@ export default function LocalWhisperPicker({
modelType: "whisper",
onDownloadComplete: () => {
loadModels();
- // Notify parent when a model is downloaded (use ref to avoid stale closure)
if (downloadingModelRef.current && onModelDownloaded) {
onModelDownloaded(downloadingModelRef.current);
}
@@ -78,7 +100,6 @@ export default function LocalWhisperPicker({
onModelsCleared: loadModels,
});
- // Keep ref in sync with downloadingModel to avoid stale closure in onDownloadComplete
useEffect(() => {
downloadingModelRef.current = downloadingModel;
}, [downloadingModel]);
@@ -89,21 +110,30 @@ export default function LocalWhisperPicker({
title: "Delete Model",
description:
"Are you sure you want to delete this model? You'll need to re-download it if you want to use it again.",
- onConfirm: () => deleteModel(modelId, loadModels),
+ onConfirm: async () => {
+ await deleteModel(modelId, async () => {
+ const result = await window.electronAPI?.listWhisperModels();
+ if (result?.success) {
+ setModels(result.models);
+ validateAndSelectModel(result.models);
+ }
+ });
+ },
variant: "destructive",
});
},
- [showConfirmDialog, deleteModel, loadModels]
+ [showConfirmDialog, deleteModel, validateAndSelectModel]
);
const progressDisplay = useMemo(() => {
if (!downloadingModel) return null;
-
const modelInfo = WHISPER_MODEL_INFO[downloadingModel];
- const modelName = modelInfo?.name || downloadingModel;
-
return (
- <DownloadProgressBar modelName={modelName} progress={downloadProgress} styles={styles} />
+ <DownloadProgressBar
+ modelName={modelInfo?.name || downloadingModel}
+ progress={downloadProgress}
+ styles={styles}
+ />
);
}, [downloadingModel, downloadProgress, styles]);
diff --git a/src/components/TranscriptionModelPicker.tsx b/src/components/TranscriptionModelPicker.tsx
index 3f58933d2..6cc8689d8 100644
--- a/src/components/TranscriptionModelPicker.tsx
+++ b/src/components/TranscriptionModelPicker.tsx
@@ -1,6 +1,6 @@
import { useState, useEffect, useCallback, useMemo, useRef } from "react";
import { Button } from "./ui/button";
-import { Download, Trash2, Check, Cloud, Lock } from "lucide-react";
+import { Download, Trash2, Check, Cloud, Lock, X } from "lucide-react";
import { ProviderIcon } from "./ui/ProviderIcon";
import { ProviderTabs } from "./ui/ProviderTabs";
import ModelCardList from "./ui/ModelCardList";
@@ -78,15 +78,36 @@ export default function TranscriptionModelPicker({
const isLoadingRef = useRef(false);
const loadLocalModelsRef = useRef<(() => Promise<void>) | null>(null);
const ensureValidCloudSelectionRef = useRef<(() => void) | null>(null);
+ const selectedLocalModelRef = useRef(selectedLocalModel);
+ const onLocalModelSelectRef = useRef(onLocalModelSelect);
const { confirmDialog, showConfirmDialog, hideConfirmDialog } = useDialogs();
const colorScheme: ColorScheme = variant === "settings" ? "purple" : "blue";
const styles = useMemo(() => MODEL_PICKER_COLORS[colorScheme], [colorScheme]);
-
const cloudProviders = useMemo(() => getTranscriptionProviders(), []);
+ useEffect(() => {
+ selectedLocalModelRef.current = selectedLocalModel;
+ }, [selectedLocalModel]);
+ useEffect(() => {
+ onLocalModelSelectRef.current = onLocalModelSelect;
+ }, [onLocalModelSelect]);
+
+ const validateAndSelectModel = useCallback((loadedModels: WhisperModel[]) => {
+ const current = selectedLocalModelRef.current;
+ if (!current) return;
+
+ const downloaded = loadedModels.filter((m) => m.downloaded);
+ const isCurrentDownloaded = loadedModels.find((m) => m.model === current)?.downloaded;
+
+ if (!isCurrentDownloaded && downloaded.length > 0) {
+ onLocalModelSelectRef.current(downloaded[0].model);
+ } else if (!isCurrentDownloaded && downloaded.length === 0) {
+ onLocalModelSelectRef.current("");
+ }
+ }, []);
+
const loadLocalModels = useCallback(async () => {
- // Prevent concurrent loading
if (isLoadingRef.current) return;
isLoadingRef.current = true;
@@ -94,6 +115,7 @@ export default function TranscriptionModelPicker({
const result = await window.electronAPI?.listWhisperModels();
if (result?.success) {
setLocalModels(result.models);
+ validateAndSelectModel(result.models);
}
} catch (error) {
console.error("[TranscriptionModelPicker] Failed to load models:", error);
@@ -101,7 +123,7 @@ export default function TranscriptionModelPicker({
} finally {
isLoadingRef.current = false;
}
- }, []);
+ }, [validateAndSelectModel]);
const ensureValidCloudSelection = useCallback(() => {
const isValidProvider = VALID_CLOUD_PROVIDER_IDS.includes(selectedCloudProvider);
@@ -128,16 +150,13 @@ export default function TranscriptionModelPicker({
onCloudModelSelect,
]);
- // Keep refs in sync to avoid stale closures in the mode-switching effect
useEffect(() => {
loadLocalModelsRef.current = loadLocalModels;
}, [loadLocalModels]);
-
useEffect(() => {
ensureValidCloudSelectionRef.current = ensureValidCloudSelection;
}, [ensureValidCloudSelection]);
- // Only load models once on mount when in local mode, or when switching to local mode
useEffect(() => {
if (useLocalWhisper) {
if (!hasLoadedRef.current) {
@@ -145,7 +164,7 @@ export default function TranscriptionModelPicker({
loadLocalModelsRef.current?.();
}
} else {
- hasLoadedRef.current = false; // Reset when switching to cloud
+ hasLoadedRef.current = false;
ensureValidCloudSelectionRef.current?.();
}
}, [useLocalWhisper]);
@@ -156,18 +175,23 @@ export default function TranscriptionModelPicker({
return () => window.removeEventListener("openwhispr-models-cleared", handleModelsCleared);
}, [loadLocalModels]);
- const { downloadingModel, downloadProgress, downloadModel, deleteModel, isDownloadingModel } =
- useModelDownload({
- modelType: "whisper",
- onDownloadComplete: loadLocalModels,
- });
+ const {
+ downloadingModel,
+ downloadProgress,
+ downloadModel,
+ deleteModel,
+ isDownloadingModel,
+ cancelDownload,
+ isCancelling,
+ } = useModelDownload({
+ modelType: "whisper",
+ onDownloadComplete: loadLocalModels,
+ });
const handleModeChange = useCallback(
(isLocal: boolean) => {
onModeChange(isLocal);
- if (!isLocal) {
- ensureValidCloudSelection();
- }
+ if (!isLocal) ensureValidCloudSelection();
},
[onModeChange, ensureValidCloudSelection]
);
@@ -187,7 +211,6 @@ export default function TranscriptionModelPicker({
(providerId: string) => {
const tab = LOCAL_PROVIDER_TABS.find((t) => t.id === providerId);
if (tab?.disabled) return;
-
setInternalLocalProvider(providerId);
onLocalProviderSelect?.(providerId);
},
@@ -200,16 +223,25 @@ export default function TranscriptionModelPicker({
title: "Delete Model",
description:
"Are you sure you want to delete this model? You'll need to re-download it if you want to use it again.",
- onConfirm: () => deleteModel(modelId, loadLocalModels),
+ onConfirm: async () => {
+ await deleteModel(modelId, async () => {
+ const result = await window.electronAPI?.listWhisperModels();
+ if (result?.success) {
+ setLocalModels(result.models);
+ validateAndSelectModel(result.models);
+ }
+ });
+ },
variant: "destructive",
});
},
- [showConfirmDialog, deleteModel, loadLocalModels]
+ [showConfirmDialog, deleteModel, validateAndSelectModel]
);
- const currentCloudProvider = useMemo<TranscriptionProviderData | undefined>(() => {
- return cloudProviders.find((p) => p.id === selectedCloudProvider);
- }, [cloudProviders, selectedCloudProvider]);
+ const currentCloudProvider = useMemo<TranscriptionProviderData | undefined>(
+ () => cloudProviders.find((p) => p.id === selectedCloudProvider),
+ [cloudProviders, selectedCloudProvider]
+ );
const cloudModelOptions = useMemo(() => {
if (!currentCloudProvider) return [];
@@ -223,108 +255,111 @@ export default function TranscriptionModelPicker({
const progressDisplay = useMemo(() => {
if (!downloadingModel || !useLocalWhisper) return null;
-
const modelInfo = WHISPER_MODEL_INFO[downloadingModel];
- const modelName = modelInfo?.name || downloadingModel;
-
return (
- <DownloadProgressBar modelName={modelName} progress={downloadProgress} styles={styles} />
+ <DownloadProgressBar
+ modelName={modelInfo?.name || downloadingModel}
+ progress={downloadProgress}
+ styles={styles}
+ />
);
}, [downloadingModel, downloadProgress, useLocalWhisper, styles]);
- const renderLocalModels = () => {
- return (
- <div className="space-y-2">
- {localModels.map((model) => {
- const modelId = model.model;
- const info = WHISPER_MODEL_INFO[modelId] || {
- name: modelId,
- description: "Model",
- size: "Unknown",
- };
- const isSelected = modelId === selectedLocalModel;
- const isDownloading = isDownloadingModel(modelId);
- const isDownloaded = model.downloaded;
-
- return (
- <div
- key={modelId}
- className={`p-3 rounded-lg border-2 transition-all ${
- isSelected ? styles.modelCard.selected : styles.modelCard.default
- }`}
- >
- <div className="flex items-center justify-between">
- <div className="flex-1">
- <div className="flex items-center gap-2">
- <ProviderIcon provider="whisper" className="w-4 h-4" />
- <span className="font-medium text-gray-900">{info.name}</span>
- {isSelected && <span className={styles.badges.selected}>✓ Selected</span>}
- {info.recommended && (
- <span className={styles.badges.recommended}>Recommended</span>
- )}
- </div>
- <div className="flex items-center gap-2 mt-1">
- <span className="text-xs text-gray-600">{info.description}</span>
- <span className="text-xs text-gray-500">
- • {model.size_mb ? `${model.size_mb}MB` : info.size}
+ const renderLocalModels = () => (
+ <div className="space-y-2">
+ {localModels.map((model) => {
+ const modelId = model.model;
+ const info = WHISPER_MODEL_INFO[modelId] || {
+ name: modelId,
+ description: "Model",
+ size: "Unknown",
+ };
+ const isSelected = modelId === selectedLocalModel;
+ const isDownloading = isDownloadingModel(modelId);
+ const isDownloaded = model.downloaded;
+
+ return (
+ <div
+ key={modelId}
+ className={`p-3 rounded-lg border-2 transition-all ${
+ isSelected ? styles.modelCard.selected : styles.modelCard.default
+ }`}
+ >
+ <div className="flex items-center justify-between">
+ <div className="flex-1">
+ <div className="flex items-center gap-2">
+ <ProviderIcon provider="whisper" className="w-4 h-4" />
+ <span className="font-medium text-gray-900">{info.name}</span>
+ {isSelected && <span className={styles.badges.selected}>✓ Selected</span>}
+ {info.recommended && (
+ <span className={styles.badges.recommended}>Recommended</span>
+ )}
+ </div>
+ <div className="flex items-center gap-2 mt-1">
+ <span className="text-xs text-gray-600">{info.description}</span>
+ <span className="text-xs text-gray-500">
+ • {model.size_mb ? `${model.size_mb}MB` : info.size}
+ </span>
+ {isDownloaded && (
+ <span className={styles.badges.downloaded}>
+ <Check className="inline w-3 h-3 mr-1" />
+ Downloaded
</span>
- {isDownloaded && (
- <span className={styles.badges.downloaded}>
- <Check className="inline w-3 h-3 mr-1" />
- Downloaded
- </span>
- )}
- </div>
+ )}
</div>
+ </div>
- <div className="flex gap-2">
- {isDownloaded ? (
- <>
- {!isSelected && (
- <Button
- onClick={() => onLocalModelSelect(modelId)}
- size="sm"
- variant="outline"
- className={styles.buttons.select}
- >
- Select
- </Button>
- )}
+ <div className="flex gap-2">
+ {isDownloaded ? (
+ <>
+ {!isSelected && (
<Button
- onClick={() => handleDelete(modelId)}
+ onClick={() => onLocalModelSelect(modelId)}
size="sm"
variant="outline"
- className={styles.buttons.delete}
+ className={styles.buttons.select}
>
- <Trash2 size={14} />
- <span className="ml-1">Delete</span>
+ Select
</Button>
- </>
- ) : (
+ )}
<Button
- onClick={() => downloadModel(modelId, onLocalModelSelect)}
+ onClick={() => handleDelete(modelId)}
size="sm"
- disabled={isDownloading}
- className={styles.buttons.download}
+ variant="outline"
+ className={styles.buttons.delete}
>
- {isDownloading ? (
- `${Math.round(downloadProgress.percentage)}%`
- ) : (
- <>
- <Download size={14} />
- <span className="ml-1">Download</span>
- </>
- )}
+ <Trash2 size={14} />
+ <span className="ml-1">Delete</span>
</Button>
- )}
- </div>
+ </>
+ ) : isDownloading ? (
+ <Button
+ onClick={cancelDownload}
+ disabled={isCancelling}
+ size="sm"
+ variant="outline"
+ className="text-red-600 border-red-300 hover:bg-red-50"
+ >
+ <X size={14} />
+ <span className="ml-1">{isCancelling ? "..." : "Cancel"}</span>
+ </Button>
+ ) : (
+ <Button
+ onClick={() => downloadModel(modelId, onLocalModelSelect)}
+ size="sm"
+ className={styles.buttons.download}
+ >
+ <Download size={14} />
+ <span className="ml-1">Download</span>
+ </Button>
+ )}
</div>
</div>
- );
- })}
- </div>
- );
- };
+ </div>
+ );
+ })}
+ </div>
+ );
const renderLocalProviderTab = (
provider: (typeof LOCAL_PROVIDER_TABS)[0],