Live video on the AT Protocol
1import { SessionManager } from "@atproto/api/dist/session-manager";
2import { useEffect, useRef } from "react";
3import { useGetChatProfile } from "../streamplace-store";
4import { makeStreamplaceStore } from "../streamplace-store/streamplace-store";
5import { StreamplaceContext } from "./context";
6import Poller from "./poller";
7
8export function StreamplaceProvider({
9 children,
10 url,
11 oauthSession,
12}: {
13 children: React.ReactNode;
14 url: string;
15 oauthSession?: SessionManager | null;
16}) {
17 // todo: handle url changes?
18 const store = useRef(makeStreamplaceStore({ url })).current;
19
20 useEffect(() => {
21 store.setState({ url });
22 }, [url]);
23
24 useEffect(() => {
25 store.setState({ oauthSession });
26 }, [oauthSession]);
27
28 return (
29 <StreamplaceContext.Provider value={{ store: store }}>
30 <ChatProfileCreator oauthSession={oauthSession}>
31 <Poller>{children}</Poller>
32 </ChatProfileCreator>
33 </StreamplaceContext.Provider>
34 );
35}
36
37export function ChatProfileCreator({
38 oauthSession,
39 children,
40}: {
41 oauthSession?: SessionManager | null;
42 children: React.ReactNode;
43}) {
44 const getChatProfile = useGetChatProfile();
45 useEffect(() => {
46 if (oauthSession) {
47 getChatProfile();
48 }
49 }, [oauthSession]);
50 return <>{children}</>;
51}