This commit is contained in:
2024-06-28 09:21:03 +08:00
parent 57ecc2ef87
commit a3a6fab668
7 changed files with 55 additions and 10 deletions

View File

@@ -1,3 +1,6 @@
import { AxiosError } from "axios";
import toast from "react-hot-toast";
export function getTimeOfDay(): number {
const currentHour = new Date().getHours();
@@ -9,3 +12,30 @@ export function getTimeOfDay(): number {
return 2; // Evening
}
}
export const popToast = (message: string, type: number) => {
const words = message.split(" ");
const duration = Math.max(4, words.length * 1);
toast(message, {
duration: duration * 1000, // Convert to milliseconds
position: "top-center",
// Custom Icon
icon: type === 0 ? "" : type === 1 ? "✅" : type === 2 ? "❌" : undefined,
// Aria
ariaProps: {
role: "status",
"aria-live": "polite",
},
});
};
export const popErrorToast = (error: any) => {
try {
popToast(((error as AxiosError).response?.data as any).message, 2);
} catch {
popToast("An unexpected error occured!\nPlease try again later.", 2);
}
};