1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-10-13 11:17:26 +02:00
unleash.unleash/frontend/src/hooks/useToast.tsx
Gastón Fournier abe160eb7d
feat: Unleash v7 ESM migration (#9877)
We're migrating to ESM, which will allow us to import the latest
versions of our dependencies.

Co-Authored-By: Christopher Kolstad <chriswk@getunleash.io>
2025-05-14 09:47:12 +02:00

42 lines
1.1 KiB
TypeScript

import { useCallback, useContext } from 'react';
import UIContext from '../contexts/UIContext.ts';
import type { IToast } from '../interfaces/toast.ts';
const useToast = () => {
const { setToast } = useContext(UIContext);
const hideToast = () =>
setToast((prev: IToast) => ({
...prev,
show: false,
}));
const setToastApiError = useCallback(
(text: string, overrides?: IToast) => {
setToast({
text: text || 'Something went wrong.',
type: 'error',
show: true,
autoHideDuration: 12000,
...overrides,
});
},
[setToast],
);
const setToastData = useCallback(
(toast: IToast) => {
if (toast.persist) {
setToast({ ...toast, show: true });
} else {
setToast({ autoHideDuration: 6000, ...toast, show: true });
}
},
[setToast],
);
return { setToastData, setToastApiError, hideToast };
};
export default useToast;