Elffuss BraidHub

kikocis/continue public

main · 6.8 KiBRawPermalink
1import {
2 CheckIcon,
3 ChevronDownIcon,
4 XMarkIcon,
5} from "@heroicons/react/24/outline";
6import React from "react";
7import ToggleSwitch from "../../../components/gui/Switch";
8import { ToolTip } from "../../../components/gui/Tooltip";
9import {
10 Listbox,
11 ListboxButton,
12 ListboxOption,
13 ListboxOptions,
14} from "../../../components/ui";
15
16interface BaseUserSettingProps {
17 title: string;
18 description: React.ReactNode;
19 disabled?: boolean;
20}
21
22interface ToggleUserSettingProps extends BaseUserSettingProps {
23 type: "toggle";
24 value: boolean;
25 onChange: (value: boolean) => void;
26}
27
28interface NumberUserSettingProps extends BaseUserSettingProps {
29 type: "number";
30 value: number;
31 onChange: (value: number) => void;
32 min?: number;
33 max?: number;
34}
35
36interface SelectUserSettingProps extends BaseUserSettingProps {
37 type: "select";
38 value: string;
39 onChange: (value: string) => void;
40 options: { label: string; value: string }[];
41}
42
43interface InputUserSettingProps extends BaseUserSettingProps {
44 type: "input";
45 value: string;
46 onChange: (value: string) => void;
47 className?: string;
48 placeholder?: string;
49 onSubmit?: () => void;
50 onCancel?: () => void;
51 isDirty?: boolean;
52 isValid?: boolean;
53}
54
55type UserSettingProps =
56 | ToggleUserSettingProps
57 | NumberUserSettingProps
58 | SelectUserSettingProps
59 | InputUserSettingProps;
60
61export function UserSetting(props: UserSettingProps) {
62 const { title, description, disabled = false } = props;
63
64 const renderControl = () => {
65 switch (props.type) {
66 case "toggle":
67 return (
68 <ToggleSwitch
69 isToggled={props.value}
70 disabled={disabled}
71 onToggle={() => props.onChange(!props.value)}
72 text=""
73 />
74 );
75
76 case "number":
77 return (
78 <div className="border-command-border bg-vsc-input-background focus-within:border-border-focus focus-within:ring-border-focus flex w-20 items-center rounded-md border border-solid focus-within:ring-1">
79 <input
80 type="number"
81 value={props.value}
82 onChange={(e) => props.onChange(Number(e.target.value))}
83 min={props.min ?? 0}
84 max={props.max ?? 100}
85 disabled={disabled}
86 className="text-vsc-foreground flex-1 border-none bg-transparent px-2 py-1 text-right outline-none focus:outline-none focus:ring-0 [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none"
87 />
88 </div>
89 );
90
91 case "select":
92 return (
93 <Listbox
94 value={props.value}
95 onChange={props.onChange}
96 disabled={disabled}
97 >
98 <ListboxButton className="border-command-border !w-20 w-20 !flex-none justify-between !rounded-md px-2 py-1">
99 {props.options.find((opt) => opt.value === props.value)?.label ||
100 props.value}
101 <ChevronDownIcon className="h-3 w-3" />
102 </ListboxButton>
103 <ListboxOptions className="!w-20 !min-w-0">
104 {props.options.map((option) => (
105 <ListboxOption key={option.value} value={option.value}>
106 {option.label}
107 </ListboxOption>
108 ))}
109 </ListboxOptions>
110 </Listbox>
111 );
112
113 case "input":
114 if (props.onSubmit) {
115 return (
116 <form
117 onSubmit={(e) => {
118 e.preventDefault();
119 props.onSubmit?.();
120 }}
121 >
122 <div className="flex items-center gap-2">
123 <div
124 className={`border-command-border bg-vsc-input-background focus-within:border-border-focus focus-within:ring-border-focus flex w-full flex-row overflow-hidden rounded-md border border-solid focus-within:ring-1 ${
125 props.isDirty
126 ? !props.isValid
127 ? "outline outline-red-500"
128 : "outline outline-green-500"
129 : ""
130 } ${disabled ? "cursor-not-allowed opacity-50" : ""}`}
131 >
132 <input
133 type="text"
134 value={props.value}
135 onChange={(e) => props.onChange(e.target.value)}
136 placeholder={props.placeholder}
137 disabled={disabled}
138 className="text-vsc-foreground flex-1 border-none bg-inherit px-1.5 py-1 outline-none ring-0 focus:border-none focus:outline-none focus:ring-0 disabled:cursor-not-allowed"
139 />
140 </div>
141 {props.isDirty && (
142 <div className="flex flex-row items-center gap-1">
143 <ToolTip content="Save">
144 <div onClick={props.onSubmit} className="cursor-pointer">
145 <CheckIcon className="h-4 w-4 text-green-500 hover:opacity-80" />
146 </div>
147 </ToolTip>
148 <ToolTip content="Cancel">
149 <div onClick={props.onCancel} className="cursor-pointer">
150 <XMarkIcon className="h-4 w-4 text-red-500 hover:opacity-80" />
151 </div>
152 </ToolTip>
153 </div>
154 )}
155 </div>
156 </form>
157 );
158 }
159 return (
160 <div
161 className={`border-command-border bg-vsc-input-background focus-within:border-border-focus focus-within:ring-border-focus flex w-full flex-row overflow-hidden rounded-md border border-solid focus-within:ring-1 ${disabled ? "cursor-not-allowed opacity-50" : ""}`}
162 >
163 <input
164 type="text"
165 value={props.value}
166 onChange={(e) => props.onChange(e.target.value)}
167 disabled={disabled}
168 className="text-vsc-foreground flex-1 border-none bg-inherit px-1.5 py-1 outline-none ring-0 focus:border-none focus:outline-none focus:ring-0 disabled:cursor-not-allowed"
169 />
170 </div>
171 );
172
173 default:
174 return null;
175 }
176 };
177
178 // Use vertical layout for input types, horizontal for others
179 const isInputType = props.type === "input";
180
181 if (isInputType) {
182 return (
183 <div className="flex flex-col gap-3">
184 <div className="flex flex-col">
185 <span className="text-sm font-medium">{title}</span>
186 <div className="mt-0.5 text-xs text-gray-500">{description}</div>
187 </div>
188 {renderControl()}
189 </div>
190 );
191 }
192
193 return (
194 <div className="flex items-start justify-start gap-4">
195 <div className="flex flex-1 flex-col">
196 <span className="text-sm font-medium">{title}</span>
197 <div className="mt-0.5 text-xs text-gray-500">{description}</div>
198 </div>
199 {renderControl()}
200 </div>
201 );
202}