mirror of
https://github.com/StanGirard/quivr.git
synced 2024-12-24 03:41:56 +03:00
33 lines
826 B
TypeScript
33 lines
826 B
TypeScript
|
import { useState } from "react";
|
||
|
import { ToastContent, ToastData, ToastPublisher } from "../domain/types";
|
||
|
import { generateToastUniqueId } from "../helpers/generateToastUniqueId";
|
||
|
|
||
|
// ⚠️ You should not probably import this. use `useToast` instead
|
||
|
export const useToastBuilder = () => {
|
||
|
const [toasts, setToasts] = useState<ToastContent[]>([]);
|
||
|
|
||
|
const toggleToast = (value: boolean, index: number) => {
|
||
|
setToasts((toasts) =>
|
||
|
toasts.map((toast, i) => {
|
||
|
if (i === index) {
|
||
|
toast.open = value;
|
||
|
}
|
||
|
return toast;
|
||
|
})
|
||
|
);
|
||
|
};
|
||
|
|
||
|
const publish: ToastPublisher = (newTost: ToastData) => {
|
||
|
setToasts((toasts) => [
|
||
|
...toasts,
|
||
|
{ ...newTost, open: true, id: generateToastUniqueId() },
|
||
|
]);
|
||
|
};
|
||
|
|
||
|
return {
|
||
|
publish,
|
||
|
toggleToast,
|
||
|
toasts,
|
||
|
};
|
||
|
};
|