SPB Git

Repository Public

Countdown.tsx 4.7 KB 110 lines tsx

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : UQO Éval — Le Grand Défi Immobilier8 * File             : apps/web/src/components/Countdown.tsx9 * Module           : components10 * Repository       : https://git.spboucher.ai/uqo-eval-jeux11 * Project Website  : https://jeux.uqo-eval.app12 * Created          : 2026-11-2513 * Last Modified    : 2026-11-2514 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: contact@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243import { useEffect, useState } from "react";4445function remaining(target: number): { total: number; days: number; hours: number; minutes: number; seconds: number } {46  const total = Math.max(0, target - Date.now());47  const s = Math.floor(total / 1000);48  return { total, days: Math.floor(s / 86400), hours: Math.floor((s % 86400) / 3600), minutes: Math.floor((s % 3600) / 60), seconds: s % 60 };49}5051/** Temps restant avant l'heure limite, rafraîchi chaque seconde. */52export function useCountdown(endsAt: string | null | undefined) {53  const target = endsAt ? new Date(endsAt).getTime() : null;54  const [left, setLeft] = useState(() => (target ? remaining(target) : null));55  useEffect(() => {56    if (!target) {57      setLeft(null);58      return;59    }60    setLeft(remaining(target));61    const timer = window.setInterval(() => setLeft(remaining(target)), 1000);62    return () => window.clearInterval(timer);63  }, [target]);64  return left;65}6667const two = (n: number): string => String(n).padStart(2, "0");6869/** Phrase lue par les lecteurs d'écran (mise à jour à la minute, pas à la seconde). */70function spoken(left: NonNullable<ReturnType<typeof useCountdown>>): string {71  if (left.total === 0) return "L'heure limite est passée.";72  const parts = [];73  if (left.days) parts.push(`${left.days} jour${left.days > 1 ? "s" : ""}`);74  if (left.hours) parts.push(`${left.hours} heure${left.hours > 1 ? "s" : ""}`);75  parts.push(`${left.minutes} minute${left.minutes > 1 ? "s" : ""}`);76  return `Il reste ${parts.join(", ")} pour terminer.`;77}7879/**80 * Compte à rebours. `variant="blocks"` : quatre cases (jours, heures, minutes, secondes) ;81 * `variant="inline"` : forme compacte pour l'en-tête de jeu.82 */83export function Countdown({ endsAt, variant = "blocks", label }: { endsAt: string | null | undefined; variant?: "blocks" | "inline"; label?: string }) {84  const left = useCountdown(endsAt);85  if (!left) return null;86  const urgent = left.total > 0 && left.total < 24 * 3600 * 1000;87  if (variant === "inline") {88    const text = left.total === 0 ? "Terminé" : left.days > 0 ? `${left.days} j ${two(left.hours)} h` : `${two(left.hours)}:${two(left.minutes)}:${two(left.seconds)}`;89    return (90      <span className={`countdown-inline${urgent ? " is-urgent" : ""}${left.total === 0 ? " is-over" : ""}`} role="timer" aria-label={spoken(left)}>91        <span aria-hidden="true">{text}</span>92      </span>93    );94  }95  const cells: [number, string][] = [[left.days, "jours"], [left.hours, "heures"], [left.minutes, "minutes"], [left.seconds, "secondes"]];96  return (97    <div className={`countdown${urgent ? " is-urgent" : ""}${left.total === 0 ? " is-over" : ""}`}>98      {label && <p className="klabel">{label}</p>}99      <div className="countdown-cells" role="timer" aria-label={spoken(left)}>100        {cells.map(([value, unit]) => (101          <span key={unit} className="countdown-cell" aria-hidden="true">102            <span className="num">{two(value)}</span>103            <span className="klabel">{unit}</span>104          </span>105        ))}106      </div>107    </div>108  );109}110
Files main
Menu

Theme