80 lines
1.9 KiB
TypeScript
80 lines
1.9 KiB
TypeScript
import { type ClassValue, clsx } from "clsx";
|
|
import { DateTime } from "luxon";
|
|
import type { NavigateFunction } from "react-router";
|
|
import { twMerge } from "tailwind-merge";
|
|
|
|
export function cn(...inputs: ClassValue[]) {
|
|
return twMerge(clsx(inputs));
|
|
}
|
|
|
|
/**
|
|
* === STRING UTILS ===
|
|
*/
|
|
export function capitalizeFirstLetter(str: string): string {
|
|
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
}
|
|
|
|
export function titleCase(str: string) {
|
|
return str.replace(
|
|
/\w\S*/g,
|
|
(text) => text.charAt(0).toUpperCase() + text.substring(1).toLowerCase()
|
|
);
|
|
}
|
|
|
|
/**
|
|
* === NAVIGATION UTILS ===
|
|
*/
|
|
// Force a reload of the page
|
|
export function forceReload() {
|
|
window.location.reload();
|
|
}
|
|
|
|
export function goBack(navigate: NavigateFunction) {
|
|
let canGoBack = false;
|
|
// Check if there is a history stack to go back to
|
|
try {
|
|
canGoBack = window.history.length > 1;
|
|
} catch (e) {
|
|
canGoBack = false;
|
|
}
|
|
if (!canGoBack) {
|
|
return navigate("/");
|
|
} else {
|
|
return navigate(-1);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* === COLLES UTILS ===
|
|
*/
|
|
export const formatDate = (date: string) => {
|
|
const dt = DateTime.fromISO(date).setLocale("fr");
|
|
const str = dt.toLocaleString({
|
|
weekday: "long",
|
|
day: "numeric",
|
|
month: "long",
|
|
year: "numeric",
|
|
});
|
|
return titleCase(str);
|
|
};
|
|
|
|
export const formatTime = (date: string) => {
|
|
const dt = DateTime.fromISO(date).setLocale("fr");
|
|
return dt.toLocaleString({
|
|
hour: "2-digit",
|
|
minute: "2-digit",
|
|
})?.replace(":", "h") || "N/A";
|
|
};
|
|
|
|
export const formatGrade = (grade?: number) => {
|
|
if (grade === undefined || grade === null || grade < 0 || grade > 20)
|
|
return "N/A";
|
|
|
|
const rounded = Math.round(grade * 10) / 10;
|
|
const str =
|
|
rounded % 1 === 0
|
|
? rounded.toFixed(0) // no decimals if .0
|
|
: rounded.toFixed(1); // one decimal otherwise
|
|
|
|
return str.replace(".", ",").padStart(2, "0"); // pad with zero if needed
|
|
};
|