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
@@ -19,7 +19,7 @@ import {
|
||||
squashAtomCommandFailure,
|
||||
} from "@t3tools/client-runtime/state/runtime";
|
||||
import { AndroidScreenHeader } from "../../components/AndroidScreenHeader";
|
||||
import { AppText as Text } from "../../components/AppText";
|
||||
import { AppText as Text, AppTextInput as TextInput } from "../../components/AppText";
|
||||
import { supportsAgentAwarenessPush } from "../agent-awareness/capabilities";
|
||||
import { setLiveActivityUpdatesEnabled } from "../agent-awareness/liveActivityPreferences";
|
||||
import { requestAgentNotificationPermission } from "../agent-awareness/notificationPermissions";
|
||||
@@ -36,7 +36,17 @@ import { runtime } from "../../lib/runtime";
|
||||
import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/preferences";
|
||||
import { serverEnvironment } from "../../state/server";
|
||||
import { useAtomCommand } from "../../state/use-atom-command";
|
||||
import type { EnvironmentId } from "@t3tools/contracts";
|
||||
import { useEnvironments } from "../../state/environments";
|
||||
import {
|
||||
DEFAULT_SERVER_SETTINGS,
|
||||
MAX_SIDEBAR_AUTO_SETTLE_AFTER_DAYS,
|
||||
MIN_SIDEBAR_AUTO_SETTLE_AFTER_DAYS,
|
||||
type ServerSettingsPatch,
|
||||
} from "@t3tools/contracts";
|
||||
import {
|
||||
findSharedSettingsMismatches,
|
||||
pickSharedServerSettings,
|
||||
} from "@t3tools/client-runtime/state/shared-settings";
|
||||
import { useThreadListV2Enabled } from "../threads/use-thread-list-v2-enabled";
|
||||
import {
|
||||
type AppUpdateCheckState,
|
||||
@@ -530,51 +540,136 @@ function ConfiguredSettingsRouteScreen() {
|
||||
}
|
||||
|
||||
function GeneralSettingsSection() {
|
||||
const { savedConnectionsById } = useSavedRemoteConnections();
|
||||
const connections = Object.values(savedConnectionsById).sort((left, right) =>
|
||||
left.environmentLabel.localeCompare(right.environmentLabel),
|
||||
);
|
||||
|
||||
return (
|
||||
<SettingsSection title="General">
|
||||
<SettingsRow icon="folder" label="Project Grouping" target="SettingsProjectGrouping" />
|
||||
{connections.map((connection) => (
|
||||
<EnvironmentAutoSettleSwitch
|
||||
key={connection.environmentId}
|
||||
environmentId={connection.environmentId}
|
||||
environmentLabel={connection.environmentLabel}
|
||||
/>
|
||||
))}
|
||||
<AutoSettleSettingsRows />
|
||||
<SettingsRow icon="chart.bar.xaxis" label="Usage" target="SettingsUsage" />
|
||||
</SettingsSection>
|
||||
);
|
||||
}
|
||||
|
||||
function EnvironmentAutoSettleSwitch(props: {
|
||||
readonly environmentId: EnvironmentId;
|
||||
readonly environmentLabel: string;
|
||||
}) {
|
||||
const settings = useAtomValue(serverEnvironment.settingsValueAtom(props.environmentId));
|
||||
const config = useAtomValue(serverEnvironment.configValueAtom(props.environmentId));
|
||||
const AUTO_SETTLE_DEFAULT_DAYS = DEFAULT_SERVER_SETTINGS.sidebarAutoSettleAfterDays ?? 3;
|
||||
|
||||
/**
|
||||
* Auto-settlement is a user preference that every server has to hold. Mobile
|
||||
* has no primary environment, so the first connected environment that
|
||||
* supports it is the reference value. Edits fan out to every connected
|
||||
* environment, and a mismatch row lets the user push the reference out.
|
||||
*/
|
||||
function AutoSettleSettingsRows() {
|
||||
const { environments } = useEnvironments();
|
||||
const updateSettings = useAtomCommand(serverEnvironment.updateSettings, {
|
||||
label: "auto-settle settings update",
|
||||
label: "server settings update",
|
||||
reportFailure: true,
|
||||
});
|
||||
if (config?.environment.capabilities.threadAutoSettlement !== true || settings === null) {
|
||||
|
||||
const connected = environments.filter(
|
||||
(environment) =>
|
||||
environment.connection.phase === "connected" &&
|
||||
environment.serverConfig?.environment.capabilities.threadAutoSettlement === true,
|
||||
);
|
||||
const reference = connected[0] ?? null;
|
||||
const referenceSettings = reference?.serverConfig?.settings ?? null;
|
||||
|
||||
const [daysDraft, setDaysDraft] = useState<string | null>(null);
|
||||
|
||||
if (reference === null || referenceSettings === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const writeToAll = (patch: ServerSettingsPatch) => {
|
||||
for (const environment of connected) {
|
||||
void updateSettings({ environmentId: environment.environmentId, input: { patch } });
|
||||
}
|
||||
};
|
||||
|
||||
const mismatches = findSharedSettingsMismatches({
|
||||
primaryEnvironmentId: reference.environmentId,
|
||||
primarySettings: referenceSettings,
|
||||
environments: environments.map((environment) => ({
|
||||
environmentId: environment.environmentId,
|
||||
label: environment.label,
|
||||
connected: environment.connection.phase === "connected",
|
||||
settings: environment.serverConfig?.settings ?? null,
|
||||
})),
|
||||
});
|
||||
|
||||
const afterDays = referenceSettings.sidebarAutoSettleAfterDays;
|
||||
const commitDays = () => {
|
||||
const draft = (daysDraft ?? "").trim();
|
||||
setDaysDraft(null);
|
||||
// Whole-string check so "3.5" and "3days" are rejected instead of
|
||||
// silently becoming 3 on every connected environment.
|
||||
const parsed = /^\d+$/.test(draft) ? Number(draft) : Number.NaN;
|
||||
if (
|
||||
Number.isInteger(parsed) &&
|
||||
parsed >= MIN_SIDEBAR_AUTO_SETTLE_AFTER_DAYS &&
|
||||
parsed <= MAX_SIDEBAR_AUTO_SETTLE_AFTER_DAYS &&
|
||||
parsed !== afterDays
|
||||
) {
|
||||
writeToAll({ sidebarAutoSettleAfterDays: parsed });
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<SettingsSwitchRow
|
||||
icon="arrow.triangle.branch"
|
||||
label={`Auto-settle merged threads · ${props.environmentLabel}`}
|
||||
value={settings?.sidebarAutoSettleOnMerge ?? true}
|
||||
onValueChange={(value) => {
|
||||
void updateSettings({
|
||||
environmentId: props.environmentId,
|
||||
input: { patch: { sidebarAutoSettleOnMerge: value } },
|
||||
});
|
||||
}}
|
||||
/>
|
||||
<>
|
||||
<SettingsSwitchRow
|
||||
icon="arrow.triangle.branch"
|
||||
label="Auto-settle merged threads"
|
||||
value={referenceSettings.sidebarAutoSettleOnMerge}
|
||||
onValueChange={(value) => writeToAll({ sidebarAutoSettleOnMerge: value })}
|
||||
/>
|
||||
<SettingsSwitchRow
|
||||
icon="clock"
|
||||
label="Auto-settle inactive threads"
|
||||
subtitle={afterDays === null ? undefined : `After ${afterDays} days without activity`}
|
||||
value={afterDays !== null}
|
||||
onValueChange={(value) =>
|
||||
writeToAll({ sidebarAutoSettleAfterDays: value ? AUTO_SETTLE_DEFAULT_DAYS : null })
|
||||
}
|
||||
/>
|
||||
{afterDays !== null ? (
|
||||
<View className="flex-row items-center gap-4 border-t border-border-subtle p-4">
|
||||
<Text className="flex-1 text-lg text-foreground">Days before auto-settle</Text>
|
||||
<TextInput
|
||||
className="min-h-10 w-20 rounded-xl px-3 py-2 text-center text-base"
|
||||
keyboardType="number-pad"
|
||||
returnKeyType="done"
|
||||
value={daysDraft ?? String(afterDays)}
|
||||
onChangeText={setDaysDraft}
|
||||
onBlur={commitDays}
|
||||
onSubmitEditing={commitDays}
|
||||
accessibilityLabel="Days before auto-settle"
|
||||
/>
|
||||
</View>
|
||||
) : null}
|
||||
{mismatches.length > 0 ? (
|
||||
<View className="flex-row items-center gap-4 border-t border-border-subtle p-4">
|
||||
<View className="min-w-0 flex-1">
|
||||
<Text className="text-lg text-foreground">Settings differ</Text>
|
||||
<Text className="text-sm text-foreground-muted">
|
||||
{mismatches.map((mismatch) => mismatch.label).join(", ")}
|
||||
</Text>
|
||||
</View>
|
||||
<Pressable
|
||||
accessibilityRole="button"
|
||||
onPress={() => {
|
||||
const patch = pickSharedServerSettings(referenceSettings);
|
||||
for (const mismatch of mismatches) {
|
||||
void updateSettings({
|
||||
environmentId: mismatch.environmentId,
|
||||
input: { patch },
|
||||
});
|
||||
}
|
||||
}}
|
||||
className="rounded-full bg-subtle px-4 py-2 active:opacity-70"
|
||||
>
|
||||
<Text className="text-base font-t3-medium text-foreground">Apply to all</Text>
|
||||
</Pressable>
|
||||
</View>
|
||||
) : null}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -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