fix(settings): re-show a start-page card with an explicit false (#1212)

This commit is contained in:
Menghuan1918 committed 2026-11-18 00:07:48 +08:00
1 parent c337adbbf7
commit 58cf84b733
3 files changed
+76 -9

No files matched your search

+12 -4
View File
@@ -738,14 +738,20 @@ export function SideCardSection({ store, service, guideCatalog }: SideCardSectio
setPrefs(outcome.ok ? outcome.prefs : previous)
}
/** Optimistically apply one pref patch, then commit (revert on failure). */
const applyPref = (patch: Record<string, unknown>): void => {
/**
* Optimistically apply one pref patch, then commit (revert on failure).
* @param patch - the full local value (the UI reads this shape).
* @param wirePatch - the patch to send (defaults to `patch`); the host
* deep-merges nested plain objects, so a sparse local map must send only
* the touched key.
*/
const applyPref = (patch: Record<string, unknown>, wirePatch: Record<string, unknown> = patch): void => {
const previous = optimisticRef.current
const next = { ...previous, ...patch } as SidebarPrefs
optimisticRef.current = next
setPrefs(next)
setError(null)
void commit(patch).then(outcome => applyOutcome(previous, outcome))
void commit(wirePatch).then(outcome => applyOutcome(previous, outcome))
}
/** Flip one per-tab enable switch (merge into the tabsEnabled map). */
@@ -763,7 +769,9 @@ export function SideCardSection({ store, service, guideCatalog }: SideCardSectio
const next = { ...optimisticRef.current.guideHidden }
if (shown) delete next[key]
else next[key] = true
applyPref({ guideHidden: next })
// A deletion cannot ride the host's deep merge, so a re-shown card sends
// the explicit `false` while the UI keeps the sparse map.
applyPref({ guideHidden: next }, { guideHidden: { [key]: !shown } })
}
/**
+5 -5
View File
@@ -27,11 +27,11 @@ export function saveGuideOrder(store: SidebarStore, order: readonly string[]): v
/** Persist one card's start-page visibility. */
export function setGuideEntryHidden(store: SidebarStore, key: string, hidden: boolean): void {
const current = store.getPrefs().guideHidden
const next = { ...current }
if (hidden) next[key] = true
else delete next[key]
writeGuidePrefs(store, { guideHidden: next })
// The host deep-merges nested plain objects, so deleting the key from the
// patch cannot express "shown": the stored true would survive the merge.
// Send the explicit boolean for the touched key instead. Every reader asks
// `guideHidden[key] === true`, so the stored false already reads as shown.
writeGuidePrefs(store, { guideHidden: { [key]: hidden } })
}
/** Persist one Better Sidebar type's feature switch (the sidebar-content state). */
+59
View File
@@ -17,6 +17,23 @@ import type { NativeGuideEntry } from '../src/client/native/guide-catalog.ts'
import { SIDEBAR_PREFS_DEFAULTS } from '../src/prefs-shared.ts'
import { renderRoot, setupReactAct } from './test-utils.ts'
/** The settings wire the start-page writes end up on (asserted below). */
const settingsUpdate = vi.fn<
(patch: Record<string, unknown>) => Promise<{ value: unknown; revision: number }>
>(async () => ({ value: {}, revision: 2 }))
// Partial mock: only the write this spec drives is stubbed, everything else
// keeps the real client.
vi.mock('../src/client/api.ts', async (importOriginal) => {
const actual = await importOriginal<typeof import('../src/client/api.ts')>()
return {
...actual,
api: {
...actual.api,
settingsUpdate: (patch: Record<string, unknown>) => settingsUpdate(patch),
},
}
})
setupReactAct()
beforeAll(() => {
@@ -291,6 +308,48 @@ describe('NativeGuide', () => {
expect(close).toHaveBeenCalled()
mounted.unmount()
})
it('re-showing a hidden card sends the explicit false on the wire (#1212)', async () => {
settingsUpdate.mockClear()
// The host resolves the merged document: the touched key is now false.
settingsUpdate.mockResolvedValue({ value: { guideHidden: { a: false } }, revision: 2 })
const store = createSidebarStore()
store.setSession('s1')
store.setPrefs({ ...store.getPrefs(), guideHidden: { a: true } })
const service = createBetterSidebarService(store)
// One stable snapshot: useSyncExternalStore loops on a fresh array.
const catalogEntries = [entry('a')]
const catalog = { getSnapshot: () => catalogEntries, subscribe: () => () => {} }
const mounted = renderRoot(createElement(NativeGuide, { catalog, store, service }))
await act(async () => {
;(mounted.container.querySelector('[data-guide-more]') as HTMLButtonElement).click()
await Promise.resolve()
})
const reorder = [...document.querySelectorAll('[role="menuitem"]')]
.find(node => node.textContent?.includes('Reorder'))
expect(reorder).toBeDefined()
await act(async () => {
;(reorder as HTMLElement).click()
await Promise.resolve()
})
const toggle = document.querySelector('input[aria-label="Show on the start page: A"]') as HTMLInputElement
expect(toggle).not.toBeNull()
expect(toggle.checked).toBe(false)
await act(async () => {
toggle.click()
// The host deep-merges nested plain objects, so a patch with the key
// absent would leave the stored true in place: the wire must carry false.
await vi.waitFor(() => {
expect(settingsUpdate).toHaveBeenCalledWith({ guideHidden: { a: false } })
})
// Every reader asks `guideHidden[key] === true`, so a stored false
// already reads as shown; no map normalization is needed.
await vi.waitFor(() => {
expect(store.getPrefs().guideHidden.a).not.toBe(true)
})
})
mounted.unmount()
})
})
describe('registerNativeSurface start-page takeover', () => {