import React, { useEffect, useState, useMemo, useRef, Fragment } from 'react'; import { HMSConfig } from '@100mslive/hms-video'; import { HMSRoomState, selectIsLocalAudioEnabled, selectIsLocalVideoDisplayEnabled, selectLocalPeer, selectRoomState, selectIsAllowedToPublish, } from '@100mslive/hms-video-store'; import { useHMSTheme } from '../../hooks/HMSThemeProvider'; import { Button } from '../Button'; import { ProgressIcon, DotIcon } from '../Icons'; import { VideoTile, VideoTileProps } from '../VideoTile'; import { VideoTileClasses } from '../VideoTile/VideoTile'; import { PreviewControls } from './Controls'; import { Input } from '../Input'; import { Text } from '../Text'; import { ButtonClasses } from '../Button/Button'; import { hmsUiClassParserGenerator } from '../../utils/classes'; import { useHMSActions, useHMSStore } from '../../hooks/HMSRoomProvider'; import { isBrowser } from '../../utils/is-browser'; import { isMobileDevice, isSafari } from '../../utils'; interface JoinInfo { audioMuted?: boolean; videoMuted?: boolean; name?: string; } export interface PreviewClasses { root?: string; containerRoot?: string; header?: string; messageModal?: string; helloDiv?: string; nameDiv?: string; inputRoot?: string; joinButton?: Partial; videoTile?: Partial; } const defaultClasses: PreviewClasses = { root: 'flex w-screen h-full mls:h-auto bg-white dark:bg-black justify-center items-center', containerRoot: 'flex flex-col justify-center items-center w-37.5 h-full md:h-400 pb-4 box-border bg-gray-700 dark:bg-gray-100 text-gray-100 dark:text-white overflow-hidden md:rounded-2xl', header: 'w-4/5 h-2/5 md:w-22.5 md:h-22.5 mt-1.875 mb-7 grid place-items-center', helloDiv: 'text-2xl font-medium mb-2', nameDiv: 'text-lg leading-6 mb-2', inputRoot: 'p-2 mb-1', }; export interface PreviewProps { config: HMSConfig; joinOnClick: ({ audioMuted, videoMuted, name }: JoinInfo) => void; videoTileProps?: Partial; onNameChange?: (name: string) => void; /** * extra classes added by user */ classes?: Partial; } export const Preview = ({ config, joinOnClick, videoTileProps, classes, onNameChange, }: PreviewProps) => { const { tw } = useHMSTheme(); const localPeer = useHMSStore(selectLocalPeer); const hmsActions = useHMSActions(); const roomState = useHMSStore(selectRoomState); const styler = useMemo( () => hmsUiClassParserGenerator({ tw, classes, defaultClasses, tag: 'hmsui-preview', }), [], ); /** This is to show error message only when input it touched or button is clicked */ const [showValidation, setShowValidation] = useState(false); const [inProgress, setInProgress] = useState(false); const audioEnabled = useHMSStore(selectIsLocalAudioEnabled); const videoEnabled = useHMSStore(selectIsLocalVideoDisplayEnabled); const isAllowedToPublish = useHMSStore(selectIsAllowedToPublish); const [name, setName] = useState(config.userName || ''); const inputRef = useRef(null); useEffect(() => { // Call preview only when roomState is in disconnected state if (roomState === HMSRoomState.Disconnected) { hmsActions.preview(config); } if (isBrowser) { window.onunload = () => hmsActions.leave(); } }, [config.authToken, roomState]); const inputProps = { compact: true, onChange: (e: React.ChangeEvent) => { setName(e.target.value); onNameChange?.(e.target.value); setShowValidation(true); }, value: name, validation: showValidation && (!name || !name.replace(/\u200b/g, ' ').trim()) ? 'Please enter name' : '', required: true, }; const disableJoin = inProgress || roomState !== HMSRoomState.Preview; return ( {/* root */}
{/* header */}
{/* videoTile */} {localPeer ? ( } /> ) : ( )}
{/* helloDiv */}
Hi There
{/* nameDiv */}
What's your name?
{/* inputFieldRoot */}
{/*Warning for safari audio output change*/} {isMobileDevice() && isSafari() && (
Warning: The audio output can be redirected to earpiece
)} {/* joinButton */}
); };