quintodrome/ui/src/share/ShareList.jsx
Deluan Quintão fcdd30ba8f
build(ui): migrate from CRA/Jest to Vite/Vitest (#3311)
* feat: create vite project

* feat: it's alive!

* feat: `make dev` working!

* feat: replace custom serviceWorker with vite plugin

* test: replace Jest with Vitest

* fix: run prettier

* fix: skip eslint for now.

* chore: remove ui.old folder

* refactor: replace lodash.pick with simple destructuring

* fix: eslint errors (wip)

* fix: eslint errors (wip)

* fix: display-name eslint errors (wip)

* fix: no-console eslint errors (wip)

* fix: react-refresh/only-export-components eslint errors (wip)

* fix: react-refresh/only-export-components eslint errors (wip)

* fix: react-refresh/only-export-components eslint errors (wip)

* fix: react-refresh/only-export-components eslint errors (wip)

* fix: build

* fix: pwa manifest

* refactor: pwa manifest

* refactor: simplify PORT configuration

* refactor: rename simple JS files

* test: cover playlistUtils

* fix: react-image-lightbox

* feat(ui): add sourcemaps to help debug issues
2024-09-28 11:54:36 -04:00

120 lines
3.4 KiB
JavaScript

import {
Datagrid,
FunctionField,
BooleanField,
List,
NumberField,
SimpleList,
TextField,
useNotify,
useTranslate,
} from 'react-admin'
import React from 'react'
import { IconButton, Link, useMediaQuery } from '@material-ui/core'
import ShareIcon from '@material-ui/icons/Share'
import { DateField, QualityInfo } from '../common'
import { sharePlayerUrl } from '../utils'
import config from '../config'
export const FormatInfo = ({ record, size }) => {
const r = { suffix: record.format, bitRate: record.maxBitRate }
r.suffix =
r.suffix || (r.bitRate ? config.defaultDownsamplingFormat : 'Original')
return <QualityInfo record={r} size={size} />
}
const ShareList = (props) => {
const isXsmall = useMediaQuery((theme) => theme.breakpoints.down('xs'))
const isDesktop = useMediaQuery((theme) => theme.breakpoints.up('lg'))
const translate = useTranslate()
const notify = useNotify()
const handleShare = (r) => (e) => {
const url = sharePlayerUrl(r?.id)
if (navigator.clipboard && window.isSecureContext) {
navigator.clipboard
.writeText(url)
.then(() => {
notify(translate('message.shareSuccess', { url }), {
type: 'info',
multiLine: true,
duration: 0,
})
})
.catch((err) => {
notify(
translate('message.shareFailure', { url }) + ': ' + err.message,
{
type: 'warning',
multiLine: true,
duration: 0,
},
)
})
} else prompt(translate('message.shareCopyToClipboard'), url)
e.preventDefault()
e.stopPropagation()
}
return (
<List
{...props}
sort={{ field: 'createdAt', order: 'DESC' }}
exporter={false}
>
{isXsmall ? (
<SimpleList
leftIcon={(r) => (
<IconButton onClick={handleShare(r)}>
<ShareIcon />
</IconButton>
)}
primaryText={(r) => r.description || r.contents || r.id}
secondaryText={(r) => (
<>
{translate('resources.share.fields.expiresAt')}:{' '}
<DateField record={r} source={'expiresAt'} />
</>
)}
tertiaryText={(r) =>
`${translate('resources.share.fields.visitCount')}: ${
r.visitCount || '0'
}`
}
/>
) : (
<Datagrid rowClick="edit">
<FunctionField
source={'id'}
render={(r) => (
<Link
href={sharePlayerUrl(r.id)}
label="URL"
target="_blank"
rel="noopener noreferrer"
onClick={(e) => {
e.stopPropagation()
}}
>
{r.id}
</Link>
)}
/>
<TextField source="username" />
<TextField source="description" />
{isDesktop && <TextField source="contents" />}
{isDesktop && <FormatInfo source="format" />}
{config.enableDownloads && <BooleanField source="downloadable" />}
<NumberField source="visitCount" />
{isDesktop && (
<DateField source="lastVisitedAt" showTime sortByOrder={'DESC'} />
)}
<DateField source="expiresAt" showTime />
</Datagrid>
)}
</List>
)
}
export default ShareList