2022-04-11 21:57:07 +00:00
|
|
|
import { PureComponent, Suspense, lazy } from 'react';
|
2021-08-15 21:28:37 +00:00
|
|
|
|
2022-04-11 21:57:07 +00:00
|
|
|
import { convertTimezone } from 'modules/helpers/date';
|
|
|
|
import EventBus from 'modules/helpers/eventbus';
|
2021-04-13 10:47:12 +00:00
|
|
|
|
2022-04-11 21:57:07 +00:00
|
|
|
import './clock.scss';
|
2020-11-29 14:32:08 +00:00
|
|
|
|
2022-04-11 21:57:07 +00:00
|
|
|
const Analog = lazy(() => import('react-clock'));
|
2021-04-11 10:17:09 +00:00
|
|
|
|
2021-08-14 19:10:48 +00:00
|
|
|
export default class Clock extends PureComponent {
|
2021-03-23 13:10:34 +00:00
|
|
|
constructor() {
|
|
|
|
super();
|
2020-07-17 12:37:33 +00:00
|
|
|
|
|
|
|
this.timer = undefined;
|
2019-10-20 12:39:01 +00:00
|
|
|
this.state = {
|
2022-04-11 21:57:07 +00:00
|
|
|
time: '',
|
2022-08-07 22:16:58 +00:00
|
|
|
finalHour: '',
|
|
|
|
finalMinute: '',
|
|
|
|
finalSeconds: '',
|
2022-04-11 21:57:07 +00:00
|
|
|
ampm: '',
|
2022-04-14 21:09:10 +00:00
|
|
|
nowGlobal: new Date(),
|
2022-08-08 11:47:29 +00:00
|
|
|
minuteColour: localStorage.getItem('minuteColour'),
|
2022-08-16 15:38:22 +00:00
|
|
|
hourColour: localStorage.getItem('hourColour'),
|
2019-10-20 12:39:01 +00:00
|
|
|
};
|
|
|
|
}
|
2020-07-20 21:39:31 +00:00
|
|
|
|
2022-04-08 13:48:36 +00:00
|
|
|
startTime(
|
2022-04-11 21:57:07 +00:00
|
|
|
time = localStorage.getItem('seconds') === 'true' ||
|
|
|
|
localStorage.getItem('timeType') === 'analogue'
|
2022-04-08 13:48:36 +00:00
|
|
|
? 1000 - (Date.now() % 1000)
|
2022-04-11 21:57:07 +00:00
|
|
|
: 60000 - (Date.now() % 60000),
|
2022-04-08 13:48:36 +00:00
|
|
|
) {
|
2020-07-16 16:40:24 +00:00
|
|
|
this.timer = setTimeout(() => {
|
2021-07-17 14:51:57 +00:00
|
|
|
let now = new Date();
|
2022-04-11 21:57:07 +00:00
|
|
|
const timezone = localStorage.getItem('timezone');
|
|
|
|
if (timezone && timezone !== 'auto') {
|
2021-08-16 11:21:37 +00:00
|
|
|
now = convertTimezone(now, timezone);
|
2021-07-17 14:51:57 +00:00
|
|
|
}
|
2020-07-20 21:39:31 +00:00
|
|
|
|
2022-04-11 21:57:07 +00:00
|
|
|
switch (localStorage.getItem('timeType')) {
|
|
|
|
case 'percentageComplete':
|
2021-03-17 13:01:53 +00:00
|
|
|
this.setState({
|
2022-04-11 21:57:07 +00:00
|
|
|
time: (now.getHours() / 24).toFixed(2).replace('0.', '') + '%',
|
|
|
|
ampm: '',
|
2021-01-17 16:25:21 +00:00
|
|
|
});
|
2022-04-08 13:48:36 +00:00
|
|
|
break;
|
2022-04-11 21:57:07 +00:00
|
|
|
case 'analogue':
|
2021-03-23 11:45:09 +00:00
|
|
|
// load analog clock css
|
2022-04-17 09:24:51 +00:00
|
|
|
import('react-clock/dist/Clock.css');
|
2021-03-17 13:01:53 +00:00
|
|
|
|
2021-03-23 11:45:09 +00:00
|
|
|
this.setState({
|
2022-04-08 13:48:36 +00:00
|
|
|
time: now,
|
2021-03-23 11:45:09 +00:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Default clock
|
2022-04-08 13:48:36 +00:00
|
|
|
let time,
|
2022-04-11 21:57:07 +00:00
|
|
|
sec = '';
|
|
|
|
const zero = localStorage.getItem('zero');
|
2021-03-23 11:45:09 +00:00
|
|
|
|
2022-04-11 21:57:07 +00:00
|
|
|
if (localStorage.getItem('seconds') === 'true') {
|
|
|
|
sec = `:${('00' + now.getSeconds()).slice(-2)}`;
|
2022-08-07 22:16:58 +00:00
|
|
|
this.setState({ finalSeconds: `:${('00' + now.getSeconds()).slice(-2)}` });
|
2021-01-16 22:43:46 +00:00
|
|
|
}
|
2020-08-10 18:49:46 +00:00
|
|
|
|
2022-04-11 21:57:07 +00:00
|
|
|
if (localStorage.getItem('timeformat') === 'twentyfourhour') {
|
|
|
|
if (zero === 'false') {
|
|
|
|
time = `${now.getHours()}:${('00' + now.getMinutes()).slice(-2)}${sec}`;
|
2022-08-07 22:16:58 +00:00
|
|
|
this.setState({
|
|
|
|
finalHour: `${now.getHours()}`,
|
|
|
|
finalMinute: `${('00' + now.getMinutes()).slice(-2)}`,
|
|
|
|
});
|
2021-03-23 11:45:09 +00:00
|
|
|
} else {
|
2022-04-11 21:57:07 +00:00
|
|
|
time = `${('00' + now.getHours()).slice(-2)}:${('00' + now.getMinutes()).slice(
|
|
|
|
-2,
|
|
|
|
)}${sec}`;
|
2022-08-07 22:16:58 +00:00
|
|
|
this.setState({
|
|
|
|
finalHour: `${('00' + now.getHours()).slice(-2)}`,
|
|
|
|
finalMinute: `${('00' + now.getMinutes()).slice(-2)}`,
|
|
|
|
});
|
2021-03-23 11:45:09 +00:00
|
|
|
}
|
2020-08-10 18:49:46 +00:00
|
|
|
|
2021-03-23 11:45:09 +00:00
|
|
|
this.setState({
|
2021-08-27 18:42:40 +00:00
|
|
|
time,
|
2022-04-11 21:57:07 +00:00
|
|
|
ampm: '',
|
2021-03-23 11:45:09 +00:00
|
|
|
});
|
2021-01-16 22:43:46 +00:00
|
|
|
} else {
|
2021-03-23 11:45:09 +00:00
|
|
|
// 12 hour
|
|
|
|
let hours = now.getHours();
|
|
|
|
|
|
|
|
if (hours > 12) {
|
|
|
|
hours -= 12;
|
2021-08-18 10:28:01 +00:00
|
|
|
} else if (hours === 0) {
|
|
|
|
hours = 12;
|
2021-03-23 11:45:09 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 21:57:07 +00:00
|
|
|
if (zero === 'false') {
|
|
|
|
time = `${hours}:${('00' + now.getMinutes()).slice(-2)}${sec}`;
|
2022-08-07 22:16:58 +00:00
|
|
|
this.setState({
|
|
|
|
finalHour: `${hours}`,
|
|
|
|
finalMinute: `${('00' + now.getMinutes()).slice(-2)}`,
|
|
|
|
});
|
2021-03-23 11:45:09 +00:00
|
|
|
} else {
|
2022-04-11 21:57:07 +00:00
|
|
|
time = `${('00' + hours).slice(-2)}:${('00' + now.getMinutes()).slice(-2)}${sec}`;
|
2022-08-07 22:16:58 +00:00
|
|
|
this.setState({
|
|
|
|
finalHour: `${('00' + hours).slice(-2)}`,
|
|
|
|
finalMinute: `${('00' + now.getMinutes()).slice(-2)}`,
|
|
|
|
});
|
2021-03-23 11:45:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({
|
2021-08-27 18:42:40 +00:00
|
|
|
time,
|
2022-04-11 21:57:07 +00:00
|
|
|
ampm: now.getHours() > 11 ? 'PM' : 'AM',
|
2021-03-23 11:45:09 +00:00
|
|
|
});
|
2021-03-17 13:01:53 +00:00
|
|
|
}
|
2021-03-23 11:45:09 +00:00
|
|
|
break;
|
2020-07-16 19:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.startTime();
|
|
|
|
}, time);
|
2019-10-20 12:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2022-04-11 21:57:07 +00:00
|
|
|
EventBus.on('refresh', (data) => {
|
|
|
|
if (data === 'clock' || data === 'timezone') {
|
|
|
|
const element = document.querySelector('.clock-container');
|
2021-04-13 10:47:12 +00:00
|
|
|
|
2022-04-11 21:57:07 +00:00
|
|
|
if (localStorage.getItem('time') === 'false') {
|
|
|
|
return (element.style.display = 'none');
|
2021-04-13 10:47:12 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 14:06:15 +00:00
|
|
|
this.timer = null;
|
|
|
|
this.startTime(0);
|
|
|
|
|
2022-04-11 21:57:07 +00:00
|
|
|
element.style.display = 'block';
|
2022-04-08 13:48:36 +00:00
|
|
|
element.style.fontSize = `${
|
2022-04-11 21:57:07 +00:00
|
|
|
4 * Number((localStorage.getItem('zoomClock') || 100) / 100)
|
2022-04-08 13:48:36 +00:00
|
|
|
}em`;
|
2021-04-13 10:47:12 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-04-11 21:57:07 +00:00
|
|
|
if (localStorage.getItem('timeType') !== 'analogue') {
|
|
|
|
document.querySelector('.clock-container').style.fontSize = `${
|
|
|
|
4 * Number((localStorage.getItem('zoomClock') || 100) / 100)
|
2022-04-08 13:48:36 +00:00
|
|
|
}em`;
|
2021-05-23 11:23:16 +00:00
|
|
|
}
|
2021-05-02 11:11:07 +00:00
|
|
|
|
2020-07-16 16:40:24 +00:00
|
|
|
this.startTime(0);
|
2019-10-20 12:39:01 +00:00
|
|
|
}
|
|
|
|
|
2021-07-02 20:29:33 +00:00
|
|
|
componentWillUnmount() {
|
2022-04-11 21:57:07 +00:00
|
|
|
EventBus.off('refresh');
|
2021-07-02 20:29:33 +00:00
|
|
|
}
|
|
|
|
|
2019-10-20 12:39:01 +00:00
|
|
|
render() {
|
2021-04-10 18:36:58 +00:00
|
|
|
const enabled = (setting) => {
|
2022-04-11 21:57:07 +00:00
|
|
|
return localStorage.getItem(setting) === 'true';
|
2021-04-08 18:52:17 +00:00
|
|
|
};
|
2021-03-17 18:26:09 +00:00
|
|
|
|
2022-04-11 21:57:07 +00:00
|
|
|
if (localStorage.getItem('timeType') === 'analogue') {
|
2022-08-31 12:31:49 +00:00
|
|
|
return (
|
2022-08-26 09:45:32 +00:00
|
|
|
<Suspense fallback={<></>}>
|
2023-01-21 12:10:40 +00:00
|
|
|
<div className={`clockBackground ${enabled('roundClock') ? 'round' : ''}`}>
|
2022-04-08 13:48:36 +00:00
|
|
|
<Analog
|
|
|
|
className="analogclock clock-container"
|
|
|
|
value={this.state.time}
|
2022-11-06 11:59:59 +00:00
|
|
|
size={1.5 * Number(localStorage.getItem('zoomClock') || 100)}
|
2022-04-11 21:57:07 +00:00
|
|
|
renderMinuteMarks={enabled('minuteMarks')}
|
|
|
|
renderHourMarks={enabled('hourMarks')}
|
|
|
|
renderSecondHand={enabled('secondHand')}
|
|
|
|
renderMinuteHand={enabled('minuteHand')}
|
|
|
|
renderHourHand={enabled('hourHand')}
|
2022-04-08 13:48:36 +00:00
|
|
|
/>
|
|
|
|
</div>
|
2021-08-14 19:10:48 +00:00
|
|
|
</Suspense>
|
2021-03-17 18:26:09 +00:00
|
|
|
);
|
2021-01-16 22:43:46 +00:00
|
|
|
}
|
2020-10-16 12:15:10 +00:00
|
|
|
|
2022-08-08 12:43:47 +00:00
|
|
|
if (localStorage.getItem('timeType') === 'verticalClock') {
|
2022-08-31 12:31:49 +00:00
|
|
|
return (
|
|
|
|
<span className="new-clock clock-container">
|
|
|
|
{' '}
|
|
|
|
<div className="hour" style={{ color: this.state.hourColour }}>
|
|
|
|
{this.state.finalHour}
|
|
|
|
</div>{' '}
|
|
|
|
<div className="minute" style={{ color: this.state.minuteColour }}>
|
|
|
|
{this.state.finalMinute}
|
|
|
|
</div>{' '}
|
|
|
|
<div className="seconds">{this.state.finalSeconds}</div>{' '}
|
|
|
|
</span>
|
2022-08-07 22:16:58 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-31 12:31:49 +00:00
|
|
|
return (
|
|
|
|
<span className="clock clock-container">
|
|
|
|
{this.state.time}
|
|
|
|
<span className="ampm">{this.state.ampm}</span>
|
|
|
|
</span>
|
|
|
|
);
|
2019-10-20 12:39:01 +00:00
|
|
|
}
|
|
|
|
}
|