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

LoadBalancingSettings.tsx

101 lines3.5 KB

1import { connectionStatusText } from "@t3tools/client-runtime/connection";
2​
3import {
4 useClientSettings,
5 useClientSettingsHydrated,
6 useUpdateClientSettings,
7} from "~/hooks/useSettings";
8import type { EnvironmentPresentation } from "~/state/environments";
9import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select";
10import { Switch } from "../ui/switch";
11import { SettingsRow, SettingsSection } from "./settingsLayout";
12import { searchableSetting } from "./settingsSearch";
13​
14const preferences = [
15 { value: 100, label: "Prefer" },
16 { value: 50, label: "Normal" },
17 { value: 25, label: "Less often" },
18 { value: 0, label: "Manual only" },
19];
20​
21export function LoadBalancingSettings({
22 environments,
23}: {
24 environments: ReadonlyArray<EnvironmentPresentation>;
25}) {
26 const settings = useClientSettings();
27 const settingsHydrated = useClientSettingsHydrated();
28 const updateSettings = useUpdateClientSettings();
29​
30 if (environments.length < 2) {
31 return (
32 <SettingsSection {...searchableSetting("load-balancing")} variant="plain">
33 <p className="px-3 text-sm text-muted-foreground sm:px-4">
34 Connect another machine to automatically balance load across environments.
35 </p>
36 </SettingsSection>
37 );
38 }
39​
40 return (
41 <SettingsSection {...searchableSetting("load-balancing")}>
42 <SettingsRow
43 title="Automatically balance load"
44 description="Choose a machine automatically for new threads in shared projects."
45 control={
46 <Switch
47 aria-label="Automatically balance load"
48 checked={settings.loadBalancingEnabled}
49 disabled={!settingsHydrated}
50 onCheckedChange={(loadBalancingEnabled) => updateSettings({ loadBalancingEnabled })}
51 />
52 }
53 />
54 {environments.map((environment) => {
55 const weight = settings.loadBalancingWeights[environment.environmentId] ?? 50;
56 // Keep saved slider weights until the user chooses a different preference.
57 const preference = weight === 0 ? 0 : weight < 50 ? 25 : weight === 50 ? 50 : 100;
58​
59 return (
60 <SettingsRow
61 key={environment.environmentId}
62 title={environment.label}
63 description={connectionStatusText(environment.connection)}
64 control={
65 <Select
66 items={preferences}
67 value={preference}
68 disabled={!settingsHydrated || !settings.loadBalancingEnabled}
69 onValueChange={(value) => {
70 if (value !== null) {
71 updateSettings({
72 loadBalancingWeights: {
73 ...settings.loadBalancingWeights,
74 [environment.environmentId]: value,
75 },
76 });
77 }
78 }}
79 >
80 <SelectTrigger
81 size="sm"
82 className="w-full sm:w-40"
83 aria-label={`${environment.label} load preference`}
84 >
85 <SelectValue />
86 </SelectTrigger>
87 <SelectPopup align="end" alignItemWithTrigger={false}>
88 {preferences.map(({ value, label }) => (
89 <SelectItem key={value} value={value}>
90 {label}
91 </SelectItem>
92 ))}
93 </SelectPopup>
94 </Select>
95 }
96 />
97 );
98 })}
99 </SettingsSection>
100 );