Refactor settings dialog to use URL-based state management with tab synchronization

This commit is contained in:
Chris Scott committed 2026-02-06 21:56:28 -05:00
1 parent b6473fc821
commit b7315daa7e
7 files changed
+140 -78

No files matched your search

+1 -3
View File
@@ -9,7 +9,6 @@ import { Login } from './pages/Login'
import { Register } from './pages/Register'
import { Setup } from './pages/Setup'
import { SettingsDialog } from './components/settings/SettingsDialog'
import { useSettingsDialog } from './hooks/useSettingsDialog'
import { useTheme } from './hooks/useTheme'
import { TTSProvider } from './contexts/TTSContext'
import { AuthProvider } from './contexts/AuthContext'
@@ -49,7 +48,6 @@ function PermissionDialogWrapper() {
}
function AppShell() {
const { isOpen, close } = useSettingsDialog()
useTheme()
useEffect(() => {
@@ -66,7 +64,7 @@ function AppShell() {
<EventProvider>
<Outlet />
<PermissionDialogWrapper />
<SettingsDialog open={isOpen} onOpenChange={close} />
<SettingsDialog />
<Toaster
position="bottom-right"
expand={false}
@@ -9,38 +9,44 @@ import { VoiceSettings } from '@/components/settings/VoiceSettings'
import { NotificationSettings } from '@/components/settings/NotificationSettings'
import { Dialog, DialogContent } from '@/components/ui/dialog'
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'
import { Settings2, Keyboard, Code, ChevronLeft, X, Key, GitBranch, User, Volume2, Bell } from 'lucide-react'
import { Settings2, Keyboard, Code, ChevronLeft, Key, GitBranch, User, Volume2, Bell, X } from 'lucide-react'
import { Button } from '@/components/ui/button'
import { useSwipeBack } from '@/hooks/useMobile'
interface SettingsDialogProps {
open: boolean
onOpenChange: (open: boolean) => void
}
import { useSettingsDialog } from '@/hooks/useSettingsDialog'
type SettingsView = 'menu' | 'general' | 'git' | 'shortcuts' | 'opencode' | 'providers' | 'account' | 'voice' | 'notifications'
export function SettingsDialog({ open, onOpenChange }: SettingsDialogProps) {
export function SettingsDialog() {
const { isOpen, close, activeTab, setActiveTab } = useSettingsDialog()
const [mobileView, setMobileView] = useState<SettingsView>('menu')
const contentRef = useRef<HTMLDivElement>(null)
const handleSwipeBack = useCallback(() => {
if (mobileView === 'menu') {
setMobileView('menu')
onOpenChange(false)
close()
} else {
setMobileView('menu')
}
}, [mobileView, onOpenChange])
}, [mobileView, close])
const { bind: bindSwipe, swipeStyles } = useSwipeBack(handleSwipeBack, {
enabled: open,
enabled: isOpen,
})
useEffect(() => {
return bindSwipe(contentRef.current)
}, [bindSwipe])
useEffect(() => {
if (!isOpen) return
const handleKeyDown = (e: KeyboardEvent) => {
if (e.key === 'Escape') close()
}
document.addEventListener('keydown', handleKeyDown)
return () => document.removeEventListener('keydown', handleKeyDown)
}, [isOpen, close])
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' },
@@ -52,26 +58,33 @@ export function SettingsDialog({ open, onOpenChange }: SettingsDialogProps) {
{ id: 'providers', icon: Key, label: 'Providers', description: 'Manage AI provider API keys' },
]
const handleClose = () => {
setMobileView('menu')
onOpenChange(false)
const handleTabChange = (tab: string) => {
setActiveTab(tab as SettingsView)
}
return (
<Dialog open={open} onOpenChange={handleClose}>
<DialogContent
ref={contentRef}
className="w-full h-[100vh] sm:h-auto sm:w-[95vw] sm:max-h-[90vh] bg-gradient-to-br from-background via-background to-background border-border p-0 sm:rounded-lg overflow-hidden !flex !flex-col left-0 top-0 translate-x-0 translate-y-0 max-h-[100vh] sm:left-[50%] sm:top-[50%] sm:translate-x-[-50%] sm:translate-y-[-50%] [&>button:last-child]:hidden"
style={swipeStyles}
>
<div className="hidden sm:flex sm:flex-col sm:h-[90vh]">
<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">
<h2 className="text-2xl font-semibold bg-gradient-to-r from-foreground to-muted-foreground bg-clip-text text-transparent">
Settings
</h2>
</div>
<Tabs defaultValue="account" className="w-full flex flex-col flex-1 min-h-0">
return (
<Dialog open={isOpen} modal={false}>
<DialogContent
ref={contentRef}
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 z-[100]"
style={swipeStyles}
fullscreen
>
<div className="hidden sm:flex sm:flex-col sm:h-full">
<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">
@@ -117,31 +130,31 @@ export function SettingsDialog({ open, onOpenChange }: SettingsDialogProps) {
</div>
<div className="sm:hidden flex flex-col h-full min-h-0 pt-safe">
<div className="flex-shrink-0 bg-gradient-to-b from-background via-background to-transparent border-b border-border backdrop-blur-sm px-4 py-4 flex items-center justify-between">
<div className="flex items-center gap-2 flex-1">
{mobileView !== 'menu' && (
<Button
variant="ghost"
size="icon"
onClick={() => setMobileView('menu')}
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={handleClose}
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-shrink-0 bg-gradient-to-b from-background via-background to-transparent border-b border-border backdrop-blur-sm px-4 py-4 flex items-center justify-between">
<div className="flex items-center gap-2 flex-1">
{mobileView !== 'menu' && (
<Button
variant="ghost"
size="icon"
onClick={() => setMobileView('menu')}
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-4 pb-32">
{mobileView === 'menu' && (
@@ -149,7 +162,10 @@ export function SettingsDialog({ open, onOpenChange }: SettingsDialogProps) {
{menuItems.map((item) => (
<button
key={item.id}
onClick={() => setMobileView(item.id as SettingsView)}
onClick={() => {
setMobileView(item.id as SettingsView)
setActiveTab(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">
+2 -2
View File
@@ -39,10 +39,10 @@ const DialogContent = React.forwardRef<
DialogContentProps
>(({ className, children, hideCloseButton, fullscreen, mobileFullscreen, ...props }, ref) => {
const isMobileFullscreenMode = fullscreen || mobileFullscreen
return (
<DialogPortal>
<DialogOverlay />
{!fullscreen && <DialogOverlay />}
<DialogPrimitive.Content
ref={ref}
aria-describedby={undefined}
+4 -4
View File
@@ -175,7 +175,7 @@ function HeaderMobileDropdown({ children, className }: { children: ReactNode; cl
const isMobile = useMobile();
const { pendingCount: permissionCount, setShowDialog } = usePermissions();
const { pendingCount: questionCount, navigateToCurrent } = useQuestions();
const { open: openSettings } = useSettingsDialog();
const { open } = useSettingsDialog();
const totalPending = permissionCount + questionCount;
@@ -216,7 +216,7 @@ function HeaderMobileDropdown({ children, className }: { children: ReactNode; cl
{totalPending > 0 && children && <DropdownMenuSeparator />}
{children}
{children && <DropdownMenuSeparator />}
<DropdownMenuItem onClick={openSettings} className="gap-2">
<DropdownMenuItem onClick={open} className="gap-2">
<Settings className="w-4 h-4" />
<span>Settings</span>
</DropdownMenuItem>
@@ -226,13 +226,13 @@ function HeaderMobileDropdown({ children, className }: { children: ReactNode; cl
}
function HeaderSettingsButton() {
const { open: openSettings } = useSettingsDialog();
const { open } = useSettingsDialog();
return (
<Button
variant="ghost"
size="icon"
onClick={openSettings}
onClick={open}
className="text-muted-foreground hover:text-foreground hover:bg-accent transition-all duration-200 h-8 w-8"
>
<Settings className="w-4 h-4" />
-2
View File
@@ -2,7 +2,6 @@
import { createContext, useEffect, useState, useCallback, type ReactNode } from 'react'
import { useSession, signIn, signUp, signOut, authClient, type AuthUser } from '@/lib/auth-client'
import { useNavigate, useLocation } from 'react-router-dom'
import { useSettingsDialog } from '@/hooks/useSettingsDialog'
interface AuthConfig {
enabledProviders: string[]
@@ -113,7 +112,6 @@ export function AuthProvider({ children }: AuthProviderProps) {
}, [])
const logout = useCallback(async () => {
useSettingsDialog.getState().close()
await signOut()
await refetch()
navigate('/login', { replace: true })
+3 -13
View File
@@ -1,15 +1,5 @@
import { create } from 'zustand'
import { useSettingsDialogUrl } from './useSettingsDialogUrl'
interface SettingsDialogStore {
isOpen: boolean
open: () => void
close: () => void
toggle: () => void
export const useSettingsDialog = () => {
return useSettingsDialogUrl()
}
export const useSettingsDialog = create<SettingsDialogStore>((set) => ({
isOpen: false,
open: () => set({ isOpen: true }),
close: () => set({ isOpen: false }),
toggle: () => set((state) => ({ isOpen: !state.isOpen })),
}))
@@ -0,0 +1,60 @@
import { useCallback } from 'react'
import { useNavigate, useLocation } from 'react-router-dom'
type Tab = 'account' | 'general' | 'notifications' | 'voice' | 'git' | 'shortcuts' | 'opencode' | 'providers' | 'menu'
interface UseSettingsDialogUrlReturn {
isOpen: boolean
open: () => void
close: () => void
toggle: () => void
activeTab: Tab
setActiveTab: (tab: Tab) => void
}
export function useSettingsDialogUrl(): UseSettingsDialogUrlReturn {
const navigate = useNavigate()
const location = useLocation()
const searchParams = new URLSearchParams(location.search)
const isOpen = searchParams.get('settings') === 'open'
const activeTab = (searchParams.get('tab') as Tab) || 'menu'
const open = useCallback(() => {
const newParams = new URLSearchParams(location.search)
newParams.set('settings', 'open')
newParams.set('tab', activeTab)
navigate({ search: newParams.toString() }, { replace: true })
}, [activeTab, navigate, location.search])
const close = useCallback(() => {
const newParams = new URLSearchParams(location.search)
newParams.delete('settings')
newParams.delete('tab')
navigate({ search: newParams.toString() }, { replace: true })
}, [navigate, location.search])
const toggle = useCallback(() => {
if (isOpen) {
close()
} else {
open()
}
}, [isOpen, open, close])
const setActiveTab = useCallback((tab: Tab) => {
const newParams = new URLSearchParams(location.search)
newParams.set('settings', 'open')
newParams.set('tab', tab)
navigate({ search: newParams.toString() }, { replace: true })
}, [navigate, location.search])
return {
isOpen,
open,
close,
toggle,
activeTab,
setActiveTab,
}
}