fix(settings): sync auto-settle and other shared preferences across environments #9147
Mergedt3dotgg(GitHub profile, opens in a new tab)merged 3 commits into
main fromrefs/pull/9147/headmerged by t3dotgg(GitHub profile, opens in a new tab) View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
- apps/mobile/src/features/settings/SettingsRouteScreen.tsx lines 600 to 600OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -565,0 +580,21 @@580+581+ const writeToAll = (patch: ServerSettingsPatch) => {582+ for (const environment of connected) {583+ void updateSettings({ environmentId: environment.environmentId, input: { patch } });584+ }585+ };586+587+ const mismatches = findSharedSettingsMismatches({588+ primaryEnvironmentId: reference.environmentId,589+ primarySettings: referenceSettings,590+ environments: environments.map((environment) => ({591+ environmentId: environment.environmentId,592+ label: environment.label,593+ connected: environment.connection.phase === "connected",594+ settings: environment.serverConfig?.settings ?? null,595+ })),596+ });597+598+ const afterDays = referenceSettings.sidebarAutoSettleAfterDays;599+ const commitDays = () => {600+ const parsed = Number.parseInt(daysDraft ?? "", 10);packages/client-runtime/src/state/sharedSettings.ts lines 76 to 76Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +50,21 @@50+ return Struct.pick(settings, SHARED_SERVER_SETTING_KEYS);51+}52+53+export interface SharedSettingsEnvironment {54+ readonly environmentId: EnvironmentId;55+ readonly label: string;56+ readonly connected: boolean;57+ readonly settings: ServerSettings | null;58+}59+60+/**61+ * Connected environments whose shared settings differ from the primary62+ * environment's. Offline environments are skipped: nothing can be read from63+ * or written to them, and the warning would never clear.64+ */65+export function findSharedSettingsMismatches(input: {66+ readonly primaryEnvironmentId: EnvironmentId | null;67+ readonly primarySettings: ServerSettings;68+ readonly environments: ReadonlyArray<SharedSettingsEnvironment>;69+}): ReadonlyArray<{ readonly environmentId: EnvironmentId; readonly label: string }> {70+ const expected = pickSharedServerSettings(input.primarySettings);
- apps/web/src/hooks/useSettings.ts lines 403 to 404Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -337,9 +371,18 @@337371 });338372 }339373 },340- [environmentId, persistServerSettings],374+ [connectedEnvironmentIds, environmentId, persistServerSettings],341375 );342376343377 return updateSettings;344378 }345379380+/**381+ * Connected environments whose shared settings differ from the primary's,382+ * plus an action that writes the primary's values to all of them. Drift383+ * happens when an environment was offline during an edit or was changed by384+ * an older client.385+ */386+export function useSharedSettingsSync() {387+ const primaryEnvironment = usePrimaryEnvironment();388+ const primaryEnvironmentId = primaryEnvironment?.environmentId ?? null;
- apps/web/src/hooks/useSettings.ts line 407OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -345,0 +387,21 @@387+ const primaryEnvironment = usePrimaryEnvironment();388+ const primaryEnvironmentId = primaryEnvironment?.environmentId ?? null;389+ // Read the loaded config, not `primaryServerSettingsAtom`: that atom falls390+ // back to defaults while the primary is disconnected, and "apply to all"391+ // must never push defaults over real values.392+ const primarySettings = primaryEnvironment?.serverConfig?.settings ?? null;393+ const { environments } = useEnvironments();394+ const persistServerSettings = useAtomCommand(395+ serverEnvironment.updateSettings,396+ "server settings update",397+ );398+399+ const mismatches = useMemo(400+ () =>401+ findSharedSettingsMismatches({402+ primaryEnvironmentId,403+ primarySettings,404+ environments: environments.map((environment) => ({405+ environmentId: environment.environmentId,406+ label: environment.label,407+ connected: environment.connection.phase === "connected",
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed