Skip to content

Commit 5cafe09

Browse files
authored
feat(client): Toast Provider to show Toasts from higher on the DOM tree (#1110)
1 parent 81a90d2 commit 5cafe09

File tree

5 files changed

+34
-11
lines changed

5 files changed

+34
-11
lines changed

client/src/App.jsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { RouterProvider } from 'react-router-dom';
44
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
55
import { QueryClient, QueryClientProvider, QueryCache } from '@tanstack/react-query';
66
import { ScreenshotProvider, ThemeProvider, useApiErrorBoundary } from './hooks';
7+
import { ToastProvider } from './Providers';
78
import Toast from './components/ui/Toast';
89
import { router } from './routes';
910

@@ -25,10 +26,12 @@ const App = () => {
2526
<RecoilRoot>
2627
<ThemeProvider>
2728
<RadixToast.Provider>
28-
<RouterProvider router={router} />
29-
<ReactQueryDevtools initialIsOpen={false} position="top-right" />
30-
<Toast />
31-
<RadixToast.Viewport className="pointer-events-none fixed inset-0 z-[60] mx-auto my-2 flex max-w-[560px] flex-col items-stretch justify-start md:pb-5" />
29+
<ToastProvider>
30+
<RouterProvider router={router} />
31+
<ReactQueryDevtools initialIsOpen={false} position="top-right" />
32+
<Toast />
33+
<RadixToast.Viewport className="pointer-events-none fixed inset-0 z-[60] mx-auto my-2 flex max-w-[560px] flex-col items-stretch justify-start md:pb-5" />
34+
</ToastProvider>
3235
</RadixToast.Provider>
3336
</ThemeProvider>
3437
</RecoilRoot>

client/src/Providers/ToastContext.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { createContext } from 'react';
2+
import type { TShowToast } from '~/common';
3+
import { useToast } from '~/hooks';
4+
5+
type ToastContextType = {
6+
showToast: ({ message, severity, showIcon }: TShowToast) => void;
7+
};
8+
9+
export const ToastContext = createContext<ToastContextType>({
10+
showToast: () => ({}),
11+
});
12+
13+
export default function ToastProvider({ children }) {
14+
const { showToast } = useToast();
15+
16+
return <ToastContext.Provider value={{ showToast }}>{children}</ToastContext.Provider>;
17+
}

client/src/Providers/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { default as ToastProvider } from './ToastContext';
2+
export * from './ToastContext';

client/src/common/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ export enum NotificationSeverity {
2828
ERROR = 'error',
2929
}
3030

31+
export type TShowToast = {
32+
message: string;
33+
severity?: NotificationSeverity;
34+
showIcon?: boolean;
35+
};
36+
3137
export type TBaseSettingsProps = {
3238
conversation: TConversation | TPreset | null;
3339
className?: string;

client/src/hooks/useToast.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { useRef, useEffect } from 'react';
21
import { useRecoilState } from 'recoil';
2+
import { useRef, useEffect } from 'react';
3+
import type { TShowToast } from '~/common';
34
import { NotificationSeverity } from '~/common';
45
import store from '~/store';
56

@@ -15,12 +16,6 @@ export default function useToast(timeoutDuration = 100) {
1516
};
1617
}, []);
1718

18-
type TShowToast = {
19-
message: string;
20-
severity?: NotificationSeverity;
21-
showIcon?: boolean;
22-
};
23-
2419
const showToast = ({
2520
message,
2621
severity = NotificationSeverity.SUCCESS,

0 commit comments

Comments
 (0)