|
| 1 | +import React, { useState, useEffect } from "react"; |
| 2 | +import { ChevronRight, RotateCcw } from "lucide-react"; |
| 3 | +import { Switch, Button, Tooltip } from "antd"; |
| 4 | +import { MessagesSquare } from "lucide-react"; |
| 5 | +import { useSettingsStore } from "./store"; |
| 6 | +import { SettingsSidebar } from "./sidebar"; |
| 7 | +import { SettingsSection } from "./types"; |
| 8 | +import { LucideIcon } from "lucide-react"; |
| 9 | + |
| 10 | +interface SettingToggleProps { |
| 11 | + checked: boolean; |
| 12 | + onChange: (checked: boolean) => void; |
| 13 | + label: string; |
| 14 | + description?: string; |
| 15 | +} |
| 16 | + |
| 17 | +interface SectionHeaderProps { |
| 18 | + title: string; |
| 19 | + icon: LucideIcon; |
| 20 | + onReset: () => void; |
| 21 | +} |
| 22 | + |
| 23 | +const SettingToggle: React.FC<SettingToggleProps> = ({ |
| 24 | + checked, |
| 25 | + onChange, |
| 26 | + label, |
| 27 | + description, |
| 28 | +}) => ( |
| 29 | + <div className="flex justify-between items-start p-4 hover:bg-secondary/5 rounded-lg transition-colors"> |
| 30 | + <div className="flex flex-col gap-1"> |
| 31 | + <label className="font-medium">{label}</label> |
| 32 | + {description && ( |
| 33 | + <span className="text-sm text-secondary">{description}</span> |
| 34 | + )} |
| 35 | + </div> |
| 36 | + <Switch defaultValue={checked} onChange={onChange} /> |
| 37 | + </div> |
| 38 | +); |
| 39 | + |
| 40 | +const SectionHeader: React.FC<SectionHeaderProps> = ({ |
| 41 | + title, |
| 42 | + icon: Icon, |
| 43 | + onReset, |
| 44 | +}) => ( |
| 45 | + <div className="flex items-center justify-between mb-6"> |
| 46 | + <div className="flex items-center gap-2"> |
| 47 | + <Icon className="text-accent" size={20} /> |
| 48 | + <h2 className="text-lg font-semibold">{title}</h2> |
| 49 | + </div> |
| 50 | + <Tooltip title="Reset section settings"> |
| 51 | + <Button |
| 52 | + icon={<RotateCcw className="w-4 h-4" />} |
| 53 | + onClick={onReset} |
| 54 | + type="text" |
| 55 | + /> |
| 56 | + </Tooltip> |
| 57 | + </div> |
| 58 | +); |
| 59 | + |
| 60 | +export const SettingsManager: React.FC = () => { |
| 61 | + const [isSidebarOpen, setIsSidebarOpen] = useState(() => { |
| 62 | + if (typeof window !== "undefined") { |
| 63 | + const stored = localStorage.getItem("settingsSidebar"); |
| 64 | + return stored !== null ? JSON.parse(stored) : true; |
| 65 | + } |
| 66 | + return true; |
| 67 | + }); |
| 68 | + |
| 69 | + const { |
| 70 | + playground, |
| 71 | + updatePlaygroundSettings, |
| 72 | + resetPlaygroundSettings, |
| 73 | + resetAllSettings, |
| 74 | + } = useSettingsStore(); |
| 75 | + |
| 76 | + const sections: SettingsSection[] = [ |
| 77 | + { |
| 78 | + id: "playground", |
| 79 | + title: "Playground", |
| 80 | + icon: MessagesSquare, |
| 81 | + content: () => ( |
| 82 | + <> |
| 83 | + <SectionHeader |
| 84 | + title="Playground" |
| 85 | + icon={MessagesSquare} |
| 86 | + onReset={resetPlaygroundSettings} |
| 87 | + /> |
| 88 | + <div className="space-y-2 rounded-xl border border-secondary"> |
| 89 | + <SettingToggle |
| 90 | + checked={playground.showLLMEvents} |
| 91 | + onChange={(checked) => |
| 92 | + updatePlaygroundSettings({ showLLMEvents: checked }) |
| 93 | + } |
| 94 | + label={"Show LLM Events" + playground.showLLMEvents} |
| 95 | + description="Display detailed LLM call logs in the message thread" |
| 96 | + /> |
| 97 | + </div> |
| 98 | + </> |
| 99 | + ), |
| 100 | + }, |
| 101 | + ]; |
| 102 | + |
| 103 | + const [currentSection, setCurrentSection] = useState<SettingsSection>( |
| 104 | + sections[0] |
| 105 | + ); |
| 106 | + |
| 107 | + useEffect(() => { |
| 108 | + if (typeof window !== "undefined") { |
| 109 | + localStorage.setItem("settingsSidebar", JSON.stringify(isSidebarOpen)); |
| 110 | + } |
| 111 | + }, [isSidebarOpen]); |
| 112 | + |
| 113 | + return ( |
| 114 | + <div className="relative flex h-full w-full"> |
| 115 | + <div |
| 116 | + className={`absolute left-0 top-0 h-full transition-all duration-200 ease-in-out ${ |
| 117 | + isSidebarOpen ? "w-64" : "w-12" |
| 118 | + }`} |
| 119 | + > |
| 120 | + <SettingsSidebar |
| 121 | + isOpen={isSidebarOpen} |
| 122 | + sections={sections} |
| 123 | + currentSection={currentSection} |
| 124 | + onToggle={() => setIsSidebarOpen(!isSidebarOpen)} |
| 125 | + onSelectSection={setCurrentSection} |
| 126 | + /> |
| 127 | + </div> |
| 128 | + |
| 129 | + <div |
| 130 | + className={`flex-1 transition-all max-w-5xl -mr-6 duration-200 ${ |
| 131 | + isSidebarOpen ? "ml-64" : "ml-12" |
| 132 | + }`} |
| 133 | + > |
| 134 | + <div className="p-4 pt-2"> |
| 135 | + <div className="flex items-center gap-2 mb-4 text-sm"> |
| 136 | + <span className="text-primary font-medium">Settings</span> |
| 137 | + <ChevronRight className="w-4 h-4 text-secondary" /> |
| 138 | + <span className="text-secondary">{currentSection.title}</span> |
| 139 | + </div> |
| 140 | + |
| 141 | + <currentSection.content /> |
| 142 | + |
| 143 | + <div className="mt-12 pt-6 border-t border-secondary flex justify-between items-center"> |
| 144 | + <p className="text-xs text-secondary"> |
| 145 | + Settings are automatically saved and synced across browser |
| 146 | + sessions |
| 147 | + </p> |
| 148 | + <Button |
| 149 | + type="text" |
| 150 | + danger |
| 151 | + icon={<RotateCcw className="w-4 h-4 mr-1" />} |
| 152 | + onClick={resetAllSettings} |
| 153 | + > |
| 154 | + Reset All Settings |
| 155 | + </Button> |
| 156 | + </div> |
| 157 | + </div> |
| 158 | + </div> |
| 159 | + </div> |
| 160 | + ); |
| 161 | +}; |
| 162 | + |
| 163 | +export default SettingsManager; |
0 commit comments