@streamplace/components 0.0.1 → 0.7.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.
Files changed (169) hide show
  1. package/LICENSE +18 -0
  2. package/README.md +35 -0
  3. package/dist/components/chat/chat-box.js +109 -0
  4. package/dist/components/chat/chat-message.js +76 -0
  5. package/dist/components/chat/chat.js +56 -0
  6. package/dist/components/chat/mention-suggestions.js +39 -0
  7. package/dist/components/chat/mod-view.js +33 -0
  8. package/dist/components/mobile-player/fullscreen.js +69 -0
  9. package/dist/components/mobile-player/fullscreen.native.js +151 -0
  10. package/dist/components/mobile-player/player.js +103 -0
  11. package/dist/components/mobile-player/props.js +1 -0
  12. package/dist/components/mobile-player/shared.js +51 -0
  13. package/dist/components/mobile-player/ui/countdown.js +79 -0
  14. package/dist/components/mobile-player/ui/index.js +5 -0
  15. package/dist/components/mobile-player/ui/input.js +38 -0
  16. package/dist/components/mobile-player/ui/metrics.js +40 -0
  17. package/dist/components/mobile-player/ui/streamer-context-menu.js +4 -0
  18. package/dist/components/mobile-player/ui/viewer-context-menu.js +20 -0
  19. package/dist/components/mobile-player/use-webrtc.js +232 -0
  20. package/dist/components/mobile-player/video.js +375 -0
  21. package/dist/components/mobile-player/video.native.js +238 -0
  22. package/dist/components/mobile-player/webrtc-diagnostics.js +106 -0
  23. package/dist/components/mobile-player/webrtc-primitives.js +25 -0
  24. package/dist/components/mobile-player/webrtc-primitives.native.js +1 -0
  25. package/dist/components/ui/button.js +220 -0
  26. package/dist/components/ui/dialog.js +203 -0
  27. package/dist/components/ui/dropdown.js +148 -0
  28. package/dist/components/ui/icons.js +22 -0
  29. package/dist/components/ui/index.js +22 -0
  30. package/dist/components/ui/input.js +202 -0
  31. package/dist/components/ui/loader.js +7 -0
  32. package/dist/components/ui/primitives/button.js +121 -0
  33. package/dist/components/ui/primitives/input.js +202 -0
  34. package/dist/components/ui/primitives/modal.js +203 -0
  35. package/dist/components/ui/primitives/text.js +286 -0
  36. package/dist/components/ui/resizeable.js +101 -0
  37. package/dist/components/ui/text.js +175 -0
  38. package/dist/components/ui/textarea.js +17 -0
  39. package/dist/components/ui/toast.js +129 -0
  40. package/dist/components/ui/view.js +250 -0
  41. package/dist/hooks/index.js +9 -0
  42. package/dist/hooks/useAvatars.js +32 -0
  43. package/dist/hooks/useCameraToggle.js +9 -0
  44. package/dist/hooks/useKeyboard.js +33 -0
  45. package/dist/hooks/useKeyboardSlide.js +11 -0
  46. package/dist/hooks/useLivestreamInfo.js +62 -0
  47. package/dist/hooks/useOuterAndInnerDimensions.js +27 -0
  48. package/dist/hooks/usePlayerDimensions.js +19 -0
  49. package/dist/hooks/useSegmentTiming.js +62 -0
  50. package/dist/index.js +16 -0
  51. package/dist/lib/facet.js +88 -0
  52. package/dist/lib/theme/atoms.js +620 -0
  53. package/dist/lib/theme/atoms.types.js +5 -0
  54. package/dist/lib/theme/index.js +9 -0
  55. package/dist/lib/theme/theme.js +248 -0
  56. package/dist/lib/theme/tokens.js +383 -0
  57. package/dist/lib/utils.js +94 -0
  58. package/dist/livestream-provider/index.js +25 -0
  59. package/dist/livestream-provider/websocket.js +41 -0
  60. package/dist/livestream-store/chat.js +186 -0
  61. package/dist/livestream-store/context.js +2 -0
  62. package/dist/livestream-store/index.js +4 -0
  63. package/dist/livestream-store/livestream-state.js +1 -0
  64. package/dist/livestream-store/livestream-store.js +42 -0
  65. package/dist/livestream-store/stream-key.js +115 -0
  66. package/dist/livestream-store/websocket-consumer.js +55 -0
  67. package/dist/player-store/context.js +2 -0
  68. package/dist/player-store/index.js +6 -0
  69. package/dist/player-store/player-provider.js +52 -0
  70. package/dist/player-store/player-state.js +22 -0
  71. package/dist/player-store/player-store.js +159 -0
  72. package/dist/player-store/single-player-provider.js +109 -0
  73. package/dist/streamplace-provider/context.js +2 -0
  74. package/dist/streamplace-provider/index.js +16 -0
  75. package/dist/streamplace-provider/poller.js +46 -0
  76. package/dist/streamplace-provider/xrpc.js +0 -0
  77. package/dist/streamplace-store/block.js +23 -0
  78. package/dist/streamplace-store/index.js +3 -0
  79. package/dist/streamplace-store/stream.js +193 -0
  80. package/dist/streamplace-store/streamplace-store.js +37 -0
  81. package/dist/streamplace-store/user.js +47 -0
  82. package/dist/streamplace-store/xrpc.js +12 -0
  83. package/node-compile-cache/v22.15.0-x64-efe9a9df-0/37be0eec +0 -0
  84. package/node-compile-cache/v22.15.0-x64-efe9a9df-0/56540125 +0 -0
  85. package/node-compile-cache/v22.15.0-x64-efe9a9df-0/67b1eb60 +0 -0
  86. package/node-compile-cache/v22.15.0-x64-efe9a9df-0/7c275f90 +0 -0
  87. package/package.json +50 -8
  88. package/src/components/chat/chat-box.tsx +195 -0
  89. package/src/components/chat/chat-message.tsx +192 -0
  90. package/src/components/chat/chat.tsx +128 -0
  91. package/src/components/chat/mention-suggestions.tsx +71 -0
  92. package/src/components/chat/mod-view.tsx +118 -0
  93. package/src/components/mobile-player/fullscreen.native.tsx +193 -0
  94. package/src/components/mobile-player/fullscreen.tsx +79 -0
  95. package/src/components/mobile-player/player.tsx +134 -0
  96. package/src/components/mobile-player/props.tsx +11 -0
  97. package/src/components/mobile-player/shared.tsx +56 -0
  98. package/src/components/mobile-player/ui/countdown.tsx +119 -0
  99. package/src/components/mobile-player/ui/index.ts +5 -0
  100. package/src/components/mobile-player/ui/input.tsx +85 -0
  101. package/src/components/mobile-player/ui/metrics.tsx +69 -0
  102. package/src/components/mobile-player/ui/streamer-context-menu.tsx +3 -0
  103. package/src/components/mobile-player/ui/viewer-context-menu.tsx +70 -0
  104. package/src/components/mobile-player/use-webrtc.tsx +282 -0
  105. package/src/components/mobile-player/video.native.tsx +360 -0
  106. package/src/components/mobile-player/video.tsx +557 -0
  107. package/src/components/mobile-player/webrtc-diagnostics.tsx +149 -0
  108. package/src/components/mobile-player/webrtc-primitives.native.tsx +6 -0
  109. package/src/components/mobile-player/webrtc-primitives.tsx +33 -0
  110. package/src/components/ui/button.tsx +309 -0
  111. package/src/components/ui/dialog.tsx +376 -0
  112. package/src/components/ui/dropdown.tsx +399 -0
  113. package/src/components/ui/icons.tsx +50 -0
  114. package/src/components/ui/index.ts +33 -0
  115. package/src/components/ui/input.tsx +350 -0
  116. package/src/components/ui/loader.tsx +9 -0
  117. package/src/components/ui/primitives/button.tsx +292 -0
  118. package/src/components/ui/primitives/input.tsx +422 -0
  119. package/src/components/ui/primitives/modal.tsx +421 -0
  120. package/src/components/ui/primitives/text.tsx +499 -0
  121. package/src/components/ui/resizeable.tsx +169 -0
  122. package/src/components/ui/text.tsx +330 -0
  123. package/src/components/ui/textarea.tsx +34 -0
  124. package/src/components/ui/toast.tsx +203 -0
  125. package/src/components/ui/view.tsx +344 -0
  126. package/src/hooks/index.ts +9 -0
  127. package/src/hooks/useAvatars.tsx +44 -0
  128. package/src/hooks/useCameraToggle.ts +12 -0
  129. package/src/hooks/useKeyboard.tsx +41 -0
  130. package/src/hooks/useKeyboardSlide.ts +12 -0
  131. package/src/hooks/useLivestreamInfo.ts +67 -0
  132. package/src/hooks/useOuterAndInnerDimensions.tsx +32 -0
  133. package/src/hooks/usePlayerDimensions.ts +23 -0
  134. package/src/hooks/useSegmentTiming.tsx +88 -0
  135. package/src/index.tsx +27 -0
  136. package/src/lib/facet.ts +131 -0
  137. package/src/lib/theme/atoms.ts +760 -0
  138. package/src/lib/theme/atoms.types.ts +258 -0
  139. package/src/lib/theme/index.ts +48 -0
  140. package/src/lib/theme/theme.tsx +436 -0
  141. package/src/lib/theme/tokens.ts +409 -0
  142. package/src/lib/utils.ts +132 -0
  143. package/src/livestream-provider/index.tsx +48 -0
  144. package/src/livestream-provider/websocket.tsx +47 -0
  145. package/src/livestream-store/chat.tsx +261 -0
  146. package/src/livestream-store/context.tsx +10 -0
  147. package/src/livestream-store/index.tsx +4 -0
  148. package/src/livestream-store/livestream-state.tsx +21 -0
  149. package/src/livestream-store/livestream-store.tsx +59 -0
  150. package/src/livestream-store/stream-key.tsx +124 -0
  151. package/src/livestream-store/websocket-consumer.tsx +62 -0
  152. package/src/player-store/context.tsx +11 -0
  153. package/src/player-store/index.tsx +6 -0
  154. package/src/player-store/player-provider.tsx +89 -0
  155. package/src/player-store/player-state.tsx +187 -0
  156. package/src/player-store/player-store.tsx +239 -0
  157. package/src/player-store/single-player-provider.tsx +181 -0
  158. package/src/streamplace-provider/context.tsx +10 -0
  159. package/src/streamplace-provider/index.tsx +32 -0
  160. package/src/streamplace-provider/poller.tsx +55 -0
  161. package/src/streamplace-provider/xrpc.tsx +0 -0
  162. package/src/streamplace-store/block.tsx +29 -0
  163. package/src/streamplace-store/index.tsx +3 -0
  164. package/src/streamplace-store/stream.tsx +262 -0
  165. package/src/streamplace-store/streamplace-store.tsx +89 -0
  166. package/src/streamplace-store/user.tsx +57 -0
  167. package/src/streamplace-store/xrpc.tsx +15 -0
  168. package/tsconfig.json +9 -0
  169. package/tsconfig.tsbuildinfo +1 -0
