Open History to see the last commit
1import {
2 useClientSettings,
3 useClientSettingsHydrated,
4 useUpdateClientSettings,
5} from "~/hooks/useSettings";
6import type { EnvironmentPresentation } from "~/state/environments";
7import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select";
8import { Switch } from "../ui/switch";
9import { SettingsRow, SettingsSection } from "./settingsLayout";
10import { searchableSetting } from "./settingsSearch";
11
12const preferences = [
13 { value: 100, label: "Prefer" },
14 { value: 50, label: "Normal" },
15 { value: 25, label: "Less often" },
16 { value: 0, label: "Manual only" },
17];
18
19export function LoadBalancingSettings({
20 environments,
21}: {
22 environments: ReadonlyArray<EnvironmentPresentation>;
23}) {
24 const settings = useClientSettings();
25 const settingsHydrated = useClientSettingsHydrated();
26 const updateSettings = useUpdateClientSettings();
27
28 if (environments.length < 2) {
29 return (
30 <SettingsSection {...searchableSetting("load-balancing")} variant="plain">
31 <p className="px-3 text-sm text-muted-foreground sm:px-4">
32 Connect another machine to automatically balance load across environments.
33 </p>
34 </SettingsSection>
35 );
36 }
37
38 return (
39 <SettingsSection {...searchableSetting("load-balancing")}>
40 <SettingsRow
41 title="Automatically balance load"
42 description="Choose a machine automatically for new threads in shared projects."
43 control={
44 <Switch
45 aria-label="Automatically balance load"
46 checked={settings.loadBalancingEnabled}
47 disabled={!settingsHydrated}
48 onCheckedChange={(loadBalancingEnabled) => updateSettings({ loadBalancingEnabled })}
49 />
50 }
51 />
52 </SettingsSection>
53 );
54}
55
56export function LoadBalancingPreference({ environment }: { environment: EnvironmentPresentation }) {
57 const settings = useClientSettings();
58 const settingsHydrated = useClientSettingsHydrated();
59 const updateSettings = useUpdateClientSettings();
60 const weight = settings.loadBalancingWeights[environment.environmentId] ?? 50;
61 // Keep saved slider weights until the user chooses a different preference.
62 const preference = weight === 0 ? 0 : weight < 50 ? 25 : weight === 50 ? 50 : 100;
63
64 return (
65 <SettingsRow
66 title="Load preference"
67 description="How often this machine receives new threads when load balancing is on."
68 control={
69 <Select
70 items={preferences}
71 value={preference}
72 disabled={!settingsHydrated || !settings.loadBalancingEnabled}
73 onValueChange={(value) => {
74 if (value !== null) {
75 updateSettings({
76 loadBalancingWeights: {
77 ...settings.loadBalancingWeights,
78 [environment.environmentId]: value,
79 },
80 });
81 }
82 }}
83 >
84 <SelectTrigger
85 size="sm"
86 className="w-full sm:w-40"
87 aria-label={`${environment.label} load preference`}
88 >
89 <SelectValue />
90 </SelectTrigger>
91 <SelectPopup align="end" alignItemWithTrigger={false}>
92 {preferences.map(({ value, label }) => (
93 <SelectItem key={value} value={value}>
94 {label}
95 </SelectItem>
96 ))}
97 </SelectPopup>
98 </Select>
99 }
100 />
101 );
102}