1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-05-08 01:15:49 +02:00
unleash.unleash/frontend/src/hooks/useToast.tsx
olav 010f766de9 refactor: replace ts-ignore with ts-expect-error (#681)
* refactor: replace ts-ignore with ts-expect-error

* refactor: remove unused ts-expect-errors
2022-02-09 13:39:18 +01:00

55 lines
1.4 KiB
TypeScript

import { Dispatch, SetStateAction, useContext } from 'react';
import UIContext, { IToastData } from '../contexts/UIContext';
export interface IToast {
show: boolean;
type: 'success' | 'info' | 'warning' | 'error';
text: string;
}
export type TSetToastData = Dispatch<SetStateAction<IToast>>;
interface IToastOptions {
title: string;
text?: string;
type: string;
persist?: boolean;
confetti?: boolean;
autoHideDuration?: number;
show?: boolean;
}
const useToast = () => {
// @ts-expect-error
const { setToast } = useContext(UIContext);
const hideToast = () =>
setToast((prev: IToastData) => ({
...prev,
show: false,
}));
const setToastApiError = (errorText: string, overrides?: IToastOptions) => {
setToast({
title: 'Something went wrong',
text: `We had trouble talking to our API. Here's why: ${errorText}`,
type: 'error',
show: true,
autoHideDuration: 6000,
...overrides,
});
};
const setToastData = (options: IToastOptions) => {
if (options.persist) {
setToast({ ...options, show: true });
} else {
setToast({ ...options, show: true, autoHideDuration: 6000 });
}
};
return { setToastData, setToastApiError, hideToast };
};
export default useToast;