1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-19 13:19:44 +02:00
planka/client/src/components/CardModal/Attachments/Item.jsx

140 lines
3.8 KiB
React
Raw Normal View History

2020-04-21 05:04:34 +05:00
import React, { useCallback } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { useTranslation } from 'react-i18next';
2020-04-23 03:02:53 +05:00
import { Button, Icon, Label, Loader } from 'semantic-ui-react';
2020-04-21 05:04:34 +05:00
import EditPopup from './EditPopup';
import styles from './Item.module.css';
const Item = React.memo(
2020-04-23 03:02:53 +05:00
({
name,
url,
coverUrl,
createdAt,
isCover,
isPersisted,
onCoverSelect,
onCoverDeselect,
onUpdate,
onDelete,
}) => {
2020-04-21 05:04:34 +05:00
const [t] = useTranslation();
const handleClick = useCallback(() => {
window.open(url, '_blank');
}, [url]);
2020-04-23 03:02:53 +05:00
const handleToggleCoverClick = useCallback(
(event) => {
event.stopPropagation();
if (isCover) {
onCoverDeselect();
} else {
onCoverSelect();
}
},
[isCover, onCoverSelect, onCoverDeselect],
);
2020-04-21 05:04:34 +05:00
if (!isPersisted) {
return (
<div className={classNames(styles.wrapper, styles.wrapperSubmitting)}>
<Loader inverted />
</div>
);
}
const filename = url.split('/').pop();
const extension = filename.slice((Math.max(0, filename.lastIndexOf('.')) || Infinity) + 1);
return (
/* eslint-disable jsx-a11y/click-events-have-key-events,
jsx-a11y/no-static-element-interactions */
<div className={styles.wrapper} onClick={handleClick}>
{/* eslint-enable jsx-a11y/click-events-have-key-events,
jsx-a11y/no-static-element-interactions */}
<div
className={styles.thumbnail}
style={{
2020-04-23 03:02:53 +05:00
backgroundImage: coverUrl && `url(${coverUrl}`,
2020-04-21 05:04:34 +05:00
}}
>
2020-04-23 03:02:53 +05:00
{coverUrl ? (
isCover && (
<Label corner="left" size="tiny" icon="star" className={styles.thumbnailLabel} />
)
) : (
<span className={styles.extension}>{extension || '-'}</span>
)}
2020-04-21 05:04:34 +05:00
</div>
<div className={styles.details}>
<span className={styles.name}>{name}</span>
2020-04-23 03:02:53 +05:00
<span className={styles.date}>
2020-04-21 05:04:34 +05:00
{t('format:longDateTime', {
postProcess: 'formatDate',
value: createdAt,
})}
</span>
2020-04-23 03:02:53 +05:00
{coverUrl && (
<span className={styles.options}>
<button type="button" className={styles.option} onClick={handleToggleCoverClick}>
<Icon
name="window maximize outline"
flipped="vertically"
size="small"
className={styles.optionIcon}
/>
<span className={styles.optionText}>
{isCover
? t('action.removeCover', {
context: 'title',
})
: t('action.makeCover', {
context: 'title',
})}
</span>
</button>
</span>
)}
2020-04-21 05:04:34 +05:00
</div>
<EditPopup
defaultData={{
name,
}}
onUpdate={onUpdate}
onDelete={onDelete}
>
<Button className={classNames(styles.button, styles.target)}>
<Icon fitted name="pencil" size="small" />
</Button>
</EditPopup>
</div>
);
},
);
Item.propTypes = {
name: PropTypes.string.isRequired,
url: PropTypes.string,
2020-04-23 03:02:53 +05:00
coverUrl: PropTypes.string,
2020-04-21 05:04:34 +05:00
createdAt: PropTypes.instanceOf(Date),
2020-04-23 03:02:53 +05:00
isCover: PropTypes.bool.isRequired,
2020-04-21 05:04:34 +05:00
isPersisted: PropTypes.bool.isRequired,
2020-04-23 03:02:53 +05:00
onCoverSelect: PropTypes.func.isRequired,
onCoverDeselect: PropTypes.func.isRequired,
2020-04-21 05:04:34 +05:00
onUpdate: PropTypes.func.isRequired,
onDelete: PropTypes.func.isRequired,
};
Item.defaultProps = {
url: undefined,
2020-04-23 03:02:53 +05:00
coverUrl: undefined,
2020-04-21 05:04:34 +05:00
createdAt: undefined,
};
export default Item;