@@ -26,6 +26,7 @@ import {
2626} from "@t3tools/contracts/settings" ;
2727import { safeErrorLogAttributes } from "@t3tools/client-runtime/errors" ;
2828import {
29+ filterSharedServerPatch ,
2930 findSharedSettingsMismatches ,
3031 pickSharedServerSettings ,
3132 splitSharedServerPatch ,
@@ -368,18 +369,6 @@ export function usePrimarySettingsAvailable(): boolean {
368369 return primaryEnvironment !== null || ! isHostedStaticApp ( ) ;
369370}
370371
371- /** Environments that can receive a shared settings write right now. */
372- function useSharedSettingsSyncTargetIds ( ) : ReadonlyArray < EnvironmentId > {
373- const { environments } = useEnvironments ( ) ;
374- return useMemo (
375- ( ) =>
376- environments
377- . filter ( supportsSharedSettingsSync )
378- . map ( ( environment ) => environment . environmentId ) ,
379- [ environments ] ,
380- ) ;
381- }
382-
383372/**
384373 * Returns an updater that routes each key to the correct backing store.
385374 *
@@ -394,7 +383,7 @@ function useUpdateSettingsTarget(environmentId: EnvironmentId | null) {
394383 serverEnvironment . updateSettings ,
395384 "server settings update" ,
396385 ) ;
397- const sharedSettingsSyncTargetIds = useSharedSettingsSyncTargetIds ( ) ;
386+ const { environments } = useEnvironments ( ) ;
398387 const updateSettings = useCallback (
399388 ( patch : UnifiedSettingsPatch ) => {
400389 const { serverPatch, clientPatch } = splitPatch ( patch ) ;
@@ -419,17 +408,25 @@ function useUpdateSettingsTarget(environmentId: EnvironmentId | null) {
419408 }
420409 }
421410 if ( Object . keys ( sharedPatch ) . length > 0 ) {
422- const targets = new Set ( sharedSettingsSyncTargetIds ) ;
411+ const targets = new Set (
412+ environments . filter ( supportsSharedSettingsSync ) . map ( ( target ) => target . environmentId ) ,
413+ ) ;
423414 if ( environmentId ) {
424415 targets . add ( environmentId ) ;
425416 }
426417 if ( targets . size === 0 ) {
427418 warnUnsaved ( ) ;
428419 }
429420 for ( const targetId of targets ) {
421+ const target = environments . find ( ( candidate ) => candidate . environmentId === targetId ) ;
422+ const targetPatch = filterSharedServerPatch (
423+ sharedPatch ,
424+ target ?. serverConfig ?. environment . capabilities ,
425+ ) ;
426+ if ( Object . keys ( targetPatch ) . length === 0 ) continue ;
430427 void persistServerSettings ( {
431428 environmentId : targetId ,
432- input : { patch : sharedPatch } ,
429+ input : { patch : targetPatch } ,
433430 } ) ;
434431 }
435432 }
@@ -438,7 +435,7 @@ function useUpdateSettingsTarget(environmentId: EnvironmentId | null) {
438435 persistClientSettingsPatch ( clientPatch ) ;
439436 }
440437 } ,
441- [ environmentId , persistServerSettings , sharedSettingsSyncTargetIds ] ,
438+ [ environmentId , environments , persistServerSettings ] ,
442439 ) ;
443440
444441 return updateSettings ;
@@ -453,6 +450,7 @@ function useUpdateSettingsTarget(environmentId: EnvironmentId | null) {
453450export function useSharedSettingsSync ( ) {
454451 const primaryEnvironment = usePrimaryEnvironment ( ) ;
455452 const primaryEnvironmentId = primaryEnvironment ?. environmentId ?? null ;
453+ const primaryCapabilities = primaryEnvironment ?. serverConfig ?. environment . capabilities ;
456454 // Read the loaded config, not `primaryServerSettingsAtom`: that atom falls
457455 // back to defaults while the primary is disconnected, and "apply to all"
458456 // must never push defaults over real values. Same for a primary too old to
@@ -472,28 +470,35 @@ export function useSharedSettingsSync() {
472470 findSharedSettingsMismatches ( {
473471 primaryEnvironmentId,
474472 primarySettings,
473+ primaryCapabilities,
475474 environments : environments . map ( ( environment ) => ( {
476475 environmentId : environment . environmentId ,
477476 label : environment . label ,
478477 syncEligible : supportsSharedSettingsSync ( environment ) ,
479478 settings : environment . serverConfig ?. settings ?? null ,
479+ capabilities : environment . serverConfig ?. environment . capabilities ,
480480 } ) ) ,
481481 } ) ,
482- [ environments , primaryEnvironmentId , primarySettings ] ,
482+ [ environments , primaryEnvironmentId , primarySettings , primaryCapabilities ] ,
483483 ) ;
484484
485485 const applyToAll = useCallback ( ( ) => {
486486 if ( primarySettings === null ) {
487487 return ;
488488 }
489- const patch = pickSharedServerSettings ( primarySettings ) ;
489+ const patch = pickSharedServerSettings ( primarySettings , primaryCapabilities ) ;
490490 for ( const mismatch of mismatches ) {
491+ const target = environments . find (
492+ ( candidate ) => candidate . environmentId === mismatch . environmentId ,
493+ ) ;
491494 void persistServerSettings ( {
492495 environmentId : mismatch . environmentId ,
493- input : { patch } ,
496+ input : {
497+ patch : filterSharedServerPatch ( patch , target ?. serverConfig ?. environment . capabilities ) ,
498+ } ,
494499 } ) ;
495500 }
496- } , [ mismatches , persistServerSettings , primarySettings ] ) ;
501+ } , [ environments , mismatches , persistServerSettings , primarySettings , primaryCapabilities ] ) ;
497502
498503 return { mismatches, applyToAll } ;
499504}
0 commit comments