2021-09-10 15:38:53 +00:00
|
|
|
import variables from 'modules/variables';
|
2021-08-27 22:08:32 +00:00
|
|
|
import { PureComponent, createRef } from 'react';
|
2021-07-17 14:51:57 +00:00
|
|
|
|
2021-08-28 14:34:12 +00:00
|
|
|
import { nth, convertTimezone } from 'modules/helpers/date';
|
|
|
|
import EventBus from 'modules/helpers/eventbus';
|
2021-03-21 14:39:12 +00:00
|
|
|
|
2020-11-29 14:32:08 +00:00
|
|
|
import './greeting.scss';
|
|
|
|
|
2021-08-14 19:10:48 +00:00
|
|
|
export default class Greeting extends PureComponent {
|
2021-03-23 13:10:34 +00:00
|
|
|
constructor() {
|
|
|
|
super();
|
2019-10-20 12:39:01 +00:00
|
|
|
this.state = {
|
2022-04-08 13:48:36 +00:00
|
|
|
greeting: '',
|
2019-10-20 12:39:01 +00:00
|
|
|
};
|
2021-03-23 11:45:09 +00:00
|
|
|
this.timer = undefined;
|
2021-08-27 22:08:32 +00:00
|
|
|
this.greeting = createRef();
|
2020-10-13 14:30:53 +00:00
|
|
|
}
|
2019-10-20 12:39:01 +00:00
|
|
|
|
2020-07-17 12:37:33 +00:00
|
|
|
doEvents(time, message) {
|
2021-01-16 22:43:46 +00:00
|
|
|
if (localStorage.getItem('events') === 'false') {
|
|
|
|
return message;
|
|
|
|
}
|
2020-07-16 16:40:24 +00:00
|
|
|
|
2020-07-17 12:37:33 +00:00
|
|
|
// Get current month & day
|
2021-01-17 16:25:21 +00:00
|
|
|
const month = time.getMonth();
|
|
|
|
const date = time.getDate();
|
2020-07-16 16:40:24 +00:00
|
|
|
|
2021-03-17 14:41:50 +00:00
|
|
|
// If it's December 25th, set the greeting string to "Merry Christmas"
|
2021-01-17 16:25:21 +00:00
|
|
|
if (month === 11 && date === 25) {
|
2022-08-26 09:45:32 +00:00
|
|
|
message = variables.getMessage('widgets.greeting.christmas');
|
2022-04-08 13:48:36 +00:00
|
|
|
// If the date is January 1st, set the greeting string to "Happy new year"
|
2021-01-17 16:25:21 +00:00
|
|
|
} else if (month === 0 && date === 1) {
|
2022-08-26 09:45:32 +00:00
|
|
|
message = variables.getMessage('widgets.greeting.newyear');
|
2022-04-08 13:48:36 +00:00
|
|
|
// If it's October 31st, set the greeting string to "Happy Halloween"
|
2021-01-17 16:25:21 +00:00
|
|
|
} else if (month === 9 && date === 31) {
|
2022-08-26 09:45:32 +00:00
|
|
|
message = variables.getMessage('widgets.greeting.halloween');
|
2021-01-16 22:43:46 +00:00
|
|
|
}
|
2020-07-20 21:39:31 +00:00
|
|
|
|
2020-07-17 12:37:33 +00:00
|
|
|
return message;
|
2020-07-16 16:40:24 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 14:39:12 +00:00
|
|
|
calculateAge(date) {
|
|
|
|
const diff = Date.now() - date.getTime();
|
|
|
|
const birthday = new Date(diff);
|
|
|
|
return Math.abs(birthday.getUTCFullYear() - 1970);
|
|
|
|
}
|
|
|
|
|
2022-04-08 13:48:36 +00:00
|
|
|
getGreeting(time = 60000 - (Date.now() % 60000)) {
|
2021-03-23 11:45:09 +00:00
|
|
|
this.timer = setTimeout(() => {
|
2021-07-17 14:51:57 +00:00
|
|
|
let now = new Date();
|
|
|
|
const timezone = localStorage.getItem('timezone');
|
2021-08-16 11:21:37 +00:00
|
|
|
if (timezone && timezone !== 'auto') {
|
|
|
|
now = convertTimezone(now, timezone);
|
2021-07-17 14:51:57 +00:00
|
|
|
}
|
|
|
|
|
2021-03-23 11:45:09 +00:00
|
|
|
const hour = now.getHours();
|
|
|
|
|
|
|
|
// Set the default greeting string to "Good evening"
|
2022-08-26 10:44:24 +00:00
|
|
|
let message = variables.getMessage('widgets.greeting.evening');
|
2021-03-23 11:45:09 +00:00
|
|
|
// If it's before 12am, set the greeting string to "Good morning"
|
|
|
|
if (hour < 12) {
|
2022-08-26 09:45:32 +00:00
|
|
|
message = variables.getMessage('widgets.greeting.morning');
|
2022-04-08 13:48:36 +00:00
|
|
|
// If it's before 6pm, set the greeting string to "Good afternoon"
|
2021-03-23 11:45:09 +00:00
|
|
|
} else if (hour < 18) {
|
2022-08-26 10:44:24 +00:00
|
|
|
message = variables.getMessage('widgets.greeting.afternoon');
|
2021-03-23 11:45:09 +00:00
|
|
|
}
|
2019-11-06 19:03:17 +00:00
|
|
|
|
2021-08-15 19:33:06 +00:00
|
|
|
// Events and custom
|
2021-03-23 11:45:09 +00:00
|
|
|
const custom = localStorage.getItem('defaultGreetingMessage');
|
|
|
|
if (custom === 'false') {
|
|
|
|
message = '';
|
2021-08-15 19:33:06 +00:00
|
|
|
} else {
|
|
|
|
message = this.doEvents(now, message);
|
2021-03-23 11:45:09 +00:00
|
|
|
}
|
2020-07-17 12:37:33 +00:00
|
|
|
|
2021-03-23 11:45:09 +00:00
|
|
|
// Name
|
|
|
|
let name = '';
|
|
|
|
const data = localStorage.getItem('greetingName');
|
2020-07-17 12:37:33 +00:00
|
|
|
|
2021-03-23 11:45:09 +00:00
|
|
|
if (typeof data === 'string') {
|
|
|
|
if (data.replace(/\s/g, '').length > 0) {
|
|
|
|
name = `, ${data.trim()}`;
|
|
|
|
}
|
2021-02-08 17:29:07 +00:00
|
|
|
}
|
2019-12-01 13:29:07 +00:00
|
|
|
|
2021-08-22 14:00:05 +00:00
|
|
|
const birthday = localStorage.getItem('birthdayenabled');
|
|
|
|
|
|
|
|
if (custom === 'false' && birthday !== 'true') {
|
2021-03-23 11:45:09 +00:00
|
|
|
name = name.replace(',', '');
|
|
|
|
}
|
2020-07-21 10:02:22 +00:00
|
|
|
|
2021-03-23 11:45:09 +00:00
|
|
|
// Birthday
|
2021-08-22 14:00:05 +00:00
|
|
|
if (birthday === 'true') {
|
2021-08-11 15:21:35 +00:00
|
|
|
const birth = new Date(localStorage.getItem('birthday'));
|
|
|
|
|
|
|
|
if (birth.getDate() === now.getDate() && birth.getMonth() === now.getMonth()) {
|
2022-11-06 11:59:59 +00:00
|
|
|
if (localStorage.getItem('birthdayage') === 'true' && this.calculateAge(birth) !== 0) {
|
2022-08-28 19:19:47 +00:00
|
|
|
const text = variables.getMessage('widgets.greeting.birthday').split(' ');
|
2021-08-16 11:21:37 +00:00
|
|
|
message = `${text[0]} ${nth(this.calculateAge(birth))} ${text[1]}`;
|
2021-08-11 15:21:35 +00:00
|
|
|
} else {
|
2022-08-26 10:44:24 +00:00
|
|
|
message = variables.getMessage('widgets.greeting.birthday');
|
2021-08-11 15:21:35 +00:00
|
|
|
}
|
2021-03-23 11:45:09 +00:00
|
|
|
}
|
2021-03-21 14:39:12 +00:00
|
|
|
}
|
2020-11-30 16:47:22 +00:00
|
|
|
|
2021-03-23 11:45:09 +00:00
|
|
|
// Set the state to the greeting string
|
|
|
|
this.setState({
|
2022-04-08 13:48:36 +00:00
|
|
|
greeting: `${message}${name}`,
|
2021-03-23 11:45:09 +00:00
|
|
|
});
|
2022-04-08 13:48:36 +00:00
|
|
|
|
2021-03-23 11:45:09 +00:00
|
|
|
this.getGreeting();
|
|
|
|
}, time);
|
2019-10-20 12:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2021-04-13 10:47:12 +00:00
|
|
|
EventBus.on('refresh', (data) => {
|
2021-07-17 14:51:57 +00:00
|
|
|
if (data === 'greeting' || data === 'timezone') {
|
2021-04-13 10:47:12 +00:00
|
|
|
if (localStorage.getItem('greeting') === 'false') {
|
2022-04-08 13:48:36 +00:00
|
|
|
return (this.greeting.current.style.display = 'none');
|
2021-04-13 10:47:12 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 14:06:15 +00:00
|
|
|
this.timer = null;
|
|
|
|
this.getGreeting(0);
|
|
|
|
|
2021-08-27 22:08:32 +00:00
|
|
|
this.greeting.current.style.display = 'block';
|
2022-04-08 13:48:36 +00:00
|
|
|
this.greeting.current.style.fontSize = `${
|
|
|
|
1.6 * Number((localStorage.getItem('zoomGreeting') || 100) / 100)
|
|
|
|
}em`;
|
2021-04-13 10:47:12 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-05-02 11:11:07 +00:00
|
|
|
// this comment can apply to all widget zoom features apart from the general one in the Accessibility section
|
2022-04-08 13:48:36 +00:00
|
|
|
// in a nutshell: 1.6 is the current font size, and we do "localstorage || 100" so we don't have to try that 4.0 -> 5.0 thing again
|
|
|
|
this.greeting.current.style.fontSize = `${
|
|
|
|
1.6 * Number((localStorage.getItem('zoomGreeting') || 100) / 100)
|
|
|
|
}em`;
|
2021-05-02 11:11:07 +00:00
|
|
|
|
2021-03-23 11:45:09 +00:00
|
|
|
this.getGreeting(0);
|
2019-10-20 12:39:01 +00:00
|
|
|
}
|
2020-07-20 21:39:31 +00:00
|
|
|
|
2021-07-02 20:29:33 +00:00
|
|
|
componentWillUnmount() {
|
2021-08-20 21:06:40 +00:00
|
|
|
EventBus.off('refresh');
|
2021-07-02 20:29:33 +00:00
|
|
|
}
|
|
|
|
|
2019-10-20 12:39:01 +00:00
|
|
|
render() {
|
2021-08-27 22:08:32 +00:00
|
|
|
return (
|
2022-04-08 13:48:36 +00:00
|
|
|
<span className="greeting" ref={this.greeting}>
|
2021-08-27 22:08:32 +00:00
|
|
|
{this.state.greeting}
|
2022-04-08 13:48:36 +00:00
|
|
|
</span>
|
2021-08-27 22:08:32 +00:00
|
|
|
);
|
2019-10-20 12:39:01 +00:00
|
|
|
}
|
2019-11-29 12:04:12 +00:00
|
|
|
}
|