import { useStore } from '@nanostores/react' import type * as React from 'react' import { useCallback, useEffect, useMemo, useRef, useState } from 'react' import { CodeEditor } from '@/components/chat/code-editor' import { PageLoader } from '@/components/page-loader' import { Button } from '@/components/ui/button' import { ProfileGlyph } from '@/components/ui/profile-glyph' import { getProfileSoul, type ProfileInfo, updateProfileSoul } from '@/hermes' import { useI18n } from '@/i18n' import { displayPath } from '@/lib/display-path' import { AlertTriangle, Save } from '@/lib/icons' import { resolveProfileColor } from '@/lib/profile-color' import { normalize } from '@/lib/text' import { notify, notifyError } from '@/store/notifications' import { $profileColors, profileLabel, refreshProfiles } from '@/store/profile' import { useRefreshHotkey } from '../hooks/use-refresh-hotkey' import { Panel, PanelAddButton, PanelBody, PanelDetail, PanelEmpty, PanelHeader, PanelList, PanelListRow, type PanelMenuItem, PanelMeta, PanelPill, PanelSectionLabel } from '../overlays/panel' import { CreateProfileDialog } from './create-profile-dialog' import { DeleteProfileDialog } from './delete-profile-dialog' import { RenameProfileDialog } from './rename-profile-dialog' interface ProfilesViewProps { onClose: () => void } export function ProfilesView({ onClose }: ProfilesViewProps) { const { t } = useI18n() const p = t.profiles const [profiles, setProfiles] = useState(null) const [selectedName, setSelectedName] = useState(null) const [query, setQuery] = useState('') const [createOpen, setCreateOpen] = useState(false) const [pendingRename, setPendingRename] = useState(null) const [pendingDelete, setPendingDelete] = useState(null) const refresh = useCallback(async () => { try { const list = await refreshProfiles() setProfiles(list) setSelectedName(current => { if (current && list.some(p => p.name === current)) { return current } return list.find(p => p.is_default)?.name ?? list[0]?.name ?? null }) } catch (err) { notifyError(err, p.failedLoad) } }, [p]) useRefreshHotkey(refresh) useEffect(() => { void refresh() }, [refresh]) const selected = useMemo(() => { if (!profiles) { return null } return profiles.find(p => p.name === selectedName) ?? profiles[0] ?? null }, [profiles, selectedName]) const visibleProfiles = useMemo(() => { const q = normalize(query) if (!profiles || !q) { return profiles ?? [] } return profiles.filter( profile => profile.name.toLowerCase().includes(q) || (profile.model ?? '').toLowerCase().includes(q) ) }, [profiles, query]) // The shared Create/Rename dialogs own the createProfile / renameProfile / // updateProfileSoul calls; the panel just selects the resulting profile and // re-pulls the list. const selectAndRefresh = useCallback( async (name: string) => { setSelectedName(name) await refresh() }, [refresh] ) return ( {!profiles ? ( ) : profiles.length === 0 ? ( setCreateOpen(true)} size="sm"> {p.newProfile} } description={p.createDesc} icon="organization" title={p.noProfiles} /> ) : ( <> {visibleProfiles.map(profile => ( setPendingRename(profile) }] : [ { icon: 'edit', label: p.renameMenu, onSelect: () => setPendingRename(profile) }, { icon: 'trash', label: t.common.delete, onSelect: () => setPendingDelete(profile), tone: 'danger' } ] } onSelect={() => setSelectedName(profile.name)} profile={profile} /> ))} setCreateOpen(true)} /> {selected ? ( ) : ( )} )} setPendingRename(null)} onRenamed={selectAndRefresh} open={pendingRename !== null} /> setCreateOpen(false)} onCreated={selectAndRefresh} open={createOpen} profiles={profiles ?? []} /> setPendingDelete(null)} onDeleted={async () => { setSelectedName(null) await refresh() }} open={pendingDelete !== null} profile={pendingDelete} /> ) } function ProfileRow({ active, menuItems, onSelect, profile }: { active: boolean menuItems: PanelMenuItem[] onSelect: () => void profile: ProfileInfo }) { const colors = useStore($profileColors) return (