-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmagicProvider.tsx
More file actions
50 lines (39 loc) · 1011 Bytes
/
magicProvider.tsx
File metadata and controls
50 lines (39 loc) · 1011 Bytes
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
"use client"
import {
createContext,
ReactNode,
useContext,
useEffect,
useMemo,
useState,
} from "react"
import { Magic as MagicBase } from "magic-sdk"
export type Magic = MagicBase<OAuthExtension[]>
type MagicContextType = {
magic: Magic | null
}
const MagicContext = createContext<MagicContextType>({
magic: null,
})
export const useMagic = () => useContext(MagicContext)
const MagicProvider = ({ children }: { children: ReactNode }) => {
const [magic, setMagic] = useState<Magic | null>(null)
useEffect(() => {
if (process.env.NEXT_PUBLIC_MAGIC_API_KEY) {
const magic = new MagicBase(process.env.NEXT_PUBLIC_MAGIC_API_KEY, {
network: {
rpcUrl: "https://rpc2.sepolia.org/",
chainId: 11155111,
},
})
setMagic(magic)
}
}, [])
const value = useMemo(() => {
return {
magic,
}
}, [magic])
return <MagicContext.Provider value={value}>{children}</MagicContext.Provider>
}
export default MagicProvider