Open History to see the last commit
1import { resolveEnvironmentMachineKind } from "@t3tools/contracts";
2
3import {
4 useClientSettings,
5 useClientSettingsHydrated,
6 updateClientSettings,
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
24function 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
34function 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
58 if (environments.length < 2) return null;
59
60 const { id, title } = searchableSetting("load-balancing");
61 return (
62 <FoldedSettingsSection
63 id={id}
64 title={title}
65 summary={
66 settings.loadBalancingEnabled
67 ? summarizeLoadPreferences(environments, settings.loadBalancingWeights)
68 : "Off"
69 }
70 control={
71 <Switch
72 aria-label="Automatically balance load"
73 checked={settings.loadBalancingEnabled}
74 disabled={!settingsHydrated}
75 onCheckedChange={(loadBalancingEnabled) => updateClientSettings({ loadBalancingEnabled })}
76 />
77 }
78 >
79 <p className="px-3 py-2.5 text-xs text-muted-foreground sm:px-4">
80 New threads in shared projects start on the machine with the most free CPU and memory,
81 weighted by each machine's preference.
82 </p>
83 {environments.map((environment) => (
84 <EnvironmentRow
85 key={environment.environmentId}
86 kind={resolveEnvironmentMachineKind(environment.serverConfig)}
87 label={environment.label}
88 subtitle={<EnvironmentTransportLabel environment={environment} />}
89 >
90 <Select
91 items={preferences}
92 value={loadPreferenceForWeight(
93 settings.loadBalancingWeights[environment.environmentId],
94 )}
95 disabled={!settingsHydrated || !settings.loadBalancingEnabled}
96 onValueChange={(value) => {
97 if (value === null) return;
98 updateClientSettings({
99 loadBalancingWeights: {
100 ...settings.loadBalancingWeights,
101 [environment.environmentId]: value,
102 },
103 });
104 }}
105 >
106 <SelectTrigger
107 size="xs"
108 className="w-32"
109 aria-label={`${environment.label} load preference`}
110 >
111 <SelectValue />
112 </SelectTrigger>
113 <SelectPopup align="end" alignItemWithTrigger={false}>
114 {preferences.map(({ value, label }) => (
115 <SelectItem key={value} value={value}>
116 {label}
117 </SelectItem>
118 ))}
119 </SelectPopup>
120 </Select>
121 </EnvironmentRow>
122 ))}
123 </FoldedSettingsSection>
124 );
125}