mirror of
https://github.com/Unleash/unleash.git
synced 2025-09-19 17:52:45 +02:00
* feat: add create and edit project screen * feat: fix correct permission and validate projectId * feat: remove unused variable and logs * feat: remove unused import * fix: delete unused project components * fix: add unique validation * fix: add unused import * fix: project header Co-authored-by: Fredrik Oseberg <fredrik.no@gmail.com>
55 lines
1.4 KiB
TypeScript
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-ignore
|
|
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;
|