|
1 | | -"use client"; |
| 1 | +import React, { useEffect, useState } from 'react'; |
| 2 | +import { Temporal } from '@js-temporal/polyfill'; |
| 3 | +import { timeAgo } from '@/utils/timeAgo'; |
2 | 4 |
|
3 | | -import { useEffect, useState } from "react"; |
4 | | - |
5 | | -export function Counter() { |
6 | | - const [daysWithoutIncidents, setDaysWithoutIncidents] = useState<number>(0); |
| 5 | +export function Counter({ title, lastIncidentDate }: { title: string; lastIncidentDate: Temporal.Instant }) { |
| 6 | + const [now, setNow] = useState(() => Temporal.Now.instant()); |
7 | 7 |
|
8 | 8 | useEffect(() => { |
9 | | - // Only using this to save the date of the last incident in localStorage |
10 | | - const lastIncident = localStorage.getItem("lastIncidentDate"); |
11 | | - if (lastIncident) { |
12 | | - const diff = |
13 | | - (Date.now() - new Date(lastIncident).getTime()) / (1000 * 60 * 60 * 24); |
14 | | - setDaysWithoutIncidents(Math.floor(diff)); |
15 | | - } else { |
16 | | - setDaysWithoutIncidents(0); |
17 | | - } |
| 9 | + const interval = setInterval(() => { |
| 10 | + setNow(Temporal.Now.instant()); |
| 11 | + }, 1000); |
| 12 | + return () => clearInterval(interval); |
18 | 13 | }, []); |
19 | 14 |
|
20 | | - const variantClass = |
21 | | - daysWithoutIncidents === 0 |
22 | | - ? "bg-red-600 text-white" |
23 | | - : daysWithoutIncidents > 10 |
24 | | - ? "bg-green-600 text-white" |
25 | | - : daysWithoutIncidents > 5 |
26 | | - ? "bg-yellow-400 text-black" |
27 | | - : "bg-blue-600 text-white"; |
| 15 | + const relative = timeAgo({ pastDate: lastIncidentDate, targetDate: now }); |
| 16 | + const lastIncidentPlainDate = Temporal.PlainDate.from(lastIncidentDate.toString().slice(0, 10)); |
| 17 | + const todayPlainDate = Temporal.PlainDate.from(now.toString().slice(0, 10)); |
| 18 | + const days = lastIncidentPlainDate.until(todayPlainDate).days; |
| 19 | + const daysString = Math.max(0, days).toString().padStart(8, '0'); |
28 | 20 |
|
29 | 21 | return ( |
30 | | - <div className={`${variantClass} px-6 py-4 rounded-lg shadow-md text-center w-full max-w-sm`}> |
31 | | - <span className="text-xs block">Days without incidents</span> |
32 | | - <span className="text-3xl font-bold">{daysWithoutIncidents}</span> |
| 22 | + <div className='bg-black text-white rounded-lg max-w-3xl w-full shadow-xl divide-y-2 divide-gray-900'> |
| 23 | + <div className='relative bg-black text-center rounded-t-lg py-3'> |
| 24 | + <div className='absolute left-4 top-1/2 -translate-y-1/2'> |
| 25 | + <span className='block w-3.5 h-3.5 rounded-full bg-yellow-400 animate-ping opacity-40 absolute'></span> |
| 26 | + <span className='block w-3.5 h-3.5 rounded-full bg-yellow-400 relative'></span> |
| 27 | + </div> |
| 28 | + |
| 29 | + <span className='opacity-60 mr-2'>Days since</span> |
| 30 | + <span className='font-bold text-xl'>{title}</span> |
| 31 | + |
| 32 | + <div className='absolute right-4 top-1/2 -translate-y-1/2'> |
| 33 | + <span className='block w-3.5 h-3.5 rounded-full bg-yellow-400 animate-ping opacity-40 absolute'></span> |
| 34 | + <span className='block w-3.5 h-3.5 rounded-full bg-yellow-400 relative'></span> |
| 35 | + </div> |
| 36 | + </div> |
| 37 | + |
| 38 | + <div className='bg-white flex justify-center'> |
| 39 | + <div className='flex w-full divide-x-2 divide-gray-900'> |
| 40 | + {daysString.split('').map((digit, index) => ( |
| 41 | + <span |
| 42 | + key={index} |
| 43 | + className='bg-white text-black text-6xl font-[DigitalDisplay] leading-none flex items-center justify-center w-16 h-24 flex-1' |
| 44 | + > |
| 45 | + {digit} |
| 46 | + </span> |
| 47 | + ))} |
| 48 | + </div> |
| 49 | + </div> |
| 50 | + |
| 51 | + <div className='bg-yellow-400 rounded-b-lg text-black text-center py-3 px-4 text-base flex'> |
| 52 | + <div className='flex-3 flex-grow-[3] flex items-center justify-start'> |
| 53 | + <span className='opacity-60 mr-1'>Last:</span> |
| 54 | + <span>{relative}</span> |
| 55 | + </div> |
| 56 | + <div className='flex-1 flex-grow flex items-center justify-end gap-2'> |
| 57 | + <button className='bg-black text-yellow-400 px-3 py-1 rounded hover:bg-gray-800 transition'>🗑️</button> |
| 58 | + <button className='bg-black text-yellow-400 px-3 py-1 rounded hover:bg-gray-800 transition'>🔄</button> |
| 59 | + <button className='bg-black text-yellow-400 px-3 py-1 rounded hover:bg-gray-800 transition'>↕️</button> |
| 60 | + </div> |
| 61 | + </div> |
33 | 62 | </div> |
34 | 63 | ); |
35 | 64 | } |
| 65 | + |
| 66 | +export default Counter; |
0 commit comments