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

LoadBalancingSettings.tsx

94 lines3.4 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 return (
31 <SettingsSection
32 {...searchableSetting("load-balancing")}
33 description="Choose how often each machine is used. Prefer gives a machine more work when it has capacity; Less often gives it less. Manual only excludes it from automatic selection. Preferences are saved for this client."
34 >
35 <SettingsRow
36 title="Automatically balance load"
37 description="Automatically choose a connected machine for new threads in shared projects. You can choose a machine in the composer."
38 control={
39 <Switch
40 aria-label="Automatically balance load"
41 checked={settings.loadBalancingEnabled}
42 disabled={!settingsHydrated}
43 onCheckedChange={(loadBalancingEnabled) => updateSettings({ loadBalancingEnabled })}
44 />
45 }
46 />
47 {environments.map((environment) => {
48 const weight = settings.loadBalancingWeights[environment.environmentId] ?? 50;
49 // Keep saved slider weights until the user chooses a different preference.
50 const preference = weight === 0 ? 0 : weight < 50 ? 25 : weight === 50 ? 50 : 100;
51​
52 return (
53 <SettingsRow
54 key={environment.environmentId}
55 title={environment.label}
56 description={connectionStatusText(environment.connection)}
57 control={
58 <Select
59 items={preferences}
60 value={preference}
61 disabled={!settingsHydrated || !settings.loadBalancingEnabled}
62 onValueChange={(value) => {
63 if (value !== null) {
64 updateSettings({
65 loadBalancingWeights: {
66 ...settings.loadBalancingWeights,
67 [environment.environmentId]: value,
68 },
69 });
70 }
71 }}
72 >
73 <SelectTrigger
74 size="sm"
75 className="w-full sm:w-40"
76 aria-label={`${environment.label} load preference`}
77 >
78 <SelectValue />
79 </SelectTrigger>
80 <SelectPopup align="end" alignItemWithTrigger={false}>
81 {preferences.map(({ value, label }) => (
82 <SelectItem key={value} value={value}>
83 {label}
84 </SelectItem>
85 ))}
86 </SelectPopup>
87 </Select>
88 }
89 />
90 );
91 })}
92 </SettingsSection>
93 );