import { Button, Dialog, DialogActions, DialogContent, DialogTitle, FormControlLabel, Switch, } from '@material-ui/core' import { SelectInput, SimpleForm, useCreate, useGetList, useNotify, } from 'react-admin' import { useMemo, useState } from 'react' import { shareUrl } from '../utils' export const ShareDialog = ({ open, close, onClose, ids, resource, title }) => { const notify = useNotify() const [format, setFormat] = useState('') const [maxBitRate, setMaxBitRate] = useState(0) const [originalFormat, setUseOriginalFormat] = useState(true) const { data: formats, loading } = useGetList( 'transcoding', { page: 1, perPage: 1000, }, { field: 'name', order: 'ASC' } ) const formatOptions = useMemo( () => loading ? [] : Object.values(formats).map((f) => { return { id: f.targetFormat, name: f.targetFormat } }), [formats, loading] ) const handleOriginal = (e) => { const original = e.target.checked setUseOriginalFormat(original) if (original) { setFormat('') setMaxBitRate(0) } } const [createShare] = useCreate( 'share', { resourceType: resource, resourceIds: ids?.join(','), format, maxBitRate, }, { onSuccess: (res) => { const url = shareUrl(res?.data?.id) close() navigator.clipboard .writeText(url) .then(() => { notify(`URL copied to clipboard: ${url}`, { type: 'info', multiLine: true, duration: 0, }) }) .catch((err) => { notify(`Error copying URL ${url} to clipboard: ${err.message}`, { type: 'warning', multiLine: true, duration: 0, }) }) }, onFailure: (error) => notify(`Error sharing media: ${error.message}`, { type: 'warning' }), } ) return ( {title} } label={'Share in original format'} onChange={handleOriginal} /> {!originalFormat && ( { setFormat(event.target.value) }} /> )} {!originalFormat && ( { setMaxBitRate(event.target.value) }} /> )} ) }