feat: redesign habit detail paper flow
This commit is contained in:
@@ -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 }>
|
||||
|
||||
Reference in New Issue
Block a user