forked from microsoft/github-copilot-vibe-coding-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppContext.jsx
More file actions
59 lines (49 loc) · 1.33 KB
/
Copy pathAppContext.jsx
File metadata and controls
59 lines (49 loc) · 1.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
import { createContext, useContext, useState } from 'react';
const AppContext = createContext();
export const useApp = () => {
const context = useContext(AppContext);
if (!context) {
throw new Error('useApp must be used within an AppProvider');
}
return context;
};
export const AppProvider = ({ children }) => {
const [error, setError] = useState(null);
const [loading, setLoading] = useState(false);
const [serverStatus, setServerStatus] = useState('unknown'); // 'online', 'offline', 'unknown'
const handleError = (error) => {
console.error('App Error:', error);
setError(error.message || 'An unexpected error occurred');
// Check if it's a server connectivity issue
if (error.message && error.message.includes('unavailable')) {
setServerStatus('offline');
}
};
const clearError = () => {
setError(null);
};
const setServerOnline = () => {
setServerStatus('online');
if (error && error.includes('unavailable')) {
clearError();
}
};
const setServerOffline = () => {
setServerStatus('offline');
};
const value = {
error,
loading,
serverStatus,
setLoading,
handleError,
clearError,
setServerOnline,
setServerOffline,
};
return (
<AppContext.Provider value={value}>
{children}
</AppContext.Provider>
);
};