feat: redesign habit detail paper flow
ci / gitleaks (push) Successful in 9s
ci / docker (push) Successful in 3m46s

This commit is contained in:
2026-09-18 21:10:50 +08:00
parent 432a133a42
commit 0baedbc99e
6 changed files with 322 additions and 10 deletions
+57
View File
@@ -107,6 +107,63 @@ export function formatHabitHistoryNumber(value: number) {
return Number.isInteger(value) ? String(value) : String(Number(value.toFixed(6)))
}
type HabitDetailFields = {
kind?: string
target?: number
unit?: string
archived_at?: string | null
schedule_type?: 'daily' | 'weekly' | 'monthly' | 'interval'
weekdays?: number[] | null
month_days?: number[] | null
interval_days?: number | null
}
export function formatHabitRecordMode(habit: HabitDetailFields) {
if (habit.kind !== 'numeric') return '完成 / 未完成'
const unit = habit.unit ? ` ${habit.unit}` : ''
return `按数量记录 · 目标 ${formatHabitHistoryNumber(habit.target ?? 1)}${unit}`
}
export function formatHabitSchedule(habit: HabitDetailFields) {
if (habit.schedule_type === 'weekly') {
const weekdays = habit.weekdays ?? []
if (!weekdays.length) return '每周(日期未设置)'
const labels = ['一', '二', '三', '四', '五', '六', '日']
return `${weekdays.map((day) => `${labels[day] ?? day}`).join('、')}`
}
if (habit.schedule_type === 'monthly') {
const monthDays = habit.month_days ?? []
return monthDays.length ? `每月 ${monthDays.join('、')}` : '每月(日期未设置)'
}
if (habit.schedule_type === 'interval') {
return habit.interval_days ? `${habit.interval_days}` : '按间隔(天数未设置)'
}
return '每天'
}
export function formatHabitDetailDate(day?: string) {
if (!day) return '未提供'
const [year, month, date] = day.split('-').map(Number)
if (!year || !month || !date) return day
return `${year}${month}${date}`
}
export function formatHabitDetailProgress(habit: Pick<HabitDetailFields, 'kind' | 'target' | 'unit' | 'archived_at'> & { value?: number | boolean }) {
if (habit.archived_at) return null
const target = habit.target ?? 1
const value = Number(habit.value ?? 0)
if (habit.kind !== 'numeric') {
const complete = Boolean(habit.value)
return { primary: complete ? '已完成' : '未完成', note: complete ? '今日目标已达成' : '今天尚未完成', semantic: complete ? '已完成' : '未完成' }
}
const complete = value >= target
return {
primary: `${formatHabitHistoryNumber(value)} / ${formatHabitHistoryNumber(target)}`,
note: complete ? '今日目标已达成' : `今天还差 ${formatHabitHistoryNumber(Math.max(0, target - value))}${habit.unit ? ` ${habit.unit}` : ''}`,
semantic: complete ? '已达标' : `${Math.round(target > 0 ? value / target * 100 : 0)}%`,
}
}
type HabitSchedule = {
kind?: string
cells?: Array<{ day: string; scheduled?: boolean; paused?: boolean; value?: number | boolean }>