navidrome/ui/src/dialogs/ShareDialog.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

146 lines
3.8 KiB
JavaScript
Raw Normal View History

2023-01-20 04:52:55 +01:00
import {
Button,
Dialog,
DialogActions,
DialogContent,
DialogTitle,
} from '@material-ui/core'
import {
SimpleForm,
TextInput,
BooleanInput,
useCreate,
useNotify,
useTranslate,
} from 'react-admin'
import { useEffect, useState } from 'react'
2023-01-20 04:52:55 +01:00
import { shareUrl } from '../utils'
import { useTranscodingOptions } from './useTranscodingOptions'
2023-01-24 19:04:00 +01:00
import { useDispatch, useSelector } from 'react-redux'
import { closeShareMenu } from '../actions'
import config from '../config'
2023-01-20 04:52:55 +01:00
2023-01-24 19:04:00 +01:00
export const ShareDialog = () => {
2023-01-24 22:31:58 +01:00
const {
open,
ids,
resource,
name,
label = 'message.shareDialogTitle',
} = useSelector((state) => state.shareDialog)
2023-01-24 19:04:00 +01:00
const dispatch = useDispatch()
2023-01-20 04:52:55 +01:00
const notify = useNotify()
const translate = useTranslate()
const [description, setDescription] = useState('')
const [downloadable, setDownloadable] = useState(
config.defaultDownloadableShare
)
useEffect(() => {
setDescription('')
}, [ids])
const { TranscodingOptionsInput, format, maxBitRate, originalFormat } =
useTranscodingOptions()
2023-01-20 04:52:55 +01:00
const [createShare] = useCreate(
'share',
{
resourceType: resource,
resourceIds: ids?.join(','),
description,
downloadable,
...(!originalFormat && { format }),
...(!originalFormat && { maxBitRate }),
2023-01-20 04:52:55 +01:00
},
{
onSuccess: (res) => {
const url = shareUrl(res?.data?.id)
if (navigator.clipboard && window.isSecureContext) {
navigator.clipboard
.writeText(url)
.then(() => {
notify('message.shareSuccess', 'info', { url }, false, 0)
})
.catch((err) => {
notify(
translate('message.shareFailure', { url }) + ': ' + err.message,
{
type: 'warning',
multiLine: true,
duration: 0,
}
)
})
} else prompt(translate('message.shareCopyToClipboard'), url)
2023-01-20 04:52:55 +01:00
},
onFailure: (error) =>
2023-01-24 19:04:00 +01:00
notify(translate('ra.page.error') + ': ' + error.message, {
type: 'warning',
}),
2023-01-20 04:52:55 +01:00
}
)
2023-01-24 19:04:00 +01:00
const handleShare = (e) => {
createShare()
dispatch(closeShareMenu())
e.stopPropagation()
}
const handleClose = (e) => {
dispatch(closeShareMenu())
e.stopPropagation()
}
2023-01-20 04:52:55 +01:00
return (
<Dialog
open={open}
2023-01-24 19:04:00 +01:00
onClose={handleClose}
onBackdropClick={handleClose}
2023-01-21 01:53:53 +01:00
aria-labelledby="share-dialog"
2023-01-20 04:52:55 +01:00
fullWidth={true}
maxWidth={'sm'}
2023-01-20 04:52:55 +01:00
>
<DialogTitle id="share-dialog">
2023-01-24 19:04:00 +01:00
{resource &&
2023-01-24 22:31:58 +01:00
translate(label, {
2023-01-24 19:04:00 +01:00
resource: translate(`resources.${resource}.name`, {
smart_count: ids?.length,
}).toLocaleLowerCase(),
name,
2023-01-24 22:31:58 +01:00
smart_count: ids?.length,
2023-01-24 19:04:00 +01:00
})}
</DialogTitle>
2023-01-20 04:52:55 +01:00
<DialogContent>
<SimpleForm toolbar={null} variant={'outlined'}>
<TextInput
resource={'share'}
source={'description'}
fullWidth
onChange={(event) => {
setDescription(event.target.value)
}}
/>
<BooleanInput
resource={'share'}
source={'downloadable'}
defaultValue={downloadable}
onChange={(value) => {
setDownloadable(value)
}}
/>
<TranscodingOptionsInput
fullWidth
label={translate('message.shareOriginalFormat')}
/>
2023-01-20 04:52:55 +01:00
</SimpleForm>
</DialogContent>
<DialogActions>
2023-01-24 19:04:00 +01:00
<Button onClick={handleClose} color="primary">
{translate('ra.action.close')}
2023-01-20 04:52:55 +01:00
</Button>
2023-01-24 19:04:00 +01:00
<Button onClick={handleShare} color="primary">
{translate('ra.action.share')}
</Button>
2023-01-20 04:52:55 +01:00
</DialogActions>
</Dialog>
)
}