Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.
JavaScript aianthropiccross-platformgeminigroqlinuxmacosnvidiaopen-sourceopenaiparakeetspeech-to-texttranscribewhisperwindows
refactor: clean up API key persistence and add recovery mechanism
Gabriel Steinccdff0125012
diff --git a/src/components/OnboardingFlow.tsx b/src/components/OnboardingFlow.tsxindex 2dd66ef33..1eef460d4 100644--- a/src/components/OnboardingFlow.tsx+++ b/src/components/OnboardingFlow.tsx@@ -174,9 +174,7 @@ export default function OnboardingFlow({ onComplete }: OnboardingFlowProps) {}try {- if (window.electronAPI?.saveOpenAIKey) {- await window.electronAPI.saveOpenAIKey(trimmedKey);- }+ // updateApiKeys handles both localStorage and IPC saveupdateApiKeys({ openaiApiKey: trimmedKey });return true;} catch (error) {diff --git a/src/helpers/environment.js b/src/helpers/environment.jsindex 8c8a8d144..d6ffb6c12 100644--- a/src/helpers/environment.js+++ b/src/helpers/environment.js@@ -43,16 +43,11 @@ class EnvironmentManager {}saveOpenAIKey(key) {- try {- // Update the environment variable in memory for immediate use- process.env.OPENAI_API_KEY = key;- // Persist all keys to file- this.saveAllKeysToEnvFile();- return { success: true };- } catch (error) {- // Silent error - already throwing- throw error;- }+ // Update the environment variable in memory for immediate use+ process.env.OPENAI_API_KEY = key;+ // Persist all keys to file+ this.saveAllKeysToEnvFile();+ return { success: true };}getAnthropicKey() {@@ -61,16 +56,11 @@ class EnvironmentManager {}saveAnthropicKey(key) {- try {- // Update the environment variable in memory for immediate use- process.env.ANTHROPIC_API_KEY = key;- // Persist all keys to file- this.saveAllKeysToEnvFile();- return { success: true };- } catch (error) {- // Silent error - already throwing- throw error;- }+ // Update the environment variable in memory for immediate use+ process.env.ANTHROPIC_API_KEY = key;+ // Persist all keys to file+ this.saveAllKeysToEnvFile();+ return { success: true };}getGeminiKey() {@@ -79,16 +69,11 @@ class EnvironmentManager {}saveGeminiKey(key) {- try {- // Update the environment variable in memory for immediate use- process.env.GEMINI_API_KEY = key;- // Persist all keys to file- this.saveAllKeysToEnvFile();- return { success: true };- } catch (error) {- // Silent error - already throwing- throw error;- }+ // Update the environment variable in memory for immediate use+ process.env.GEMINI_API_KEY = key;+ // Persist all keys to file+ this.saveAllKeysToEnvFile();+ return { success: true };}getGroqKey() {@@ -97,36 +82,26 @@ class EnvironmentManager {}saveGroqKey(key) {- try {- // Update the environment variable in memory for immediate use- process.env.GROQ_API_KEY = key;- // Persist all keys to file- this.saveAllKeysToEnvFile();- return { success: true };- } catch (error) {- // Silent error - already throwing- throw error;- }+ // Update the environment variable in memory for immediate use+ process.env.GROQ_API_KEY = key;+ // Persist all keys to file+ this.saveAllKeysToEnvFile();+ return { success: true };}createProductionEnvFile(apiKey) {- try {- const envPath = path.join(app.getPath("userData"), ".env");+ const envPath = path.join(app.getPath("userData"), ".env");- const envContent = `# OpenWhispr Environment Variables+ const envContent = `# OpenWhispr Environment Variables# This file was created automatically for production useOPENAI_API_KEY=${apiKey}`;- fs.writeFileSync(envPath, envContent, "utf8");+ fs.writeFileSync(envPath, envContent, "utf8");- require("dotenv").config({ path: envPath });+ require("dotenv").config({ path: envPath });- return { success: true, path: envPath };- } catch (error) {- // Silent error - already throwing- throw error;- }+ return { success: true, path: envPath };}saveAllKeysToEnvFile() {diff --git a/src/hooks/useSettings.ts b/src/hooks/useSettings.tsindex b7722501d..fff7c6542 100644--- a/src/hooks/useSettings.ts+++ b/src/hooks/useSettings.ts@@ -1,4 +1,4 @@-import { useCallback } from "react";+import { useCallback, useEffect, useRef } from "react";import { useLocalStorage } from "./useLocalStorage";import { getModelProvider } from "../models/ModelRegistry";import { API_ENDPOINTS } from "../config/constants";@@ -147,6 +147,40 @@ export function useSettings() {deserialize: String,});+ // Sync API keys from main process on first mount (if localStorage was cleared)+ const hasRunApiKeySync = useRef(false);+ useEffect(() => {+ if (hasRunApiKeySync.current) return;+ hasRunApiKeySync.current = true;++ const syncKeys = async () => {+ if (typeof window === "undefined" || !window.electronAPI) return;++ // Only sync keys that are missing from localStorage+ if (!openaiApiKey) {+ const envKey = await window.electronAPI.getOpenAIKey?.();+ if (envKey) setOpenaiApiKeyLocal(envKey);+ }+ if (!anthropicApiKey) {+ const envKey = await window.electronAPI.getAnthropicKey?.();+ if (envKey) setAnthropicApiKeyLocal(envKey);+ }+ if (!geminiApiKey) {+ const envKey = await window.electronAPI.getGeminiKey?.();+ if (envKey) setGeminiApiKeyLocal(envKey);+ }+ if (!groqApiKey) {+ const envKey = await window.electronAPI.getGroqKey?.();+ if (envKey) setGroqApiKeyLocal(envKey);+ }+ };++ syncKeys().catch(() => {+ // Silently ignore sync errors+ });+ // eslint-disable-next-line react-hooks/exhaustive-deps+ }, []); // Run only once on mount - ref ensures this never runs twice+// Wrapped setters that sync to Electron IPC and invalidate cacheconst setOpenaiApiKey = useCallback((key: string) => {