|
| 1 | +import { DisconnectButton, TrackToggle, useConnectionState, useLocalParticipant } from '@livekit/components-react'; |
| 2 | +import ScreenShareOff from '../icons/ScreenShareOff'; |
| 3 | +import ScreenShareIcon from '../icons/ScreenShareIcon'; |
| 4 | +import mediaStyle from '@/styles/Components.module.css'; |
| 5 | +import { ConnectionState, Track } from 'livekit-client'; |
| 6 | +import { useSetConnectionState, useSetCurrentRoomId, useSetCurrentRoomName, useSetCurrentRoomServerId, useSetToken, useConnectionRef, useSetUserSettings, useUserSettings } from '@/lib/store'; |
| 7 | +import { Channel } from '@/types/dbtypes'; |
| 8 | +import CameraOffIcon from '../icons/CameraOffIcon'; |
| 9 | +import CameraIcon from '../icons/CameraIcon'; |
| 10 | +import JoinCallIcon from '../icons/JoinCallIcon'; |
| 11 | +import HangUpIcon from '../icons/HangUpIcon'; |
| 12 | +import MicrophoneIcon from '../icons/MicrophoneIcon'; |
| 13 | +import MicrophoneOff from '../icons/MicroPhoneOff'; |
| 14 | + |
| 15 | +export function FloatingCallControl({ visibleChannel, token }: { visibleChannel?: Channel, token?: string }) { |
| 16 | + const connectionState = useConnectionState(); |
| 17 | + const currentParticipant = useLocalParticipant(); |
| 18 | + const setConnectionState = useSetConnectionState(); |
| 19 | + const setToken = useSetToken(); |
| 20 | + const setRoomServerId = useSetCurrentRoomServerId(); |
| 21 | + const setRoomIdRef = useSetCurrentRoomId(); |
| 22 | + const setRoomName = useSetCurrentRoomName(); |
| 23 | + const settingsRef = useSetUserSettings(); |
| 24 | + const userSettings = useUserSettings(); |
| 25 | + |
| 26 | + const handleConnect = () => { |
| 27 | + setConnectionState(true), |
| 28 | + setToken(token), |
| 29 | + setRoomIdRef(visibleChannel?.channel_id); |
| 30 | + setRoomName(visibleChannel?.name); |
| 31 | + setRoomServerId(visibleChannel?.server_id); |
| 32 | + }; |
| 33 | + |
| 34 | + return ( |
| 35 | + <div |
| 36 | + className={` |
| 37 | + flex |
| 38 | + flex-row |
| 39 | + justify-evenly |
| 40 | + ${mediaStyle.mediaControls} |
| 41 | + min-w-0 |
| 42 | + bg-grey-950 |
| 43 | + items-center |
| 44 | + rounded-xl |
| 45 | + inset-x-1 |
| 46 | + mx-auto |
| 47 | + py-3 |
| 48 | + z-10 |
| 49 | + `} |
| 50 | + draggable |
| 51 | + > |
| 52 | + {connectionState !== ConnectionState.Connected ? ( |
| 53 | + <button disabled className='w-7 h-7 bg-grey-900 rounded-lg text-lg flex items-center justify-center'> |
| 54 | + <CameraIcon className='text-grey-600'/> |
| 55 | + </button>) : ( |
| 56 | + <TrackToggle |
| 57 | + showIcon={false} |
| 58 | + className={ |
| 59 | + 'w-7 h-7 bg-grey-900 hover:bg-grey-800 rounded-lg text-lg flex items-center justify-center' |
| 60 | + } |
| 61 | + source={Track.Source.Camera} |
| 62 | + > |
| 63 | + {currentParticipant.isCameraEnabled ? ( |
| 64 | + <CameraOffIcon |
| 65 | + /> |
| 66 | + ) : ( |
| 67 | + <CameraIcon |
| 68 | + /> |
| 69 | + )} |
| 70 | + </TrackToggle> |
| 71 | + )} |
| 72 | + {connectionState !== ConnectionState.Connected ? ( |
| 73 | + <button disabled |
| 74 | + className={`w-7 h-7 bg-grey-900 rounded-lg text-lg flex items-center justify-center ${mediaStyle.disappear}`}> |
| 75 | + <ScreenShareIcon className='text-grey-600'/> |
| 76 | + </button>) : ( |
| 77 | + <TrackToggle |
| 78 | + showIcon={false} |
| 79 | + className={ |
| 80 | + `w-7 h-7 bg-grey-900 hover:bg-grey-800 rounded-lg text-lg flex items-center justify-center ${mediaStyle.disappear}` |
| 81 | + } |
| 82 | + source={Track.Source.ScreenShare} |
| 83 | + disabled={true} |
| 84 | + > |
| 85 | + {currentParticipant.isScreenShareEnabled ? ( |
| 86 | + <ScreenShareOff /> |
| 87 | + ) : ( |
| 88 | + <ScreenShareIcon /> |
| 89 | + )} |
| 90 | + </TrackToggle> |
| 91 | + )} |
| 92 | + {connectionState !== ConnectionState.Connected ? ( |
| 93 | + <> |
| 94 | + {' '} |
| 95 | + {userSettings ? ( |
| 96 | + <button |
| 97 | + onClick={() => settingsRef(false)} |
| 98 | + className={'w-7 h-7 rounded-lg p-1 bg-grey-900 hover:bg-grey-800 flex items-center justify-center'} |
| 99 | + > |
| 100 | + <MicrophoneIcon width={6} height={6}/> |
| 101 | + </button> |
| 102 | + ) : ( |
| 103 | + <button |
| 104 | + onClick={() => settingsRef(true)} |
| 105 | + className={'w-7 h-7 rounded-lg p-1 bg-grey-900 hover:bg-grey-800 flex items-center justify-center'} |
| 106 | + > |
| 107 | + <MicrophoneOff width={6} height={6} /> |
| 108 | + </button> |
| 109 | + )} |
| 110 | + </> |
| 111 | + ) : ( |
| 112 | + <TrackToggle |
| 113 | + showIcon={false} |
| 114 | + source={Track.Source.Microphone} |
| 115 | + onClick={() => settingsRef(false)} |
| 116 | + className='w-7 h-7 rounded-lg p-1 bg-grey-900 hover:bg-grey-800 flex items-center justify-center' |
| 117 | + > |
| 118 | + {currentParticipant.isMicrophoneEnabled ? ( |
| 119 | + <MicrophoneIcon width={6} height={6}/> |
| 120 | + ) : ( |
| 121 | + <MicrophoneOff width={6} height={6} /> |
| 122 | + )} |
| 123 | + </TrackToggle> |
| 124 | + )} |
| 125 | + {connectionState !== ConnectionState.Connected ? ( |
| 126 | + <button |
| 127 | + className="w-7 h-7 bg-green-500 hover:bg-green-700 rounded-lg font-bold text-md flex items-center justify-center" |
| 128 | + onClick={() => { |
| 129 | + handleConnect(); |
| 130 | + }} |
| 131 | + > |
| 132 | + <JoinCallIcon width={6} height={6} /> |
| 133 | + </button> |
| 134 | + ) : ( |
| 135 | + <DisconnectButton |
| 136 | + className={ |
| 137 | + 'w-7 h-7 bg-red-500 hover:bg-red-700 rounded-lg flex items-center justify-center' |
| 138 | + } |
| 139 | + onClick={() => { |
| 140 | + setConnectionState(false); |
| 141 | + }} |
| 142 | + > |
| 143 | + <HangUpIcon width={6} height={6} /> |
| 144 | + </DisconnectButton> |
| 145 | + )} |
| 146 | + </div> |
| 147 | + ); |
| 148 | +} |
0 commit comments