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

BetaSettingsPanel.tsx

108 lines3.7 KB

1import { useEffect, useState } from "react";
2​
3import { useClientSettings, useUpdateClientSettings } from "../../hooks/useSettings";
4import { Input } from "../ui/input";
5import { Switch } from "../ui/switch";
6import { SettingsPageContainer, SettingsRow, SettingsSection } from "./settingsLayout";
7​
8const AUTO_SETTLE_MIN_DAYS = 1;
9const AUTO_SETTLE_MAX_DAYS = 90;
10const AUTO_SETTLE_DEFAULT_DAYS = 3;
11​
12function AutoSettleDaysInput({
13 value,
14 onCommit,
15}: {
16 value: number;
17 onCommit: (days: number) => void;
18}) {
19 // Local draft so the field can be emptied mid-edit; the setting only moves
20 // on valid input and snaps back to the persisted value on blur.
21 const [draft, setDraft] = useState(String(value));
22 useEffect(() => {
23 setDraft(String(value));
24 }, [value]);
25​
26 return (
27 <Input
28 type="number"
29 min={AUTO_SETTLE_MIN_DAYS}
30 max={AUTO_SETTLE_MAX_DAYS}
31 className="w-full sm:w-24"
32 value={draft}
33 onChange={(event) => {
34 setDraft(event.target.value);
35 // Number(), not parseInt: "3.5" must be rejected (not truncated to a
36 // committed 3 while the field shows 3.5) — commit only when the
37 // persisted value matches the displayed one.
38 const parsed = Number(event.target.value);
39 if (
40 Number.isInteger(parsed) &&
41 parsed >= AUTO_SETTLE_MIN_DAYS &&
42 parsed <= AUTO_SETTLE_MAX_DAYS
43 ) {
44 onCommit(parsed);
45 }
46 }}
47 onBlur={() => setDraft(String(value))}
48 aria-label="Days of inactivity before auto-settle"
49 />
50 );
52​
53export function BetaSettingsPanel() {
54 const sidebarV2Enabled = useClientSettings((settings) => settings.sidebarV2Enabled);
55 const sidebarAutoSettleAfterDays = useClientSettings(
56 (settings) => settings.sidebarAutoSettleAfterDays,
57 );
58 const updateSettings = useUpdateClientSettings();
59​
60 return (
61 <SettingsPageContainer>
62 <SettingsSection title="Beta features">
63 <SettingsRow
64 title="Sidebar v2"
65 description="One flat thread list in creation order. Active work renders as rich cards; settled threads collapse to compact rows. Settling requires an up-to-date server — on older servers threads simply stay active. Switch back any time."
66 control={
67 <Switch
68 checked={sidebarV2Enabled}
69 onCheckedChange={(checked) => updateSettings({ sidebarV2Enabled: Boolean(checked) })}
70 aria-label="Enable the sidebar v2 beta"
71 />
72 }
73 />
74 {sidebarV2Enabled ? (
75 <>
76 <SettingsRow
77 title="Auto-settle inactive threads"
78 description="Threads with no activity for this long settle automatically. Threads on merged or closed PRs always settle."
79 control={
80 <Switch
81 checked={sidebarAutoSettleAfterDays !== null}
82 onCheckedChange={(checked) =>
83 updateSettings({
84 sidebarAutoSettleAfterDays: checked ? AUTO_SETTLE_DEFAULT_DAYS : null,
85 })
86 }
87 aria-label="Auto-settle inactive threads"
88 />
89 }
90 />
91 {sidebarAutoSettleAfterDays !== null ? (
92 <SettingsRow
93 title="Days of inactivity before auto-settle"
94 description="Any new activity un-settles a thread automatically."
95 control={
96 <AutoSettleDaysInput
97 value={sidebarAutoSettleAfterDays}
98 onCommit={(days) => updateSettings({ sidebarAutoSettleAfterDays: days })}
99 />
100 }
101 />
102 ) : null}
103 </>
104 ) : null}
105 </SettingsSection>
106 </SettingsPageContainer>
107 );