navidrome/ui/src/dialogs/ShareDialog.js

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

106 lines
2.7 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,
useCreate,
useNotify,
useTranslate,
} from 'react-admin'
import { useState } from 'react'
2023-01-20 04:52:55 +01:00
import { shareUrl } from '../utils'
import { useTranscodingOptions } from './useTranscodingOptions'
2023-01-20 04:52:55 +01:00
export const ShareDialog = ({ open, onClose, ids, resource, name }) => {
2023-01-20 04:52:55 +01:00
const notify = useNotify()
const translate = useTranslate()
const [description, setDescription] = useState('')
const { TranscodingOptionsInput, format, maxBitRate, originalFormat } =
useTranscodingOptions()
2023-01-20 04:52:55 +01:00
const [createShare] = useCreate(
'share',
{
resourceType: resource,
resourceIds: ids?.join(','),
description,
...(!originalFormat && { format }),
...(!originalFormat && { maxBitRate }),
2023-01-20 04:52:55 +01:00
},
{
onSuccess: (res) => {
const url = shareUrl(res?.data?.id)
onClose()
2023-01-20 04:52:55 +01:00
navigator.clipboard
.writeText(url)
.then(() => {
2023-01-22 17:43:03 +01:00
notify(translate('message.shareSuccess', { url }), {
2023-01-20 04:52:55 +01:00
type: 'info',
multiLine: true,
duration: 0,
})
})
.catch((err) => {
2023-01-22 17:43:03 +01:00
notify(
translate('message.shareFailure', { url }) + ': ' + err.message,
{
type: 'warning',
multiLine: true,
duration: 0,
}
)
2023-01-20 04:52:55 +01:00
})
},
onFailure: (error) =>
notify(`Error sharing media: ${error.message}`, { type: 'warning' }),
}
)
return (
<Dialog
open={open}
onClose={onClose}
onBackdropClick={onClose}
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">
{translate('message.shareDialogTitle', {
resource: translate(`resources.${resource}.name`, {
smart_count: ids?.length,
}).toLocaleLowerCase(),
name,
})}
</DialogTitle>
2023-01-20 04:52:55 +01:00
<DialogContent>
<SimpleForm toolbar={null} variant={'outlined'}>
<TextInput
source="description"
fullWidth
onChange={(event) => {
setDescription(event.target.value)
}}
/>
<TranscodingOptionsInput
fullWidth
label={translate('message.shareOriginalFormat')}
/>
2023-01-20 04:52:55 +01:00
</SimpleForm>
</DialogContent>
<DialogActions>
<Button onClick={createShare} color="primary">
{translate('ra.action.share')}
2023-01-20 04:52:55 +01:00
</Button>
<Button onClick={onClose} color="primary">
{translate('ra.action.close')}
2023-01-20 04:52:55 +01:00
</Button>
</DialogActions>
</Dialog>
)
}