fix: persist completed item visibility
ci / gitleaks (push) Successful in 18s
ci / docker (push) Successful in 3m49s

This commit is contained in:
2026-09-08 11:24:39 +08:00
parent 356fbfa942
commit 798725ad29
5 changed files with 47 additions and 8 deletions
+14 -1
View File
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { calendarModeLabel, clampFabPosition, countdownDayText, countdownKindLabel, dateKey, defaultView, habitButtonNotice, habitButtonValue, habitWeek, isFabDrag, isHabitComplete, isHabitScheduledToday, isTaskView, nextHabitSwipeValue, nextTotalAfterLocalTaskAdd, numericHabitInputValue, previousHabitSwipeValue, quickTaskFields, readCountdownCache, readHabitGridCache, shouldToggleRowSwipe, writeCountdownCache, writeHabitGridCache } from './mvp-utils'
import { calendarModeLabel, clampFabPosition, countdownDayText, countdownKindLabel, dateKey, defaultView, habitButtonNotice, habitButtonValue, habitWeek, isFabDrag, isHabitComplete, isHabitScheduledToday, isTaskView, nextHabitSwipeValue, nextTotalAfterLocalTaskAdd, numericHabitInputValue, previousHabitSwipeValue, quickTaskFields, readCountdownCache, readHabitGridCache, readStoredBoolean, shouldToggleRowSwipe, writeCountdownCache, writeHabitGridCache, writeStoredBoolean } from './mvp-utils'
describe('MVP view utilities', () => {
it('formats a local date as YYYY-MM-DD', () => {
@@ -77,6 +77,19 @@ describe('MVP view utilities', () => {
expect(habitButtonNotice('boolean', 1, 0)).toBe('已取消完成')
})
it('persists boolean display preferences across page refreshes', () => {
const storage = new Map<string, string>()
const fakeStorage = {
getItem: (key: string) => storage.get(key) ?? null,
setItem: (key: string, value: string) => storage.set(key, value),
}
expect(readStoredBoolean(fakeStorage, 'dodo.show-completed', true)).toBe(true)
writeStoredBoolean(fakeStorage, 'dodo.show-completed', false)
expect(readStoredBoolean(fakeStorage, 'dodo.show-completed', true)).toBe(false)
storage.set('dodo.show-completed', 'invalid')
expect(readStoredBoolean(fakeStorage, 'dodo.show-completed', true)).toBe(true)
})
it('reuses the current week habit grid while refreshing in the background', () => {
const habits = [{ id: 'habit-1', name: '喝水' }]
writeHabitGridCache('2026-09-07', habits)
+15
View File
@@ -1,3 +1,18 @@
type BooleanStorage = Pick<Storage, 'getItem' | 'setItem'>
export function readStoredBoolean(storage: BooleanStorage, key: string, fallback: boolean) {
try {
const value = storage.getItem(key)
if (value === 'true') return true
if (value === 'false') return false
} catch { /* storage may be unavailable */ }
return fallback
}
export function writeStoredBoolean(storage: BooleanStorage, key: string, value: boolean) {
try { storage.setItem(key, String(value)) } catch { /* storage may be unavailable */ }
}
export function dateKey(date: Date) {
const y = date.getFullYear()
const m = `${date.getMonth() + 1}`.padStart(2, '0')