File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed
packages/react/src/context Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change 1+ import * as React from 'react' ;
2+
3+ import { UseSessionReturn } from '../hooks/useSession' ;
4+
5+ /** @internal */
6+ export const SessionContext = React . createContext < UseSessionReturn | undefined > ( undefined ) ;
7+
8+ /**
9+ * Ensures that a session is provided via context.
10+ * If no session is provided, an error is thrown.
11+ * @public
12+ */
13+ export function useSessionContext ( ) {
14+ const ctx = React . useContext ( SessionContext ) ;
15+ if ( ! ctx ) {
16+ throw Error ( 'tried to access session context outside of SessionProvider component' ) ;
17+ }
18+ return ctx ;
19+ }
20+
21+ /**
22+ * Returns the session context if it exists, otherwise undefined.
23+ * @public
24+ */
25+ export function useMaybeSessionContext ( ) {
26+ return React . useContext ( SessionContext ) ;
27+ }
28+
29+ /**
30+ * Ensures that a session is provided, either via context or explicitly as a parameter.
31+ * If no session is provided, an error is thrown.
32+ * @public
33+ */
34+ export function useEnsureSession ( session ?: UseSessionReturn ) {
35+ const context = useMaybeSessionContext ( ) ;
36+ const r = session ?? context ;
37+ if ( ! r ) {
38+ throw new Error (
39+ 'No session provided, make sure you are inside a Session context or pass the session explicitly' ,
40+ ) ;
41+ }
42+ return r ;
43+ }
You can’t perform that action at this time.
0 commit comments