Live video on the AT Protocol
1import { useState } from "react";
2import { useLivestreamStore } from "../livestream-store";
3import { usePlayerStore } from "../player-store";
4import { useCreateStreamRecord } from "../streamplace-store";
5
6export function useLivestreamInfo(url?: string) {
7 const ingest = usePlayerStore((x) => x.ingestConnectionState);
8 const profile = useLivestreamStore((x) => x.profile);
9 const ingestStarting = usePlayerStore((x) => x.ingestStarting);
10 const setIngestStarting = usePlayerStore((x) => x.setIngestStarting);
11 const setIngestLive = usePlayerStore((x) => x.setIngestLive);
12
13 const createStreamRecord = useCreateStreamRecord();
14
15 const [title, setTitle] = useState<string>("");
16 const [showCountdown, setShowCountdown] = useState(false);
17 const [recordSubmitted, setRecordSubmitted] = useState(false);
18
19 const handleSubmit = async () => {
20 try {
21 if (title !== "") {
22 setRecordSubmitted(true);
23 // Create the livestream record with title and custom url if available
24 await createStreamRecord({
25 title,
26 canonicalUrl: url || undefined,
27 });
28 }
29 } catch (error) {
30 console.error("Error creating livestream:", error);
31 throw new Error("Failed to create livestream record");
32 } finally {
33 setRecordSubmitted(false);
34 }
35 };
36
37 const toggleGoLive = (
38 keyboardHeight?: number,
39 closeKeyboard?: () => void,
40 ) => {
41 if (!ingestStarting) {
42 // Optionally close keyboard if provided
43 if (closeKeyboard) closeKeyboard();
44 setShowCountdown(true);
45 setIngestStarting(true);
46 setIngestLive(true);
47 // wait ~3 seconds before announcing
48 setTimeout(() => {
49 handleSubmit();
50 }, 3000);
51 } else {
52 setIngestStarting(false);
53 setIngestLive(false);
54 }
55 };
56
57 return {
58 ingest,
59 profile,
60 title,
61 setTitle,
62 showCountdown,
63 setShowCountdown,
64 recordSubmitted,
65 setRecordSubmitted,
66 ingestStarting,
67 setIngestStarting,
68 handleSubmit,
69 toggleGoLive,
70 };
71}