import { useState, useEffect, useRef } from "react" import { useAuth } from "../context/AuthContext" import { useDialogs } from "tldraw" import { SettingsDialog } from "./SettingsDialog" import { getFathomApiKey, saveFathomApiKey, removeFathomApiKey, isFathomApiKeyConfigured } from "../lib/fathomApiKey" interface UserSettingsModalProps { onClose: () => void isDarkMode: boolean onToggleDarkMode: () => void } export function UserSettingsModal({ onClose, isDarkMode, onToggleDarkMode }: UserSettingsModalProps) { const { session, setSession } = useAuth() const { addDialog, removeDialog } = useDialogs() const modalRef = useRef(null) const [hasApiKey, setHasApiKey] = useState(false) const [hasFathomApiKey, setHasFathomApiKey] = useState(false) const [showFathomApiKeyInput, setShowFathomApiKeyInput] = useState(false) const [fathomApiKeyInput, setFathomApiKeyInput] = useState('') const [activeTab, setActiveTab] = useState<'general' | 'ai' | 'integrations'>('general') // Check API key status const checkApiKeys = () => { const settings = localStorage.getItem("openai_api_key") try { if (settings) { try { const parsed = JSON.parse(settings) if (parsed.keys) { const hasValidKey = Object.values(parsed.keys).some(key => typeof key === 'string' && key.trim() !== '' ) setHasApiKey(hasValidKey) } else { setHasApiKey(typeof settings === 'string' && settings.trim() !== '') } } catch (e) { setHasApiKey(typeof settings === 'string' && settings.trim() !== '') } } else { setHasApiKey(false) } } catch (e) { setHasApiKey(false) } } useEffect(() => { checkApiKeys() }, []) useEffect(() => { if (session.authed && session.username) { setHasFathomApiKey(isFathomApiKeyConfigured(session.username)) } }, [session.authed, session.username]) // Handle escape key and click outside useEffect(() => { const handleEscape = (e: KeyboardEvent) => { if (e.key === 'Escape') { onClose() } } const handleClickOutside = (e: MouseEvent) => { if (modalRef.current && !modalRef.current.contains(e.target as Node)) { onClose() } } document.addEventListener('keydown', handleEscape) document.addEventListener('mousedown', handleClickOutside) return () => { document.removeEventListener('keydown', handleEscape) document.removeEventListener('mousedown', handleClickOutside) } }, [onClose]) const openApiKeysDialog = () => { addDialog({ id: "api-keys", component: ({ onClose: dialogClose }: { onClose: () => void }) => ( { dialogClose() removeDialog("api-keys") checkApiKeys() }} /> ), }) } const handleSetVault = () => { window.dispatchEvent(new CustomEvent('open-obsidian-browser')) onClose() } return (

Settings

{activeTab === 'general' && (
Appearance Toggle between light and dark mode
)} {activeTab === 'ai' && (
AI API Keys {hasApiKey ? 'Your AI models are configured and ready' : 'Configure API keys to use AI features'}
{hasApiKey ? 'Configured' : 'Not Set'}
)} {activeTab === 'integrations' && (
{/* Obsidian Vault */}
Obsidian Vault {session.obsidianVaultName ? `Connected: ${session.obsidianVaultName}` : 'Connect your Obsidian vault to import notes'}
{session.obsidianVaultName ? 'Connected' : 'Not Set'}
{/* Fathom API */}
Fathom Meetings {hasFathomApiKey ? 'Your Fathom account is connected' : 'Connect Fathom to import meeting recordings'}
{hasFathomApiKey ? 'Connected' : 'Not Set'}
{showFathomApiKeyInput ? (
setFathomApiKeyInput(e.target.value)} placeholder="Enter Fathom API key..." className="settings-input" onKeyDown={(e) => { if (e.key === 'Enter' && fathomApiKeyInput.trim()) { saveFathomApiKey(fathomApiKeyInput.trim(), session.username) setHasFathomApiKey(true) setShowFathomApiKeyInput(false) setFathomApiKeyInput('') } else if (e.key === 'Escape') { setShowFathomApiKeyInput(false) setFathomApiKeyInput('') } }} autoFocus />
) : (
{hasFathomApiKey && ( )}
)}
)}
) }