forked from SmartDropLabs/smartdrop-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorContext.tsx
More file actions
50 lines (41 loc) · 1.13 KB
/
Copy pathErrorContext.tsx
File metadata and controls
50 lines (41 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* Error context and provider for app-wide error handling.
* Provides access to the error handler and sets up global error listeners.
*/
"use client";
import { useToast, type UseToastReturn } from "@/hooks/useToast";
import { setupGlobalErrorHandlers } from "@/lib/error-handler";
import {
createContext,
useContext,
useEffect,
type ReactNode,
} from "react";
interface ErrorContextValue {
toast: UseToastReturn;
}
const ErrorContext = createContext<ErrorContextValue | null>(null);
export function ErrorProvider({ children }: { children: ReactNode }) {
const toast = useToast();
useEffect(() => {
// Set up global error handlers
const cleanup = setupGlobalErrorHandlers();
// Cleanup on unmount
return cleanup;
}, []);
return (
<ErrorContext.Provider value={{ toast }}>
{children}
</ErrorContext.Provider>
);
}
/**
* Hook to access the error handler from anywhere in the app.
*/
export function useErrorHandler() {
const context = useContext(ErrorContext);
if (!context) {
throw new Error("useErrorHandler must be used within ErrorProvider");
}
return context.toast;
}