Skip to content
TypeScript

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

Julius Marminge383d8cc6aedc
diff --git a/apps/web/src/components/settings/useScopedSettings.ts b/apps/web/src/components/settings/useScopedSettings.ts
index cb1c6b0c954..f950e233171 100644
--- a/apps/web/src/components/settings/useScopedSettings.ts
+++ b/apps/web/src/components/settings/useScopedSettings.ts
@@ -9,11 +9,11 @@ import * as Cause from "effect/Cause";
import { AsyncResult } from "effect/reactivity";
import {
DEFAULT_SERVER_SETTINGS,
+ type ClientSettings,
type ProjectScopedServerSettingKey,
type ServerSettings,
type UnifiedSettings,
} from "@t3tools/contracts";
-import { useCallback, useMemo } from "react";
import {
mergeEnvironmentSettings,
@@ -36,6 +36,12 @@ import {
type ScopedSettingsPatch,
} from "./scopedSettings";
+// Callers often pass an inline selector; a separate hook keeps the merge cached across those.
+function useMergedSettings(serverSettings: ServerSettings, clientSettings: ClientSettings) {
+ "use memo";
+ return mergeEnvironmentSettings(serverSettings, clientSettings);
+}
+
export function useScopedSettings<T = UnifiedSettings>(
selector?: (settings: UnifiedSettings) => T,
): T {
@@ -43,11 +49,8 @@ export function useScopedSettings<T = UnifiedSettings>(
const clientSettings = useClientSettings();
const serverSettings = target?.settings ?? DEFAULT_SERVER_SETTINGS;
useMigrateModelPreferences(target?.environmentId ?? null, target?.settings ?? null);
- const settings = useMemo(
- () => mergeEnvironmentSettings(serverSettings, clientSettings),
- [clientSettings, serverSettings],
- );
- return useMemo(() => (selector ? selector(settings) : (settings as T)), [selector, settings]);
+ const settings = useMergedSettings(serverSettings, clientSettings);
+ return selector ? selector(settings) : (settings as T);
}
export function useScopedSettingsMixed(keys: readonly (keyof ServerSettings)[]): boolean {
@@ -62,86 +65,77 @@ export function useScopedSettingSource(keys: readonly (keyof ServerSettings)[])
function useRunScopedPlan() {
const persistServer = useAtomCommand(serverEnvironment.updateSettings, { reportFailure: false });
- return useCallback(
- (plan: ReturnType<typeof planScopedSettingsPatch>) => {
- if (plan.unavailableReason) {
- toastManager.add({
- type: "warning",
- title: "Setting not saved",
- description: plan.unavailableReason,
- });
- return;
- }
- void persistScopedSettingsPatch(
- plan,
- async (request) => {
- const missing = requiredScopesForServerSettingsPatch(request.input.patch).find(
- (scope) => !readEnvironmentScope(request.environmentId, scope),
- );
- if (missing)
- return AsyncResult.failure(
- Cause.fail(
- new EnvironmentAuthorizationError({
- requiredScope: missing,
- message: "This connection lacks permission to change these settings.",
- }),
- ),
- );
- return persistServer(request);
- },
- persistClientSettingsPatch,
- ).then(({ failedEnvironments, savedEnvironments, savedEnvironmentCount }) => {
- if (failedEnvironments.length === 0) return;
- toastManager.add({
- type: "error",
- title:
- savedEnvironmentCount > 0 ? "Setting saved on some environments" : "Setting not saved",
- description: [
- ...failedEnvironments.map(
- ({ label, message }) => `Could not save on ${label}: ${message}`,
+ return (plan: ReturnType<typeof planScopedSettingsPatch>) => {
+ if (plan.unavailableReason) {
+ toastManager.add({
+ type: "warning",
+ title: "Setting not saved",
+ description: plan.unavailableReason,
+ });
+ return;
+ }
+ void persistScopedSettingsPatch(
+ plan,
+ async (request) => {
+ const missing = requiredScopesForServerSettingsPatch(request.input.patch).find(
+ (scope) => !readEnvironmentScope(request.environmentId, scope),
+ );
+ if (missing)
+ return AsyncResult.failure(
+ Cause.fail(
+ new EnvironmentAuthorizationError({
+ requiredScope: missing,
+ message: "This connection lacks permission to change these settings.",
+ }),
),
- ...(savedEnvironmentCount > 0
- ? [`Saved on ${savedEnvironments.map(({ label }) => label).join(", ")}.`]
- : []),
- ].join("\n"),
- });
+ );
+ return persistServer(request);
+ },
+ persistClientSettingsPatch,
+ ).then(({ failedEnvironments, savedEnvironments, savedEnvironmentCount }) => {
+ if (failedEnvironments.length === 0) return;
+ toastManager.add({
+ type: "error",
+ title:
+ savedEnvironmentCount > 0 ? "Setting saved on some environments" : "Setting not saved",
+ description: [
+ ...failedEnvironments.map(
+ ({ label, message }) => `Could not save on ${label}: ${message}`,
+ ),
+ ...(savedEnvironmentCount > 0
+ ? [`Saved on ${savedEnvironments.map(({ label }) => label).join(", ")}.`]
+ : []),
+ ].join("\n"),
});
- },
- [persistServer],
- );
+ });
+ };
}
export function useUpdateScopedSettings() {
const { scope, environments } = useSettingsScope();
const run = useRunScopedPlan();
- return useCallback(
- (patch: ScopedSettingsPatch) => run(planScopedSettingsPatch(scope, environments, patch)),
- [environments, run, scope],
- );
+ return (patch: ScopedSettingsPatch) => run(planScopedSettingsPatch(scope, environments, patch));
}
export function useClearScopedSettings() {
const context = useOptionalSettingsScope();
const run = useRunScopedPlan();
- return useCallback(
- (keys: readonly ProjectScopedServerSettingKey[]) => {
- if (context === null) return;
- run(planScopedSettingsClear(context.scope, context.environments, keys));
- },
- [context, run],
- );
+ return (keys: readonly ProjectScopedServerSettingKey[]) => {
+ if (context === null) return;
+ run(planScopedSettingsClear(context.scope, context.environments, keys));
+ };
}
export function useClearProjectOverrides() {
const context = useOptionalSettingsScope();
const run = useRunScopedPlan();
- return useCallback(
- (entries: readonly ProjectOverrideEntry[], keys: readonly ProjectScopedServerSettingKey[]) => {
- if (context === null) return;
- run(planProjectOverridesClear(context.environments, entries, keys));
- },
- [context, run],
- );
+ return (
+ entries: readonly ProjectOverrideEntry[],
+ keys: readonly ProjectScopedServerSettingKey[],
+ ) => {
+ if (context === null) return;
+ run(planProjectOverridesClear(context.environments, entries, keys));
+ };
}
export function useScopedSettingsWriteAllowed() {