@@ -9,11 +9,11 @@ import * as Cause from "effect/Cause";
99import { AsyncResult } from "effect/reactivity";
1010import {
1111 DEFAULT_SERVER_SETTINGS,
12+ type ClientSettings,
1213 type ProjectScopedServerSettingKey,
1314 type ServerSettings,
1415 type UnifiedSettings,
1516} from "@t3tools/contracts";
16- import { useCallback, useMemo } from "react";
1717
1818import {
1919 mergeEnvironmentSettings,
@@ -36,18 +36,21 @@ import {
3636 type ScopedSettingsPatch,
3737} from "./scopedSettings";
3838
39+ // Callers often pass an inline selector; a separate hook keeps the merge cached across those.
40+ function useMergedSettings(serverSettings: ServerSettings, clientSettings: ClientSettings) {
41+ "use memo";
42+ return mergeEnvironmentSettings(serverSettings, clientSettings);
43+ }
44+
3945export function useScopedSettings<T = UnifiedSettings>(
4046 selector?: (settings: UnifiedSettings) => T,
4147): T {
4248 const { target } = useSettingsScope();
4349 const clientSettings = useClientSettings();
4450 const serverSettings = target?.settings ?? DEFAULT_SERVER_SETTINGS;
4551 useMigrateModelPreferences(target?.environmentId ?? null, target?.settings ?? null);
46- const settings = useMemo(
47- () => mergeEnvironmentSettings(serverSettings, clientSettings),
48- [clientSettings, serverSettings],
49- );
50- return useMemo(() => (selector ? selector(settings) : (settings as T)), [selector, settings]);
52+ const settings = useMergedSettings(serverSettings, clientSettings);
53+ return selector ? selector(settings) : (settings as T);
5154}
5255
5356export function useScopedSettingsMixed(keys: readonly (keyof ServerSettings)[]): boolean {
@@ -62,86 +65,77 @@ export function useScopedSettingSource(keys: readonly (keyof ServerSettings)[])
6265
6366function useRunScopedPlan() {
6467 const persistServer = useAtomCommand(serverEnvironment.updateSettings, { reportFailure: false });
65- return useCallback(
66- (plan: ReturnType<typeof planScopedSettingsPatch>) => {
67- if (plan.unavailableReason) {
68- toastManager.add({
69- type: "warning",
70- title: "Setting not saved",
71- description: plan.unavailableReason,
72- });
73- return;
74- }
75- void persistScopedSettingsPatch(
76- plan,
77- async (request) => {
78- const missing = requiredScopesForServerSettingsPatch(request.input.patch).find(
79- (scope) => !readEnvironmentScope(request.environmentId, scope),
80- );
81- if (missing)
82- return AsyncResult.failure(
83- Cause.fail(
84- new EnvironmentAuthorizationError({
85- requiredScope: missing,
86- message: "This connection lacks permission to change these settings.",
87- }),
88- ),
89- );
90- return persistServer(request);
91- },
92- persistClientSettingsPatch,
93- ).then(({ failedEnvironments, savedEnvironments, savedEnvironmentCount }) => {
94- if (failedEnvironments.length === 0) return;
95- toastManager.add({
96- type: "error",
97- title:
98- savedEnvironmentCount > 0 ? "Setting saved on some environments" : "Setting not saved",
99- description: [
100- ...failedEnvironments.map(
101- ({ label, message }) => `Could not save on ${label}: ${message}`,
68+ return (plan: ReturnType<typeof planScopedSettingsPatch>) => {
69+ if (plan.unavailableReason) {
70+ toastManager.add({
71+ type: "warning",
72+ title: "Setting not saved",
73+ description: plan.unavailableReason,
74+ });
75+ return;
76+ }
77+ void persistScopedSettingsPatch(
78+ plan,
79+ async (request) => {
80+ const missing = requiredScopesForServerSettingsPatch(request.input.patch).find(
81+ (scope) => !readEnvironmentScope(request.environmentId, scope),
82+ );
83+ if (missing)
84+ return AsyncResult.failure(
85+ Cause.fail(
86+ new EnvironmentAuthorizationError({
87+ requiredScope: missing,
88+ message: "This connection lacks permission to change these settings.",
89+ }),
10290 ),
103- ...(savedEnvironmentCount > 0
104- ? [`Saved on ${savedEnvironments.map(({ label }) => label).join(", ")}.`]
105- : []),
106- ].join("\n"),
107- });
91+ );
92+ return persistServer(request);
93+ },
94+ persistClientSettingsPatch,
95+ ).then(({ failedEnvironments, savedEnvironments, savedEnvironmentCount }) => {
96+ if (failedEnvironments.length === 0) return;
97+ toastManager.add({
98+ type: "error",
99+ title:
100+ savedEnvironmentCount > 0 ? "Setting saved on some environments" : "Setting not saved",
101+ description: [
102+ ...failedEnvironments.map(
103+ ({ label, message }) => `Could not save on ${label}: ${message}`,
104+ ),
105+ ...(savedEnvironmentCount > 0
106+ ? [`Saved on ${savedEnvironments.map(({ label }) => label).join(", ")}.`]
107+ : []),
108+ ].join("\n"),
108109 });
109- },
110- [persistServer],
111- );
110+ });
111+ };
112112}
113113
114114export function useUpdateScopedSettings() {
115115 const { scope, environments } = useSettingsScope();
116116 const run = useRunScopedPlan();
117- return useCallback(
118- (patch: ScopedSettingsPatch) => run(planScopedSettingsPatch(scope, environments, patch)),
119- [environments, run, scope],
120- );
117+ return (patch: ScopedSettingsPatch) => run(planScopedSettingsPatch(scope, environments, patch));
121118}
122119
123120export function useClearScopedSettings() {
124121 const context = useOptionalSettingsScope();
125122 const run = useRunScopedPlan();
126- return useCallback(
127- (keys: readonly ProjectScopedServerSettingKey[]) => {
128- if (context === null) return;
129- run(planScopedSettingsClear(context.scope, context.environments, keys));
130- },
131- [context, run],
132- );
123+ return (keys: readonly ProjectScopedServerSettingKey[]) => {
124+ if (context === null) return;
125+ run(planScopedSettingsClear(context.scope, context.environments, keys));
126+ };
133127}
134128
135129export function useClearProjectOverrides() {
136130 const context = useOptionalSettingsScope();
137131 const run = useRunScopedPlan();
138- return useCallback (
139- ( entries: readonly ProjectOverrideEntry[], keys: readonly ProjectScopedServerSettingKey[]) => {
140- if (context === null) return;
141- run(planProjectOverridesClear(context.environments, entries, keys));
142- },
143- [ context, run],
144- ) ;
132+ return (
133+ entries: readonly ProjectOverrideEntry[],
134+ keys: readonly ProjectScopedServerSettingKey[],
135+ ) => {
136+ if (context === null) return;
137+ run(planProjectOverridesClear( context.environments, entries, keys));
138+ } ;
145139}
146140
147141export function useScopedSettingsWriteAllowed() {
0 commit comments