@@ -29,6 +29,7 @@ import {
2929 findSharedSettingsMismatches ,
3030 pickSharedServerSettings ,
3131 splitSharedServerPatch ,
32+ supportsSharedSettingsSync ,
3233} from "@t3tools/client-runtime/state/shared-settings" ;
3334import { ensureLocalApi } from "~/localApi" ;
3435import {
@@ -42,11 +43,7 @@ import * as Struct from "effect/Struct";
4243import { toastManager } from "~/components/ui/toast" ;
4344import { isHostedStaticApp } from "~/hostedPairing" ;
4445import { primaryServerSettingsAtom , serverEnvironment } from "~/state/server" ;
45- import {
46- type EnvironmentPresentation ,
47- useEnvironments ,
48- usePrimaryEnvironment ,
49- } from "~/state/environments" ;
46+ import { useEnvironments , usePrimaryEnvironment } from "~/state/environments" ;
5047import { useAtomCommand } from "~/state/use-atom-command" ;
5148import { useTheme } from "./useTheme" ;
5249
@@ -332,26 +329,14 @@ export function usePrimarySettingsAvailable(): boolean {
332329 return primaryEnvironment !== null || ! isHostedStaticApp ( ) ;
333330}
334331
335- /**
336- * Whether an environment can hold every shared key right now. Gated on the
337- * auto-settlement capability because it is the newest of the shared keys: a
338- * server that has it has all of them. Older servers drop unknown keys on
339- * write, so a mismatch against them could never clear, and their decoded
340- * defaults must not be treated as real values.
341- */
342- function supportsSharedSettings ( environment : EnvironmentPresentation ) : boolean {
343- return (
344- environment . connection . phase === "connected" &&
345- environment . serverConfig ?. environment . capabilities . threadAutoSettlement === true
346- ) ;
347- }
348-
349332/** Environments that can receive a shared settings write right now. */
350- function useConnectedEnvironmentIds ( ) : ReadonlyArray < EnvironmentId > {
333+ function useSharedSettingsSyncTargetIds ( ) : ReadonlyArray < EnvironmentId > {
351334 const { environments } = useEnvironments ( ) ;
352335 return useMemo (
353336 ( ) =>
354- environments . filter ( supportsSharedSettings ) . map ( ( environment ) => environment . environmentId ) ,
337+ environments
338+ . filter ( supportsSharedSettingsSync )
339+ . map ( ( environment ) => environment . environmentId ) ,
355340 [ environments ] ,
356341 ) ;
357342}
@@ -361,16 +346,16 @@ function useConnectedEnvironmentIds(): ReadonlyArray<EnvironmentId> {
361346 *
362347 * Server keys are optimistically patched in atom-backed server state, then
363348 * persisted via RPC. Shared server keys (see `SHARED_SERVER_SETTING_KEYS`)
364- * are written to every connected environment , not only the target, so a user
365- * preference does not silently drift between machines. Client keys go through
366- * client persistence.
349+ * are written to every eligible sync target , not only the selected target, so
350+ * a user preference does not silently drift between machines. Client keys go
351+ * through client persistence.
367352 */
368353function useUpdateSettingsTarget ( environmentId : EnvironmentId | null ) {
369354 const persistServerSettings = useAtomCommand (
370355 serverEnvironment . updateSettings ,
371356 "server settings update" ,
372357 ) ;
373- const connectedEnvironmentIds = useConnectedEnvironmentIds ( ) ;
358+ const sharedSettingsSyncTargetIds = useSharedSettingsSyncTargetIds ( ) ;
374359 const updateSettings = useCallback (
375360 ( patch : UnifiedSettingsPatch ) => {
376361 const { serverPatch, clientPatch } = splitPatch ( patch ) ;
@@ -395,7 +380,7 @@ function useUpdateSettingsTarget(environmentId: EnvironmentId | null) {
395380 }
396381 }
397382 if ( Object . keys ( sharedPatch ) . length > 0 ) {
398- const targets = new Set ( connectedEnvironmentIds ) ;
383+ const targets = new Set ( sharedSettingsSyncTargetIds ) ;
399384 if ( environmentId ) {
400385 targets . add ( environmentId ) ;
401386 }
@@ -417,14 +402,14 @@ function useUpdateSettingsTarget(environmentId: EnvironmentId | null) {
417402 } ) ;
418403 }
419404 } ,
420- [ connectedEnvironmentIds , environmentId , persistServerSettings ] ,
405+ [ environmentId , persistServerSettings , sharedSettingsSyncTargetIds ] ,
421406 ) ;
422407
423408 return updateSettings ;
424409}
425410
426411/**
427- * Connected environments whose shared settings differ from the primary's,
412+ * Shared-settings sync targets whose values differ from the primary's,
428413 * plus an action that writes the primary's values to all of them. Drift
429414 * happens when an environment was offline during an edit or was changed by
430415 * an older client.
@@ -437,7 +422,7 @@ export function useSharedSettingsSync() {
437422 // must never push defaults over real values. Same for a primary too old to
438423 // hold the shared keys: its decoded defaults are not a source of truth.
439424 const primarySettings =
440- primaryEnvironment !== null && supportsSharedSettings ( primaryEnvironment )
425+ primaryEnvironment !== null && supportsSharedSettingsSync ( primaryEnvironment )
441426 ? ( primaryEnvironment . serverConfig ?. settings ?? null )
442427 : null ;
443428 const { environments } = useEnvironments ( ) ;
@@ -454,7 +439,7 @@ export function useSharedSettingsSync() {
454439 environments : environments . map ( ( environment ) => ( {
455440 environmentId : environment . environmentId ,
456441 label : environment . label ,
457- connected : supportsSharedSettings ( environment ) ,
442+ syncEligible : supportsSharedSettingsSync ( environment ) ,
458443 settings : environment . serverConfig ?. settings ?? null ,
459444 } ) ) ,
460445 } ) ,
0 commit comments