-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppContext.tsx
73 lines (59 loc) · 2.02 KB
/
AppContext.tsx
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { ReactComponentElement, createContext, useContext, useEffect, useState } from "react";
export interface IStackedView<T = unknown> {
id: number;
title: String,
action?: ReactComponentElement<any, any>;
content: ReactComponentElement<any, any>;
props: T;
// Add any other properties your stacked views may have
}
export type PopupAction = {
type: "close" | "submit";
payload?: any;
}
export type AppContextType<T = unknown> = {
currentPopup: IStackedView<T> | null;
stackedViews: IStackedView<T>[];
addStackedView: (view: IStackedView<T>) => void;
removeStackedView: (id: number) => void;
dispatchPopupAction: (action: PopupAction) => void;
lastPopupAction: PopupAction | null;
};
export const AppContext = createContext<AppContextType | undefined>(undefined);
export function AppContextProvider({ children }) {
const [stackedViews, setStackedViews] = useState<IStackedView[]>([]);
const [currentPopup, setCurrentPopup] = useState<IStackedView | null>(null);
const [lastPopupAction, setLastPopupAction] = useState<PopupAction | null>(null);
useEffect(() => {
if (stackedViews.length > 0) {
setCurrentPopup(stackedViews[stackedViews.length - 1]);
} else {
setCurrentPopup(null);
}
}, [stackedViews])
const addStackedView = (view: IStackedView) => {
setStackedViews((prevViews) => [...prevViews, view]);
};
const removeStackedView = (id: number) => {
setStackedViews((prevViews) => prevViews.filter((view) => view.id !== id));
};
const dispatchPopupAction = (action: PopupAction) => {
setLastPopupAction(action);
}
const value: AppContextType = {
currentPopup,
stackedViews,
addStackedView,
removeStackedView,
dispatchPopupAction,
lastPopupAction,
};
return <AppContext.Provider value={value}>{children}</AppContext.Provider>;
}
export const useAppContext = () => {
const context = useContext(AppContext);
if (!context) {
throw new Error('useAppContext must be used within a AppContextProvider');
}
return context;
};