@@ -0,0 +1,25 @@
1
+ import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { useContext, useRef } from "react";
3
+ import { LivestreamContext, makeLivestreamStore } from "../livestream-store";
4
+ import { useLivestreamWebsocket } from "./websocket";
5
+ export function LivestreamProvider({ children, src, }) {
6
+ const context = useContext(LivestreamContext);
7
+ const store = useRef(makeLivestreamStore()).current;
8
+ if (context) {
9
+ // this is ok, there's use cases for having one in another
10
+ // like having a player component that's independently usable
11
+ // but can also be embedded within an entire livestream page
12
+ return _jsx(_Fragment, { children: children });
13
+ }
14
+ window.livestreamStore = store;
15
+ return (_jsx(LivestreamContext.Provider, { value: { store: store }, children: _jsx(LivestreamPoller, { src: src, children: children }) }));
16
+ }
17
+ export function WebsocketWatcher({ src }) {
18
+ useLivestreamWebsocket(src);
19
+ return _jsx(_Fragment, {});
20
+ }
21
+ export function LivestreamPoller({ children, src, }) {
22
+ // Websocket watcher is a sibling instead of a parent to avoid
23
+ // re-rendering when the websocket does stuff
24
+ return (_jsxs(_Fragment, { children: [_jsx(WebsocketWatcher, { src: src }), children] }));
25
+ }
@@ -0,0 +1,41 @@
1
+ import { useRef } from "react";
2
+ import useWebSocket from "react-use-websocket";
3
+ import { useHandleWebsocketMessages } from "../livestream-store";
4
+ import { useUrl } from "../streamplace-store";
5
+ export function useLivestreamWebsocket(src) {
6
+ const url = useUrl();
7
+ const handleWebSocketMessages = useHandleWebsocketMessages();
8
+ let wsUrl = url.replace(/^http\:/, "ws:");
9
+ wsUrl = wsUrl.replace(/^https\:/, "wss:");
10
+ const ref = useRef([]);
11
+ const handle = useRef(null);
12
+ const { readyState } = useWebSocket(`${wsUrl}/api/websocket/${src}`, {
13
+ reconnectInterval: 1000,
14
+ shouldReconnect: () => true,
15
+ onOpen: () => {
16
+ ref.current = [];
17
+ },
18
+ onError: (e) => {
19
+ console.log("onError", e);
20
+ },
21
+ // spamming the redux store with messages causes a zillion re-renders,
22
+ // so we batch them up a bit
23
+ onMessage: (msg) => {
24
+ try {
25
+ const data = JSON.parse(msg.data);
26
+ ref.current.push(data);
27
+ if (handle.current) {
28
+ return;
29
+ }
30
+ handle.current = setTimeout(() => {
31
+ handleWebSocketMessages(ref.current);
32
+ ref.current = [];
33
+ handle.current = null;
34
+ }, 250);
35
+ }
36
+ catch (e) {
37
+ console.log("onMessage parse error", e);
38
+ }
39
+ },
40
+ });
41
+ }
@@ -0,0 +1,186 @@
1
+ import { RichText } from "@atproto/api";
2
+ import { useCallback } from "react";
3
+ import { useChatProfile, useDID, useHandle } from "../streamplace-store";
4
+ import { usePDSAgent } from "../streamplace-store/xrpc";
5
+ import { getStoreFromContext, useLivestreamStore } from "./livestream-store";
6
+ export const useReplyToMessage = () => useLivestreamStore((state) => state.replyToMessage);
7
+ export const useSetReplyToMessage = () => {
8
+ const store = getStoreFromContext();
9
+ return useCallback((message) => {
10
+ store.setState({ replyToMessage: message });
11
+ }, [store]);
12
+ };
13
+ export const useCreateChatMessage = () => {
14
+ const pdsAgent = usePDSAgent();
15
+ const store = getStoreFromContext();
16
+ const userDID = useDID();
17
+ const userHandle = useHandle();
18
+ const chatProfile = useChatProfile();
19
+ return async (msg) => {
20
+ if (!pdsAgent || !userDID) {
21
+ throw new Error("No PDS agent or user DID found");
22
+ }
23
+ let state = store.getState();
24
+ const streamerProfile = state.profile;
25
+ if (!streamerProfile) {
26
+ throw new Error("Profile not found");
27
+ }
28
+ const rt = new RichText({ text: msg.text });
29
+ await rt.detectFacets(pdsAgent);
30
+ const record = {
31
+ text: msg.text,
32
+ createdAt: new Date().toISOString(),
33
+ streamer: streamerProfile.did,
34
+ facets: rt.facets,
35
+ ...(msg.reply
36
+ ? {
37
+ reply: {
38
+ root: {
39
+ cid: msg.reply.cid,
40
+ uri: msg.reply.uri,
41
+ },
42
+ parent: {
43
+ cid: msg.reply.cid,
44
+ uri: msg.reply.uri,
45
+ },
46
+ },
47
+ }
48
+ : {}),
49
+ };
50
+ const localChat = {
51
+ uri: `local-${Date.now()}`,
52
+ cid: "",
53
+ author: {
54
+ did: userDID,
55
+ handle: userHandle || userDID,
56
+ },
57
+ record: record,
58
+ indexedAt: new Date().toISOString(),
59
+ chatProfile: chatProfile || undefined,
60
+ };
61
+ state = reduceChat(state, [localChat], []);
62
+ store.setState(state);
63
+ await pdsAgent.com.atproto.repo.createRecord({
64
+ repo: userDID,
65
+ collection: "place.stream.chat.message",
66
+ record,
67
+ });
68
+ };
69
+ };
70
+ const buildSortedChatList = (chatIndex, existingChatList, newMessages, removedKeys) => {
71
+ const sortedKeys = Object.keys(chatIndex).sort((a, b) => {
72
+ const aTime = parseInt(a.split("-")[0], 10);
73
+ const bTime = parseInt(b.split("-")[0], 10);
74
+ return bTime - aTime;
75
+ });
76
+ return sortedKeys.map((key) => chatIndex[key]);
77
+ };
78
+ const profileIsDifferent = (newProfile, oldProfile) => {
79
+ if (!oldProfile) {
80
+ return true;
81
+ }
82
+ if (!newProfile) {
83
+ return false;
84
+ }
85
+ if (!oldProfile.color) {
86
+ return true;
87
+ }
88
+ if (!newProfile.color) {
89
+ // idk. shouldn't happen.
90
+ return false;
91
+ }
92
+ const { red: newRed, green: newGreen, blue: newBlue } = newProfile.color;
93
+ const { red: oldRed, green: oldGreen, blue: oldBlue } = oldProfile.color;
94
+ return newRed !== oldRed || newGreen !== oldGreen || newBlue !== oldBlue;
95
+ };
96
+ export const reduceChatIncremental = (state, newMessages, blocks) => {
97
+ if (newMessages.length === 0 && blocks.length === 0) {
98
+ return state;
99
+ }
100
+ const newChatIndex = { ...state.chatIndex };
101
+ const newAuthors = { ...state.authors };
102
+ let hasChanges = false;
103
+ const removedKeys = new Set();
104
+ // handle blocks
105
+ if (blocks.length > 0) {
106
+ const blockedDIDs = new Set(blocks.map((block) => block.record.subject));
107
+ for (const [key, message] of Object.entries(newChatIndex)) {
108
+ if (blockedDIDs.has(message.author.did)) {
109
+ delete newChatIndex[key];
110
+ removedKeys.add(key);
111
+ hasChanges = true;
112
+ }
113
+ }
114
+ }
115
+ const messagesToAdd = [];
116
+ for (const message of newMessages) {
117
+ const date = new Date(message.record.createdAt);
118
+ const key = `${date.getTime()}-${message.uri}`;
119
+ // only change the ref if the profile is different to avoid re-renders elsewhere
120
+ if (profileIsDifferent(message.chatProfile, newAuthors[message.author.handle])) {
121
+ newAuthors[message.author.handle] = message.chatProfile;
122
+ }
123
+ // skip messages we already have
124
+ if (newChatIndex[key] && newChatIndex[key].uri === message.uri) {
125
+ continue;
126
+ }
127
+ // if we have a local message, replace it with the new one
128
+ if (!message.uri.startsWith("local-")) {
129
+ const existingLocalKey = Object.keys(newChatIndex).find((k) => {
130
+ const msg = newChatIndex[k];
131
+ return (msg.uri.startsWith("local-") &&
132
+ msg.record.text === message.record.text &&
133
+ msg.author.did === message.author.did &&
134
+ Math.abs(new Date(msg.record.createdAt).getTime() - date.getTime()) <
135
+ 10000 // Within 10 seconds
136
+ );
137
+ });
138
+ if (existingLocalKey) {
139
+ delete newChatIndex[existingLocalKey];
140
+ removedKeys.add(existingLocalKey);
141
+ hasChanges = true;
142
+ }
143
+ }
144
+ // add reply info
145
+ let processedMessage = message;
146
+ if (message.record.reply) {
147
+ const reply = message.record.reply;
148
+ const parentUri = reply?.parent?.uri || reply?.root?.uri;
149
+ if (parentUri) {
150
+ const parentMsgKey = Object.keys(newChatIndex).find((k) => newChatIndex[k].uri === parentUri);
151
+ if (parentMsgKey) {
152
+ const parentMsg = newChatIndex[parentMsgKey];
153
+ processedMessage = {
154
+ ...message,
155
+ replyTo: {
156
+ cid: parentMsg.cid,
157
+ uri: parentMsg.uri,
158
+ author: parentMsg.author,
159
+ record: parentMsg.record,
160
+ chatProfile: parentMsg.chatProfile,
161
+ indexedAt: parentMsg.indexedAt,
162
+ },
163
+ };
164
+ }
165
+ }
166
+ }
167
+ messagesToAdd.push({ key, message: processedMessage });
168
+ hasChanges = true;
169
+ }
170
+ // Add new messages to index
171
+ for (const { key, message } of messagesToAdd) {
172
+ newChatIndex[key] = message;
173
+ }
174
+ // only rebuild if we have changes
175
+ if (!hasChanges) {
176
+ return state;
177
+ }
178
+ // Build the new sorted chat list efficiently
179
+ const newChatList = buildSortedChatList(newChatIndex, state.chat, messagesToAdd, removedKeys);
180
+ return {
181
+ ...state,
182
+ chatIndex: newChatIndex,
183
+ chat: newChatList,
184
+ };
185
+ };
186
+ export const reduceChat = reduceChatIncremental;
@@ -0,0 +1,2 @@
1
+ import { createContext } from "react";
2
+ export const LivestreamContext = createContext(null);
@@ -0,0 +1,4 @@
1
+ export * from "./chat";
2
+ export * from "./context";
3
+ export * from "./livestream-store";
4
+ export * from "./stream-key";
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,42 @@
1
+ import { useContext } from "react";
2
+ import { createStore, useStore } from "zustand";
3
+ import { LivestreamContext } from "./context";
4
+ import { handleWebSocketMessages } from "./websocket-consumer";
5
+ export const makeLivestreamStore = () => {
6
+ return createStore()((set) => ({
7
+ profile: null,
8
+ chatIndex: {},
9
+ chat: [],
10
+ livestream: null,
11
+ viewers: null,
12
+ segment: null,
13
+ renditions: [],
14
+ replyToMessage: null,
15
+ streamKey: null,
16
+ setStreamKey: (sk) => set({ streamKey: sk }),
17
+ authors: {},
18
+ }));
19
+ };
20
+ export function getStoreFromContext() {
21
+ const context = useContext(LivestreamContext);
22
+ if (!context) {
23
+ throw new Error("useLivestreamStore must be used within a LivestreamProvider");
24
+ }
25
+ return context.store;
26
+ }
27
+ export function useLivestreamStore(selector) {
28
+ const store = getStoreFromContext();
29
+ return useStore(store, selector);
30
+ }
31
+ export const useHandleWebsocketMessages = () => {
32
+ const store = getStoreFromContext();
33
+ return (messages) => {
34
+ store.setState((state) => handleWebSocketMessages(state, messages));
35
+ };
36
+ };
37
+ export const useChat = () => useLivestreamStore((x) => x.chat);
38
+ export const useProfile = () => useLivestreamStore((x) => x.profile);
39
+ export const useViewers = () => useLivestreamStore((x) => x.viewers);
40
+ export const useLivestream = () => useLivestreamStore((x) => x.livestream);
41
+ export const useSegment = () => useLivestreamStore((x) => x.segment);
42
+ export const useRenditions = () => useLivestreamStore((x) => x.renditions);
@@ -0,0 +1,115 @@
1
+ import { bytesToMultibase, Secp256k1Keypair } from "@atproto/crypto";
2
+ import { useEffect, useState } from "react";
3
+ import { Platform } from "react-native";
4
+ import { privateKeyToAccount } from "viem/accounts";
5
+ import { usePDSAgent } from "../streamplace-store/xrpc";
6
+ import { useLivestreamStore } from "./livestream-store";
7
+ function getBrowserName(userAgent) {
8
+ // The order matters here, and this may report false positives for unlisted browsers.
9
+ if (userAgent.includes("Firefox")) {
10
+ // "Mozilla/5.0 (X11; Linux i686; rv:104.0) Gecko/20100101 Firefox/104.0"
11
+ return "Mozilla Firefox";
12
+ }
13
+ else if (userAgent.includes("SamsungBrowser")) {
14
+ // "Mozilla/5.0 (Linux; Android 9; SAMSUNG SM-G955F Build/PPR1.180610.011) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/9.4 Chrome/67.0.3396.87 Mobile Safari/537.36"
15
+ return "Samsung Internet";
16
+ }
17
+ else if (userAgent.includes("Opera") || userAgent.includes("OPR")) {
18
+ // "Mozilla/5.0 (Macintosh; Intel Mac OS X 12_5_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36 OPR/90.0.4480.54"
19
+ return "Opera";
20
+ }
21
+ else if (userAgent.includes("Edge")) {
22
+ // "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299"
23
+ return "Microsoft Edge (Legacy)";
24
+ }
25
+ else if (userAgent.includes("Edg")) {
26
+ // "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36 Edg/104.0.1293.70"
27
+ return "Microsoft Edge (Chromium)";
28
+ }
29
+ else if (userAgent.includes("Chrome")) {
30
+ // "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"
31
+ return "Google Chrome or Chromium";
32
+ }
33
+ else if (userAgent.includes("Safari")) {
34
+ // "Mozilla/5.0 (iPhone; CPU iPhone OS 15_6_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.6 Mobile/15E148 Safari/604.1"
35
+ return "Apple Safari";
36
+ }
37
+ return "unknown";
38
+ }
39
+ export const useStreamKey = () => {
40
+ const pdsAgent = usePDSAgent();
41
+ const streamKey = useLivestreamStore((state) => state.streamKey);
42
+ const setStreamKey = useLivestreamStore((state) => state.setStreamKey);
43
+ const [key, setKey] = useState(streamKey ? JSON.parse(streamKey) : null);
44
+ const [error, setError] = useState(null);
45
+ useEffect(() => {
46
+ if (key)
47
+ return; // already have key
48
+ const generateKey = async () => {
49
+ if (!pdsAgent) {
50
+ setError("PDS Agent is not available");
51
+ return;
52
+ }
53
+ let did = pdsAgent.did;
54
+ if (!did) {
55
+ setError("PDS Agent did is not available (not logged in?)");
56
+ return;
57
+ }
58
+ const keypair = await Secp256k1Keypair.create({ exportable: true });
59
+ const exportedKey = await keypair.export();
60
+ const didBytes = new TextEncoder().encode(did);
61
+ const combinedKey = new Uint8Array([...exportedKey, ...didBytes]);
62
+ const multibaseKey = bytesToMultibase(combinedKey, "base58btc");
63
+ const hexKey = Array.from(exportedKey)
64
+ .map((b) => b.toString(16).padStart(2, "0"))
65
+ .join("");
66
+ const account = privateKeyToAccount(`0x${hexKey}`);
67
+ const newKey = {
68
+ privateKey: multibaseKey,
69
+ did: keypair.did(),
70
+ address: account.address.toLowerCase(),
71
+ };
72
+ let platform = Platform.OS;
73
+ if (Platform.OS === "web" &&
74
+ typeof window !== "undefined" &&
75
+ window.navigator) {
76
+ if (window.navigator.userAgent.includes("streamplace-desktop")) {
77
+ platform = "Desktop";
78
+ }
79
+ else {
80
+ platform = getBrowserName(window.navigator.userAgent);
81
+ if (platform !== "unknown") {
82
+ platform = platform;
83
+ }
84
+ }
85
+ }
86
+ else if (platform === "android") {
87
+ platform = "Android";
88
+ }
89
+ else if (platform === "ios") {
90
+ platform = "iOS";
91
+ }
92
+ else if (platform === "macos") {
93
+ platform = "macOS";
94
+ }
95
+ else if (platform === "windows") {
96
+ platform = "Windows";
97
+ }
98
+ const record = {
99
+ signingKey: keypair.did(),
100
+ createdAt: new Date().toISOString(),
101
+ createdBy: "Streamplace on " + platform,
102
+ };
103
+ await pdsAgent.com.atproto.repo.createRecord({
104
+ repo: did,
105
+ collection: "place.stream.key",
106
+ record,
107
+ });
108
+ setStreamKey(JSON.stringify(newKey));
109
+ setKey(newKey);
110
+ };
111
+ generateKey();
112
+ // eslint-disable-next-line react-hooks/exhaustive-deps
113
+ }, [key, setStreamKey]);
114
+ return { streamKey: key, error };
115
+ };
@@ -0,0 +1,55 @@
1
+ import { AppBskyActorDefs } from "@atproto/api";
2
+ import { PlaceStreamChatDefs, PlaceStreamDefs, PlaceStreamLivestream, PlaceStreamSegment, } from "streamplace";
3
+ import { reduceChat } from "./chat";
4
+ export const handleWebSocketMessages = (state, messages) => {
5
+ for (const message of messages) {
6
+ if (PlaceStreamLivestream.isLivestreamView(message)) {
7
+ state = {
8
+ ...state,
9
+ livestream: message,
10
+ };
11
+ }
12
+ else if (PlaceStreamLivestream.isViewerCount(message)) {
13
+ state = {
14
+ ...state,
15
+ viewers: message.count,
16
+ };
17
+ }
18
+ else if (PlaceStreamChatDefs.isMessageView(message)) {
19
+ // Explicitly map MessageView to MessageViewHydrated
20
+ const hydrated = {
21
+ uri: message.uri,
22
+ cid: message.cid,
23
+ author: message.author,
24
+ record: message.record,
25
+ indexedAt: message.indexedAt,
26
+ chatProfile: message.chatProfile,
27
+ replyTo: message.replyTo,
28
+ };
29
+ state = reduceChat(state, [hydrated], []);
30
+ }
31
+ else if (PlaceStreamSegment.isRecord(message)) {
32
+ state = {
33
+ ...state,
34
+ segment: message,
35
+ };
36
+ }
37
+ else if (PlaceStreamDefs.isBlockView(message)) {
38
+ const block = message;
39
+ state = reduceChat(state, [], [block]);
40
+ }
41
+ else if (PlaceStreamDefs.isRenditions(message)) {
42
+ state = {
43
+ ...state,
44
+ renditions: message.renditions,
45
+ };
46
+ }
47
+ else if (AppBskyActorDefs.isProfileViewBasic(message)) {
48
+ state = {
49
+ ...state,
50
+ profile: message,
51
+ };
52
+ }
53
+ }
54
+ return reduceChat(state, [], []);
55
+ };
@@ -0,0 +1,2 @@
1
+ import { createContext } from "react";
2
+ export const PlayerContext = createContext(null);
@@ -0,0 +1,6 @@
1
+ // barrel file :)
2
+ export * from "./context";
3
+ export * from "./player-provider";
4
+ export * from "./player-state";
5
+ export * from "./player-store";
6
+ export * from "./single-player-provider";
@@ -0,0 +1,52 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { useCallback, useMemo, useState } from "react";
3
+ import { PlayerContext } from "./context";
4
+ import { makePlayerStore } from "./player-store";
5
+ export const PlayerProvider = ({ children, initialPlayers = [], defaultId = Math.random().toString(36).slice(8), }) => {
6
+ const [players, setPlayers] = useState(() => {
7
+ // Initialize with any initial player IDs provided
8
+ const initialPlayerStores = {};
9
+ for (const playerId of initialPlayers) {
10
+ initialPlayerStores[playerId] = makePlayerStore(playerId);
11
+ }
12
+ // Always create at least one player by default
13
+ if (initialPlayers.length === 0) {
14
+ initialPlayerStores[defaultId] = makePlayerStore(defaultId);
15
+ }
16
+ return initialPlayerStores;
17
+ });
18
+ const createPlayer = useCallback((id) => {
19
+ const playerId = id || Math.random().toString(36).slice(8);
20
+ const playerStore = makePlayerStore(playerId);
21
+ setPlayers((prev) => ({
22
+ ...prev,
23
+ [playerId]: playerStore,
24
+ }));
25
+ return playerId;
26
+ }, []);
27
+ const removePlayer = useCallback((id) => {
28
+ setPlayers((prev) => {
29
+ // Don't remove the last player
30
+ if (Object.keys(prev).length <= 1) {
31
+ console.warn("Cannot remove the last player");
32
+ return prev;
33
+ }
34
+ const newPlayers = { ...prev };
35
+ delete newPlayers[id];
36
+ return newPlayers;
37
+ });
38
+ }, []);
39
+ const contextValue = useMemo(() => ({
40
+ players,
41
+ createPlayer,
42
+ removePlayer,
43
+ }), [players, createPlayer, removePlayer]);
44
+ return (_jsx(PlayerContext.Provider, { value: contextValue, children: children }));
45
+ };
46
+ // HOC to wrap components that need player context
47
+ export function withPlayerProvider(Component) {
48
+ return function WithPlayerProvider(props) {
49
+ const { initialPlayers, ...componentProps } = props;
50
+ return (_jsx(PlayerProvider, { initialPlayers: initialPlayers, children: _jsx(Component, { ...componentProps }) }));
51
+ };
52
+ }
@@ -0,0 +1,22 @@
1
+ export var PlayerProtocol;
2
+ (function (PlayerProtocol) {
3
+ PlayerProtocol["WEBRTC"] = "webrtc";
4
+ PlayerProtocol["HLS"] = "hls";
5
+ PlayerProtocol["PROGRESSIVE_MP4"] = "progressive-mp4";
6
+ PlayerProtocol["PROGRESSIVE_WEBM"] = "progressive-webm";
7
+ })(PlayerProtocol || (PlayerProtocol = {}));
8
+ export var PlayerStatus;
9
+ (function (PlayerStatus) {
10
+ PlayerStatus["START"] = "start";
11
+ PlayerStatus["PLAYING"] = "playing";
12
+ PlayerStatus["STALLED"] = "stalled";
13
+ PlayerStatus["SUSPEND"] = "suspend";
14
+ PlayerStatus["WAITING"] = "waiting";
15
+ PlayerStatus["PAUSE"] = "pause";
16
+ PlayerStatus["MUTE"] = "mute";
17
+ })(PlayerStatus || (PlayerStatus = {}));
18
+ export var IngestMediaSource;
19
+ (function (IngestMediaSource) {
20
+ IngestMediaSource["USER"] = "user";
21
+ IngestMediaSource["DISPLAY"] = "display";
22
+ })(IngestMediaSource || (IngestMediaSource = {}));