forked from livekit-examples/meet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
161 lines (148 loc) · 4.33 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import type { NextPage } from 'next';
import { useRouter } from 'next/router';
import React, { ReactElement, useCallback, useEffect, useState } from 'react';
import styles from '../styles/Home.module.css';
interface TabsProps {
children: ReactElement[];
selectedIndex?: number;
onTabSelected?: (index: number) => void;
}
function Tabs(props: TabsProps) {
const activeIndex = props.selectedIndex ?? 0;
if (!props.children) {
return <></>;
}
let tabs = React.Children.map(props.children, (child, index) => {
return (
<button
className="lk-button"
onClick={() => {
if (props.onTabSelected) props.onTabSelected(index);
}}
aria-pressed={activeIndex === index}
>
{child?.props.label}
</button>
);
});
return (
<div className={styles.tabContainer}>
<div className={styles.tabSelect}>{tabs}</div>
{props.children[activeIndex]}
</div>
);
}
function DemoMeetingTab({ label }: { label: string }) {
const router = useRouter();
const startMeeting = () => {
router.push(`/rooms/${generateRoomId()}`);
};
return (
<div className={styles.tabContent}>
<p style={{ marginTop: 0 }}>Try LiveKit Meet for free with our live demo project.</p>
<button className="lk-button" onClick={startMeeting}>
Start Meeting
</button>
</div>
);
}
function CustomConnectionTab({ label }: { label: string }) {
const [liveKitUrl, setLiveKitUrl] = useState<string | undefined>();
const [token, setToken] = useState<string | undefined>();
const router = useRouter();
const join = () => {
router.push(`/custom/?liveKitUrl=${liveKitUrl}&token=${token}`);
};
return (
<div className={styles.tabContent}>
<p style={{ marginTop: 0 }}>
Connect LiveKit Meet with a custom server using LiveKit Cloud or LiveKit Server.
</p>
{/* <label>LiveKit URL</label> */}
<input type="url" placeholder="URL" onChange={(ev) => setLiveKitUrl(ev.target.value)}></input>
{/* <label>Token</label> */}
<input type="text" placeholder="Token" onChange={(ev) => setToken(ev.target.value)}></input>
<hr
style={{ width: '100%', borderColor: 'rgba(255, 255, 255, 0.15)', marginBlock: '1rem' }}
/>
<button
style={{
paddingInline: '1.25rem',
width: '100%',
}}
className="lk-button"
onClick={join}
>
Connect
</button>
</div>
);
}
const Home: NextPage = () => {
const router = useRouter();
const [tabIndex, setTabIndex] = useState<number>(0);
const { tab } = router.query;
useEffect(() => {
if (tab === 'custom') {
setTabIndex(1);
} else {
setTabIndex(0);
}
}, [tab]);
const onTabSelected = useCallback((index: number) => {
setTabIndex(index);
const tab = index === 1 ? 'custom' : 'demo';
router.push(
{},
{ query: { tab } },
{
shallow: true,
},
);
}, []);
return (
<>
<main className={styles.main} data-lk-theme="default">
<div className="header">
<img src="/images/livekit-meet-home.svg" alt="LiveKit Meet" width="360" height="45" />
<h2>
Open source video conferencing app built on LiveKit Components, LiveKit Cloud,
and Next.js.
</h2>
</div>
<Tabs selectedIndex={tabIndex} onTabSelected={onTabSelected}>
<DemoMeetingTab label="Demo" />
<CustomConnectionTab label="Custom" />
</Tabs>
</main>
<footer data-lk-theme="default">
Hosted on{' '}
<a href="https://livekit.io/cloud?ref=meet" target="_blank" rel="noreferrer">
LiveKit Cloud
</a>
. Source code on{' '}
<a
href="https://github.com/livekit/components-js?ref=meet"
target="_blank"
rel="noreferrer"
>
GitHub
</a>
.
</footer>
</>
);
};
export default Home;
function generateRoomId(): string {
return `${randomString(4)}-${randomString(4)}`;
}
function randomString(length: number): string {
let result = '';
const characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}