Open History to see the last commit
1import {
2 DEFAULT_SERVER_SETTINGS,
3 type ProjectScopedServerSettingKey,
4 type ServerSettings,
5 type UnifiedSettings,
6} from "@t3tools/contracts";
7import { useCallback, useMemo } from "react";
8
9import {
10 mergeEnvironmentSettings,
11 persistClientSettingsPatch,
12 useClientSettings,
13} from "../../hooks/useSettings";
14import { serverEnvironment } from "../../state/server";
15import { useAtomCommand } from "../../state/use-atom-command";
16import { toastManager } from "../ui/toast";
17import { useOptionalSettingsScope, useSettingsScope } from "./SettingsScopeContext";
18import {
19 persistScopedSettingsPatch,
20 planProjectOverridesClear,
21 planScopedSettingsClear,
22 planScopedSettingsPatch,
23 scopedSettingsAreMixed,
24 scopedSettingsSource,
25 type ProjectOverrideEntry,
26 type ScopedSettingsPatch,
27} from "./scopedSettings";
28
29/** Effective settings for the representative target: project overrides applied on top of its environment. */
30export function useScopedSettings<T = UnifiedSettings>(
31 selector?: (settings: UnifiedSettings) => T,
32): T {
33 const { target } = useSettingsScope();
34 const clientSettings = useClientSettings();
35 const serverSettings = target?.settings ?? DEFAULT_SERVER_SETTINGS;
36 const settings = useMemo(
37 () => mergeEnvironmentSettings(serverSettings, clientSettings),
38 [clientSettings, serverSettings],
39 );
40 return useMemo(() => (selector ? selector(settings) : (settings as T)), [selector, settings]);
41}
42
43export function useScopedSettingsMixed(keys: readonly (keyof ServerSettings)[]): boolean {
44 const { targets } = useSettingsScope();
45 return scopedSettingsAreMixed(targets, keys);
46}
47
48/** Where the keys' effective values come from across the selected targets. */
49export function useScopedSettingSource(keys: readonly (keyof ServerSettings)[]) {
50 const { targets } = useSettingsScope();
51 return scopedSettingsSource(targets, keys);
52}
53
54function useRunScopedPlan() {
55 const persistServer = useAtomCommand(serverEnvironment.updateSettings, { reportFailure: false });
56 return useCallback(
57 (plan: ReturnType<typeof planScopedSettingsPatch>) => {
58 if (plan.unavailableReason) {
59 toastManager.add({
60 type: "warning",
61 title: "Setting not saved",
62 description: plan.unavailableReason,
63 });
64 return;
65 }
66 void persistScopedSettingsPatch(plan, persistServer, persistClientSettingsPatch).then(
67 ({ failedEnvironments, savedEnvironmentCount }) => {
68 if (failedEnvironments.length === 0) return;
69 toastManager.add({
70 type: "error",
71 title:
72 savedEnvironmentCount > 0
73 ? "Setting saved on some environments"
74 : "Setting not saved",
75 description: `Could not update ${failedEnvironments.map((environment) => environment.label).join(", ")}.${savedEnvironmentCount > 0 ? " The other selected environments saved the change." : ""}`,
76 });
77 },
78 );
79 },
80 [persistServer],
81 );
82}
83
84export function useUpdateScopedSettings() {
85 const { scope, environments } = useSettingsScope();
86 const run = useRunScopedPlan();
87 return useCallback(
88 (patch: ScopedSettingsPatch) => run(planScopedSettingsPatch(scope, environments, patch)),
89 [environments, run, scope],
90 );
91}
92
93/**
94 * Drop the project overrides for `keys` so the selected checkouts inherit
95 * again. Rows also render outside the settings layout (provider cards,
96 * dialogs), where there is no scope and nothing to clear.
97 */
98export function useClearScopedSettings() {
99 const context = useOptionalSettingsScope();
100 const run = useRunScopedPlan();
101 return useCallback(
102 (keys: readonly ProjectScopedServerSettingKey[]) => {
103 if (context === null) return;
104 run(planScopedSettingsClear(context.scope, context.environments, keys));
105 },
106 [context, run],
107 );
108}
109
110/** Clear `keys` on specific project entries, from an environment scope's chain popover. */
111export function useClearProjectOverrides() {
112 const context = useOptionalSettingsScope();
113 const run = useRunScopedPlan();
114 return useCallback(
115 (entries: readonly ProjectOverrideEntry[], keys: readonly ProjectScopedServerSettingKey[]) => {
116 if (context === null) return;
117 run(planProjectOverridesClear(context.environments, entries, keys));
118 },
119 [context, run],
120 );
121}