import { Check } from 'lucide-react'
import { useEffect, useState, type ReactNode } from 'react'
import { fmtNumber, t } from '../../i18n'
import { formatDay, mediaLabel, periodLabel, quantity, type ChallengeProgress } from '../../lib/challenges'
import { cx } from '../../lib/utils'
import type { Challenge } from '../../types'
/** Anneau de progression (piste discrète, arc à la couleur d'accent). */
export function ProgressRing({ value, size = 56, stroke = 5, done, children, animate = true, label }: { label?: string; value: number; size?: number; stroke?: number; done?: boolean; children?: ReactNode; animate?: boolean }) {
const r = (size - stroke) / 2
const c = 2 * Math.PI * r
const target = Math.max(0, Math.min(1, value))
// L'arc se remplit à l'apparition (sauf mouvement réduit, géré par le CSS de transition)
const [shown, setShown] = useState(animate ? 0 : target)
useEffect(() => {
const id = requestAnimationFrame(() => setShown(target))
return () => cancelAnimationFrame(id)
}, [target])
return (
{done ? (
) : (
children
)}
)
}
export const percent = (p: ChallengeProgress, c: Challenge) => Math.min(100, Math.floor((p.count / c.target) * 100))
/** « 3 films d'avance », « Réussi le 4 octobre »… */
export function paceText(c: Challenge, p: ChallengeProgress): string {
if (p.state === 'done') return p.doneAt ? t('challenges.doneOn', { date: formatDay(p.doneAt) }) : t('challenges.done')
if (p.state === 'missed') return t('challenges.missed')
if (p.state === 'upcoming') return t('challenges.startsIn', { count: p.startsIn })
if (p.pace >= 1) return t('challenges.ahead', { what: quantity(Math.floor(p.pace), c.media, c.unit) })
if (p.pace <= -1) return t('challenges.behind', { what: quantity(Math.floor(-p.pace), c.media, c.unit) })
return t('challenges.onPace')
}
export function daysText(p: ChallengeProgress): string | null {
if (p.state !== 'active') return null
return p.daysLeft === 0 ? t('challenges.lastDay') : t('challenges.daysLeft', { count: p.daysLeft })
}
/** « 12 / 52 films » */
export function CountLine({ c, p, className }: { c: Challenge; p: ChallengeProgress; className?: string }) {
return (
{fmtNumber(p.count)}
/
{quantity(c.target, c.media, c.unit)}
)
}
/** Ligne de défi (accueil et liste) : anneau, progression, période, rythme. */
export function ChallengeSummary({ c, p, onClick, ring = 56 }: { c: Challenge; p: ChallengeProgress; onClick: () => void; ring?: number }) {
const days = daysText(p)
return (
)
}