-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathsessionContext.tsx
More file actions
97 lines (86 loc) · 2.9 KB
/
sessionContext.tsx
File metadata and controls
97 lines (86 loc) · 2.9 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
/**
* @license
* Copyright 2019-2020 CERN and copyright holders of ALICE O2.
* See http://alice-o2.web.cern.ch/copyright for details of the copyright holders.
* All rights not expressly granted are reserved.
*
* This software is distributed under the terms of the GNU General Public
* License v3 (GPL Version 3), copied verbatim in the file "COPYING".
*
* In applying this license CERN does not waive the privileges and immunities
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/
import React, { createContext, useState, useEffect, useMemo, useCallback } from 'react';
import { useLocation, useNavigate } from 'react-router';
interface Session {
personid: string | null;
name: string | null;
token: string | null;
username: string | null;
access: string[] | null;
}
type SessionKey = keyof Session;
const defaultSession = {
personid: null,
name: null,
token: null,
username: null,
access: null,
};
// List ["personid", "name", "token", ...]
const sessionKeys = Object.keys(defaultSession) as SessionKey[];
interface SessionContextType {
session: Session;
hasAccess: (role: string) => boolean;
}
/**
* React context for managing user session state.
* Provides session data and access control functionality.
*/
export const SessionContext = createContext<SessionContextType>({
session: defaultSession,
hasAccess: () => false,
});
/**
* Session provider component that manages user session state.
*
* Automatically extracts session data from URL parameters on mount and
* provides session context to child components.
*
* @param children - React components that need access to session context
*
* @example
* ```tsx
* <SessionProvider>
* <App />
* </SessionProvider>
* ```
*/
export const SessionProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
const [session, setSession] = useState<Session>(defaultSession);
const location = useLocation();
const navigate = useNavigate();
useEffect(() => {
let sessionLoad: Session = { ...defaultSession };
const params = new URLSearchParams(location.search);
for (const sessionKey of sessionKeys) {
const value = params.get(sessionKey);
if (value && sessionKey === 'access') {
sessionLoad = { ...sessionLoad, [sessionKey]: value.split(',') };
} else if (value) {
sessionLoad = { ...sessionLoad, [sessionKey]: value };
}
}
setSession(sessionLoad);
navigate(location.pathname, { replace: true });
// It should run only once when we start page
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const hasAccess = useCallback(
(role: string) => session.access?.includes(role) ?? false,
[session],
);
const value = useMemo(() => ({ session, hasAccess }), [session, hasAccess]);
return <SessionContext.Provider value={value}>{children}</SessionContext.Provider>;
};