Openstatus www.openstatus.dev
at main 72 lines 2.0 kB view raw
1import type { ExternalToast } from "sonner"; 2import { toast } from "sonner"; 3 4type ToastType = 5 | "default" 6 | "description" 7 | "success" 8 | "warning" 9 | "info" 10 | "error" 11 | "promise"; 12 13const config = { 14 error: { 15 type: "error", 16 title: "Something went wrong", 17 description: "Please try again", 18 action: { 19 label: "Discord", 20 onClick: () => { 21 if (typeof window === "undefined") return; 22 window.open("/discord", "_blank"); 23 }, 24 }, 25 }, 26 "unique-slug": { 27 type: "warning", 28 title: "Slug is already taken", 29 description: "Please select another slug. Every slug is unique.", 30 }, 31 success: { type: "success", title: "Success" }, 32 deleted: { type: "success", title: "Deleted successfully" }, // TODO: we are not informing the user besides the visual changes when an entry has been deleted 33 removed: { type: "success", title: "Removed successfully" }, 34 saved: { type: "success", title: "Saved successfully" }, 35 "test-error": { 36 type: "error", 37 title: "Connection Failed", 38 description: "Please enter a correct URL", 39 }, 40 "test-warning-empty-url": { 41 type: "warning", 42 title: "URL is Empty", 43 description: "Please enter a valid, non-empty URL", 44 }, 45 "test-success": { 46 type: "success", 47 title: "Connection Established", 48 }, 49} as const; 50 51const _config: Record< 52 string, 53 Pick<ExternalToast, "action" | "description"> & { 54 type: ToastType; 55 title: string; 56 } 57> = config; 58 59type ToastAction = keyof typeof config; 60 61export function toastAction(action: ToastAction) { 62 const { title, type, ...props } = _config[action]; 63 64 if (type === "default") return toast(title, props); 65 if (type === "success") return toast.success(title, props); 66 if (type === "error") return toast.error(title, props); 67 if (type === "warning") return toast.warning(title, props); 68 if (type === "description") return toast.message(title, props); 69 if (type === "info") return toast.info(title, props); 70} 71 72export { toast };