Open History to see the last commit
1import type { StorageCleanupSettings, WorktreeCleanupRules } from "@t3tools/contracts";
2import { resolveWorktreeCleanup } from "@t3tools/shared/projectSettings";
3import { useState } from "react";
4
5import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select";
6import { Switch } from "../ui/switch";
7import {
8 NumberField,
9 NumberFieldDecrement,
10 NumberFieldGroup,
11 NumberFieldIncrement,
12 NumberFieldInput,
13} from "../ui/number-field";
14import { SettingsPageContainer, SettingsRow, SettingsSection } from "./settingsLayout";
15import { SettingsScopeNotice } from "./SettingsScopeNotice";
16import type { ScopedSettingsTarget } from "./scopedSettings";
17import { useSettingsScope } from "./SettingsScopeContext";
18import {
19 useClearScopedSettings,
20 useScopedSettings,
21 useUpdateScopedSettings,
22} from "./useScopedSettings";
23
24function RetentionControl({
25 label,
26 value,
27 onChange,
28}: {
29 label: string;
30 value: number | null;
31 onChange: (value: number | null) => void;
32}) {
33 const [draft, setDraft] = useState(value);
34 const [savedValue, setSavedValue] = useState(value);
35 if (savedValue !== value) {
36 setSavedValue(value);
37 setDraft(value);
38 }
39
40 return (
41 <div className="flex items-center gap-3">
42 {value !== null ? (
43 <NumberField
44 value={draft}
45 min={1}
46 max={3650}
47 step={1}
48 size="sm"
49 className="w-auto"
50 onValueChange={setDraft}
51 onValueCommitted={(next) => {
52 if (next === null) setDraft(value);
53 else {
54 const days = Math.min(3650, Math.max(1, Math.round(next)));
55 setDraft(days);
56 onChange(days);
57 }
58 }}
59 >
60 <NumberFieldGroup>
61 <NumberFieldDecrement aria-label={`Decrease ${label}`} />
62 <NumberFieldInput
63 aria-label={`${label} in days`}
64 size={new Intl.NumberFormat().format(draft ?? value).length}
65 className="field-sizing-content w-auto min-w-[1ch] grow-0 text-right in-data-[size=sm]:px-1"
66 />
67 <span aria-hidden="true" className="self-center pr-2 text-xs">
68 days
69 </span>
70 <NumberFieldIncrement aria-label={`Increase ${label}`} />
71 </NumberFieldGroup>
72 </NumberField>
73 ) : (
74 <span className="text-xs text-muted-foreground">Off</span>
75 )}
76 <Switch
77 aria-label={label}
78 checked={value !== null}
79 onCheckedChange={(enabled) => onChange(enabled ? 8 : null)}
80 />
81 </div>
82 );
83}
84
85export function StorageSettingsPanel() {
86 const { scope, connectedEnvironments, targets, target } = useSettingsScope();
87 const scopedSettings = useScopedSettings();
88 const isProjectScope = scope.kind === "project" || scope.kind === "checkout";
89 const settings = {
90 ...scopedSettings.storageCleanup,
91 ...resolveWorktreeCleanup(scopedSettings, null),
92 };
93 const projectMode = (entry: ScopedSettingsTarget | null) =>
94 entry?.sources.worktreeCleanup === "project"
95 ? (entry.settings.worktreeCleanup?.mode ?? "inherit")
96 : "inherit";
97 const mode = projectMode(target);
98 const mixedModes = targets.some((entry) => projectMode(entry) !== mode);
99 const updateSettings = useUpdateScopedSettings();
100 const clearSettings = useClearScopedSettings();
101 const ruleStatus = (key: keyof StorageCleanupSettings) =>
102 targets.some(
103 (target) =>
104 ({ ...target.settings.storageCleanup, ...resolveWorktreeCleanup(target.settings, null) })[
105 key
106 ] !== settings[key],
107 )
108 ? "Mixed across selected machines"
109 : undefined;
110 const update = (patch: Partial<StorageCleanupSettings>) =>
111 updateSettings({ storageCleanup: patch });
112 const updateWorktree = (patch: Partial<WorktreeCleanupRules>) =>
113 isProjectScope
114 ? updateSettings({ worktreeCleanup: { mode: "custom", rules: patch } })
115 : update(patch);
116
117 if (
118 isProjectScope &&
119 connectedEnvironments.some(
120 (environment) =>
121 environment.serverConfig?.environment.capabilities.projectWorktreeCleanup !== true,
122 )
123 ) {
124 return (
125 <SettingsScopeNotice target="all">
126 Update the selected machines to configure project worktree cleanup.
127 </SettingsScopeNotice>
128 );
129 }
130
131 if (
132 connectedEnvironments.some(
133 (environment) => environment.serverConfig?.environment.capabilities.storageCleanup !== true,
134 )
135 ) {
136 return (
137 <SettingsScopeNotice
138 target="environment"
139 eligibleEnvironmentIds={connectedEnvironments
140 .filter(
141 (environment) =>
142 environment.serverConfig?.environment.capabilities.storageCleanup === true,
143 )
144 .map((environment) => environment.environmentId)}
145 >
146 Update the selected environments to use storage cleanup, or choose a machine that supports
147 it.
148 </SettingsScopeNotice>
149 );
150 }
151
152 return (
153 <SettingsPageContainer>
154 <SettingsSection id="storage-worktrees" title="Worktrees">
155 {isProjectScope && (
156 <SettingsRow
157 title="Automatic worktree cleanup"
158 description={
159 mode === "off"
160 ? "Keep this project's worktrees until you delete them manually."
161 : mode === "custom"
162 ? "Use these rules for this project."
163 : "Use each machine's worktree cleanup settings."
164 }
165 serverScoped
166 settingKeys={["worktreeCleanup"]}
167 mixed={mixedModes}
168 control={
169 <Select
170 value={mixedModes ? null : mode}
171 onValueChange={(next) => {
172 if (next === "inherit") clearSettings(["worktreeCleanup"]);
173 else if (next === "off") updateSettings({ worktreeCleanup: { mode: "off" } });
174 else if (next === "custom")
175 updateSettings({ worktreeCleanup: { mode: "custom", rules: {} } });
176 }}
177 >
178 <SelectTrigger size="sm" aria-label="Automatic worktree cleanup">
179 <SelectValue>
180 {mixedModes
181 ? "Mixed"
182 : mode === "inherit"
183 ? "Inherit"
184 : mode === "off"
185 ? "Off"
186 : "Custom"}
187 </SelectValue>
188 </SelectTrigger>
189 <SelectPopup align="end" alignItemWithTrigger={false}>
190 <SelectItem value="inherit">Inherit</SelectItem>
191 <SelectItem value="off">Off</SelectItem>
192 <SelectItem value="custom">Custom</SelectItem>
193 </SelectPopup>
194 </Select>
195 }
196 />
197 )}
198 {(!isProjectScope || (!mixedModes && mode === "custom")) && (
199 <>
200 <SettingsRow
201 title="Delete worktrees with deleted threads"
202 status={ruleStatus("worktreeOnDelete")}
203 description="Remove unused worktrees when active or archived threads are deleted. Worktrees with local changes are kept."
204 serverScoped={!isProjectScope}
205 control={
206 <Switch
207 aria-label="Delete worktrees with deleted threads"
208 checked={settings.worktreeOnDelete}
209 onCheckedChange={(worktreeOnDelete) => updateWorktree({ worktreeOnDelete })}
210 />
211 }
212 />
213 <SettingsRow
214 title="Delete inactive worktrees"
215 status={ruleStatus("worktreeAfterDays")}
216 description="Remove worktrees after their threads have been inactive for this many days. Branches and thread history are kept."
217 serverScoped={!isProjectScope}
218 control={
219 <RetentionControl
220 label="Delete inactive worktrees"
221 value={settings.worktreeAfterDays}
222 onChange={(worktreeAfterDays) => updateWorktree({ worktreeAfterDays })}
223 />
224 }
225 />
226 <SettingsRow
227 title="Delete merged worktrees"
228 status={ruleStatus("worktreeOnMerge")}
229 description="Remove worktrees whose pull request is merged and whose commits are included in the default branch."
230 serverScoped={!isProjectScope}
231 control={
232 <Switch
233 aria-label="Delete merged worktrees"
234 checked={settings.worktreeOnMerge}
235 onCheckedChange={(worktreeOnMerge) => updateWorktree({ worktreeOnMerge })}
236 />
237 }
238 />
239 <SettingsRow
240 title="Delete unchanged worktrees"
241 status={ruleStatus("worktreeUnchanged")}
242 description="Remove worktrees with no commits beyond the default branch."
243 serverScoped={!isProjectScope}
244 control={
245 <Switch
246 aria-label="Delete unchanged worktrees"
247 checked={settings.worktreeUnchanged}
248 onCheckedChange={(worktreeUnchanged) => updateWorktree({ worktreeUnchanged })}
249 />
250 }
251 />
252 </>
253 )}
254 </SettingsSection>
255
256 {!isProjectScope && (
257 <SettingsSection id="storage-artifacts" title="Artifacts and logs">
258 <SettingsRow
259 title="Delete old browser artifacts"
260 status={ruleStatus("browserArtifactsAfterDays")}
261 description="Delete saved browser captures after this many days. Older capture links will no longer open."
262 serverScoped
263 control={
264 <RetentionControl
265 label="Delete old browser artifacts"
266 value={settings.browserArtifactsAfterDays}
267 onChange={(browserArtifactsAfterDays) => update({ browserArtifactsAfterDays })}
268 />
269 }
270 />
271 <SettingsRow
272 title="Delete old rotated logs"
273 status={ruleStatus("logsAfterDays")}
274 description="Delete inactive rotated log files after this many days. Current logs are kept."
275 serverScoped
276 control={
277 <RetentionControl
278 label="Delete old rotated logs"
279 value={settings.logsAfterDays}
280 onChange={(logsAfterDays) => update({ logsAfterDays })}
281 />
282 }
283 />
284 </SettingsSection>
285 )}
286 </SettingsPageContainer>
287 );
288}