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

ProjectsSettings.tsx

160 lines5.6 KB

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