@devries-cameron20 / t3code-1 / commits / 383d8cc6ae
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

perf(web): scoped settings hooks compile without manual memoization (#20677)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 16, 2026 at 21:54 UTC 383d8cc6aedc7ffb27984c8dc0a64be6a3b67f7d
1 file changed +63 -69
apps/web/src/components/settings/useScopedSettings.ts
+63 -69
@@ -9,11 +9,11 @@ import * as Cause from "effect/Cause";
9 import { AsyncResult } from "effect/reactivity";
10 import {
11 DEFAULT_SERVER_SETTINGS,
12 + type ClientSettings,
13 type ProjectScopedServerSettingKey,
14 type ServerSettings,
15 type UnifiedSettings,
16 } from "@t3tools/contracts";
16 -import { useCallback, useMemo } from "react";
17
18 import {
19 mergeEnvironmentSettings,
@@ -36,6 +36,12 @@ import {
36 type ScopedSettingsPatch,
37 } from "./scopedSettings";
38
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 +
45 export function useScopedSettings<T = UnifiedSettings>(
46 selector?: (settings: UnifiedSettings) => T,
47 ): T {
@@ -43,11 +49,8 @@ export function useScopedSettings<T = UnifiedSettings>(
49 const clientSettings = useClientSettings();
50 const serverSettings = target?.settings ?? DEFAULT_SERVER_SETTINGS;
51 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);
54 }
55
56 export function useScopedSettingsMixed(keys: readonly (keyof ServerSettings)[]): boolean {
@@ -62,86 +65,77 @@ export function useScopedSettingSource(keys: readonly (keyof ServerSettings)[])
65
66 function useRunScopedPlan() {
67 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 + }),
90 ),
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"),
109 });
109 - },
110 - [persistServer],
111 - );
110 + });
111 + };
112 }
113
114 export function useUpdateScopedSettings() {
115 const { scope, environments } = useSettingsScope();
116 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));
118 }
119
120 export function useClearScopedSettings() {
121 const context = useOptionalSettingsScope();
122 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 + };
127 }
128
129 export function useClearProjectOverrides() {
130 const context = useOptionalSettingsScope();
131 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 + };
139 }
140
141 export function useScopedSettingsWriteAllowed() {