Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ProjectsSettings.tsx

168 lines6.0 KB

1import { resolveEnvironmentMachineKind } from "@t3tools/contracts";
2import { ChevronDownIcon, FolderIcon } from "lucide-react";
3import { type ReactNode, useState } from "react";
4import { EnvironmentMachineIcon } from "../EnvironmentMachineIcon";
5import { ProjectFavicon } from "../ProjectFavicon";
6import { WorkspacePageContainer } from "../WorkspacePageContainer";
7import { useEnvironments } from "../../state/environments";
8import { Toggle, ToggleGroup } from "../ui/toggle-group";
9import {
10 Combobox,
11 ComboboxEmpty,
12 ComboboxSearchInput,
13 ComboboxItem,
14 ComboboxList,
15 ComboboxPopup,
16 ComboboxTrigger,
17} from "../ui/combobox";
18import { selectTriggerVariants } from "../ui/select";
19import { cn } from "../../lib/utils";
20import { ProjectSettingsPanel, useSettingsProjectGroups } from "./ProjectSettingsPanel";
21import { ProjectDefaultsSettings } from "./ProjectDefaultsSettings";
22​
23function ScopePicker({
24 label,
25 value,
26 options,
27 onChange,
28}: {
29 label: "project" | "machine";
30 value: string | null;
31 options: ReadonlyArray<{ value: string; label: string; icon?: ReactNode }>;
32 onChange: (value: string | null) => void;
33}) {
34 const [query, setQuery] = useState("");
35 const selected = options.find((option) => option.value === value);
36 const allIcon =
37 label === "project" ? <FolderIcon aria-hidden className="size-3.5 shrink-0" /> : null;
38 const items = [{ value: "all", label: `All ${label}s`, icon: allIcon }, ...options];
39 return (
40 <Combobox
41 items={items}
42 value={items.find((item) => item.value === (value ?? "all")) ?? null}
43 inputValue={query}
44 onInputValueChange={setQuery}
45 onOpenChange={() => setQuery("")}
46 onValueChange={(next) => {
47 if (next) onChange(next.value === "all" ? null : next.value);
48 }}
49 >
50 <ComboboxTrigger
51 aria-label={`${label === "project" ? "Project" : "Machine"} scope`}
52 className={cn(selectTriggerVariants({ size: "compact" }), "w-auto min-w-0 max-w-52")}
53 >
54 <span className="flex min-w-0 items-center gap-1.5">
55 {value === null ? allIcon : selected?.icon}
56 <span className="truncate">
57 {value === null ? `All ${label}s` : (selected?.label ?? `Unavailable ${label}`)}
58 </span>
59 </span>
60 <ChevronDownIcon aria-hidden className="-me-1 size-3 opacity-50" />
61 </ComboboxTrigger>
62 <ComboboxPopup align="start" className="w-64">
63 <ComboboxSearchInput aria-label={`Search ${label}s`} placeholder={`Search ${label}s...`} />
64 <ComboboxEmpty>No matching {label}s.</ComboboxEmpty>
65 <ComboboxList>
66 {(item: (typeof items)[number]) => (
67 <ComboboxItem
68 key={item.value}
69 value={item}
70 contentClassName="flex min-w-0 items-center gap-2"
71 >
72 {item.icon}
73 <span className="truncate">{item.label}</span>
74 </ComboboxItem>
75 )}
76 </ComboboxList>
77 </ComboboxPopup>
78 </Combobox>
79 );
81​
82export function ProjectsSettings({
83 projectKey,
84 machineId,
85 onScopeChange,
86}: {
87 projectKey: string | null;
88 machineId: string | null;
89 onScopeChange: (project: string | null, machine: string | null) => void;
90}) {
91 const groups = useSettingsProjectGroups();
92 const { environments } = useEnvironments();
93 const machine = environments.find((environment) => environment.environmentId === machineId);
94 const machineOptions = environments.map((environment) => ({
95 value: environment.environmentId,
96 label: environment.label,
97 icon: (
98 <EnvironmentMachineIcon
99 aria-hidden
100 kind={resolveEnvironmentMachineKind(environment.serverConfig)}
101 className="size-3.5 shrink-0"
102 />
103 ),
104 }));
105 return (
106 <div className="flex min-h-0 flex-1 flex-col">
107 <div className="scrollbar-gutter-both shrink-0 overflow-y-auto">
108 <WorkspacePageContainer className="pb-0">
109 <div
110 className="flex flex-wrap items-center gap-2"
111 role="group"
112 aria-label="Settings scope"
113 >
114 {environments.length > 3 ? (
115 <ScopePicker
116 label="machine"
117 value={machineId}
118 options={machineOptions}
119 onChange={(value) => onScopeChange(projectKey, value)}
120 />
121 ) : (
122 <ToggleGroup
123 aria-label="Machine scope"
124 variant="segmented"
125 className="max-w-full flex-wrap"
126 value={[machineId ?? "all"]}
127 onValueChange={(next) => {
128 const value = next[0];
129 if (value) onScopeChange(projectKey, value === "all" ? null : value);
130 }}
131 >
132 <Toggle value="all">All machines</Toggle>
133 {machineOptions.map((option) => (
134 <Toggle key={option.value} value={option.value} title={option.label}>
135 {option.icon}
136 <span className="max-w-36 truncate">{option.label}</span>
137 </Toggle>
138 ))}
139 </ToggleGroup>
140 )}
141 <div className="ms-auto">
142 <ScopePicker
143 label="project"
144 value={projectKey}
145 options={groups.map((group) => ({
146 value: group.projectKey,
147 label: group.displayName,
148 icon: <ProjectFavicon project={group} className="size-3.5" />,
149 }))}
150 onChange={(value) => onScopeChange(value, machineId)}
151 />
152 </div>
153 </div>
154 </WorkspacePageContainer>
155 </div>
156 {machineId !== null && !machine ? (
157 <p className="p-8 text-sm text-muted-foreground">This machine is no longer available.</p>
158 ) : projectKey === null ? (
159 <ProjectDefaultsSettings environmentId={machine?.environmentId ?? null} />
160 ) : (
161 <ProjectSettingsPanel
162 projectKey={projectKey}
163 environmentId={machine?.environmentId ?? null}
164 />
165 )}
166 </div>
167 );