-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
179 lines (166 loc) · 3.82 KB
/
index.js
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* eslint-disable unicorn/no-null */
/* eslint-disable unicorn/no-useless-undefined */
import { createContext, createElement } from 'preact'
import {
useCallback,
useContext,
useEffect,
useMemo,
useState,
} from 'preact/hooks'
import * as odd from '@oddjs/odd'
/** @type {import('preact').Context<import('./types.js').OddContext>} */
// @ts-ignore - TODO fix this
const OddContext = createContext({
isLoading: true,
error: undefined,
session: null,
program: undefined,
login: undefined,
logout: undefined,
register: undefined,
})
/**
*
* @param {import('./types.js').OddContextProviderProps & {children : import('preact').ComponentChildren}} props
* @returns
*/
export function OddContextProvider({
components,
componentsFactory,
config,
children,
}) {
// State
const [error, setError] = useState(
/** @type {odd.ProgramError |undefined} */ (undefined)
)
const [program, setProgram] = useState(
/** @type {odd.Program | undefined} */ (undefined)
)
const [session, setSession] = useState(
/** @type {import('@oddjs/odd').Session | null} */ (null)
)
// Effects
useEffect(() => {
let mounted = true
async function setup() {
if (mounted) {
try {
let comps
if (components) {
comps = components
}
if (componentsFactory) {
comps = await componentsFactory(config)
}
const program = await odd.program({
...config,
...comps,
})
setProgram(program)
setSession(program.session)
} catch (error) {
setError(/** @type {odd.ProgramError} */ (error))
}
}
}
setup()
return () => {
mounted = false
}
}, [components, componentsFactory, config])
const login = useCallback(
async (/** @type {string | undefined} */ username) => {
if (!program) {
throw new Error('Needs program.')
}
// @ts-ignore
await program.auth.register({ username })
const session = await program.auth.session()
if (session) {
setSession(session)
return session
}
},
[program]
)
const logout = useCallback(
async (/** @type {string | undefined} */ username) => {
if (!program) {
throw new Error('Needs program.')
}
if (session) {
await session.destroy()
setSession(null)
}
},
[program, session]
)
/** @type {import('./types.js').OddContext} */
const value = useMemo(() => {
if (!program) {
return {
isLoading: true,
error: undefined,
session: null,
program: undefined,
register: login,
login,
logout,
}
}
if (error) {
return {
isLoading: false,
error,
session: null,
program: undefined,
register: login,
login,
logout,
}
}
if (program) {
return {
isLoading: false,
error,
session,
program,
register: login,
login,
logout,
}
}
return {
isLoading: false,
error: undefined,
session,
program,
register: login,
login,
logout,
}
}, [program, error, session, login, logout])
return createElement(OddContext.Provider, { value, children })
}
/**
* Hook to access the ODD program and session from the `OddContextProvider`.
*
* @example
* ```jsx
* // home.jsx
* import { useOddContext } from '@oddjs/preact'
*
export default function Home(props) {
const { session, isLoading, login, logout, register } = useOddContext()
}
* ```
*/
export function useOddContext() {
const context = useContext(OddContext)
if (context === undefined) {
throw new Error(`useOddContext must be used within a OddContextProvider.`)
}
return context
}