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() {
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 await createStreamRecord(title);
24 }
25 } catch (error) {
26 console.error("Error creating livestream:", error);
27 throw new Error("Failed to create livestream record");
28 } finally {
29 setRecordSubmitted(false);
30 }
31 };
32
33 const toggleGoLive = (
34 keyboardHeight?: number,
35 closeKeyboard?: () => void,
36 ) => {
37 if (!ingestStarting) {
38 // Optionally close keyboard if provided
39 if (closeKeyboard) closeKeyboard();
40 setShowCountdown(true);
41 setIngestStarting(true);
42 setIngestLive(true);
43 // wait ~3 seconds before announcing
44 setTimeout(() => {
45 handleSubmit();
46 }, 3000);
47 } else {
48 setIngestStarting(false);
49 setIngestLive(false);
50 }
51 };
52
53 return {
54 ingest,
55 profile,
56 title,
57 setTitle,
58 showCountdown,
59 setShowCountdown,
60 recordSubmitted,
61 setRecordSubmitted,
62 ingestStarting,
63 setIngestStarting,
64 handleSubmit,
65 toggleGoLive,
66 };
67}