dashboard/src/components/greeter.tsx

140 lines
3.3 KiB
TypeScript
Raw Normal View History

2020-07-08 19:36:36 +02:00
import styled from "styled-components";
const GreeterContainer = styled.div`
padding: 2rem 0;
`;
const GreetText = styled.h1`
font-weight: 900;
font-size: 3rem;
margin: 0.5rem 0;
2021-07-10 23:57:08 +02:00
color: ${(props) => props.theme.mainColor};
2020-07-08 19:36:36 +02:00
`;
const DateText = styled.h3`
font-weight: 400;
font-size: 1rem;
text-transform: uppercase;
margin: 0;
2021-07-10 23:57:08 +02:00
color: ${(props) => props.theme.accentColor};
2020-07-08 19:36:36 +02:00
`;
2021-06-14 13:17:31 +02:00
export interface IGreeterComponentProps {
2022-02-13 20:57:26 +01:00
greeter?: IGreeterDataProps;
}
export interface IGreeterDataProps {
greeter: IGreeterProps;
2021-06-14 13:17:31 +02:00
}
2021-04-03 16:54:44 +02:00
export interface IGreeterProps {
months: Array<string>;
days: Array<string>;
greetings: Array<IGreetingProps>;
dateformat: string;
}
interface IGreetingProps {
greeting: string;
start: number;
end: number;
}
/**
2021-06-14 11:29:03 +02:00
* Checks if a number is between two numbers
* @param {number} a number that's supposed to be checked
* @param {number} b minimum
* @param {number} c maximum
2021-04-03 16:54:44 +02:00
*/
2021-06-14 13:17:31 +02:00
export const isBetween = (a: number, b: number, c: number): boolean =>
2021-05-15 16:09:12 +02:00
a >= b && a <= c;
2021-03-21 19:59:18 +01:00
/**
* Returns a greeting based on the current time
2021-06-14 11:29:03 +02:00
* @param {Array<IGreetingProps>} greetings a list of greetings with start and end date
* @returns {string} a greeting
2021-03-21 19:59:18 +01:00
*/
2021-06-14 13:17:31 +02:00
export const getGreeting = (greetings: Array<IGreetingProps>): string => {
2022-04-10 21:31:40 +02:00
const hours = Math.floor(new Date().getHours());
2021-04-03 16:54:44 +02:00
let result = "";
2021-05-15 16:09:12 +02:00
greetings.forEach((greeting) => {
if (isBetween(hours, greeting.start, greeting.end))
result = greeting.greeting;
});
2021-04-03 16:54:44 +02:00
return result;
2020-07-08 19:36:36 +02:00
};
2021-03-21 19:59:18 +01:00
/**
* Returns the appropriate extension for a number (eg. 'rd' for '3' to make '3rd')
2021-06-14 11:29:03 +02:00
* @param {number} day number of a day within a month
* @returns {string} extension for that number
2021-03-21 19:59:18 +01:00
*/
2021-06-14 13:17:31 +02:00
export const getExtension = (day: number) => {
2021-07-11 15:58:42 +02:00
let extension = "th";
2020-07-08 19:36:36 +02:00
2021-11-18 15:46:39 +01:00
if (day % 10 === 1 && day !== 11) {
2020-07-08 19:36:36 +02:00
extension = "st";
2021-11-18 15:46:39 +01:00
} else if (day % 10 === 2 && day !== 12) {
2020-07-08 19:36:36 +02:00
extension = "nd";
2021-11-18 15:46:39 +01:00
} else if (day % 10 === 3 && day !== 13) {
2020-07-08 19:36:36 +02:00
extension = "rd";
}
return extension;
};
2021-03-21 19:59:18 +01:00
/**
* Generates the current date
2021-06-14 11:29:03 +02:00
* @param {string} format format of the date string
* @returns {string} current date as a string
2021-03-21 19:59:18 +01:00
*/
2021-06-14 13:17:31 +02:00
export const getDateString = (
2021-05-15 16:09:12 +02:00
weekdays: Array<string>,
months: Array<string>,
2021-06-14 11:29:03 +02:00
format: string,
2022-04-20 12:29:00 +02:00
date?: Date,
2021-05-15 16:09:12 +02:00
) => {
2022-04-20 12:29:00 +02:00
const currentDate = date ? date : new Date();
2022-04-10 21:31:40 +02:00
const weekday = weekdays[currentDate.getDay()];
const day = currentDate.getDate();
const month = months[currentDate.getMonth()];
const extension = getExtension(day);
const year = currentDate.getFullYear();
const isodate = currentDate.toISOString().slice(0, 10);
2021-04-03 16:54:44 +02:00
2021-05-15 16:09:12 +02:00
return format
.replace("%wd", weekday)
.replace("%d", day.toString())
.replace("%e", extension)
.replace("%m", month)
2021-09-17 17:51:26 +02:00
.replace("%y", year.toString())
.replace("%isodate", isodate);
2020-07-08 19:36:36 +02:00
};
2021-03-21 19:59:18 +01:00
/**
* Renders the Greeter
2021-06-14 11:29:03 +02:00
* @param {IGreeterComponentProps} data required greeter data
* @returns {React.ReactNode} the greeter
2021-03-21 19:59:18 +01:00
*/
2022-02-13 20:57:26 +01:00
const Greeter = ({ greeter }: IGreeterComponentProps) => {
if (greeter === undefined) return <></>;
return (
<GreeterContainer>
<DateText>
{getDateString(
greeter.greeter.days,
greeter.greeter.months,
greeter.greeter.dateformat,
)}
</DateText>
<GreetText>{getGreeting(greeter.greeter.greetings)}</GreetText>
</GreeterContainer>
);
};
2020-07-08 19:36:36 +02:00
export default Greeter;