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

BetaSettingsPanel.tsx

119 lines4.0 KB

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