diff options
author | Eric Bailey <git@esb.lol> | 2024-05-07 20:25:58 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-07 20:25:58 -0500 |
commit | 165fdb704959837d244eebf12f61803676b15366 (patch) | |
tree | d3d2e28631ddd643bd05fe46d63792b5a661ac7b /src/state/messages/current-convo-id.tsx | |
parent | 37f22ca2246f81e17be113f05aeeb4cd1a886b1b (diff) | |
download | voidsky-165fdb704959837d244eebf12f61803676b15366.tar.zst |
[🐴] Integrate global event bus (#3904)
* Conditionally run global event bus * Add current convo id context, bundle providers
Diffstat (limited to 'src/state/messages/current-convo-id.tsx')
-rw-r--r-- | src/state/messages/current-convo-id.tsx | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/state/messages/current-convo-id.tsx b/src/state/messages/current-convo-id.tsx new file mode 100644 index 000000000..69133a371 --- /dev/null +++ b/src/state/messages/current-convo-id.tsx @@ -0,0 +1,38 @@ +import React from 'react' + +const CurrentConvoIdContext = React.createContext<{ + currentConvoId: string | undefined + setCurrentConvoId: (convoId: string | undefined) => void +}>({ + currentConvoId: undefined, + setCurrentConvoId: () => {}, +}) + +export function useCurrentConvoId() { + const ctx = React.useContext(CurrentConvoIdContext) + if (!ctx) { + throw new Error( + 'useCurrentConvoId must be used within a CurrentConvoIdProvider', + ) + } + return ctx +} + +export function CurrentConvoIdProvider({ + children, +}: { + children: React.ReactNode +}) { + const [currentConvoId, setCurrentConvoId] = React.useState< + string | undefined + >() + const ctx = React.useMemo( + () => ({currentConvoId, setCurrentConvoId}), + [currentConvoId], + ) + return ( + <CurrentConvoIdContext.Provider value={ctx}> + {children} + </CurrentConvoIdContext.Provider> + ) +} |