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

BetaSettingsPanel.tsx

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