Fix legacy patch fields overriding explicit backgroundActivity and extract duplicated settings utilities

- Fix applyServerSettingsPatch: skip legacy-derived backgroundActivityPatch when
  an explicit backgroundActivity object is present in the patch, preventing silent
  override of new-style patches by legacy fields.

- Extract shared background activity utilities (BackgroundActivityOverridePatch,
  durationToSeconds, normalizeIntervalSeconds, backgroundActivityOverrideSettings,
  BackgroundPolicyTooltip) into backgroundActivityUtils.tsx to eliminate duplication
  between SettingsPanels.tsx and SourceControlSettings.tsx.
This commit is contained in:
Cursor Agent committed 2026-05-13 22:37:29 +00:00
1 parent 038e209ece
commit 6d184f0660
4 files changed
+99 -139

No files matched your search

@@ -1,7 +1,6 @@
import {
ArchiveIcon,
ArchiveX,
InfoIcon,
LoaderIcon,
PlusIcon,
RefreshCwIcon,
@@ -106,6 +105,12 @@ import {
SettingsSection,
useRelativeTimeTick,
} from "./settingsLayout";
import {
BackgroundPolicyTooltip as PolicyTooltip,
backgroundActivityOverrideSettings,
durationToSeconds,
normalizeIntervalSeconds,
} from "./backgroundActivityUtils";
import { ProjectFavicon } from "../ProjectFavicon";
import { useServerObservability, useServerProviders } from "../../rpc/serverState";
@@ -137,11 +142,6 @@ const BACKGROUND_ACTIVITY_PROFILE_LABELS: Record<BackgroundActivityProfile, stri
};
type BackgroundActivityProfileOption = BackgroundActivityProfile | "advanced";
type BackgroundActivityOverridePatch = Partial<{
[K in keyof BackgroundActivitySettings["overrides"]]:
| BackgroundActivitySettings["overrides"][K]
| undefined;
}>;
const BACKGROUND_ACTIVITY_PROFILE_OPTION_LABELS: Record<BackgroundActivityProfileOption, string> = {
...BACKGROUND_ACTIVITY_PROFILE_LABELS,
@@ -174,17 +174,6 @@ const BACKGROUND_ACTIVITY_BOOLEAN_OVERRIDES: ReadonlyArray<{
{ key: "pauseWhenOnBattery", label: "Pause on battery" },
];
function durationToSeconds(duration: Duration.Duration): number {
return Math.round(Duration.toMillis(duration) / 1_000);
}
function normalizeIntervalSeconds(value: number | null): number {
if (value === null || !Number.isFinite(value)) {
return 0;
}
return Math.max(0, Math.round(value));
}
function resolveBackgroundActivityProfileOption(settings: {
readonly backgroundActivity: BackgroundActivitySettings;
}): BackgroundActivityProfileOption {
@@ -209,50 +198,6 @@ function backgroundActivityProfileSettings(profile: BackgroundActivityProfile) {
};
}
function backgroundActivityOverrideSettings(
current: BackgroundActivitySettings,
overrides: BackgroundActivityOverridePatch,
) {
const nextOverrides: BackgroundActivityOverridePatch = {
...current.overrides,
...overrides,
};
for (const [key, value] of Object.entries(nextOverrides)) {
if (value === undefined) {
delete nextOverrides[key as keyof typeof nextOverrides];
}
}
return {
backgroundActivity: {
schemaVersion: 1 as const,
profile: "custom" as const,
baseProfile: getBackgroundActivityBaseProfile(current),
overrides: nextOverrides as BackgroundActivitySettings["overrides"],
},
};
}
function PolicyTooltip({ children }: { readonly children: string }) {
return (
<Tooltip>
<TooltipTrigger
render={
<button
type="button"
className="inline-flex size-5 items-center justify-center rounded-sm text-muted-foreground hover:text-foreground"
aria-label="Background policy details"
>
<InfoIcon className="size-3.5" />
</button>
}
/>
<TooltipPopup side="top" className="max-w-72">
{children}
</TooltipPopup>
</Tooltip>
);
}
function withoutProviderInstanceKey<V>(
record: Readonly<Record<ProviderInstanceId, V>> | undefined,
key: ProviderInstanceId,
@@ -1,9 +1,8 @@
import { ChevronDownIcon, GitPullRequestIcon, InfoIcon, RefreshCwIcon } from "lucide-react";
import { ChevronDownIcon, GitPullRequestIcon, RefreshCwIcon } from "lucide-react";
import * as Duration from "effect/Duration";
import * as Option from "effect/Option";
import { useState, type ReactNode } from "react";
import type {
BackgroundActivitySettings,
SourceControlProviderKind,
SourceControlDiscoveryResult,
SourceControlProviderAuth,
@@ -55,6 +54,12 @@ import {
} from "../Icons";
import { RedactedSensitiveText } from "./RedactedSensitiveText";
import { SettingResetButton, SettingsPageContainer, SettingsSection } from "./settingsLayout";
import {
BackgroundPolicyTooltip,
backgroundActivityOverrideSettings,
durationToSeconds,
normalizeIntervalSeconds as normalizeFetchIntervalSeconds,
} from "./backgroundActivityUtils";
const EMPTY_DISCOVERY_RESULT: SourceControlDiscoveryResult = {
versionControlSystems: [],
@@ -75,43 +80,6 @@ const VCS_ICONS: Partial<Record<VcsDriverKind, Icon>> = {
const SOURCE_CONTROL_SKELETON_ROWS = ["primary", "secondary"] as const;
const GIT_FETCH_INTERVAL_STEP_SECONDS = 5;
type BackgroundActivityOverridePatch = Partial<{
[K in keyof BackgroundActivitySettings["overrides"]]:
| BackgroundActivitySettings["overrides"][K]
| undefined;
}>;
function durationToSeconds(duration: Duration.Duration): number {
return Math.round(Duration.toMillis(duration) / 1_000);
}
function normalizeFetchIntervalSeconds(value: number | null): number {
if (value === null || !Number.isFinite(value)) {
return 0;
}
return Math.max(0, Math.round(value));
}
function BackgroundPolicyTooltip({ children }: { readonly children: string }) {
return (
<Tooltip>
<TooltipTrigger
render={
<button
type="button"
className="inline-flex size-5 items-center justify-center rounded-sm text-muted-foreground hover:text-foreground"
aria-label="Background policy details"
>
<InfoIcon className="size-3.5" />
</button>
}
/>
<TooltipPopup side="top" className="max-w-72">
{children}
</TooltipPopup>
</Tooltip>
);
}
function optionLabel(value: Option.Option<string>): string | null {
return Option.getOrNull(value);
@@ -335,28 +303,6 @@ function GitFetchIntervalSettings() {
);
const canResetFetchInterval =
automaticGitFetchIntervalSeconds !== defaultAutomaticGitFetchIntervalSeconds;
const backgroundActivityOverrideSettings = (
current: BackgroundActivitySettings,
overrides: BackgroundActivityOverridePatch,
) => {
const nextOverrides: BackgroundActivityOverridePatch = {
...current.overrides,
...overrides,
};
for (const [key, value] of Object.entries(nextOverrides)) {
if (value === undefined) {
delete nextOverrides[key as keyof typeof nextOverrides];
}
}
return {
backgroundActivity: {
schemaVersion: 1 as const,
profile: "custom" as const,
baseProfile: getBackgroundActivityBaseProfile(current),
overrides: nextOverrides as BackgroundActivitySettings["overrides"],
},
};
};
return (
<div className="grid gap-3">
@@ -0,0 +1,67 @@
import { InfoIcon } from "lucide-react";
import * as Duration from "effect/Duration";
import type { BackgroundActivitySettings } from "@t3tools/contracts";
import { getBackgroundActivityBaseProfile } from "@t3tools/shared/backgroundActivitySettings";
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
export type BackgroundActivityOverridePatch = Partial<{
[K in keyof BackgroundActivitySettings["overrides"]]:
| BackgroundActivitySettings["overrides"][K]
| undefined;
}>;
export function durationToSeconds(duration: Duration.Duration): number {
return Math.round(Duration.toMillis(duration) / 1_000);
}
export function normalizeIntervalSeconds(value: number | null): number {
if (value === null || !Number.isFinite(value)) {
return 0;
}
return Math.max(0, Math.round(value));
}
export function backgroundActivityOverrideSettings(
current: BackgroundActivitySettings,
overrides: BackgroundActivityOverridePatch,
) {
const nextOverrides: BackgroundActivityOverridePatch = {
...current.overrides,
...overrides,
};
for (const [key, value] of Object.entries(nextOverrides)) {
if (value === undefined) {
delete nextOverrides[key as keyof typeof nextOverrides];
}
}
return {
backgroundActivity: {
schemaVersion: 1 as const,
profile: "custom" as const,
baseProfile: getBackgroundActivityBaseProfile(current),
overrides: nextOverrides as BackgroundActivitySettings["overrides"],
},
};
}
export function BackgroundPolicyTooltip({ children }: { readonly children: string }) {
return (
<Tooltip>
<TooltipTrigger
render={
<button
type="button"
className="inline-flex size-5 items-center justify-center rounded-sm text-muted-foreground hover:text-foreground"
aria-label="Background policy details"
>
<InfoIcon className="size-3.5" />
</button>
}
/>
<TooltipPopup side="top" className="max-w-72">
{children}
</TooltipPopup>
</Tooltip>
);
}
+19 -17
View File
@@ -89,26 +89,28 @@ export function applyServerSettingsPatch(
...patchForMerge
} = patch;
const backgroundActivityPatch =
backgroundActivityProfile !== undefined
? {
schemaVersion: 1 as const,
profile: backgroundActivityProfile,
overrides: {},
}
: automaticGitFetchInterval !== undefined || providerHealthRefreshInterval !== undefined
backgroundActivity !== undefined
? undefined
: backgroundActivityProfile !== undefined
? {
schemaVersion: 1 as const,
profile: "custom" as const,
baseProfile: getBackgroundActivityBaseProfile(current.backgroundActivity),
overrides: {
...current.backgroundActivity.overrides,
...(automaticGitFetchInterval !== undefined ? { automaticGitFetchInterval } : {}),
...(providerHealthRefreshInterval !== undefined
? { providerHealthRefreshInterval }
: {}),
},
profile: backgroundActivityProfile,
overrides: {},
}
: undefined;
: automaticGitFetchInterval !== undefined || providerHealthRefreshInterval !== undefined
? {
schemaVersion: 1 as const,
profile: "custom" as const,
baseProfile: getBackgroundActivityBaseProfile(current.backgroundActivity),
overrides: {
...current.backgroundActivity.overrides,
...(automaticGitFetchInterval !== undefined ? { automaticGitFetchInterval } : {}),
...(providerHealthRefreshInterval !== undefined
? { providerHealthRefreshInterval }
: {}),
},
}
: undefined;
const next = deepMerge(current, patchForMerge);
const nextWithReplacementsBase = {
...next,