Open History to see the last commit
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 {...searchableSetting("load-balancing")}>
32 <SettingsRow
33 title="Automatically balance load"
34 description="Choose a machine automatically for new threads in shared projects."
35 control={
36 <Switch
37 aria-label="Automatically balance load"
38 checked={settings.loadBalancingEnabled}
39 disabled={!settingsHydrated}
40 onCheckedChange={(loadBalancingEnabled) => updateSettings({ loadBalancingEnabled })}
41 />
42 }
43 />
44 {environments.map((environment) => {
45 const weight = settings.loadBalancingWeights[environment.environmentId] ?? 50;
46 // Keep saved slider weights until the user chooses a different preference.
47 const preference = weight === 0 ? 0 : weight < 50 ? 25 : weight === 50 ? 50 : 100;
48
49 return (
50 <SettingsRow
51 key={environment.environmentId}
52 title={environment.label}
53 description={connectionStatusText(environment.connection)}
54 control={
55 <Select
56 items={preferences}
57 value={preference}
58 disabled={!settingsHydrated || !settings.loadBalancingEnabled}
59 onValueChange={(value) => {
60 if (value !== null) {
61 updateSettings({
62 loadBalancingWeights: {
63 ...settings.loadBalancingWeights,
64 [environment.environmentId]: value,
65 },
66 });
67 }
68 }}
69 >
70 <SelectTrigger
71 size="sm"
72 className="w-full sm:w-40"
73 aria-label={`${environment.label} load preference`}
74 >
75 <SelectValue />
76 </SelectTrigger>
77 <SelectPopup align="end" alignItemWithTrigger={false}>
78 {preferences.map(({ value, label }) => (
79 <SelectItem key={value} value={value}>
80 {label}
81 </SelectItem>
82 ))}
83 </SelectPopup>
84 </Select>
85 }
86 />
87 );
88 })}
89 </SettingsSection>
90 );
91}