1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/frontend/src/hooks/useToast.tsx
olav 3add10ccbe refactor: simplify error toast text (#1081)
* refactor: simplify error toast text

* refactor: simplify 404 error text
2022-06-09 15:36:01 +02:00

43 lines
1.1 KiB
TypeScript

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