fix: keep today habits visible while refreshing
ci / docker (push) Successful in 5m48s

This commit is contained in:
2026-09-07 17:27:24 +08:00
parent 3772b56617
commit 5d564b0269
4 changed files with 41 additions and 8 deletions
+8 -1
View File
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { calendarModeLabel, clampFabPosition, countdownDayText, countdownKindLabel, dateKey, defaultView, habitButtonValue, habitWeek, isFabDrag, isHabitComplete, isHabitScheduledToday, isTaskView, nextHabitSwipeValue, numericHabitInputValue, previousHabitSwipeValue, quickTaskFields, shouldToggleRowSwipe } from './mvp-utils'
import { calendarModeLabel, clampFabPosition, countdownDayText, countdownKindLabel, dateKey, defaultView, habitButtonValue, habitWeek, isFabDrag, isHabitComplete, isHabitScheduledToday, isTaskView, nextHabitSwipeValue, nextTotalAfterLocalTaskAdd, numericHabitInputValue, previousHabitSwipeValue, quickTaskFields, readHabitGridCache, shouldToggleRowSwipe, writeHabitGridCache } from './mvp-utils'
describe('MVP view utilities', () => {
it('formats a local date as YYYY-MM-DD', () => {
@@ -70,6 +70,13 @@ describe('MVP view utilities', () => {
expect(habitButtonValue('boolean', 1, 1)).toBe(0)
})
it('reuses the current week habit grid while refreshing in the background', () => {
const habits = [{ id: 'habit-1', name: '喝水' }]
writeHabitGridCache('2026-09-07', habits)
expect(readHabitGridCache('2026-09-07')).toEqual(habits)
expect(readHabitGridCache('2026-09-14')).toBeNull()
})
it('renders countdown labels from date-only day values', () => {
expect(countdownDayText(8)).toBe('还有 8 天')
expect(countdownDayText(0)).toBe('就是今天')
+10
View File
@@ -114,6 +114,16 @@ export function isFabDrag(deltaX: number, deltaY: number, threshold = 8) {
return Math.hypot(deltaX, deltaY) >= threshold
}
let habitGridCache: { week: string; habits: unknown[] } | null = null
export function readHabitGridCache<T>(week: string): T[] | null {
return habitGridCache?.week === week ? habitGridCache.habits as T[] : null
}
export function writeHabitGridCache<T>(week: string, habits: T[]) {
habitGridCache = { week, habits }
}
export function formatApiErrorDetail(detail: unknown): string {
if (typeof detail === 'string') return detail
if (Array.isArray(detail)) {