|
| 1 | +import React, { useState } from 'react' |
| 2 | +import PropTypes from 'prop-types' |
| 3 | + |
| 4 | +import { useClient, models } from 'cozy-client' |
| 5 | + |
| 6 | +import Alert from '../../Alert' |
| 7 | +import Button from '../../Buttons' |
| 8 | +import Link from '../../Link' |
| 9 | +import Typography from '../../Typography' |
| 10 | +import { withViewerLocales } from '../hoc/withViewerLocales' |
| 11 | +import { useI18n } from '../../I18n' |
| 12 | +import { formatLocallyDistanceToNow } from '../../I18n/format' |
| 13 | + |
| 14 | +const FILES_DOCTYPE = 'io.cozy.files' |
| 15 | + |
| 16 | +const { computeExpirationDate, computeExpirationNoticeLink } = models.paper |
| 17 | + |
| 18 | +const ExpirationAlert = ({ file }) => { |
| 19 | + const { t } = useI18n() |
| 20 | + const client = useClient() |
| 21 | + const [isBusy, setIsBusy] = useState(false) |
| 22 | + |
| 23 | + const handleClose = async () => { |
| 24 | + setIsBusy(true) |
| 25 | + await client.collection(FILES_DOCTYPE).updateMetadataAttribute(file.id, { |
| 26 | + ...file.metadata, |
| 27 | + hideExpirationAlert: true |
| 28 | + }) |
| 29 | + setIsBusy(false) |
| 30 | + } |
| 31 | + |
| 32 | + const expirationDate = computeExpirationDate(file) |
| 33 | + const expirationNoticeLink = computeExpirationNoticeLink(file) |
| 34 | + |
| 35 | + return ( |
| 36 | + <Alert |
| 37 | + severity="warning" |
| 38 | + block |
| 39 | + action={ |
| 40 | + <Button |
| 41 | + color="warning" |
| 42 | + variant="text" |
| 43 | + size="small" |
| 44 | + busy={isBusy} |
| 45 | + label={t('Viewer.panel.expiration.dismiss')} |
| 46 | + onClick={handleClose} |
| 47 | + /> |
| 48 | + } |
| 49 | + className="u-mt-1 u-mh-1" |
| 50 | + > |
| 51 | + <Typography component="span" variant="inherit"> |
| 52 | + <Typography component="span" variant="inherit"> |
| 53 | + {t('Viewer.panel.expiration.description', { |
| 54 | + duration: formatLocallyDistanceToNow(expirationDate) |
| 55 | + })} |
| 56 | + </Typography> |
| 57 | + {expirationNoticeLink && ( |
| 58 | + <> |
| 59 | + <Typography component="span" variant="inherit"> |
| 60 | + {' : '} |
| 61 | + </Typography> |
| 62 | + <Link |
| 63 | + href={expirationNoticeLink} |
| 64 | + rel="noreferrer" |
| 65 | + target="_blank" |
| 66 | + className="u-warning" |
| 67 | + > |
| 68 | + {new URL(expirationNoticeLink).hostname} |
| 69 | + </Link> |
| 70 | + </> |
| 71 | + )} |
| 72 | + </Typography> |
| 73 | + </Alert> |
| 74 | + ) |
| 75 | +} |
| 76 | + |
| 77 | +ExpirationAlert.propTypes = { |
| 78 | + file: PropTypes.object.isRequired |
| 79 | +} |
| 80 | + |
| 81 | +export default withViewerLocales(ExpirationAlert) |
0 commit comments