28 lines
769 B
TypeScript
28 lines
769 B
TypeScript
import { useMessage } from "naive-ui";
|
|
|
|
const MIN_MS = 2500;
|
|
const MAX_MS = 8000;
|
|
const BASE_CHARS = 6;
|
|
const EXTRA_MS = 300;
|
|
|
|
export function toastDuration(text: string): number {
|
|
const extra = Math.max(0, Array.from(text).length - BASE_CHARS);
|
|
return Math.min(MAX_MS, MIN_MS + extra * EXTRA_MS);
|
|
}
|
|
|
|
export function useToast() {
|
|
const message = useMessage();
|
|
|
|
function show(type: "success" | "error" | "info" | "warning", text: string) {
|
|
return message[type](text, { duration: toastDuration(text), closable: true });
|
|
}
|
|
|
|
return {
|
|
show,
|
|
success: (text: string) => show("success", text),
|
|
error: (text: string) => show("error", text),
|
|
info: (text: string) => show("info", text),
|
|
warning: (text: string) => show("warning", text),
|
|
};
|
|
}
|