fix(settings): sync auto-settle and other shared preferences across environments (#9147)
Co-authored-by: Claude Fable 5.1 <[email protected]>
This commit is contained in:
1 parent
5014e5fcdd
commit
8efd4e95fc
10 files changed
+489
-44
No files matched your search
@@ -112,6 +112,7 @@ import {
|
||||
TYPOGRAPHY_ADVANCED_STORAGE_KEY,
|
||||
} from "../../appearanceFonts";
|
||||
import { CodeFontPreview, PromptFontPreview, TerminalFontPreview } from "./SettingsFontPreviews";
|
||||
import { SharedSettingsMismatchAlert } from "./SharedSettingsMismatchAlert";
|
||||
import { discoverInstalledFonts, FontFamilyPicker, useFontEnumeration } from "./FontFamilyPicker";
|
||||
import {
|
||||
NumberField,
|
||||
@@ -1928,6 +1929,7 @@ export function GeneralSettingsPanel() {
|
||||
|
||||
return (
|
||||
<SettingsPageContainer>
|
||||
<SharedSettingsMismatchAlert />
|
||||
<SettingsSection title="General">
|
||||
<SettingsRow
|
||||
{...searchableSetting("project-grouping")}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
import { TriangleAlertIcon } from "lucide-react";
|
||||
|
||||
import { useSharedSettingsSync } from "../../hooks/useSettings";
|
||||
import { Alert, AlertAction, AlertDescription } from "../ui/alert";
|
||||
import { Button } from "../ui/button";
|
||||
|
||||
/**
|
||||
* Warns when a connected environment holds different shared settings than
|
||||
* the primary one, and offers to write the primary's values everywhere.
|
||||
* Renders nothing when every connected environment agrees.
|
||||
*/
|
||||
export function SharedSettingsMismatchAlert() {
|
||||
const { mismatches, applyToAll } = useSharedSettingsSync();
|
||||
if (mismatches.length === 0) {
|
||||
return null;
|
||||
}
|
||||
const labels = mismatches.map((mismatch) => mismatch.label).join(", ");
|
||||
return (
|
||||
<Alert variant="warning" className="mx-3 sm:mx-4">
|
||||
<TriangleAlertIcon />
|
||||
<AlertDescription>
|
||||
Settings differ on {labels}. Thread and source control preferences are meant to match on
|
||||
every environment.
|
||||
</AlertDescription>
|
||||
<AlertAction>
|
||||
<Button variant="outline" size="compact" onClick={applyToAll}>
|
||||
Apply to all
|
||||
</Button>
|
||||
</AlertAction>
|
||||
</Alert>
|
||||
);
|
||||
}
|
||||
@@ -18,6 +18,7 @@ import {
|
||||
} from "@t3tools/shared/backgroundActivitySettings";
|
||||
|
||||
import { usePrimarySettings, useUpdatePrimarySettings } from "../../hooks/useSettings";
|
||||
import { SharedSettingsMismatchAlert } from "./SharedSettingsMismatchAlert";
|
||||
import { cn } from "../../lib/utils";
|
||||
import { useEnvironments, usePrimaryEnvironment } from "../../state/environments";
|
||||
import { useEnvironmentQuery } from "../../state/query";
|
||||
@@ -548,6 +549,7 @@ export function SourceControlSettingsPanel() {
|
||||
|
||||
return (
|
||||
<SettingsPageContainer>
|
||||
<SharedSettingsMismatchAlert />
|
||||
{isInitialScanPending ? (
|
||||
<>
|
||||
<SourceControlSectionSkeleton title="Version Control" headerAction={scanButton} />
|
||||
|
||||
@@ -25,6 +25,11 @@ import {
|
||||
type UnifiedSettings,
|
||||
} from "@t3tools/contracts/settings";
|
||||
import { safeErrorLogAttributes } from "@t3tools/client-runtime/errors";
|
||||
import {
|
||||
findSharedSettingsMismatches,
|
||||
pickSharedServerSettings,
|
||||
splitSharedServerPatch,
|
||||
} from "@t3tools/client-runtime/state/shared-settings";
|
||||
import { ensureLocalApi } from "~/localApi";
|
||||
import {
|
||||
getThemeDefinition,
|
||||
@@ -35,7 +40,11 @@ import {
|
||||
} from "~/themePalette";
|
||||
import * as Struct from "effect/Struct";
|
||||
import { primaryServerSettingsAtom, serverEnvironment } from "~/state/server";
|
||||
import { usePrimaryEnvironment } from "~/state/environments";
|
||||
import {
|
||||
type EnvironmentPresentation,
|
||||
useEnvironments,
|
||||
usePrimaryEnvironment,
|
||||
} from "~/state/environments";
|
||||
import { useAtomCommand } from "~/state/use-atom-command";
|
||||
import { useTheme } from "./useTheme";
|
||||
|
||||
@@ -307,28 +316,69 @@ export function usePrimarySettings<T = UnifiedSettings>(
|
||||
return useMergedSettings(useAtomValue(primaryServerSettingsAtom), selector);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether an environment can hold every shared key right now. Gated on the
|
||||
* auto-settlement capability because it is the newest of the shared keys: a
|
||||
* server that has it has all of them. Older servers drop unknown keys on
|
||||
* write, so a mismatch against them could never clear, and their decoded
|
||||
* defaults must not be treated as real values.
|
||||
*/
|
||||
function supportsSharedSettings(environment: EnvironmentPresentation): boolean {
|
||||
return (
|
||||
environment.connection.phase === "connected" &&
|
||||
environment.serverConfig?.environment.capabilities.threadAutoSettlement === true
|
||||
);
|
||||
}
|
||||
|
||||
/** Environments that can receive a shared settings write right now. */
|
||||
function useConnectedEnvironmentIds(): ReadonlyArray<EnvironmentId> {
|
||||
const { environments } = useEnvironments();
|
||||
return useMemo(
|
||||
() =>
|
||||
environments.filter(supportsSharedSettings).map((environment) => environment.environmentId),
|
||||
[environments],
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an updater that routes each key to the correct backing store.
|
||||
*
|
||||
* Server keys are optimistically patched in atom-backed server state, then
|
||||
* persisted via RPC. Client keys go through client persistence.
|
||||
* persisted via RPC. Shared server keys (see `SHARED_SERVER_SETTING_KEYS`)
|
||||
* are written to every connected environment, not only the target, so a user
|
||||
* preference does not silently drift between machines. Client keys go through
|
||||
* client persistence.
|
||||
*/
|
||||
function useUpdateSettingsTarget(environmentId: EnvironmentId | null) {
|
||||
const persistServerSettings = useAtomCommand(
|
||||
serverEnvironment.updateSettings,
|
||||
"server settings update",
|
||||
);
|
||||
const connectedEnvironmentIds = useConnectedEnvironmentIds();
|
||||
const updateSettings = useCallback(
|
||||
(patch: UnifiedSettingsPatch) => {
|
||||
const { serverPatch, clientPatch } = splitPatch(patch);
|
||||
|
||||
if (Object.keys(serverPatch).length > 0) {
|
||||
if (environmentId) {
|
||||
const { sharedPatch, localPatch } = splitSharedServerPatch(serverPatch);
|
||||
if (environmentId && Object.keys(localPatch).length > 0) {
|
||||
void persistServerSettings({
|
||||
environmentId,
|
||||
input: { patch: serverPatch },
|
||||
input: { patch: localPatch },
|
||||
});
|
||||
}
|
||||
if (Object.keys(sharedPatch).length > 0) {
|
||||
const targets = new Set(connectedEnvironmentIds);
|
||||
if (environmentId) {
|
||||
targets.add(environmentId);
|
||||
}
|
||||
for (const targetId of targets) {
|
||||
void persistServerSettings({
|
||||
environmentId: targetId,
|
||||
input: { patch: sharedPatch },
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
if (Object.keys(clientPatch).length > 0) {
|
||||
persistClientSettings({
|
||||
@@ -337,12 +387,66 @@ function useUpdateSettingsTarget(environmentId: EnvironmentId | null) {
|
||||
});
|
||||
}
|
||||
},
|
||||
[environmentId, persistServerSettings],
|
||||
[connectedEnvironmentIds, environmentId, persistServerSettings],
|
||||
);
|
||||
|
||||
return updateSettings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Connected environments whose shared settings differ from the primary's,
|
||||
* plus an action that writes the primary's values to all of them. Drift
|
||||
* happens when an environment was offline during an edit or was changed by
|
||||
* an older client.
|
||||
*/
|
||||
export function useSharedSettingsSync() {
|
||||
const primaryEnvironment = usePrimaryEnvironment();
|
||||
const primaryEnvironmentId = primaryEnvironment?.environmentId ?? null;
|
||||
// Read the loaded config, not `primaryServerSettingsAtom`: that atom falls
|
||||
// back to defaults while the primary is disconnected, and "apply to all"
|
||||
// must never push defaults over real values. Same for a primary too old to
|
||||
// hold the shared keys: its decoded defaults are not a source of truth.
|
||||
const primarySettings =
|
||||
primaryEnvironment !== null && supportsSharedSettings(primaryEnvironment)
|
||||
? (primaryEnvironment.serverConfig?.settings ?? null)
|
||||
: null;
|
||||
const { environments } = useEnvironments();
|
||||
const persistServerSettings = useAtomCommand(
|
||||
serverEnvironment.updateSettings,
|
||||
"server settings update",
|
||||
);
|
||||
|
||||
const mismatches = useMemo(
|
||||
() =>
|
||||
findSharedSettingsMismatches({
|
||||
primaryEnvironmentId,
|
||||
primarySettings,
|
||||
environments: environments.map((environment) => ({
|
||||
environmentId: environment.environmentId,
|
||||
label: environment.label,
|
||||
connected: supportsSharedSettings(environment),
|
||||
settings: environment.serverConfig?.settings ?? null,
|
||||
})),
|
||||
}),
|
||||
[environments, primaryEnvironmentId, primarySettings],
|
||||
);
|
||||
|
||||
const applyToAll = useCallback(() => {
|
||||
if (primarySettings === null) {
|
||||
return;
|
||||
}
|
||||
const patch = pickSharedServerSettings(primarySettings);
|
||||
for (const mismatch of mismatches) {
|
||||
void persistServerSettings({
|
||||
environmentId: mismatch.environmentId,
|
||||
input: { patch },
|
||||
});
|
||||
}
|
||||
}, [mismatches, persistServerSettings, primarySettings]);
|
||||
|
||||
return { mismatches, applyToAll };
|
||||
}
|
||||
|
||||
export function useUpdateEnvironmentSettings(environmentId: EnvironmentId) {
|
||||
return useUpdateSettingsTarget(environmentId);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user