mue/src/components/widgets/time/Clock.jsx

81 lines
2.5 KiB
React
Raw Normal View History

2019-10-20 12:39:01 +00:00
import React from 'react';
import Analog from 'react-clock';
2019-10-20 12:39:01 +00:00
2020-11-29 14:32:08 +00:00
import './clock.scss';
export default class Clock extends React.PureComponent {
2019-10-20 12:39:01 +00:00
constructor(...args) {
super(...args);
this.timer = undefined;
2019-10-20 12:39:01 +00:00
this.state = {
time: '',
ampm: ''
2019-10-20 12:39:01 +00:00
};
}
2020-07-20 21:39:31 +00:00
startTime(time = localStorage.getItem('seconds') === 'true' || localStorage.getItem('analog') === 'true' ? (1000 - Date.now() % 1000) : (60000 - Date.now() % 60000)) {
this.timer = setTimeout(() => {
const now = new Date();
2020-07-20 21:39:31 +00:00
2020-11-28 13:24:40 +00:00
// Percentage
2020-11-29 14:32:08 +00:00
if (localStorage.getItem('percentageComplete') === 'true') return this.setState({ time: (now.getHours() / 24).toFixed(2).replace('0.', '') + '%'});
2020-11-28 13:24:40 +00:00
// Analog clock
2021-01-16 18:39:03 +00:00
if (localStorage.getItem('analog') === 'true') {
require('react-clock/dist/Clock.css');
this.setState({ time: now });
} else {
2020-11-29 14:32:08 +00:00
let time, sec = '';
2019-10-20 12:39:01 +00:00
// Extra 0
const zero = localStorage.getItem('zero');
if (localStorage.getItem('seconds') === 'true') {
if (zero === 'false') sec = `:${now.getSeconds()}`;
else sec = `:${('00' + now.getSeconds()).slice(-2)}`;
}
if (localStorage.getItem('24hour') === 'true') {
if (zero === 'false') time = `${now.getHours()}:${now.getMinutes()}${sec}`;
else time = `${('00' + now.getHours()).slice(-2)}:${('00' + now.getMinutes()).slice(-2)}${sec}`;
this.setState({ time: time });
} else {
// 12 hour support
let hours = now.getHours();
if (hours > 12) hours -= 12;
// Toggle AM/PM
let ampm = now.getHours() > 11 ? 'PM' : 'AM';
if (localStorage.getItem('ampm') === 'false') ampm = '';
if (zero === 'false') time = `${hours}:${now.getMinutes()}${sec}`;
else time = `${('00' + hours).slice(-2)}:${('00' + now.getMinutes()).slice(-2)}${sec}`;
2020-09-21 21:23:04 +00:00
this.setState({
time: time,
ampm: ampm
});
}
}
this.startTime();
}, time);
2019-10-20 12:39:01 +00:00
}
componentDidMount() {
if (localStorage.getItem('time') === 'false') return;
this.startTime(0);
2019-10-20 12:39:01 +00:00
}
render() {
2020-10-16 12:15:10 +00:00
if (localStorage.getItem('time') === 'false') return null;
let clockHTML = <h1 className='clock'>{this.state.time}<span className='ampm'>{this.state.ampm}</span> </h1>;
if (localStorage.getItem('analog') === 'true') clockHTML = <Analog className='analogclock' value={this.state.time} renderHourMarks={false} renderMinuteMarks={false} />;
2020-10-16 12:15:10 +00:00
return clockHTML;
2019-10-20 12:39:01 +00:00
}
}