All files / src/screens Settings.tsx

0% Statements 0/29
0% Branches 0/26
0% Functions 0/9
0% Lines 0/29

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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208                                                                                                                                                                                                                                                                                                                                                                                                                               
import { useState, useEffect } from 'react'
import {
  Bot,
  AlertTriangle,
  Loader2,
  Globe,
  Code,
  Settings as SettingsIcon,
  Users,
} from 'lucide-react'
import { toast } from 'sonner'
import {
  api,
  type AIPrompt,
  type AIModel,
  type AIStatus,
  type Source,
  type ProfileWithStats,
} from '../api/client'
import { cn } from '../lib/utils'
import { useProfile } from '../contexts/ProfileContext'
import NavHeader from '../components/NavHeader'
import {
  SettingsGeneral,
  SettingsPrompts,
  SettingsSources,
  SettingsSelectors,
  SettingsUsers,
  SettingsDanger,
} from '../components/settings'
 
type Tab = 'general' | 'prompts' | 'sources' | 'selectors' | 'users' | 'danger'
 
export default function Settings() {
  const { profile } = useProfile()
  const currentProfileId = profile?.id
 
  const [activeTab, setActiveTab] = useState<Tab>('general')
  const [aiStatus, setAiStatus] = useState<AIStatus | null>(null)
  const [prompts, setPrompts] = useState<AIPrompt[]>([])
  const [models, setModels] = useState<AIModel[]>([])
  const [sources, setSources] = useState<Source[]>([])
  const [profiles, setProfiles] = useState<ProfileWithStats[]>([])
  const [loading, setLoading] = useState(true)
 
  useEffect(() => {
    loadData()
  }, [])
 
  const loadData = async () => {
    try {
      const [statusData, promptsData, modelsData, sourcesData, profilesData] =
        await Promise.all([
          api.ai.status(),
          api.ai.prompts.list(),
          api.ai.models(),
          api.sources.list(),
          api.profiles.list(),
        ])
      setAiStatus(statusData)
      setPrompts(promptsData)
      setModels(modelsData)
      setSources(sourcesData)
      setProfiles(profilesData)
    } catch (error) {
      console.error('Failed to load settings:', error)
      toast.error('Failed to load settings')
    } finally {
      setLoading(false)
    }
  }
 
  return (
    <div className="min-h-screen bg-background">
      <NavHeader />
 
      {/* Tab navigation */}
      <div className="border-b border-border px-4">
        <div className="mx-auto max-w-4xl">
          <div className="flex gap-4 overflow-x-auto">
            <button
              onClick={() => setActiveTab('general')}
              className={cn(
                'border-b-2 py-3 text-sm font-medium transition-colors whitespace-nowrap',
                activeTab === 'general'
                  ? 'border-primary text-primary'
                  : 'border-transparent text-muted-foreground hover:text-foreground'
              )}
            >
              <span className="flex items-center gap-2">
                <SettingsIcon className="h-4 w-4" />
                General
              </span>
            </button>
            <button
              onClick={() => setActiveTab('prompts')}
              className={cn(
                'border-b-2 py-3 text-sm font-medium transition-colors whitespace-nowrap',
                activeTab === 'prompts'
                  ? 'border-primary text-primary'
                  : 'border-transparent text-muted-foreground hover:text-foreground'
              )}
            >
              <span className="flex items-center gap-2">
                <Bot className="h-4 w-4" />
                AI Prompts
              </span>
            </button>
            <button
              onClick={() => setActiveTab('sources')}
              className={cn(
                'border-b-2 py-3 text-sm font-medium transition-colors whitespace-nowrap',
                activeTab === 'sources'
                  ? 'border-primary text-primary'
                  : 'border-transparent text-muted-foreground hover:text-foreground'
              )}
            >
              <span className="flex items-center gap-2">
                <Globe className="h-4 w-4" />
                Sources
              </span>
            </button>
            <button
              onClick={() => setActiveTab('selectors')}
              className={cn(
                'border-b-2 py-3 text-sm font-medium transition-colors whitespace-nowrap',
                activeTab === 'selectors'
                  ? 'border-primary text-primary'
                  : 'border-transparent text-muted-foreground hover:text-foreground'
              )}
            >
              <span className="flex items-center gap-2">
                <Code className="h-4 w-4" />
                Selectors
              </span>
            </button>
            <button
              onClick={() => setActiveTab('users')}
              className={cn(
                'border-b-2 py-3 text-sm font-medium transition-colors whitespace-nowrap',
                activeTab === 'users'
                  ? 'border-primary text-primary'
                  : 'border-transparent text-muted-foreground hover:text-foreground'
              )}
            >
              <span className="flex items-center gap-2">
                <Users className="h-4 w-4" />
                Users
              </span>
            </button>
            <button
              onClick={() => setActiveTab('danger')}
              className={cn(
                'border-b-2 py-3 text-sm font-medium transition-colors whitespace-nowrap',
                activeTab === 'danger'
                  ? 'border-destructive text-destructive'
                  : 'border-transparent text-muted-foreground hover:text-destructive'
              )}
            >
              <span className="flex items-center gap-2">
                <AlertTriangle className="h-4 w-4" />
                Danger Zone
              </span>
            </button>
          </div>
        </div>
      </div>
 
      {/* Content */}
      <main className="px-4 py-6">
        <div className="mx-auto max-w-4xl">
          {loading ? (
            <div className="flex items-center justify-center py-12">
              <Loader2 className="h-6 w-6 animate-spin text-muted-foreground" />
            </div>
          ) : activeTab === 'general' ? (
            <SettingsGeneral
              aiStatus={aiStatus}
              models={models}
              onAIStatusChange={setAiStatus}
              onModelsChange={setModels}
            />
          ) : activeTab === 'prompts' ? (
            <SettingsPrompts
              aiStatus={aiStatus}
              prompts={prompts}
              models={models}
              onPromptsChange={setPrompts}
            />
          ) : activeTab === 'sources' ? (
            <SettingsSources sources={sources} onSourcesChange={setSources} />
          ) : activeTab === 'selectors' ? (
            <SettingsSelectors sources={sources} onSourcesChange={setSources} />
          ) : activeTab === 'users' ? (
            <SettingsUsers
              profiles={profiles}
              currentProfileId={currentProfileId}
              onProfilesChange={setProfiles}
            />
          ) : activeTab === 'danger' ? (
            <SettingsDanger />
          ) : null}
        </div>
      </main>
    </div>
  )
}
 
← Back to Dashboard