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

LoadBalancingSettings.tsx

123 lines4.2 KB

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;
29​
30function preferenceLabel(preference: LoadPreference): string {
31 return preferences.find((entry) => entry.value === preference)!.label;
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(" · ");
46​
47export function LoadBalancingSettings({
48 environments,
49}: {
50 environments: ReadonlyArray<EnvironmentPresentation>;
51}) {
52 const settings = useClientSettings();
53 const settingsHydrated = useClientSettingsHydrated();
54 const updateSettings = useUpdateClientSettings();
55​
56 if (environments.length < 2) return null;
57​
58 const { id, title } = searchableSetting("load-balancing");
59 return (
60 <FoldedSettingsSection
61 id={id}
62 title={title}
63 summary={
64 settings.loadBalancingEnabled
65 ? summarizeLoadPreferences(environments, settings.loadBalancingWeights)
66 : "Off"
67 }
68 control={
69 <Switch
70 aria-label="Automatically balance load"
71 checked={settings.loadBalancingEnabled}
72 disabled={!settingsHydrated}
73 onCheckedChange={(loadBalancingEnabled) => updateSettings({ loadBalancingEnabled })}
74 />
75 }
76 >
77 <p className="px-3 py-2.5 text-xs text-muted-foreground sm:px-4">
78 New threads in shared projects start on the machine with the most free CPU and memory,
79 weighted by each machine's preference.
80 </p>
81 {environments.map((environment) => (
82 <EnvironmentRow
83 key={environment.environmentId}
84 kind={resolveEnvironmentMachineKind(environment.serverConfig)}
85 label={environment.label}
86 subtitle={environmentTransportLabel(environment)}
87 >
88 <Select
89 items={preferences}
90 value={loadPreferenceForWeight(
91 settings.loadBalancingWeights[environment.environmentId],
92 )}
93 disabled={!settingsHydrated || !settings.loadBalancingEnabled}
94 onValueChange={(value) => {
95 if (value === null) return;
96 updateSettings({
97 loadBalancingWeights: {
98 ...settings.loadBalancingWeights,
99 [environment.environmentId]: value,
100 },
101 });
102 }}
103 >
104 <SelectTrigger
105 size="xs"
106 className="w-32"
107 aria-label={`${environment.label} load preference`}
108 >
109 <SelectValue />
110 </SelectTrigger>
111 <SelectPopup align="end" alignItemWithTrigger={false}>
112 {preferences.map(({ value, label }) => (
113 <SelectItem key={value} value={value}>
114 {label}
115 </SelectItem>
116 ))}
117 </SelectPopup>
118 </Select>
119 </EnvironmentRow>
120 ))}
121 </FoldedSettingsSection>
122 );