-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathapp-context.tsx
More file actions
133 lines (118 loc) · 3.33 KB
/
Copy pathapp-context.tsx
File metadata and controls
133 lines (118 loc) · 3.33 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { WebSocketContext } from "@/context/ws-context";
import {
ACCESS_TOKEN,
disableGutter,
parseJwt,
retrieveSettings,
setWindowAlias,
} from "@/utils";
import type { FC, PropsWithChildren } from "react";
import {
createContext,
Dispatch,
SetStateAction,
useCallback,
useContext,
useEffect,
useState,
} from "react";
import { useTranslation } from "react-i18next";
import { useNavigate } from "react-router-dom";
import { toast } from "react-toastify";
export interface AppContextType {
isLoggedIn: boolean;
unit: Unit;
walletLocked: boolean;
isGeneratingReport: boolean;
toggleUnit: () => void;
setIsLoggedIn: Dispatch<SetStateAction<boolean>>;
logout: () => void;
setWalletLocked: Dispatch<SetStateAction<boolean>>;
setIsGeneratingReport: Dispatch<SetStateAction<boolean>>;
}
export enum Unit {
BTC = "BTC",
SAT = "SAT",
}
export const appContextDefault: AppContextType = {
isLoggedIn: false,
unit: Unit.SAT,
walletLocked: false,
isGeneratingReport: false,
toggleUnit: () => {},
setIsLoggedIn: () => {},
logout: () => {},
setWalletLocked: () => {},
setIsGeneratingReport: () => {},
};
export const AppContext = createContext<AppContextType>(appContextDefault);
const AppContextProvider: FC<PropsWithChildren> = ({ children }) => {
const { i18n } = useTranslation();
const { websocket } = useContext(WebSocketContext);
const [unit, setUnit] = useState<Unit>(Unit.SAT);
const [isLoggedIn, setIsLoggedIn] = useState(false);
const [walletLocked, setWalletLocked] = useState(false);
const [isGeneratingReport, setIsGeneratingReport] = useState(false);
const navigate = useNavigate();
const toggleUnitHandler = () => {
setUnit((prevUnit: Unit) => (prevUnit === Unit.SAT ? Unit.BTC : Unit.SAT));
};
const logoutHandler = useCallback(() => {
localStorage.removeItem(ACCESS_TOKEN);
// close EventSource on logout
if (websocket) {
websocket.close();
}
setIsLoggedIn(false);
disableGutter();
setWindowAlias(null);
toast.dismiss();
navigate("/");
}, [websocket, navigate]);
useEffect(() => {
const settings = retrieveSettings();
// check for settings in storage and set
if (settings) {
if (settings.lang) {
i18n.changeLanguage(settings.lang);
}
}
// if authenticated log in automatically
const token = localStorage.getItem(ACCESS_TOKEN);
if (token) {
try {
const payload = parseJwt(token);
if (payload.expires > Date.now()) {
setIsLoggedIn(true);
if (
window.location.pathname === "/" ||
window.location.pathname === "/login"
) {
return navigate("/home");
}
} else {
localStorage.removeItem(ACCESS_TOKEN);
console.info(`Token expired at ${payload.expires}.`);
}
} catch {
localStorage.removeItem(ACCESS_TOKEN);
console.info(`Token invalid - removed.`);
}
}
}, [i18n, navigate]);
const contextValue: AppContextType = {
isLoggedIn,
unit,
walletLocked,
isGeneratingReport,
toggleUnit: toggleUnitHandler,
setIsLoggedIn,
logout: logoutHandler,
setWalletLocked,
setIsGeneratingReport,
};
return (
<AppContext.Provider value={contextValue}>{children}</AppContext.Provider>
);
};
export default AppContextProvider;