|
1 | | -"use client"; |
| 1 | +import React, { useEffect, useState } from 'react'; |
| 2 | +import { Temporal } from '@js-temporal/polyfill'; |
| 3 | +import { WDXL_Lubrifont_JP_N } from 'next/font/google'; |
| 4 | +import { Incident } from '@/types/incident'; |
| 5 | +import { DeleteForever, ExpandMore, ExpandLess, RestartAlt, Info } from '@mui/icons-material'; |
2 | 6 |
|
3 | | -import { useEffect, useState } from "react"; |
| 7 | +const WdxlLubrifontJpN = WDXL_Lubrifont_JP_N({ |
| 8 | + subsets: ['latin'], |
| 9 | + weight: ['400'], |
| 10 | +}); |
4 | 11 |
|
5 | | -export function Counter() { |
6 | | - const [daysWithoutIncidents, setDaysWithoutIncidents] = useState<number>(0); |
| 12 | +export function Counter({ props: { title, date, history, description } }: { props: Incident }) { |
| 13 | + const [shownDate, setShownDate] = useState<Temporal.Instant>(date); |
| 14 | + const [now, setNow] = useState(() => Temporal.Now.instant()); |
| 15 | + const [showAccordion, setShowAccordion] = useState(false); |
7 | 16 |
|
8 | 17 | 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)); |
| 18 | + const interval = setInterval(() => { |
| 19 | + setNow(Temporal.Now.instant()); |
| 20 | + }, 1000); |
| 21 | + return () => clearInterval(interval); |
| 22 | + }, []); |
| 23 | + |
| 24 | + function resetDate() { |
| 25 | + const now = Temporal.Now.instant(); |
| 26 | + history.push(now); |
| 27 | + setShownDate(now); |
| 28 | + } |
| 29 | + |
| 30 | + function getBgColor(days: number) { |
| 31 | + if (days < 7) return 'bg-red-400'; |
| 32 | + if (days < 30) return 'bg-yellow-400'; |
| 33 | + if (days < 183) return 'bg-green-400'; |
| 34 | + if (days >= 365) return 'bg-blue-400'; |
| 35 | + return 'bg-green-500'; |
| 36 | + } |
| 37 | + |
| 38 | + function timeAgo({ |
| 39 | + pastDate, |
| 40 | + targetDate, |
| 41 | + alwaysRelative, |
| 42 | + }: { |
| 43 | + pastDate: Temporal.Instant; |
| 44 | + targetDate: Temporal.Instant; |
| 45 | + alwaysRelative?: boolean; |
| 46 | + }) { |
| 47 | + const dateDiff = targetDate.since(pastDate, { largestUnit: 'auto' }); |
| 48 | + const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone; |
| 49 | + const locale = Intl.DateTimeFormat().resolvedOptions().locale; |
| 50 | + |
| 51 | + const relativeTimeFormat = new Intl.RelativeTimeFormat(locale, { |
| 52 | + numeric: 'auto', |
| 53 | + }); |
| 54 | + const units: [Intl.RelativeTimeFormatUnit, number][] = [ |
| 55 | + ['second', 60], |
| 56 | + ['minute', 60], |
| 57 | + ['hour', 24], |
| 58 | + ['day', 30], |
| 59 | + ['month', 12], |
| 60 | + ['year', Number.POSITIVE_INFINITY], |
| 61 | + ]; |
| 62 | + let delta = (targetDate.epochMilliseconds - pastDate.epochMilliseconds) / 1000; |
| 63 | + |
| 64 | + if (alwaysRelative) { |
| 65 | + let value = delta; |
| 66 | + for (const [unit, limit] of units) { |
| 67 | + if (Math.abs(value) < limit) { |
| 68 | + return relativeTimeFormat.format(-Math.round(value), unit); |
| 69 | + } |
| 70 | + value /= limit; |
| 71 | + } |
15 | 72 | } else { |
16 | | - setDaysWithoutIncidents(0); |
| 73 | + if (dateDiff.months >= 1 || dateDiff.years >= 1) { |
| 74 | + const date = pastDate.toZonedDateTimeISO(timeZone); |
| 75 | + return date.toLocaleString(locale, { |
| 76 | + month: 'short', |
| 77 | + day: 'numeric', |
| 78 | + year: 'numeric', |
| 79 | + weekday: undefined, |
| 80 | + }); |
| 81 | + } |
| 82 | + for (const [unit, limit] of units.slice(0, 4)) { |
| 83 | + if (Math.abs(delta) < limit) { |
| 84 | + return relativeTimeFormat.format(-Math.round(delta), unit); |
| 85 | + } |
| 86 | + delta /= limit; |
| 87 | + } |
17 | 88 | } |
18 | | - }, []); |
19 | 89 |
|
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"; |
| 90 | + const date = pastDate.toZonedDateTimeISO(timeZone); |
| 91 | + return date.toLocaleString(locale, { |
| 92 | + month: 'short', |
| 93 | + day: 'numeric', |
| 94 | + year: 'numeric', |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + const relative = timeAgo({ pastDate: shownDate, targetDate: now }); |
| 99 | + const lastIncidentPlainDate = Temporal.PlainDate.from(shownDate.toString().slice(0, 10)); |
| 100 | + const todayPlainDate = Temporal.PlainDate.from(now.toString().slice(0, 10)); |
| 101 | + const days = lastIncidentPlainDate.until(todayPlainDate).days; |
| 102 | + const daysString = Math.max(0, days).toString().padStart(8, '0'); |
| 103 | + const bgColor = getBgColor(days); |
28 | 104 |
|
29 | 105 | 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> |
| 106 | + <div className='bg-black text-white rounded-lg max-w-3xl w-full shadow-xl divide-y-2 divide-gray-900'> |
| 107 | + <div className='relative bg-black text-center rounded-t-lg py-3'> |
| 108 | + <div className='absolute left-4 top-1/2 -translate-y-1/2'> |
| 109 | + <span |
| 110 | + className={['block w-3.5 h-3.5 rounded-full animate-ping opacity-40 absolute', bgColor].join(' ')} |
| 111 | + ></span> |
| 112 | + <span className={['block w-3.5 h-3.5 rounded-full relative', bgColor].join(' ')}></span> |
| 113 | + </div> |
| 114 | + |
| 115 | + <span className='opacity-60 mr-2'>Days since</span> |
| 116 | + <span className='font-bold text-l'>{title}</span> |
| 117 | + <span className='relative group cursor-pointer' tabIndex={0} aria-label='Info'> |
| 118 | + <Info className='ml-1 text-blue-500' /> |
| 119 | + <span |
| 120 | + className='absolute left-1/2 top-full z-10 mt-2 w-40 -translate-x-1/2 rounded bg-black px-2 py-1 text-xs text-white opacity-0 group-hover:opacity-100 group-focus:opacity-100 pointer-events-none transition-opacity' |
| 121 | + role='tooltip' |
| 122 | + > |
| 123 | + {description} |
| 124 | + </span> |
| 125 | + </span> |
| 126 | + |
| 127 | + <div className='absolute right-4 top-1/2 -translate-y-1/2'> |
| 128 | + <span |
| 129 | + className={['block w-3.5 h-3.5 rounded-full animate-ping opacity-40 absolute', bgColor].join(' ')} |
| 130 | + ></span> |
| 131 | + <span className={['block w-3.5 h-3.5 rounded-full relative', bgColor].join(' ')}></span> |
| 132 | + </div> |
| 133 | + </div> |
| 134 | + |
| 135 | + <div className='bg-white flex justify-center'> |
| 136 | + <div className='flex w-full divide-x-2 divide-gray-900'> |
| 137 | + {daysString.split('').map((digit, index) => ( |
| 138 | + <span |
| 139 | + key={index} |
| 140 | + className={`${WdxlLubrifontJpN.className} bg-white text-black text-6xl leading-none flex items-center justify-center w-16 h-24 flex-1`} |
| 141 | + > |
| 142 | + {digit} |
| 143 | + </span> |
| 144 | + ))} |
| 145 | + </div> |
| 146 | + </div> |
| 147 | + <div |
| 148 | + className={[ |
| 149 | + 'bg-yellow-200 overflow-hidden transition-[max-height,padding,overflow] duration-300', |
| 150 | + showAccordion ? 'max-h-48 overflow-y-auto px-4 py-3' : 'max-h-0 p-0 pointer-events-none', |
| 151 | + ].join(' ')} |
| 152 | + > |
| 153 | + {!history.length ? ( |
| 154 | + <p className='text-center text-gray-600'>No history yet.</p> |
| 155 | + ) : ( |
| 156 | + <ol className='relative border-s border-gray-200 dark:border-gray-700 space-y-3'> |
| 157 | + {[...history] |
| 158 | + .sort((a, b) => b.epochMilliseconds - a.epochMilliseconds) |
| 159 | + .map((date, key) => ( |
| 160 | + <li key={key} className='ms-4'> |
| 161 | + <div className='absolute w-3 h-3 bg-gray-200 rounded-full mt-1.5 -start-1.5 border border-white dark:border-gray-900 dark:bg-gray-700'></div> |
| 162 | + <time className='mb-1 text-sm font-normal leading-none text-gray-700 '> |
| 163 | + {date.toLocaleString()} ( |
| 164 | + <span>{timeAgo({ pastDate: date, targetDate: now, alwaysRelative: true })}</span>) |
| 165 | + </time> |
| 166 | + </li> |
| 167 | + ))} |
| 168 | + </ol> |
| 169 | + )} |
| 170 | + </div> |
| 171 | + |
| 172 | + <div className='bg-yellow-400 rounded-b-lg text-black text-center py-3 px-4 text-base flex'> |
| 173 | + <div className='flex-3 grow-3 flex items-center justify-start'> |
| 174 | + <span className='opacity-60 mr-1'>Last:</span> |
| 175 | + <span>{relative}</span> |
| 176 | + </div> |
| 177 | + <div className='flex-1 grow flex items-center justify-end gap-2'> |
| 178 | + <button aria-label='Delete' className='bg-black text-red-400 px-3 py-1 rounded hover:bg-gray-800 transition'> |
| 179 | + <span aria-hidden='true'> |
| 180 | + <DeleteForever /> |
| 181 | + </span> |
| 182 | + </button> |
| 183 | + <button |
| 184 | + aria-label='Reset' |
| 185 | + className='bg-black text-blue-400 px-3 py-1 rounded hover:bg-gray-800 transition' |
| 186 | + onClick={() => resetDate()} |
| 187 | + > |
| 188 | + <span aria-hidden='true'> |
| 189 | + <RestartAlt /> |
| 190 | + </span> |
| 191 | + </button> |
| 192 | + <button |
| 193 | + aria-label={showAccordion ? 'Collapse section' : 'Expand section'} |
| 194 | + aria-expanded={showAccordion} |
| 195 | + className='bg-black text-white px-3 py-1 rounded hover:bg-gray-800 transition' |
| 196 | + onClick={() => setShowAccordion(!showAccordion)} |
| 197 | + > |
| 198 | + <span aria-hidden='true'>{showAccordion ? <ExpandLess /> : <ExpandMore />}</span> |
| 199 | + </button> |
| 200 | + </div> |
| 201 | + </div> |
33 | 202 | </div> |
34 | 203 | ); |
35 | 204 | } |
| 205 | + |
| 206 | +export default Counter; |
0 commit comments