fix(mobile): skip unsupported shared settings targets (#9381)
This commit is contained in:
1 parent
57626eb6ea
commit
cfddb4201d
6 files changed
+119
-69
No files matched your search
@@ -46,6 +46,7 @@ import {
|
||||
import {
|
||||
findSharedSettingsMismatches,
|
||||
pickSharedServerSettings,
|
||||
supportsSharedSettingsSync,
|
||||
} from "@t3tools/client-runtime/state/shared-settings";
|
||||
import { useThreadListV2Enabled } from "../threads/use-thread-list-v2-enabled";
|
||||
import {
|
||||
@@ -553,9 +554,9 @@ const AUTO_SETTLE_DEFAULT_DAYS = DEFAULT_SERVER_SETTINGS.sidebarAutoSettleAfterD
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* has no primary environment, so the first eligible sync target provides the
|
||||
* reference value. Edits fan out to every eligible target, and a mismatch row
|
||||
* lets the user push the reference out.
|
||||
*/
|
||||
function AutoSettleSettingsRows() {
|
||||
const { environments } = useEnvironments();
|
||||
@@ -564,12 +565,8 @@ function AutoSettleSettingsRows() {
|
||||
reportFailure: true,
|
||||
});
|
||||
|
||||
const connected = environments.filter(
|
||||
(environment) =>
|
||||
environment.connection.phase === "connected" &&
|
||||
environment.serverConfig?.environment.capabilities.threadAutoSettlement === true,
|
||||
);
|
||||
const reference = connected[0] ?? null;
|
||||
const syncTargets = environments.filter(supportsSharedSettingsSync);
|
||||
const reference = syncTargets[0] ?? null;
|
||||
const referenceSettings = reference?.serverConfig?.settings ?? null;
|
||||
|
||||
const [daysDraft, setDaysDraft] = useState<string | null>(null);
|
||||
@@ -579,7 +576,7 @@ function AutoSettleSettingsRows() {
|
||||
}
|
||||
|
||||
const writeToAll = (patch: ServerSettingsPatch) => {
|
||||
for (const environment of connected) {
|
||||
for (const environment of syncTargets) {
|
||||
void updateSettings({ environmentId: environment.environmentId, input: { patch } });
|
||||
}
|
||||
};
|
||||
@@ -590,7 +587,7 @@ function AutoSettleSettingsRows() {
|
||||
environments: environments.map((environment) => ({
|
||||
environmentId: environment.environmentId,
|
||||
label: environment.label,
|
||||
connected: environment.connection.phase === "connected",
|
||||
syncEligible: supportsSharedSettingsSync(environment),
|
||||
settings: environment.serverConfig?.settings ?? null,
|
||||
})),
|
||||
});
|
||||
@@ -600,7 +597,7 @@ function AutoSettleSettingsRows() {
|
||||
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.
|
||||
// silently becoming 3 on every eligible sync target.
|
||||
const parsed = /^\d+$/.test(draft) ? Number(draft) : Number.NaN;
|
||||
if (
|
||||
Number.isInteger(parsed) &&
|
||||
|
||||
@@ -29,6 +29,7 @@ import {
|
||||
findSharedSettingsMismatches,
|
||||
pickSharedServerSettings,
|
||||
splitSharedServerPatch,
|
||||
supportsSharedSettingsSync,
|
||||
} from "@t3tools/client-runtime/state/shared-settings";
|
||||
import { ensureLocalApi } from "~/localApi";
|
||||
import {
|
||||
@@ -42,11 +43,7 @@ import * as Struct from "effect/Struct";
|
||||
import { toastManager } from "~/components/ui/toast";
|
||||
import { isHostedStaticApp } from "~/hostedPairing";
|
||||
import { primaryServerSettingsAtom, serverEnvironment } from "~/state/server";
|
||||
import {
|
||||
type EnvironmentPresentation,
|
||||
useEnvironments,
|
||||
usePrimaryEnvironment,
|
||||
} from "~/state/environments";
|
||||
import { useEnvironments, usePrimaryEnvironment } from "~/state/environments";
|
||||
import { useAtomCommand } from "~/state/use-atom-command";
|
||||
import { useTheme } from "./useTheme";
|
||||
|
||||
@@ -332,26 +329,14 @@ export function usePrimarySettingsAvailable(): boolean {
|
||||
return primaryEnvironment !== null || !isHostedStaticApp();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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> {
|
||||
function useSharedSettingsSyncTargetIds(): ReadonlyArray<EnvironmentId> {
|
||||
const { environments } = useEnvironments();
|
||||
return useMemo(
|
||||
() =>
|
||||
environments.filter(supportsSharedSettings).map((environment) => environment.environmentId),
|
||||
environments
|
||||
.filter(supportsSharedSettingsSync)
|
||||
.map((environment) => environment.environmentId),
|
||||
[environments],
|
||||
);
|
||||
}
|
||||
@@ -361,16 +346,16 @@ function useConnectedEnvironmentIds(): ReadonlyArray<EnvironmentId> {
|
||||
*
|
||||
* Server keys are optimistically patched in atom-backed server state, then
|
||||
* 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.
|
||||
* are written to every eligible sync target, not only the selected 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 sharedSettingsSyncTargetIds = useSharedSettingsSyncTargetIds();
|
||||
const updateSettings = useCallback(
|
||||
(patch: UnifiedSettingsPatch) => {
|
||||
const { serverPatch, clientPatch } = splitPatch(patch);
|
||||
@@ -395,7 +380,7 @@ function useUpdateSettingsTarget(environmentId: EnvironmentId | null) {
|
||||
}
|
||||
}
|
||||
if (Object.keys(sharedPatch).length > 0) {
|
||||
const targets = new Set(connectedEnvironmentIds);
|
||||
const targets = new Set(sharedSettingsSyncTargetIds);
|
||||
if (environmentId) {
|
||||
targets.add(environmentId);
|
||||
}
|
||||
@@ -417,14 +402,14 @@ function useUpdateSettingsTarget(environmentId: EnvironmentId | null) {
|
||||
});
|
||||
}
|
||||
},
|
||||
[connectedEnvironmentIds, environmentId, persistServerSettings],
|
||||
[environmentId, persistServerSettings, sharedSettingsSyncTargetIds],
|
||||
);
|
||||
|
||||
return updateSettings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Connected environments whose shared settings differ from the primary's,
|
||||
* Shared-settings sync targets whose values 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.
|
||||
@@ -437,7 +422,7 @@ export function useSharedSettingsSync() {
|
||||
// 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 !== null && supportsSharedSettingsSync(primaryEnvironment)
|
||||
? (primaryEnvironment.serverConfig?.settings ?? null)
|
||||
: null;
|
||||
const { environments } = useEnvironments();
|
||||
@@ -454,7 +439,7 @@ export function useSharedSettingsSync() {
|
||||
environments: environments.map((environment) => ({
|
||||
environmentId: environment.environmentId,
|
||||
label: environment.label,
|
||||
connected: supportsSharedSettings(environment),
|
||||
syncEligible: supportsSharedSettingsSync(environment),
|
||||
settings: environment.serverConfig?.settings ?? null,
|
||||
})),
|
||||
}),
|
||||
|
||||
Reference in new issue
Block a user