Open History to see the last commit
1import { resolveEnvironmentMachineKind } from "@t3tools/contracts";
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 { EnvironmentRow, environmentTransportLabel } from "./EnvironmentRow";
12import { FoldedSettingsSection } from "./FoldedSettingsSection";
13import { searchableSetting } from "./settingsSearch";
14
15const preferences = [
16 { value: 100, label: "Prefer" },
17 { value: 50, label: "Normal" },
18 { value: 25, label: "Less often" },
19 { value: 0, label: "Manual only" },
20] as const;
21
22type LoadPreference = (typeof preferences)[number]["value"];
23
24export function loadPreferenceForWeight(weight: number | undefined): LoadPreference {
25 if (weight === undefined || weight === 50) return 50;
26 if (weight === 0) return 0;
27 return weight < 50 ? 25 : 100;
28}
29
30function preferenceLabel(preference: LoadPreference): string {
31 return preferences.find((entry) => entry.value === preference)!.label;
32}
33
34export function summarizeLoadPreferences(
35 environments: ReadonlyArray<Pick<EnvironmentPresentation, "environmentId" | "label">>,
36 weights: Readonly<Record<string, number>>,
37): string | null {
38 const parts = environments.flatMap((environment) => {
39 const preference = loadPreferenceForWeight(weights[environment.environmentId]);
40 return preference === 50
41 ? []
42 : [`${environment.label} ${preferenceLabel(preference).toLowerCase()}`];
43 });
44 return parts.length === 0 ? null : parts.join(" · ");
45}
46
47export function LoadBalancingSettings({
48 environments,
49}: {
50 environments: ReadonlyArray<EnvironmentPresentation>;
51}) {
52 const settings = useClientSettings((current) => ({
53 loadBalancingEnabled: current.loadBalancingEnabled,
54 loadBalancingWeights: current.loadBalancingWeights,
55 }));
56 const settingsHydrated = useClientSettingsHydrated();
57 const updateSettings = useUpdateClientSettings();
58
59 if (environments.length < 2) return null;
60
61 const { id, title } = searchableSetting("load-balancing");
62 return (
63 <FoldedSettingsSection
64 id={id}
65 title={title}
66 summary={
67 settings.loadBalancingEnabled
68 ? summarizeLoadPreferences(environments, settings.loadBalancingWeights)
69 : "Off"
70 }
71 control={
72 <Switch
73 aria-label="Automatically balance load"
74 checked={settings.loadBalancingEnabled}
75 disabled={!settingsHydrated}
76 onCheckedChange={(loadBalancingEnabled) => updateSettings({ loadBalancingEnabled })}
77 />
78 }
79 >
80 <p className="px-3 py-2.5 text-xs text-muted-foreground sm:px-4">
81 New threads in shared projects start on the machine with the most free CPU and memory,
82 weighted by each machine's preference.
83 </p>
84 {environments.map((environment) => (
85 <EnvironmentRow
86 key={environment.environmentId}
87 kind={resolveEnvironmentMachineKind(environment.serverConfig)}
88 label={environment.label}
89 subtitle={environmentTransportLabel(environment)}
90 >
91 <Select
92 items={preferences}
93 value={loadPreferenceForWeight(
94 settings.loadBalancingWeights[environment.environmentId],
95 )}
96 disabled={!settingsHydrated || !settings.loadBalancingEnabled}
97 onValueChange={(value) => {
98 if (value === null) return;
99 updateSettings({
100 loadBalancingWeights: {
101 ...settings.loadBalancingWeights,
102 [environment.environmentId]: value,
103 },
104 });
105 }}
106 >
107 <SelectTrigger
108 size="xs"
109 className="w-32"
110 aria-label={`${environment.label} load preference`}
111 >
112 <SelectValue />
113 </SelectTrigger>
114 <SelectPopup align="end" alignItemWithTrigger={false}>
115 {preferences.map(({ value, label }) => (
116 <SelectItem key={value} value={value}>
117 {label}
118 </SelectItem>
119 ))}
120 </SelectPopup>
121 </Select>
122 </EnvironmentRow>
123 ))}
124 </FoldedSettingsSection>
125 );
126}