Add tab=custom to jump straight to custom connection (#43)
With the goal of fully replacing example.livekit
This commit is contained in:
parent
7f18b9574c
commit
d6d6f99288
1 changed files with 37 additions and 6 deletions
|
@ -1,11 +1,16 @@
|
||||||
import type { NextPage } from 'next';
|
import type { NextPage } from 'next';
|
||||||
import { useRouter } from 'next/router';
|
import { useRouter } from 'next/router';
|
||||||
import React, { ReactElement, ReactNode } from 'react';
|
import React, { ReactElement, useCallback, useEffect, useState } from 'react';
|
||||||
import { useState } from 'react';
|
|
||||||
import styles from '../styles/Home.module.css';
|
import styles from '../styles/Home.module.css';
|
||||||
|
|
||||||
function Tabs(props: { children: ReactElement[] }) {
|
interface TabsProps {
|
||||||
const [activeIndex, setActiveIndex] = useState(0);
|
children: ReactElement[];
|
||||||
|
selectedIndex?: number;
|
||||||
|
onTabSelected?: (index: number) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
function Tabs(props: TabsProps) {
|
||||||
|
const activeIndex = props.selectedIndex ?? 0;
|
||||||
if (!props.children) {
|
if (!props.children) {
|
||||||
return <></>;
|
return <></>;
|
||||||
}
|
}
|
||||||
|
@ -14,7 +19,9 @@ function Tabs(props: { children: ReactElement[] }) {
|
||||||
return (
|
return (
|
||||||
<button
|
<button
|
||||||
className="lk-button"
|
className="lk-button"
|
||||||
onClick={() => setActiveIndex(index)}
|
onClick={() => {
|
||||||
|
if (props.onTabSelected) props.onTabSelected(index);
|
||||||
|
}}
|
||||||
aria-pressed={activeIndex === index}
|
aria-pressed={activeIndex === index}
|
||||||
>
|
>
|
||||||
{child?.props.label}
|
{child?.props.label}
|
||||||
|
@ -79,6 +86,30 @@ function CustomConnectionTab({ label }: { label: string }) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const Home: NextPage = () => {
|
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 (
|
return (
|
||||||
<>
|
<>
|
||||||
<main className={styles.main} data-lk-theme="default">
|
<main className={styles.main} data-lk-theme="default">
|
||||||
|
@ -89,7 +120,7 @@ const Home: NextPage = () => {
|
||||||
and Next.js.
|
and Next.js.
|
||||||
</h2>
|
</h2>
|
||||||
</div>
|
</div>
|
||||||
<Tabs>
|
<Tabs selectedIndex={tabIndex} onTabSelected={onTabSelected}>
|
||||||
<DemoMeetingTab label="Demo" />
|
<DemoMeetingTab label="Demo" />
|
||||||
<CustomConnectionTab label="Custom" />
|
<CustomConnectionTab label="Custom" />
|
||||||
</Tabs>
|
</Tabs>
|
||||||
|
|
Loading…
Reference in a new issue