All files / src/screens Settings.tsx

72.41% Statements 21/29
57.14% Branches 8/14
53.84% Functions 7/13
75% Lines 21/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137                                                                17x 17x 17x     17x       17x                                                     17x 17x 17x 17x 17x   17x   17x   17x   14x                             1x             1x                 1x     17x 17x 17x   17x                                    
import { useState, type ReactNode } from 'react'
import {
  Bot,
  AlertTriangle,
  Loader2,
  Globe,
  Code,
  Settings as SettingsIcon,
  Users,
  Key,
  Smartphone,
} from 'lucide-react'
import { useProfile } from '../contexts/ProfileContext'
import { useMode } from '../router'
import { useOptionalAuth } from '../contexts/AuthContext'
import { useSettingsData } from '../hooks/useSettingsData'
import NavHeader from '../components/NavHeader'
import DeviceCodeEntry from '../components/DeviceCodeEntry'
import SettingsPasskeys from '../components/settings/SettingsPasskeys'
import {
  SettingsGeneral,
  SettingsPrompts,
  SettingsSources,
  SettingsSelectors,
  SettingsUsers,
  SettingsDanger,
} from '../components/settings'
import SettingsTabNav, { type TabConfig } from './components/SettingsTabNav'
 
type Tab = 'general' | 'passkeys' | 'pair-device' | 'prompts' | 'sources' | 'selectors' | 'users' | 'danger'
 
function useIsAdmin(): boolean {
  const mode = useMode()
  const auth = useOptionalAuth()
  Iif (mode === 'passkey') {
    return auth?.isAdmin ?? false
  }
  return true
}
 
function buildTabConfig(mode: string, isAdmin: boolean): TabConfig<Tab>[] {
  return [
    { id: 'general', label: 'General', icon: SettingsIcon, visible: true },
    { id: 'passkeys', label: 'Passkeys', icon: Key, visible: mode === 'passkey' },
    { id: 'pair-device', label: 'Pair Device', icon: Smartphone, visible: mode === 'passkey' },
    { id: 'prompts', label: 'AI Prompts', icon: Bot, visible: isAdmin },
    { id: 'sources', label: 'Sources', icon: Globe, visible: isAdmin },
    { id: 'selectors', label: 'Selectors', icon: Code, visible: isAdmin },
    { id: 'users', label: 'Users', icon: Users, visible: isAdmin },
    { id: 'danger', label: 'Danger Zone', icon: AlertTriangle, visible: isAdmin && mode !== 'passkey', variant: 'destructive' as const },
  ]
}
 
function PairDeviceContent() {
  return (
    <div className="space-y-6">
      <div className="rounded-lg border border-border bg-card p-4">
        <h2 className="mb-4 text-lg font-medium text-foreground">Pair a Device</h2>
        <p className="mb-4 text-sm text-muted-foreground">
          Enter the code shown on the device you want to pair
        </p>
        <DeviceCodeEntry />
      </div>
    </div>
  )
}
 
export default function Settings() {
  const { profile } = useProfile()
  const currentProfileId = profile?.id
  const isAdmin = useIsAdmin()
  const mode = useMode()
  const data = useSettingsData(isAdmin)
 
  const [activeTab, setActiveTab] = useState<Tab>('general')
 
  const tabs = buildTabConfig(mode, isAdmin)
 
  const tabContent: Record<Tab, () => ReactNode> = {
    general: () => (
      <SettingsGeneral
        aiStatus={data.aiStatus}
        models={data.models}
        onAIStatusChange={data.setAiStatus}
        onModelsChange={data.setModels}
        isAdmin={isAdmin}
        quotaData={data.quotaData}
        onQuotaSave={(limits) => {
          if (data.quotaData) data.setQuotaData({ ...data.quotaData, limits })
        }}
      />
    ),
    passkeys: () => <SettingsPasskeys />,
    'pair-device': () => <PairDeviceContent />,
    prompts: () => (
      <SettingsPrompts
        aiStatus={data.aiStatus}
        prompts={data.prompts}
        models={data.models}
        onPromptsChange={data.setPrompts}
      />
    ),
    sources: () => <SettingsSources sources={data.sources} onSourcesChange={data.setSources} />,
    selectors: () => <SettingsSelectors sources={data.sources} onSourcesChange={data.setSources} />,
    users: () => (
      <SettingsUsers
        profiles={data.profiles}
        currentProfileId={currentProfileId}
        onProfilesChange={data.setProfiles}
      />
    ),
    danger: () => <SettingsDanger />,
  }
 
  const renderContent = tabContent[activeTab]
  const isAdminTab = ['prompts', 'sources', 'selectors', 'users', 'danger'].includes(activeTab)
  const content = (!isAdminTab || isAdmin) ? renderContent() : null
 
  return (
    <div className="min-h-screen bg-background">
      <NavHeader />
 
      <SettingsTabNav tabs={tabs} activeTab={activeTab} onTabChange={setActiveTab} />
 
      <main className="px-4 py-6">
        <div className="mx-auto max-w-4xl">
          {data.loading ? (
            <div className="flex items-center justify-center py-12">
              <Loader2 className="h-6 w-6 animate-spin text-muted-foreground" />
            </div>
          ) : content}
        </div>
      </main>
    </div>
  )
}
 
← Back to Dashboard