feat: show habit history in details
ci / gitleaks (push) Successful in 53s
ci / docker (push) Successful in 4m59s

This commit is contained in:
2026-09-15 16:32:53 +08:00
parent a6c12ee01f
commit e857506bf6
5 changed files with 159 additions and 9 deletions
+27
View File
@@ -80,6 +80,33 @@ export function isHabitComplete(kind: string | undefined, value: number | boolea
return Boolean(value)
}
export type HabitHistoryLog = { day: string; value: number }
export function habitHistoryWindow(toDay: string, size = 90) {
const [year, month, day] = toDay.split('-').map(Number)
const to = new Date(year, month - 1, day)
const from = new Date(to)
from.setDate(from.getDate() - (size - 1))
return { from: dateKey(from), to: dateKey(to) }
}
export function dayBefore(day: string) {
const [year, month, date] = day.split('-').map(Number)
const value = new Date(year, month - 1, date)
value.setDate(value.getDate() - 1)
return dateKey(value)
}
export function mergeHabitHistory(previous: HabitHistoryLog[], incoming: HabitHistoryLog[]) {
const merged = new Map(previous.map((item) => [item.day, item]))
for (const item of incoming) merged.set(item.day, { day: item.day, value: Number(item.value) })
return [...merged.values()].sort((a, b) => b.day.localeCompare(a.day))
}
export function formatHabitHistoryNumber(value: number) {
return Number.isInteger(value) ? String(value) : String(Number(value.toFixed(6)))
}
type HabitSchedule = {
kind?: string
cells?: Array<{ day: string; scheduled?: boolean; paused?: boolean; value?: number | boolean }>