-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathSettingsShell.tsx
More file actions
210 lines (195 loc) · 6.93 KB
/
SettingsShell.tsx
File metadata and controls
210 lines (195 loc) · 6.93 KB
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
209
210
import {
CpuIcon,
GitBranchIcon,
KeyboardIcon,
PaletteIcon,
Settings2Icon,
ShieldCheckIcon,
SmartphoneIcon,
VariableIcon,
WrenchIcon,
} from "lucide-react";
import { type ReactNode, useMemo } from "react";
import { useNavigate } from "@tanstack/react-router";
import { isElectron, isMobileShell } from "../../env";
import { Button } from "../ui/button";
import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select";
import { SidebarInset, SidebarTrigger } from "../ui/sidebar";
import { cn } from "../../lib/utils";
import { useT } from "../../i18n/useI18n";
export type SettingsSectionId =
| "general"
| "authentication"
| "hotkeys"
| "environment"
| "git"
| "models"
| "mobile"
| "advanced";
export type SettingsNavId = SettingsSectionId | "style";
interface SettingsNavItem {
id: SettingsNavId;
label: string;
icon: ReactNode;
hidden?: boolean;
}
const SETTINGS_NAV_ITEMS: readonly SettingsNavItem[] = [
{ id: "general", label: "General", icon: <Settings2Icon className="size-4" /> },
{ id: "style", label: "Style", icon: <PaletteIcon className="size-4" /> },
{
id: "authentication",
label: "Authentication",
icon: <ShieldCheckIcon className="size-4" />,
},
{ id: "hotkeys", label: "Hotkeys", icon: <KeyboardIcon className="size-4" /> },
{ id: "environment", label: "Environment", icon: <VariableIcon className="size-4" /> },
{ id: "git", label: "Git", icon: <GitBranchIcon className="size-4" /> },
{ id: "models", label: "Models", icon: <CpuIcon className="size-4" /> },
{
id: "mobile",
label: "Mobile Companion",
icon: <SmartphoneIcon className="size-4" />,
hidden: isMobileShell,
},
{ id: "advanced", label: "Advanced", icon: <WrenchIcon className="size-4" /> },
];
function SettingsNavSidebar({
items,
activeItem,
onSelect,
}: {
items: readonly SettingsNavItem[];
activeItem: SettingsNavId;
onSelect: (id: SettingsNavId) => void;
}) {
return (
<nav className="flex w-52 shrink-0 flex-col gap-0.5 py-1" aria-label="Settings navigation">
{items
.filter((item) => !item.hidden)
.map((item) => (
<button
key={item.id}
type="button"
className={cn(
"flex items-center gap-2.5 rounded-lg px-3 py-2 text-left text-sm transition-colors",
activeItem === item.id
? "bg-accent text-accent-foreground font-medium"
: "text-muted-foreground hover:bg-accent/50 hover:text-foreground",
)}
onClick={() => onSelect(item.id)}
aria-current={activeItem === item.id ? "page" : undefined}
>
<span className="flex size-5 items-center justify-center opacity-70">{item.icon}</span>
{item.label}
</button>
))}
</nav>
);
}
export function SettingsShell({
activeItem,
changedSettingLabels,
onRestoreDefaults,
children,
}: {
activeItem: SettingsNavId;
changedSettingLabels: readonly string[];
onRestoreDefaults: () => Promise<void>;
children: ReactNode;
}) {
const navigate = useNavigate();
const { t } = useT();
const activeItemLabel = useMemo(
() => SETTINGS_NAV_ITEMS.find((item) => item.id === activeItem)?.label ?? "Settings",
[activeItem],
);
const handleSelect = (item: SettingsNavId) => {
if (item === "style") {
void navigate({ to: "/settings/style" });
return;
}
if (item === "general") {
void navigate({ to: "/settings", search: {} });
return;
}
void navigate({ to: "/settings", search: { section: item } });
};
return (
<SidebarInset className="h-dvh min-h-0 overflow-hidden overscroll-y-none bg-background text-foreground isolate">
<div className="flex min-h-0 min-w-0 flex-1 flex-col bg-background text-foreground">
{!isElectron && (
<header className="border-b border-border/60 px-4 py-2.5 sm:px-6">
<div className="flex items-center gap-3">
<SidebarTrigger className="size-7 shrink-0" />
<div className="flex items-center gap-1.5 text-sm">
<span className="font-medium text-foreground">Settings</span>
<span className="text-muted-foreground/50">/</span>
<span className="text-muted-foreground">{activeItemLabel}</span>
</div>
<div className="ms-auto flex items-center gap-2">
<Button
size="xs"
variant="outline"
disabled={changedSettingLabels.length === 0}
onClick={() => void onRestoreDefaults()}
>
{t("common.actions.restoreDefaults")}
</Button>
</div>
</div>
</header>
)}
{isElectron && (
<div className="drag-region flex h-[52px] shrink-0 items-center border-b border-border/60 px-5">
<div className="flex items-center gap-1.5 text-xs font-medium tracking-wide">
<span className="text-muted-foreground/70">Settings</span>
<span className="text-muted-foreground/40">/</span>
<span className="text-muted-foreground/70">{activeItemLabel}</span>
</div>
<div className="ms-auto flex items-center gap-2">
<Button
size="xs"
variant="outline"
disabled={changedSettingLabels.length === 0}
onClick={() => void onRestoreDefaults()}
>
{t("common.actions.restoreDefaults")}
</Button>
</div>
</div>
)}
<div className="flex min-h-0 flex-1">
<aside className="hidden w-56 shrink-0 overflow-y-auto border-r border-border/60 px-3 py-4 md:block">
<SettingsNavSidebar
items={SETTINGS_NAV_ITEMS}
activeItem={activeItem}
onSelect={handleSelect}
/>
</aside>
<div className="flex min-h-0 flex-1 flex-col">
<div className="border-b border-border/60 px-4 py-2 md:hidden">
<Select
value={activeItem}
onValueChange={(value) => handleSelect(value as SettingsNavId)}
>
<SelectTrigger className="w-full" aria-label="Settings section">
<SelectValue>{activeItemLabel}</SelectValue>
</SelectTrigger>
<SelectPopup>
{SETTINGS_NAV_ITEMS.filter((item) => !item.hidden).map((item) => (
<SelectItem hideIndicator key={item.id} value={item.id}>
{item.label}
</SelectItem>
))}
</SelectPopup>
</Select>
</div>
<div className="flex-1 overflow-y-auto">
<div className="mx-auto max-w-3xl p-6 sm:p-8">{children}</div>
</div>
</div>
</div>
</div>
</SidebarInset>
);
}