Files
wx_pbzc/utils/badge-state.js
T
2026-07-29 11:31:25 +08:00

29 lines
1.1 KiB
JavaScript

const TRACK_PAD = 4
const getBadgeProgress = (badges, totalDays) => {
const defs = Array.isArray(badges) ? badges : []
if (defs.length === 0) return 0
const days = Math.max(0, Number(totalDays) || 0)
if (days < defs[0].days) return 0
if (defs.length === 1 || days >= defs[defs.length - 1].days) return 100
const segmentWidth = (100 - TRACK_PAD * 2) / (defs.length - 1)
let index = 0
while (index < defs.length - 1 && days >= defs[index + 1].days) index++
const ratio = (days - defs[index].days) / (defs[index + 1].days - defs[index].days)
return Math.round((TRACK_PAD + index * segmentWidth + ratio * segmentWidth) * 10) / 10
}
const getBadgeCelebration = (previous, unlockedDays) => {
const unlocked = Array.isArray(unlockedDays) ? unlockedDays : []
if (!Array.isArray(previous)) return { newly: [], seen: Array.from(new Set(unlocked)) }
const previousSet = new Set(previous)
const newly = unlocked.filter(days => !previousSet.has(days))
return { newly, seen: Array.from(new Set(previous.concat(unlocked))) }
}
module.exports = { getBadgeProgress, getBadgeCelebration }