feat: notificaciones toast con sileo (pnpm) + aviso en campana
This commit is contained in:
1 parent
73e6e96d73
commit
0a08fa5192
6 files changed
+301
-105
No files matched your search
@@ -0,0 +1,62 @@
|
||||
"use client";
|
||||
|
||||
import { sileo, type SileoOptions } from "sileo";
|
||||
|
||||
// Wrapper fino sobre sileo para unificar copy en español y defaults.
|
||||
// Uso: notify.success("Ticket creado"), notify.error("No se pudo guardar", err)
|
||||
const DEFAULT_DURATION = 5000;
|
||||
|
||||
function base(title: string, description?: string): SileoOptions {
|
||||
return { title, description, duration: DEFAULT_DURATION };
|
||||
}
|
||||
|
||||
export const notify = {
|
||||
success(title: string, description?: string, opts?: Partial<SileoOptions>) {
|
||||
return sileo.success({ ...base(title, description), ...opts });
|
||||
},
|
||||
error(title: string, description?: string, opts?: Partial<SileoOptions>) {
|
||||
return sileo.error({ ...base(title, description), ...opts });
|
||||
},
|
||||
warning(title: string, description?: string, opts?: Partial<SileoOptions>) {
|
||||
return sileo.warning({ ...base(title, description), ...opts });
|
||||
},
|
||||
info(title: string, description?: string, opts?: Partial<SileoOptions>) {
|
||||
return sileo.info({ ...base(title, description), ...opts });
|
||||
},
|
||||
action(
|
||||
title: string,
|
||||
buttonTitle: string,
|
||||
onClick: () => void,
|
||||
description?: string,
|
||||
opts?: Partial<SileoOptions>
|
||||
) {
|
||||
return sileo.action({
|
||||
...base(title, description),
|
||||
button: { title: buttonTitle, onClick },
|
||||
...opts,
|
||||
});
|
||||
},
|
||||
promise<T>(
|
||||
promise: Promise<T> | (() => Promise<T>),
|
||||
messages: {
|
||||
loading: string;
|
||||
success: string | ((data: T) => string);
|
||||
error: string | ((err: unknown) => string);
|
||||
}
|
||||
) {
|
||||
const { loading, success, error } = messages;
|
||||
return sileo.promise(promise, {
|
||||
loading: { title: loading },
|
||||
success:
|
||||
typeof success === "function"
|
||||
? (data: T) => ({ title: (success as (d: T) => string)(data) })
|
||||
: { title: success },
|
||||
error:
|
||||
typeof error === "function"
|
||||
? (err: unknown) => ({ title: (error as (e: unknown) => string)(err) })
|
||||
: { title: error },
|
||||
});
|
||||
},
|
||||
dismiss: sileo.dismiss,
|
||||
clear: sileo.clear,
|
||||
};
|
||||
Reference in new issue
Block a user