Files
opencode-manager/frontend/src/components/settings/SettingsDialog.tsx
T
Chris Scott 617962f2dd feat: directory files upload system, deferred restart model, cache invalidation consolidation, and misc improvements (#281)
* feat: show variant label on model picker and add folder upload for agents/commands

- Replace three-dot variant menu trigger with selected variant label
- Add POST /api/settings/opencode-directory-files/install endpoint
- Upload agents/commands markdown folders into .config/opencode/{agents,commands}/
- Reload OpenCode server after upload so files are picked up

* fix: revalidate branch/git caches when mobile sheet opens and on vcs.branch.updated SSE

- add shared invalidateRepoGitCaches() helper for consistent invalidation
- revalidate repo list when mobile repo quick-switch sheet opens
- revalidate current repo when mobile More drawer opens
- revalidate repo/git caches when Source Control dialog opens
- handle vcs.branch.updated SSE event with optimistic cache update
- use helper in branch create/switch mutations in useGit and BranchesTab

* style: make variant selector look more like a button with chevron and compact sizing

* refactor: consolidate settings editors, add folder upload support, and clean up unused code

* refactor: consolidate settings editors, add folder upload support, and clean up unused code

* feat: add opencode-directory-files listing service and consolidate settings editors

* fix: distinguish scroll from tap in suggestion popups on touch devices

* refactor: consolidate settings editors and add DirectoryFilesList component

* refactor: target repo status cache updates only to affected batch entries

* refactor: streamline settings, git hooks, and query invalidation logic
2026-06-20 00:40:36 -04:00

258 lines
13 KiB
TypeScript

import { useState, useEffect, useCallback } from 'react'
import { GeneralSettings } from '@/components/settings/GeneralSettings'
import { GitSettings } from '@/components/settings/GitSettings'
import { KeyboardShortcuts } from '@/components/settings/KeyboardShortcuts'
import { OpenCodeConfigManager } from '@/components/settings/OpenCodeConfigManager'
import { OpenCodeServerAuthSettings } from '@/components/settings/OpenCodeServerAuthSettings'
import { ManagerTokenSettings } from '@/components/settings/ManagerTokenSettings'
import { ServerEnvVarsSettings } from '@/components/settings/ServerEnvVarsSettings'
import { ServerHealthStatus } from '@/components/settings/ServerHealthStatus'
import { ProviderSettings } from '@/components/settings/ProviderSettings'
import { AccountSettings } from '@/components/settings/AccountSettings'
import { VoiceSettings } from '@/components/settings/VoiceSettings'
import { NotificationSettings } from '@/components/settings/NotificationSettings'
import { VersionSelectDialog } from '@/components/settings/VersionSelectDialog'
import { Dialog, DialogContent } from '@/components/ui/dialog'
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'
import { Settings2, Keyboard, Code, ChevronLeft, Key, GitBranch, User, Volume2, Bell, X } from 'lucide-react'
import { Button } from '@/components/ui/button'
import { useSettingsDialog } from '@/hooks/useSettingsDialog'
type SettingsView = 'menu' | 'general' | 'git' | 'shortcuts' | 'opencode' | 'providers' | 'account' | 'voice' | 'notifications'
export function SettingsDialog() {
const { isOpen, close, activeTab, setActiveTab } = useSettingsDialog()
const [mobileView, setMobileView] = useState<SettingsView>('menu')
const [isVersionDialogOpen, setIsVersionDialogOpen] = useState(false)
const [sectionHistory, setSectionHistory] = useState<SettingsView[]>([])
const [authSectionsOpen, setAuthSectionsOpen] = useState(true)
const toggleAuthSections = useCallback(() => setAuthSectionsOpen((open) => !open), [])
const pushSectionHistory = useCallback((view: SettingsView) => {
if (view === 'menu') return
setSectionHistory((history) => {
if (history.at(-1) === view) return history
return [...history, view]
})
}, [])
const handleSettingsBack = useCallback(() => {
if (mobileView === 'menu') {
close()
return
}
const currentIndex = sectionHistory.lastIndexOf(mobileView)
const previousHistory = currentIndex >= 0
? sectionHistory.slice(0, currentIndex)
: sectionHistory
const previousView = previousHistory.at(-1)
if (previousView) {
setSectionHistory(previousHistory)
setMobileView(previousView)
setActiveTab(previousView)
return
}
setSectionHistory([])
setMobileView('menu')
}, [mobileView, sectionHistory, close, setActiveTab])
useEffect(() => {
if (!isOpen) {
setMobileView('menu')
setSectionHistory([])
return
}
const handleKeyDown = (e: KeyboardEvent) => {
if (e.key === 'Escape' && !isVersionDialogOpen) {
close()
}
}
document.addEventListener('keydown', handleKeyDown, { capture: true })
return () => document.removeEventListener('keydown', handleKeyDown, { capture: true })
}, [isOpen, close, isVersionDialogOpen])
const menuItems = [
{ id: 'account', icon: User, label: 'Account', description: 'Profile, passkeys, and sign out' },
{ id: 'general', icon: Settings2, label: 'General Settings', description: 'App preferences and behavior' },
{ id: 'notifications', icon: Bell, label: 'Notifications', description: 'Push notification preferences' },
{ id: 'voice', icon: Volume2, label: 'Voice', description: 'Text-to-speech and speech-to-text settings' },
{ id: 'git', icon: GitBranch, label: 'Git', description: 'Git identity and credentials for repositories' },
{ id: 'shortcuts', icon: Keyboard, label: 'Keyboard Shortcuts', description: 'Customize keyboard shortcuts' },
{ id: 'opencode', icon: Code, label: 'OpenCode Config', description: 'Manage OpenCode configurations, commands, and agents' },
{ id: 'providers', icon: Key, label: 'Providers', description: 'Manage AI provider API keys' },
]
const handleOpenMobileView = useCallback((view: SettingsView) => {
setMobileView(view)
setActiveTab(view)
pushSectionHistory(view)
}, [setActiveTab, pushSectionHistory])
const handleTabChange = (tab: string) => {
const nextView = tab as SettingsView
setActiveTab(nextView)
setMobileView(nextView)
pushSectionHistory(nextView)
}
return (
<Dialog open={isOpen} modal={false} onOpenChange={(open) => !open && close()}>
<DialogContent
className="inset-0 w-full h-full max-w-none max-h-none p-0 rounded-none bg-gradient-to-br from-background via-background to-background border-border overflow-hidden !flex !flex-col !gap-0"
fullscreen
canSwipeBack={() => mobileView !== 'menu'}
onSwipeBack={handleSettingsBack}
onInteractOutside={(e) => e.preventDefault()}
onFocusOutside={(e) => e.preventDefault()}
onPointerDownOutside={(e) => e.preventDefault()}
>
<div className="hidden sm:flex sm:flex-col sm:h-full sm:min-h-0">
<div className="sticky top-0 z-10 bg-gradient-to-b from-background via-background to-transparent border-b border-border backdrop-blur-sm px-6 py-4 flex-shrink-0 flex items-center justify-between">
<h2 className="text-2xl font-semibold bg-gradient-to-r from-foreground to-muted-foreground bg-clip-text text-transparent">
Settings
</h2>
<Button
variant="ghost"
size="icon"
onClick={close}
className="text-muted-foreground hover:text-foreground min-w-[44px] min-h-[44px]"
>
<X className="w-5 h-5" />
</Button>
</div>
<Tabs defaultValue="account" value={activeTab} onValueChange={handleTabChange} className="w-full flex flex-col flex-1 min-h-0">
<div className="px-6 pt-6 pb-4 flex-shrink-0">
<TabsList className="grid w-full grid-cols-8 bg-card p-1">
<TabsTrigger value="account" className="data-[state=active]:bg-blue-600 data-[state=active]:text-white text-muted-foreground transition-all duration-200">
Account
</TabsTrigger>
<TabsTrigger value="general" className="data-[state=active]:bg-blue-600 data-[state=active]:text-white text-muted-foreground transition-all duration-200">
General
</TabsTrigger>
<TabsTrigger value="notifications" className="data-[state=active]:bg-blue-600 data-[state=active]:text-white text-muted-foreground transition-all duration-200">
Notify
</TabsTrigger>
<TabsTrigger value="voice" className="data-[state=active]:bg-blue-600 data-[state=active]:text-white text-muted-foreground transition-all duration-200">
Voice
</TabsTrigger>
<TabsTrigger value="git" className="data-[state=active]:bg-blue-600 data-[state=active]:text-white text-muted-foreground transition-all duration-200">
Git
</TabsTrigger>
<TabsTrigger value="shortcuts" className="data-[state=active]:bg-blue-600 data-[state=active]:text-white text-muted-foreground transition-all duration-200">
Shortcuts
</TabsTrigger>
<TabsTrigger value="opencode" className="data-[state=active]:bg-blue-600 data-[state=active]:text-white text-muted-foreground transition-all duration-200">
OpenCode
</TabsTrigger>
<TabsTrigger value="providers" className="data-[state=active]:bg-blue-600 data-[state=active]:text-white text-muted-foreground transition-all duration-200">
Providers
</TabsTrigger>
</TabsList>
</div>
<div className="flex-1 min-h-0 overflow-y-auto">
<div className="px-6 pb-6">
<TabsContent key="account" value="account" className="mt-0"><AccountSettings /></TabsContent>
<TabsContent key="general" value="general" className="mt-0"><GeneralSettings /></TabsContent>
<TabsContent key="notifications" value="notifications" className="mt-0"><NotificationSettings /></TabsContent>
<TabsContent key="voice" value="voice" className="mt-0"><VoiceSettings /></TabsContent>
<TabsContent key="git" value="git" className="mt-0"><GitSettings /></TabsContent>
<TabsContent key="shortcuts" value="shortcuts" className="mt-0"><KeyboardShortcuts /></TabsContent>
<TabsContent key="opencode" value="opencode" className="mt-0">
<div className="space-y-6">
<ServerHealthStatus onOpenVersionDialog={() => setIsVersionDialogOpen(true)} />
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<OpenCodeServerAuthSettings isOpen={authSectionsOpen} onToggle={toggleAuthSections} />
<ManagerTokenSettings isOpen={authSectionsOpen} onToggle={toggleAuthSections} />
</div>
<ServerEnvVarsSettings />
<OpenCodeConfigManager hideHealthStatus />
</div>
</TabsContent>
<TabsContent key="providers" value="providers" className="mt-0"><ProviderSettings /></TabsContent>
</div>
</div>
</Tabs>
</div>
<div className="sm:hidden flex flex-col h-full min-h-0">
<div className="flex-shrink-0 bg-gradient-to-b from-background via-background to-transparent border-b border-border backdrop-blur-sm px-3 py-3 flex items-center justify-between">
<div className="flex items-center gap-2 flex-1">
{mobileView !== 'menu' && (
<Button
variant="ghost"
size="icon"
onClick={handleSettingsBack}
className="text-muted-foreground hover:text-foreground min-w-[44px] min-h-[44px]"
>
<ChevronLeft className="w-6 h-6" />
</Button>
)}
<h2 className="text-xl font-semibold bg-gradient-to-r from-foreground to-muted-foreground bg-clip-text text-transparent">
{mobileView === 'menu' ? 'Settings' : menuItems.find(item => item.id === mobileView)?.label}
</h2>
</div>
<Button
variant="ghost"
size="icon"
onClick={close}
className="text-muted-foreground hover:text-foreground min-w-[44px] min-h-[44px] flex-shrink-0"
>
<X className="w-6 h-6" />
</Button>
</div>
<div className="flex-1 min-h-0 overflow-y-auto p-3 pb-[calc(env(safe-area-inset-bottom)+1rem)]">
{mobileView === 'menu' && (
<div className="space-y-3">
{menuItems.map((item) => (
<button
key={item.id}
onClick={() => handleOpenMobileView(item.id as SettingsView)}
className="w-full bg-gradient-to-br from-card to-card-hover border border-border rounded-xl p-4 hover:border-border transition-all duration-200 text-left"
>
<div className="flex items-center gap-4">
<div className="p-3 bg-accent rounded-lg">
<item.icon className="w-6 h-6 text-blue-600 dark:text-blue-400" />
</div>
<div className="flex-1 min-w-0">
<h3 className="font-semibold text-foreground mb-1">{item.label}</h3>
<p className="text-sm text-muted-foreground">{item.description}</p>
</div>
</div>
</button>
))}
</div>
)}
{mobileView === 'account' && <div key="account"><AccountSettings /></div>}
{mobileView === 'general' && <div key="general"><GeneralSettings /></div>}
{mobileView === 'notifications' && <div key="notifications"><NotificationSettings /></div>}
{mobileView === 'voice' && <div key="voice"><VoiceSettings /></div>}
{mobileView === 'git' && <div key="git"><GitSettings /></div>}
{mobileView === 'shortcuts' && <div key="shortcuts"><KeyboardShortcuts /></div>}
{mobileView === 'opencode' && (
<div key="opencode" className="space-y-4">
<ServerHealthStatus onOpenVersionDialog={() => setIsVersionDialogOpen(true)} />
<OpenCodeServerAuthSettings />
<ManagerTokenSettings />
<ServerEnvVarsSettings />
<OpenCodeConfigManager hideHealthStatus />
</div>
)}
{mobileView === 'providers' && <div key="providers"><ProviderSettings /></div>}
</div>
</div>
</DialogContent>
<VersionSelectDialog
open={isVersionDialogOpen}
onOpenChange={setIsVersionDialogOpen}
/>
</Dialog>
)
}