From a462a2246831abf3fc8ed32173cfbcb96f62a234 Mon Sep 17 00:00:00 2001 From: TheMaddax Date: Sat, 25 Jan 2025 21:45:02 -0600 Subject: [PATCH] Create settings --- src/App.tsx | 43 ++++++--- src/components/GeminiSettings.tsx | 145 ++++++------------------------ src/components/Settings.tsx | 107 ++++++++++++++++++++++ 3 files changed, 165 insertions(+), 130 deletions(-) create mode 100644 src/components/Settings.tsx diff --git a/src/App.tsx b/src/App.tsx index 9335cc0..236ee92 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,7 +1,8 @@ import { useState, useEffect } from 'react'; import { invoke } from '@tauri-apps/api/core'; import { FileSelector } from './components/FileSelector'; -import { GeminiSettings, GeminiConfig } from './components/GeminiSettings'; +import { GeminiSettings } from './components/GeminiSettings'; +import { Settings } from './components/Settings'; import { VideoTimeline } from './components/VideoTimeline'; import './styles/index.css'; @@ -29,14 +30,12 @@ function App() { const [error, setError] = useState(null); const [addBlackBar, setAddBlackBar] = useState(true); const [resizeTo720p, setResizeTo720p] = useState(true); - const [geminiConfig, setGeminiConfig] = useState({ - enabled: false, - apiKey: 'AIzaSyAF825tPTh77oL0knsGFEyvsN0iPUO_bXc', - isDefault: true - }); + const [geminiEnabled, setGeminiEnabled] = useState(false); + const [apiKey, setApiKey] = useState('AIzaSyAF825tPTh77oL0knsGFEyvsN0iPUO_bXc'); const [selectedTime, setSelectedTime] = useState(10); const [transcription, setTranscription] = useState(null); const [thumbnails, setThumbnails] = useState([]); + const [isSettingsOpen, setIsSettingsOpen] = useState(false); useEffect(() => { invoke('check_dependencies') @@ -97,12 +96,12 @@ function App() { subtitlePath: subtitleFile.path, addBlackBar, resizeTo720p, - geminiEnabled: geminiConfig.enabled, + geminiEnabled, frameTime: selectedTime }); // Process transcription first if Gemini is enabled - if (geminiConfig.enabled) { + if (geminiEnabled) { try { console.log('Starting transcription process...'); const transcriptionResult = await invoke('process_transcription', { @@ -110,7 +109,7 @@ function App() { video_path: videoFile.path, subtitle_path: subtitleFile.path, frame_time: selectedTime, - api_key: geminiConfig.apiKey + api_key: apiKey } }); console.log('Transcription completed with full response:', transcriptionResult); @@ -180,7 +179,19 @@ function App() { return (
-

Video Subtitle Merger 2.0

+
+

Video Subtitle Merger 3.0

+ +
@@ -208,12 +219,13 @@ function App() {
- {geminiConfig.enabled && videoFile && ( + {geminiEnabled && videoFile && (
+ + setIsSettingsOpen(false)} + apiKey={apiKey} + onApiKeyChange={setApiKey} + />
); } diff --git a/src/components/GeminiSettings.tsx b/src/components/GeminiSettings.tsx index 46951d6..68655df 100644 --- a/src/components/GeminiSettings.tsx +++ b/src/components/GeminiSettings.tsx @@ -2,73 +2,25 @@ import React, { useState, useCallback } from 'react'; import { invoke } from '@tauri-apps/api/core'; interface GeminiSettingsProps { - onSettingsChange: (settings: GeminiConfig) => void; + enabled: boolean; + onEnableChange: (enabled: boolean) => void; videoFile: string | null; onThumbnailsGenerated: (thumbnails: { path: string; time: number; }[]) => void; } -export interface GeminiConfig { - enabled: boolean; - apiKey: string; - isDefault: boolean; -} - -const DEFAULT_API_KEY = 'AIzaSyAF825tPTh77oL0knsGFEyvsN0iPUO_bXc'; - -export const GeminiSettings: React.FC = ({ onSettingsChange, videoFile, onThumbnailsGenerated }) => { - const [settings, setSettings] = useState({ - enabled: false, - apiKey: DEFAULT_API_KEY, - isDefault: true - }); - const [testing, setTesting] = useState(false); - const [testResult, setTestResult] = useState(null); +export const GeminiSettings: React.FC = ({ + enabled, + onEnableChange, + videoFile, + onThumbnailsGenerated +}) => { const [generatingThumbnails, setGeneratingThumbnails] = useState(false); const [thumbnailResult, setThumbnailResult] = useState(null); const handleEnableChange = (e: React.ChangeEvent) => { - const newSettings = { - ...settings, - enabled: e.target.checked - }; - setSettings(newSettings); - onSettingsChange(newSettings); + onEnableChange(e.target.checked); }; - const handleApiKeyChange = (e: React.ChangeEvent) => { - const newSettings = { - ...settings, - apiKey: e.target.value, - isDefault: e.target.value === DEFAULT_API_KEY - }; - setSettings(newSettings); - onSettingsChange(newSettings); - }; - - const resetToDefault = () => { - const newSettings = { - ...settings, - apiKey: DEFAULT_API_KEY, - isDefault: true - }; - setSettings(newSettings); - onSettingsChange(newSettings); - }; - - const testApiKey = useCallback(async () => { - setTesting(true); - setTestResult(null); - try { - // We'll implement this Rust command later - await invoke('test_gemini_api', { apiKey: settings.apiKey }); - setTestResult('API key is valid'); - } catch (error) { - setTestResult(`Error: ${error}`); - } finally { - setTesting(false); - } - }, [settings.apiKey]); - const generateThumbnails = useCallback(async () => { if (!videoFile) { setThumbnailResult("Please select a video file before generating thumbnails"); @@ -94,7 +46,7 @@ export const GeminiSettings: React.FC = ({ onSettingsChange @@ -103,68 +55,25 @@ export const GeminiSettings: React.FC = ({ onSettingsChange - {settings.enabled && ( + {enabled && (
-
- -
- -
-
- -
- - - {!settings.isDefault && ( - +
- - {testResult && ( -
- {testResult} -
- )} - -
- -
+ {thumbnailResult && (
diff --git a/src/components/Settings.tsx b/src/components/Settings.tsx new file mode 100644 index 0000000..d754d95 --- /dev/null +++ b/src/components/Settings.tsx @@ -0,0 +1,107 @@ +import React, { useState } from 'react'; +import { invoke } from '@tauri-apps/api/core'; + +interface SettingsProps { + isOpen: boolean; + onClose: () => void; + apiKey: string; + onApiKeyChange: (key: string) => void; +} + +const DEFAULT_API_KEY = 'AIzaSyAF825tPTh77oL0knsGFEyvsN0iPUO_bXc'; + +export const Settings: React.FC = ({ isOpen, onClose, apiKey, onApiKeyChange }) => { + const [testing, setTesting] = useState(false); + const [testResult, setTestResult] = useState(null); + + const handleApiKeyChange = (e: React.ChangeEvent) => { + onApiKeyChange(e.target.value); + }; + + const resetToDefault = () => { + onApiKeyChange(DEFAULT_API_KEY); + }; + + const testApiKey = async () => { + setTesting(true); + setTestResult(null); + try { + await invoke('test_gemini_api', { apiKey }); + setTestResult('API key is valid'); + } catch (error) { + setTestResult(`Error: ${error}`); + } finally { + setTesting(false); + } + }; + + if (!isOpen) return null; + + return ( +
+
+
+

Settings

+ +
+ +
+
+ + +
+ +
+ + + {apiKey !== DEFAULT_API_KEY && ( + + )} +
+ + {testResult && ( +
+ {testResult} +
+ )} +
+ +
+ +
+
+
+ ); +};