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
@@ -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