Files
vision-start/components/Clock.tsx
T
ivanch 62e187b9f5
Build and Release to Staging / Build Vision Start (push) Successful in 1m6s
Build and Release to Staging / Build Vision Start Image (push) Successful in 2m41s
Build and Release to Staging / Deploy Vision Start (staging) (push) Successful in 18s
fixing small bugs; improving background handling and removing dead code
2026-10-28 21:52:07 -03:00

73 lines
2.0 KiB
TypeScript

import React, { useState, useEffect } from 'react';
import { getClockSizeClass } from './utils/styleUtils';
interface ClockProps {
config: {
clock: {
enabled: boolean;
size: string;
font: string;
format: string;
};
};
}
const getMinuteKey = (date: Date): number => Math.floor(date.getTime() / 60_000);
const Clock: React.FC<ClockProps> = ({ config }) => {
const [minuteKey, setMinuteKey] = useState(() => getMinuteKey(new Date()));
useEffect(() => {
if (!config.clock.enabled) return;
let timeoutId = 0;
const refresh = () => {
window.clearTimeout(timeoutId);
if (document.visibilityState !== 'visible') return;
const now = new Date();
setMinuteKey(getMinuteKey(now));
const msToNextMinute = (60 - now.getSeconds()) * 1000 - now.getMilliseconds();
timeoutId = window.setTimeout(refresh, msToNextMinute);
};
refresh();
document.addEventListener('visibilitychange', refresh);
window.addEventListener('focus', refresh);
return () => {
window.clearTimeout(timeoutId);
document.removeEventListener('visibilitychange', refresh);
window.removeEventListener('focus', refresh);
};
}, [config.clock.enabled]);
if (!config.clock.enabled) {
return null;
}
const formatTime = (date: Date) => {
const hours = date.getHours();
const minutes = date.getMinutes().toString().padStart(2, '0');
if (config.clock.format === 'HH:mm') {
return `${hours.toString().padStart(2, '0')}:${minutes}`;
}
const ampm = hours >= 12 ? 'PM' : 'AM';
const formattedHours = (hours % 12 || 12).toString();
return `${formattedHours}:${minutes} ${ampm}`;
};
return (
<div
className={`liquid-clock-text whitespace-nowrap text-white font-bold ${getClockSizeClass(config.clock.size)}`}
style={{
fontFamily: config.clock.font,
}}
>
{formatTime(new Date(minuteKey * 60_000))}
</div>
);
};
export default Clock;