dashboard/src/components/greeter.tsx

120 lines
2.2 KiB
TypeScript
Raw Normal View History

2020-07-08 19:36:36 +02:00
import React from "react";
import styled from "styled-components";
2021-03-21 18:05:24 +01:00
import selectedTheme from "../lib/theme";
2020-07-08 19:36:36 +02:00
const GreeterContainer = styled.div`
padding: 2rem 0;
`;
const GreetText = styled.h1`
font-weight: 900;
font-size: 3rem;
margin: 0.5rem 0;
color: ${selectedTheme.mainColor};
`;
const DateText = styled.h3`
font-weight: 400;
font-size: 1rem;
text-transform: uppercase;
margin: 0;
color: ${selectedTheme.accentColor};
`;
2021-03-21 19:59:18 +01:00
const monthNames = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
const weekDayNames = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
/**
* Returns a greeting based on the current time
* @returns {string} - A greeting
*/
2020-07-08 19:36:36 +02:00
const getGreeting = () => {
switch (Math.floor(new Date().getHours() / 6)) {
case 0:
2020-07-10 00:04:47 +02:00
return "Good night!";
2020-07-08 19:36:36 +02:00
case 1:
return "Good morning!";
case 2:
return "Good afternoon!";
case 3:
return "Good evening!";
default:
break;
}
};
2021-03-21 19:59:18 +01:00
/**
* Returns the appropriate extension for a number (eg. 'rd' for '3' to make '3rd')
* @param {number} day - The number of a day within a month
* @returns {string} - The extension for that number
*/
2020-07-08 19:36:36 +02:00
const getExtension = (day: number) => {
let extension = "";
if ((day > 4 && day <= 20) || (day > 20 && day % 10 >= 4)) {
extension = "th";
} else if (day % 10 === 1) {
extension = "st";
} else if (day % 10 === 2) {
extension = "nd";
} else if (day % 10 === 3) {
extension = "rd";
}
return extension;
};
2021-03-21 19:59:18 +01:00
/**
* Generates the current date
* @returns {string} - The current date as a string
*/
2020-07-08 19:36:36 +02:00
const getDateString = () => {
let currentDate = new Date();
return (
weekDayNames[currentDate.getUTCDay()] +
", " +
monthNames[currentDate.getUTCMonth()] +
" " +
currentDate.getDate() +
getExtension(currentDate.getDate()) +
" " +
currentDate.getFullYear()
);
};
2021-03-21 19:59:18 +01:00
/**
* Renders the Greeter
*/
2021-03-21 20:19:44 +01:00
const Greeter = () => (
<GreeterContainer>
<DateText>{getDateString()}</DateText>
<GreetText>{getGreeting()}</GreetText>
</GreeterContainer>
);
2020-07-08 19:36:36 +02:00
export default Greeter;