mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import React from 'react'
2
3const CurrentConvoIdContext = React.createContext<{
4 currentConvoId: string | undefined
5 setCurrentConvoId: (convoId: string | undefined) => void
6}>({
7 currentConvoId: undefined,
8 setCurrentConvoId: () => {},
9})
10
11export function useCurrentConvoId() {
12 const ctx = React.useContext(CurrentConvoIdContext)
13 if (!ctx) {
14 throw new Error(
15 'useCurrentConvoId must be used within a CurrentConvoIdProvider',
16 )
17 }
18 return ctx
19}
20
21export function CurrentConvoIdProvider({
22 children,
23}: {
24 children: React.ReactNode
25}) {
26 const [currentConvoId, setCurrentConvoId] = React.useState<
27 string | undefined
28 >()
29 const ctx = React.useMemo(
30 () => ({currentConvoId, setCurrentConvoId}),
31 [currentConvoId],
32 )
33 return (
34 <CurrentConvoIdContext.Provider value={ctx}>
35 {children}
36 </CurrentConvoIdContext.Provider>
37 )
38}