@streamplace/components 0.6.37 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +8 -34
- package/LICENSE +0 -18
- package/README.md +0 -35
- package/dist/index.js +0 -6
- package/dist/livestream-provider/index.js +0 -20
- package/dist/livestream-provider/websocket.js +0 -41
- package/dist/livestream-store/chat.js +0 -162
- package/dist/livestream-store/context.js +0 -2
- package/dist/livestream-store/index.js +0 -3
- package/dist/livestream-store/livestream-state.js +0 -1
- package/dist/livestream-store/livestream-store.js +0 -39
- package/dist/livestream-store/websocket-consumer.js +0 -55
- package/dist/player-store/context.js +0 -2
- package/dist/player-store/index.js +0 -6
- package/dist/player-store/player-provider.js +0 -53
- package/dist/player-store/player-state.js +0 -22
- package/dist/player-store/player-store.js +0 -146
- package/dist/player-store/single-player-provider.js +0 -109
- package/dist/streamplace-provider/context.js +0 -2
- package/dist/streamplace-provider/index.js +0 -16
- package/dist/streamplace-provider/poller.js +0 -46
- package/dist/streamplace-provider/xrpc.js +0 -0
- package/dist/streamplace-store/index.js +0 -2
- package/dist/streamplace-store/streamplace-store.js +0 -37
- package/dist/streamplace-store/user.js +0 -47
- package/dist/streamplace-store/xrpc.js +0 -12
- package/node-compile-cache/v22.15.0-x64-efe9a9df-0/37be0eec +0 -0
- package/node-compile-cache/v22.15.0-x64-efe9a9df-0/56540125 +0 -0
- package/node-compile-cache/v22.15.0-x64-efe9a9df-0/67b1eb60 +0 -0
- package/node-compile-cache/v22.15.0-x64-efe9a9df-0/7c275f90 +0 -0
- package/src/index.tsx +0 -6
- package/src/livestream-provider/index.tsx +0 -37
- package/src/livestream-provider/websocket.tsx +0 -47
- package/src/livestream-store/chat.tsx +0 -224
- package/src/livestream-store/context.tsx +0 -10
- package/src/livestream-store/index.tsx +0 -3
- package/src/livestream-store/livestream-state.tsx +0 -18
- package/src/livestream-store/livestream-store.tsx +0 -56
- package/src/livestream-store/websocket-consumer.tsx +0 -62
- package/src/player-store/context.tsx +0 -11
- package/src/player-store/index.tsx +0 -6
- package/src/player-store/player-provider.tsx +0 -90
- package/src/player-store/player-state.tsx +0 -159
- package/src/player-store/player-store.tsx +0 -217
- package/src/player-store/single-player-provider.tsx +0 -181
- package/src/streamplace-provider/context.tsx +0 -10
- package/src/streamplace-provider/index.tsx +0 -32
- package/src/streamplace-provider/poller.tsx +0 -55
- package/src/streamplace-provider/xrpc.tsx +0 -0
- package/src/streamplace-store/index.tsx +0 -2
- package/src/streamplace-store/streamplace-store.tsx +0 -89
- package/src/streamplace-store/user.tsx +0 -57
- package/src/streamplace-store/xrpc.tsx +0 -15
- package/tsconfig.json +0 -9
- package/tsconfig.tsbuildinfo +0 -1
|
@@ -1,217 +0,0 @@
|
|
|
1
|
-
import { useContext } from "react";
|
|
2
|
-
import { createStore, StoreApi, useStore } from "zustand";
|
|
3
|
-
import { PlayerContext } from "./context";
|
|
4
|
-
import {
|
|
5
|
-
IngestMediaSource,
|
|
6
|
-
PlayerEvent,
|
|
7
|
-
PlayerProtocol,
|
|
8
|
-
PlayerState,
|
|
9
|
-
PlayerStatus,
|
|
10
|
-
} from "./player-state";
|
|
11
|
-
|
|
12
|
-
export type PlayerStore = StoreApi<PlayerState>;
|
|
13
|
-
|
|
14
|
-
export const makePlayerStore = (id?: string): StoreApi<PlayerState> => {
|
|
15
|
-
return createStore<PlayerState>()((set) => ({
|
|
16
|
-
id: id || Math.random().toString(36).slice(8),
|
|
17
|
-
selectedRendition: "source",
|
|
18
|
-
setSelectedRendition: (rendition: string) =>
|
|
19
|
-
set((state) => ({ ...state, selectedRendition: rendition })),
|
|
20
|
-
protocol: PlayerProtocol.WEBRTC,
|
|
21
|
-
setProtocol: (protocol: PlayerProtocol) =>
|
|
22
|
-
set((state) => ({ ...state, protocol: protocol })),
|
|
23
|
-
|
|
24
|
-
src: "",
|
|
25
|
-
setSrc: (src: string) => set(() => ({ src })),
|
|
26
|
-
|
|
27
|
-
ingestStarting: false,
|
|
28
|
-
setIngestStarting: (ingestStarting: boolean) =>
|
|
29
|
-
set(() => ({ ingestStarting })),
|
|
30
|
-
|
|
31
|
-
ingestMediaSource: undefined,
|
|
32
|
-
setIngestMediaSource: (ingestMediaSource: IngestMediaSource | undefined) =>
|
|
33
|
-
set(() => ({ ingestMediaSource })),
|
|
34
|
-
|
|
35
|
-
ingestConnectionState: null,
|
|
36
|
-
setIngestConnectionState: (
|
|
37
|
-
ingestConnectionState: RTCPeerConnectionState | null,
|
|
38
|
-
) => set(() => ({ ingestConnectionState })),
|
|
39
|
-
|
|
40
|
-
ingestAutoStart: false,
|
|
41
|
-
setIngestAutoStart: (ingestAutoStart: boolean) =>
|
|
42
|
-
set(() => ({ ingestAutoStart })),
|
|
43
|
-
|
|
44
|
-
ingestStarted: null,
|
|
45
|
-
setIngestStarted: (timestamp: number | null) =>
|
|
46
|
-
set(() => ({ ingestStarted: timestamp })),
|
|
47
|
-
|
|
48
|
-
muted: false,
|
|
49
|
-
setMuted: (isMuted: boolean) =>
|
|
50
|
-
set(() => ({ muted: isMuted, muteWasForced: false })),
|
|
51
|
-
|
|
52
|
-
volume: 1.0,
|
|
53
|
-
setVolume: (volume: number) =>
|
|
54
|
-
set(() => ({ volume, muteWasForced: false })),
|
|
55
|
-
|
|
56
|
-
fullscreen: false,
|
|
57
|
-
setFullscreen: (isFullscreen: boolean) =>
|
|
58
|
-
set(() => ({ fullscreen: isFullscreen })),
|
|
59
|
-
|
|
60
|
-
status: PlayerStatus.START,
|
|
61
|
-
setStatus: (status: PlayerStatus) => set(() => ({ status })),
|
|
62
|
-
|
|
63
|
-
playTime: 0,
|
|
64
|
-
setPlayTime: (playTime: number) => set(() => ({ playTime })),
|
|
65
|
-
|
|
66
|
-
offline: false,
|
|
67
|
-
setOffline: (offline: boolean) => set(() => ({ offline })),
|
|
68
|
-
|
|
69
|
-
videoRef: undefined,
|
|
70
|
-
setVideoRef: (
|
|
71
|
-
videoRef:
|
|
72
|
-
| React.MutableRefObject<HTMLVideoElement | null>
|
|
73
|
-
| ((instance: HTMLVideoElement | null) => void)
|
|
74
|
-
| null
|
|
75
|
-
| undefined,
|
|
76
|
-
) => set(() => ({ videoRef })),
|
|
77
|
-
|
|
78
|
-
pipMode: false,
|
|
79
|
-
setPipMode: (pipMode: boolean) => set(() => ({ pipMode })),
|
|
80
|
-
|
|
81
|
-
// * Whether mute was forced by the browser or not for autoplay
|
|
82
|
-
// * Will get set to 'false' if the user has interacted with the volume
|
|
83
|
-
muteWasForced: false,
|
|
84
|
-
setMuteWasForced: (muteWasForced: boolean) =>
|
|
85
|
-
set(() => ({ muteWasForced })),
|
|
86
|
-
|
|
87
|
-
embedded: false,
|
|
88
|
-
setEmbedded: (embedded: boolean) => set(() => ({ embedded })),
|
|
89
|
-
|
|
90
|
-
showControls: true,
|
|
91
|
-
controlsTimeout: undefined,
|
|
92
|
-
setShowControls: (showControls: boolean) =>
|
|
93
|
-
set({ showControls, controlsTimeout: undefined }),
|
|
94
|
-
|
|
95
|
-
telemetry: true,
|
|
96
|
-
setTelemetry: (telemetry: boolean) => set(() => ({ telemetry })),
|
|
97
|
-
|
|
98
|
-
playerEvent: async (
|
|
99
|
-
url: string,
|
|
100
|
-
time: string,
|
|
101
|
-
eventType: string,
|
|
102
|
-
meta: { [key: string]: any },
|
|
103
|
-
) =>
|
|
104
|
-
set((x) => {
|
|
105
|
-
const data: PlayerEvent = {
|
|
106
|
-
time: time,
|
|
107
|
-
playerId: x.id,
|
|
108
|
-
eventType: eventType,
|
|
109
|
-
meta: {
|
|
110
|
-
...meta,
|
|
111
|
-
},
|
|
112
|
-
};
|
|
113
|
-
try {
|
|
114
|
-
// fetch url from sp provider
|
|
115
|
-
fetch(`${url}/api/player-event`, {
|
|
116
|
-
method: "POST",
|
|
117
|
-
body: JSON.stringify(data),
|
|
118
|
-
});
|
|
119
|
-
} catch (e) {
|
|
120
|
-
console.error("error sending player telemetry", e);
|
|
121
|
-
}
|
|
122
|
-
return {};
|
|
123
|
-
}),
|
|
124
|
-
|
|
125
|
-
// Clear the controls timeout, if it exists.
|
|
126
|
-
// Should be called on player unmount.
|
|
127
|
-
clearControlsTimeout: () =>
|
|
128
|
-
set((state) => {
|
|
129
|
-
if (state.controlsTimeout) {
|
|
130
|
-
clearTimeout(state.controlsTimeout);
|
|
131
|
-
}
|
|
132
|
-
return { controlsTimeout: undefined };
|
|
133
|
-
}),
|
|
134
|
-
|
|
135
|
-
setUserInteraction: () =>
|
|
136
|
-
set((p) => {
|
|
137
|
-
// controls timeout
|
|
138
|
-
if (p.controlsTimeout) {
|
|
139
|
-
clearTimeout(p.controlsTimeout);
|
|
140
|
-
}
|
|
141
|
-
let controlsTimeout = setTimeout(() => p.setShowControls(false), 1000);
|
|
142
|
-
return { showControls: true, controlsTimeout };
|
|
143
|
-
}),
|
|
144
|
-
}));
|
|
145
|
-
};
|
|
146
|
-
|
|
147
|
-
export function usePlayerContext() {
|
|
148
|
-
const context = useContext(PlayerContext);
|
|
149
|
-
if (!context) {
|
|
150
|
-
throw new Error("usePlayerContext must be used within a PlayerProvider");
|
|
151
|
-
}
|
|
152
|
-
return context;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
// Get a specific player store by ID
|
|
156
|
-
export function getPlayerStoreById(id: string): PlayerStore {
|
|
157
|
-
const { players } = usePlayerContext();
|
|
158
|
-
const playerStore = players[id];
|
|
159
|
-
if (!playerStore) {
|
|
160
|
-
throw new Error(`No player found with ID: ${id}`);
|
|
161
|
-
}
|
|
162
|
-
return playerStore;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// Will get the first player ID in the context
|
|
166
|
-
export function getFirstPlayerID(): string {
|
|
167
|
-
const { players } = usePlayerContext();
|
|
168
|
-
const playerIds = Object.keys(players);
|
|
169
|
-
if (playerIds.length === 0) {
|
|
170
|
-
throw new Error("No players found in context");
|
|
171
|
-
}
|
|
172
|
-
return playerIds[0];
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
export function getPlayerStoreFromContext(): PlayerStore {
|
|
176
|
-
console.warn(
|
|
177
|
-
"getPlayerStoreFromContext is deprecated. Use getPlayerStoreById instead.",
|
|
178
|
-
);
|
|
179
|
-
const { players } = usePlayerContext();
|
|
180
|
-
const playerIds = Object.keys(players);
|
|
181
|
-
if (playerIds.length === 0) {
|
|
182
|
-
throw new Error("No players found in context");
|
|
183
|
-
}
|
|
184
|
-
return players[playerIds[0]];
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
// Use a specific player store by ID
|
|
188
|
-
// If no ID is provided, it will use the first player in the context
|
|
189
|
-
export function usePlayerStore<U>(
|
|
190
|
-
selector: (state: PlayerState) => U,
|
|
191
|
-
playerId?: string,
|
|
192
|
-
): U {
|
|
193
|
-
if (!playerId) {
|
|
194
|
-
playerId = Object.keys(usePlayerContext().players)[0];
|
|
195
|
-
}
|
|
196
|
-
const store = getPlayerStoreById(playerId);
|
|
197
|
-
return useStore(store, selector);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/* Convenience selectors/hooks */
|
|
201
|
-
export const usePlayerProtocol = (
|
|
202
|
-
playerId?: string,
|
|
203
|
-
): [PlayerProtocol, (protocol: PlayerProtocol) => void] =>
|
|
204
|
-
usePlayerStore((x) => [x.protocol, x.setProtocol], playerId);
|
|
205
|
-
|
|
206
|
-
export const intoPlayerProtocol = (protocol: string): PlayerProtocol => {
|
|
207
|
-
switch (protocol) {
|
|
208
|
-
case "hls":
|
|
209
|
-
return PlayerProtocol.HLS;
|
|
210
|
-
case "progressive-mp4":
|
|
211
|
-
return PlayerProtocol.PROGRESSIVE_MP4;
|
|
212
|
-
case "progressive-webm":
|
|
213
|
-
return PlayerProtocol.PROGRESSIVE_WEBM;
|
|
214
|
-
default:
|
|
215
|
-
return PlayerProtocol.WEBRTC;
|
|
216
|
-
}
|
|
217
|
-
};
|
|
@@ -1,181 +0,0 @@
|
|
|
1
|
-
import React, { createContext, useContext, useMemo } from "react";
|
|
2
|
-
import { StoreApi, useStore } from "zustand";
|
|
3
|
-
import { usePlayerContext } from "../player-store";
|
|
4
|
-
import { PlayerProtocol, PlayerState } from "./player-state";
|
|
5
|
-
|
|
6
|
-
// Context for a single player
|
|
7
|
-
interface SinglePlayerContextType {
|
|
8
|
-
playerId: string;
|
|
9
|
-
playerStore: StoreApi<PlayerState>;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
const SinglePlayerContext = createContext<SinglePlayerContextType | null>(null);
|
|
13
|
-
|
|
14
|
-
interface SinglePlayerProviderProps {
|
|
15
|
-
children: React.ReactNode;
|
|
16
|
-
playerId?: string;
|
|
17
|
-
protocol?: PlayerProtocol;
|
|
18
|
-
rendition?: string;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Provider component for a single player that creates a scoped context
|
|
23
|
-
* This allows components to access a specific player's state without passing IDs around
|
|
24
|
-
*/
|
|
25
|
-
export const SinglePlayerProvider: React.FC<SinglePlayerProviderProps> = ({
|
|
26
|
-
children,
|
|
27
|
-
playerId: providedPlayerId,
|
|
28
|
-
protocol = PlayerProtocol.WEBRTC,
|
|
29
|
-
rendition = "auto",
|
|
30
|
-
}) => {
|
|
31
|
-
const { players, createPlayer } = usePlayerContext();
|
|
32
|
-
|
|
33
|
-
// Create or get a player ID
|
|
34
|
-
const playerId = useMemo(() => {
|
|
35
|
-
// If a player ID is provided and exists, use it
|
|
36
|
-
if (providedPlayerId && players[providedPlayerId]) {
|
|
37
|
-
return providedPlayerId;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// If a player ID is provided but doesn't exist, create it
|
|
41
|
-
if (providedPlayerId) {
|
|
42
|
-
return createPlayer(providedPlayerId);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// Otherwise create a new player
|
|
46
|
-
return createPlayer();
|
|
47
|
-
}, [providedPlayerId, players, createPlayer]);
|
|
48
|
-
|
|
49
|
-
// Get the player store
|
|
50
|
-
const playerStore = useMemo(() => {
|
|
51
|
-
return players[playerId];
|
|
52
|
-
}, [players, playerId]);
|
|
53
|
-
|
|
54
|
-
// Set initial protocol and rendition if provided
|
|
55
|
-
React.useEffect(() => {
|
|
56
|
-
if (protocol) {
|
|
57
|
-
playerStore.setState((state) => ({
|
|
58
|
-
...state,
|
|
59
|
-
protocol,
|
|
60
|
-
}));
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
if (rendition) {
|
|
64
|
-
playerStore.setState((state) => ({
|
|
65
|
-
...state,
|
|
66
|
-
selectedRendition: rendition,
|
|
67
|
-
}));
|
|
68
|
-
}
|
|
69
|
-
}, [playerStore, protocol, rendition]);
|
|
70
|
-
|
|
71
|
-
// Create context value
|
|
72
|
-
const contextValue = useMemo(
|
|
73
|
-
() => ({
|
|
74
|
-
playerId,
|
|
75
|
-
playerStore,
|
|
76
|
-
}),
|
|
77
|
-
[playerId, playerStore],
|
|
78
|
-
);
|
|
79
|
-
|
|
80
|
-
return (
|
|
81
|
-
<SinglePlayerContext.Provider value={contextValue}>
|
|
82
|
-
{children}
|
|
83
|
-
</SinglePlayerContext.Provider>
|
|
84
|
-
);
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Hook to access the current single player context
|
|
89
|
-
*/
|
|
90
|
-
export function useSinglePlayerContext() {
|
|
91
|
-
const context = useContext(SinglePlayerContext);
|
|
92
|
-
if (!context) {
|
|
93
|
-
throw new Error(
|
|
94
|
-
"useSinglePlayerContext must be used within a SinglePlayerProvider",
|
|
95
|
-
);
|
|
96
|
-
}
|
|
97
|
-
return context;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Hook to access the current player ID from the single player context
|
|
102
|
-
*/
|
|
103
|
-
export function useCurrentPlayerId(): string {
|
|
104
|
-
const { playerId } = useSinglePlayerContext();
|
|
105
|
-
return playerId;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Hook to access state from the current player without needing to specify the ID
|
|
110
|
-
*/
|
|
111
|
-
export function useCurrentPlayerStore<U>(
|
|
112
|
-
selector: (state: PlayerState) => U,
|
|
113
|
-
): U {
|
|
114
|
-
const { playerStore } = useSinglePlayerContext();
|
|
115
|
-
return useStore(playerStore, selector);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* Hook to get the protocol of the current player
|
|
120
|
-
*/
|
|
121
|
-
export function useCurrentPlayerProtocol(): [
|
|
122
|
-
PlayerProtocol,
|
|
123
|
-
(protocol: PlayerProtocol) => void,
|
|
124
|
-
] {
|
|
125
|
-
return useCurrentPlayerStore(
|
|
126
|
-
(state) => [state.protocol, state.setProtocol] as const,
|
|
127
|
-
);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Hook to get the selected rendition of the current player
|
|
132
|
-
*/
|
|
133
|
-
export function useCurrentPlayerRendition(): [
|
|
134
|
-
string,
|
|
135
|
-
(rendition: string) => void,
|
|
136
|
-
] {
|
|
137
|
-
return useCurrentPlayerStore(
|
|
138
|
-
(state) => [state.selectedRendition, state.setSelectedRendition] as const,
|
|
139
|
-
);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* Hook to get the ingest state of the current player
|
|
144
|
-
*/
|
|
145
|
-
export function useCurrentPlayerIngest(): {
|
|
146
|
-
starting: boolean;
|
|
147
|
-
setStarting: (starting: boolean) => void;
|
|
148
|
-
connectionState: RTCPeerConnectionState | null;
|
|
149
|
-
setConnectionState: (state: RTCPeerConnectionState | null) => void;
|
|
150
|
-
startedTimestamp: number | null;
|
|
151
|
-
setStartedTimestamp: (timestamp: number | null) => void;
|
|
152
|
-
} {
|
|
153
|
-
return useCurrentPlayerStore((state) => ({
|
|
154
|
-
starting: state.ingestStarting,
|
|
155
|
-
setStarting: state.setIngestStarting,
|
|
156
|
-
connectionState: state.ingestConnectionState,
|
|
157
|
-
setConnectionState: state.setIngestConnectionState,
|
|
158
|
-
startedTimestamp: state.ingestStarted,
|
|
159
|
-
setStartedTimestamp: state.setIngestStarted,
|
|
160
|
-
}));
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* HOC to wrap components with a SinglePlayerProvider
|
|
165
|
-
*/
|
|
166
|
-
export function withSinglePlayer<P extends object>(
|
|
167
|
-
Component: React.ComponentType<P>,
|
|
168
|
-
): React.FC<P & SinglePlayerProviderProps> {
|
|
169
|
-
return function WithSinglePlayer(props: P & SinglePlayerProviderProps) {
|
|
170
|
-
const { playerId, protocol, rendition, ...componentProps } = props;
|
|
171
|
-
return (
|
|
172
|
-
<SinglePlayerProvider
|
|
173
|
-
playerId={playerId}
|
|
174
|
-
protocol={protocol}
|
|
175
|
-
rendition={rendition}
|
|
176
|
-
>
|
|
177
|
-
<Component {...(componentProps as P)} />
|
|
178
|
-
</SinglePlayerProvider>
|
|
179
|
-
);
|
|
180
|
-
};
|
|
181
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { createContext } from "react";
|
|
2
|
-
import { StreamplaceStore } from "../streamplace-store/streamplace-store";
|
|
3
|
-
|
|
4
|
-
type StreamplaceContextType = {
|
|
5
|
-
store: StreamplaceStore;
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
export const StreamplaceContext = createContext<StreamplaceContextType | null>(
|
|
9
|
-
null,
|
|
10
|
-
);
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { SessionManager } from "@atproto/api/dist/session-manager";
|
|
2
|
-
import { useEffect, useRef } from "react";
|
|
3
|
-
import { makeStreamplaceStore } from "../streamplace-store/streamplace-store";
|
|
4
|
-
import { StreamplaceContext } from "./context";
|
|
5
|
-
import Poller from "./poller";
|
|
6
|
-
|
|
7
|
-
export function StreamplaceProvider({
|
|
8
|
-
children,
|
|
9
|
-
url,
|
|
10
|
-
oauthSession,
|
|
11
|
-
}: {
|
|
12
|
-
children: React.ReactNode;
|
|
13
|
-
url: string;
|
|
14
|
-
oauthSession?: SessionManager;
|
|
15
|
-
}) {
|
|
16
|
-
// todo: handle url changes?
|
|
17
|
-
const store = useRef(makeStreamplaceStore({ url })).current;
|
|
18
|
-
|
|
19
|
-
useEffect(() => {
|
|
20
|
-
store.setState({ url });
|
|
21
|
-
}, [url]);
|
|
22
|
-
|
|
23
|
-
useEffect(() => {
|
|
24
|
-
store.setState({ oauthSession });
|
|
25
|
-
}, [oauthSession]);
|
|
26
|
-
|
|
27
|
-
return (
|
|
28
|
-
<StreamplaceContext.Provider value={{ store: store }}>
|
|
29
|
-
<Poller>{children}</Poller>
|
|
30
|
-
</StreamplaceContext.Provider>
|
|
31
|
-
);
|
|
32
|
-
}
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import React, { useEffect } from "react";
|
|
2
|
-
import { StreamplaceAgent } from "streamplace";
|
|
3
|
-
import {
|
|
4
|
-
useDID,
|
|
5
|
-
useGetBskyProfile,
|
|
6
|
-
useGetChatProfile,
|
|
7
|
-
useStreamplaceStore,
|
|
8
|
-
} from "../streamplace-store";
|
|
9
|
-
import { usePDSAgent } from "../streamplace-store/xrpc";
|
|
10
|
-
|
|
11
|
-
export default function Poller({ children }: { children: React.ReactNode }) {
|
|
12
|
-
const url = useStreamplaceStore((state) => state.url);
|
|
13
|
-
const setLiveUsers = useStreamplaceStore((state) => state.setLiveUsers);
|
|
14
|
-
const did = useDID();
|
|
15
|
-
const pdsAgent = usePDSAgent();
|
|
16
|
-
const getChatProfile = useGetChatProfile();
|
|
17
|
-
const getBskyProfile = useGetBskyProfile();
|
|
18
|
-
const liveUserRefresh = useStreamplaceStore(
|
|
19
|
-
(state) => state.liveUsersRefresh,
|
|
20
|
-
);
|
|
21
|
-
|
|
22
|
-
useEffect(() => {
|
|
23
|
-
if (pdsAgent && did) {
|
|
24
|
-
getChatProfile();
|
|
25
|
-
getBskyProfile();
|
|
26
|
-
}
|
|
27
|
-
}, [pdsAgent, did]);
|
|
28
|
-
|
|
29
|
-
useEffect(() => {
|
|
30
|
-
const agent = new StreamplaceAgent(url);
|
|
31
|
-
const go = async () => {
|
|
32
|
-
setLiveUsers({
|
|
33
|
-
liveUsersLoading: true,
|
|
34
|
-
});
|
|
35
|
-
try {
|
|
36
|
-
const res = await agent.place.stream.live.getLiveUsers();
|
|
37
|
-
setLiveUsers({
|
|
38
|
-
liveUsers: res.data.streams || [],
|
|
39
|
-
liveUsersLoading: false,
|
|
40
|
-
liveUsersError: null,
|
|
41
|
-
});
|
|
42
|
-
} catch (e) {
|
|
43
|
-
setLiveUsers({
|
|
44
|
-
liveUsersLoading: false,
|
|
45
|
-
liveUsersError: e.message,
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
go();
|
|
50
|
-
const handle = setInterval(go, 3000);
|
|
51
|
-
return () => clearInterval(handle);
|
|
52
|
-
}, [url, liveUserRefresh]);
|
|
53
|
-
|
|
54
|
-
return <>{children}</>;
|
|
55
|
-
}
|
|
File without changes
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import { SessionManager } from "@atproto/api/dist/session-manager";
|
|
2
|
-
import { useContext } from "react";
|
|
3
|
-
import { PlaceStreamChatProfile, PlaceStreamLivestream } from "streamplace";
|
|
4
|
-
import { createStore, StoreApi, useStore } from "zustand";
|
|
5
|
-
import { StreamplaceContext } from "../streamplace-provider/context";
|
|
6
|
-
|
|
7
|
-
// there are three categories of XRPC that we need to handle:
|
|
8
|
-
// 1. Public (probably) OAuth XRPC to the users' PDS for apps that use this API.
|
|
9
|
-
// 2. Confidental OAuth to the Streamplace server for doing things that require
|
|
10
|
-
// server-side authentication. This isn't very much stuff yet, but you need
|
|
11
|
-
// to log into Streamplace to do things like have Streamplace update your
|
|
12
|
-
// activity status.
|
|
13
|
-
// 3. Anonymous XRPC to the Streamplace server for stuff like `getLiveUsers`. This
|
|
14
|
-
// is easy to handle internal to this library.
|
|
15
|
-
// For the Streamplace app itself, all three are the same. For apps that aren't
|
|
16
|
-
// doing OAuth through the Streamplace node, we need to expose an interface that
|
|
17
|
-
// allows them to use atcute or whatever for 1.
|
|
18
|
-
|
|
19
|
-
export interface StreamplaceState {
|
|
20
|
-
url: string;
|
|
21
|
-
liveUsers: PlaceStreamLivestream.LivestreamView[] | null;
|
|
22
|
-
setLiveUsers: (opts: {
|
|
23
|
-
liveUsers?: PlaceStreamLivestream.LivestreamView[];
|
|
24
|
-
liveUsersLoading?: boolean;
|
|
25
|
-
liveUsersError?: string | null;
|
|
26
|
-
liveUsersRefresh?: number;
|
|
27
|
-
}) => void;
|
|
28
|
-
liveUsersRefresh: number;
|
|
29
|
-
liveUsersLoading: boolean;
|
|
30
|
-
liveUsersError: string | null;
|
|
31
|
-
oauthSession: SessionManager | null;
|
|
32
|
-
handle: string | null;
|
|
33
|
-
chatProfile: PlaceStreamChatProfile.Record | null;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export type StreamplaceStore = StoreApi<StreamplaceState>;
|
|
37
|
-
|
|
38
|
-
export const makeStreamplaceStore = ({
|
|
39
|
-
url,
|
|
40
|
-
}: {
|
|
41
|
-
url: string;
|
|
42
|
-
}): StoreApi<StreamplaceState> => {
|
|
43
|
-
return createStore<StreamplaceState>()((set) => ({
|
|
44
|
-
url,
|
|
45
|
-
liveUsers: null,
|
|
46
|
-
setLiveUsers: (opts: {
|
|
47
|
-
liveUsers?: PlaceStreamLivestream.LivestreamView[];
|
|
48
|
-
liveUsersLoading?: boolean;
|
|
49
|
-
liveUsersError?: string | null;
|
|
50
|
-
liveUsersRefresh?: number;
|
|
51
|
-
}) => {
|
|
52
|
-
set({
|
|
53
|
-
...opts,
|
|
54
|
-
});
|
|
55
|
-
},
|
|
56
|
-
liveUsersRefresh: 0,
|
|
57
|
-
liveUsersLoading: true,
|
|
58
|
-
liveUsersError: null,
|
|
59
|
-
oauthSession: null,
|
|
60
|
-
handle: null,
|
|
61
|
-
chatProfile: null,
|
|
62
|
-
}));
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
export function getStreamplaceStoreFromContext(): StreamplaceStore {
|
|
66
|
-
const context = useContext(StreamplaceContext);
|
|
67
|
-
if (!context) {
|
|
68
|
-
throw new Error(
|
|
69
|
-
"useStreamplaceStore must be used within a StreamplaceProvider",
|
|
70
|
-
);
|
|
71
|
-
}
|
|
72
|
-
return context.store;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export function useStreamplaceStore<U>(
|
|
76
|
-
selector: (state: StreamplaceState) => U,
|
|
77
|
-
): U {
|
|
78
|
-
return useStore(getStreamplaceStoreFromContext(), selector);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export const useUrl = () => useStreamplaceStore((x) => x.url);
|
|
82
|
-
|
|
83
|
-
export const useDID = () => useStreamplaceStore((x) => x.oauthSession?.did);
|
|
84
|
-
|
|
85
|
-
export const useHandle = () => useStreamplaceStore((x) => x.handle);
|
|
86
|
-
export const useSetHandle = (): ((handle: string) => void) => {
|
|
87
|
-
const store = getStreamplaceStoreFromContext();
|
|
88
|
-
return (handle: string) => store.setState({ handle });
|
|
89
|
-
};
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { PlaceStreamChatProfile } from "streamplace";
|
|
2
|
-
import {
|
|
3
|
-
getStreamplaceStoreFromContext,
|
|
4
|
-
useDID,
|
|
5
|
-
useStreamplaceStore,
|
|
6
|
-
} from "./streamplace-store";
|
|
7
|
-
import { usePDSAgent } from "./xrpc";
|
|
8
|
-
|
|
9
|
-
export function useGetChatProfile() {
|
|
10
|
-
const did = useDID();
|
|
11
|
-
const pdsAgent = usePDSAgent();
|
|
12
|
-
const store = getStreamplaceStoreFromContext();
|
|
13
|
-
|
|
14
|
-
return async () => {
|
|
15
|
-
if (!did || !pdsAgent) {
|
|
16
|
-
throw new Error("No DID or PDS agent");
|
|
17
|
-
}
|
|
18
|
-
const res = await pdsAgent.com.atproto.repo.getRecord({
|
|
19
|
-
repo: did,
|
|
20
|
-
collection: "place.stream.chat.profile",
|
|
21
|
-
rkey: "self",
|
|
22
|
-
});
|
|
23
|
-
if (!res.success) {
|
|
24
|
-
throw new Error("Failed to get chat profile record");
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
if (PlaceStreamChatProfile.isRecord(res.data.value)) {
|
|
28
|
-
store.setState({ chatProfile: res.data.value });
|
|
29
|
-
} else {
|
|
30
|
-
console.log("not a record", res.data.value);
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function useGetBskyProfile() {
|
|
36
|
-
const did = useDID();
|
|
37
|
-
const pdsAgent = usePDSAgent();
|
|
38
|
-
const store = getStreamplaceStoreFromContext();
|
|
39
|
-
|
|
40
|
-
return async () => {
|
|
41
|
-
if (!did || !pdsAgent) {
|
|
42
|
-
throw new Error("No DID or PDS agent");
|
|
43
|
-
}
|
|
44
|
-
const res = await pdsAgent.app.bsky.actor.getProfile({
|
|
45
|
-
actor: did,
|
|
46
|
-
});
|
|
47
|
-
if (!res.success) {
|
|
48
|
-
throw new Error("Failed to get chat profile record");
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
store.setState({ handle: res.data.handle });
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export function useChatProfile() {
|
|
56
|
-
return useStreamplaceStore((x) => x.chatProfile);
|
|
57
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { useMemo } from "react";
|
|
2
|
-
import { StreamplaceAgent } from "streamplace";
|
|
3
|
-
import { useStreamplaceStore } from ".";
|
|
4
|
-
|
|
5
|
-
export function usePDSAgent(): StreamplaceAgent | null {
|
|
6
|
-
const oauthSession = useStreamplaceStore((state) => state.oauthSession);
|
|
7
|
-
|
|
8
|
-
return useMemo(() => {
|
|
9
|
-
if (!oauthSession) {
|
|
10
|
-
return null;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
return new StreamplaceAgent(oauthSession);
|
|
14
|
-
}, [oauthSession]);
|
|
15
|
-
}
|