2022-11-28 21:18:41 -06:00
|
|
|
import { ChangeEventHandler, useContext, useRef } from 'react'
|
2022-11-24 00:16:34 -06:00
|
|
|
import Box from '@mui/material/Box'
|
2023-09-12 09:35:32 -05:00
|
|
|
import Folder from '@mui/icons-material/Folder'
|
|
|
|
import FolderOff from '@mui/icons-material/FolderOff'
|
2022-11-24 00:16:34 -06:00
|
|
|
import Tooltip from '@mui/material/Tooltip'
|
2023-02-11 17:29:57 -06:00
|
|
|
import CircularProgress from '@mui/material/CircularProgress'
|
2022-11-24 00:16:34 -06:00
|
|
|
|
2022-11-28 21:18:41 -06:00
|
|
|
import { RoomContext } from 'contexts/RoomContext'
|
2024-01-28 20:46:59 -06:00
|
|
|
import { PeerRoom } from 'lib/PeerRoom'
|
2022-11-24 00:16:34 -06:00
|
|
|
|
2024-04-09 20:54:41 -05:00
|
|
|
import { Input } from 'components/Elements'
|
|
|
|
|
2022-11-24 00:16:34 -06:00
|
|
|
import { useRoomFileShare } from './useRoomFileShare'
|
2023-09-12 09:35:32 -05:00
|
|
|
import { MediaButton } from './MediaButton'
|
2022-11-24 00:16:34 -06:00
|
|
|
|
|
|
|
export interface RoomFileUploadControlsProps {
|
2022-11-28 21:18:41 -06:00
|
|
|
onInlineMediaUpload: (files: File[]) => void
|
2022-11-24 00:16:34 -06:00
|
|
|
peerRoom: PeerRoom
|
|
|
|
}
|
|
|
|
|
|
|
|
export function RoomFileUploadControls({
|
|
|
|
peerRoom,
|
2022-11-28 21:18:41 -06:00
|
|
|
onInlineMediaUpload,
|
2022-11-24 00:16:34 -06:00
|
|
|
}: RoomFileUploadControlsProps) {
|
2022-11-28 21:18:41 -06:00
|
|
|
const roomContext = useContext(RoomContext)
|
2022-11-24 00:16:34 -06:00
|
|
|
const fileInputRef = useRef<HTMLInputElement>(null)
|
|
|
|
|
2022-11-28 21:18:41 -06:00
|
|
|
const { isMessageSending } = roomContext
|
|
|
|
|
2022-11-24 00:16:34 -06:00
|
|
|
const {
|
2022-11-28 21:18:41 -06:00
|
|
|
isFileSharingEnabled,
|
2022-11-24 00:16:34 -06:00
|
|
|
isSharingFile,
|
|
|
|
handleFileShareStart,
|
|
|
|
handleFileShareStop,
|
|
|
|
sharedFiles,
|
|
|
|
} = useRoomFileShare({
|
|
|
|
peerRoom,
|
2022-11-28 21:18:41 -06:00
|
|
|
onInlineMediaUpload,
|
2022-11-24 00:16:34 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
const handleToggleScreenShareButtonClick = () => {
|
|
|
|
const { current: fileInput } = fileInputRef
|
|
|
|
|
|
|
|
if (isSharingFile) {
|
|
|
|
handleFileShareStop()
|
|
|
|
} else {
|
|
|
|
if (!fileInput) return
|
|
|
|
|
|
|
|
fileInput.click()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleFileSelect: ChangeEventHandler<HTMLInputElement> = e => {
|
|
|
|
const { files } = e.target
|
|
|
|
|
2023-07-25 09:42:53 -05:00
|
|
|
if (!files || files.length < 1) return
|
2022-11-24 00:16:34 -06:00
|
|
|
|
|
|
|
handleFileShareStart(files)
|
|
|
|
}
|
|
|
|
|
|
|
|
const shareFileLabel =
|
|
|
|
(sharedFiles && sharedFiles.length === 1 && sharedFiles[0].name) || 'files'
|
|
|
|
|
2022-11-28 21:18:41 -06:00
|
|
|
const disableFileUpload = !isFileSharingEnabled || isMessageSending
|
|
|
|
|
2023-09-12 09:35:32 -05:00
|
|
|
const buttonIcon = isSharingFile ? <Folder /> : <FolderOff />
|
2023-02-11 17:29:57 -06:00
|
|
|
|
2022-11-24 00:16:34 -06:00
|
|
|
return (
|
|
|
|
<Box
|
|
|
|
sx={{
|
|
|
|
alignItems: 'center',
|
|
|
|
display: 'flex',
|
|
|
|
flexDirection: 'column',
|
|
|
|
justifyContent: 'center',
|
|
|
|
px: 1,
|
|
|
|
}}
|
|
|
|
>
|
2024-04-09 20:54:41 -05:00
|
|
|
<Input
|
2022-11-24 00:16:34 -06:00
|
|
|
multiple
|
|
|
|
ref={fileInputRef}
|
|
|
|
type="file"
|
|
|
|
id="file-upload"
|
2024-04-09 20:54:41 -05:00
|
|
|
sx={{ display: 'none' }}
|
2022-11-24 00:16:34 -06:00
|
|
|
onChange={handleFileSelect}
|
|
|
|
/>
|
|
|
|
<Tooltip
|
|
|
|
title={
|
|
|
|
isSharingFile
|
|
|
|
? `Stop sharing ${shareFileLabel}`
|
|
|
|
: 'Share files with the room'
|
|
|
|
}
|
|
|
|
>
|
2023-09-12 09:35:32 -05:00
|
|
|
<MediaButton
|
|
|
|
isActive={isSharingFile}
|
2022-11-24 00:16:34 -06:00
|
|
|
aria-label="share screen"
|
|
|
|
onClick={handleToggleScreenShareButtonClick}
|
2022-11-28 21:18:41 -06:00
|
|
|
disabled={disableFileUpload}
|
2022-11-24 00:16:34 -06:00
|
|
|
>
|
2023-02-11 17:29:57 -06:00
|
|
|
{isFileSharingEnabled ? (
|
|
|
|
buttonIcon
|
|
|
|
) : (
|
|
|
|
<CircularProgress variant="indeterminate" color="inherit" />
|
|
|
|
)}
|
2023-09-12 09:35:32 -05:00
|
|
|
</MediaButton>
|
2022-11-24 00:16:34 -06:00
|
|
|
</Tooltip>
|
|
|
|
</Box>
|
|
|
|
)
|
|
|
|
}
|