1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-08-03 20:45:27 +02:00
planka/client/src/components/Header/NotificationsPopup.jsx

129 lines
3.7 KiB
React
Raw Normal View History

2022-11-20 15:30:07 +01:00
import truncate from 'lodash/truncate';
2019-08-31 04:07:25 +05:00
import React, { useCallback } from 'react';
import PropTypes from 'prop-types';
import { useTranslation, Trans } from 'react-i18next';
import { Link } from 'react-router-dom';
import { Button } from 'semantic-ui-react';
import { withPopup } from '../../lib/popup';
import { Popup } from '../../lib/custom-ui';
import Paths from '../../constants/Paths';
2022-08-04 13:31:14 +02:00
import { ActivityTypes } from '../../constants/Enums';
2019-08-31 04:07:25 +05:00
import User from '../User';
import styles from './NotificationsPopup.module.scss';
2019-08-31 04:07:25 +05:00
const NotificationsStep = React.memo(({ items, onDelete, onClose }) => {
const [t] = useTranslation();
const handleDelete = useCallback(
2020-03-25 00:15:47 +05:00
(id) => {
2019-08-31 04:07:25 +05:00
onDelete(id);
},
[onDelete],
);
const renderItemContent = useCallback(
2022-08-04 13:31:14 +02:00
({ activity, card }) => {
switch (activity.type) {
case ActivityTypes.MOVE_CARD:
2019-08-31 04:07:25 +05:00
return (
<Trans
i18nKey="common.userMovedCardFromListToList"
values={{
2022-08-04 13:31:14 +02:00
user: activity.user.name,
2019-08-31 04:07:25 +05:00
card: card.name,
2022-08-04 13:31:14 +02:00
fromList: activity.data.fromList.name,
toList: activity.data.toList.name,
2019-08-31 04:07:25 +05:00
}}
>
2022-08-04 13:31:14 +02:00
{activity.user.name}
2019-08-31 04:07:25 +05:00
{' moved '}
<Link to={Paths.CARDS.replace(':id', card.id)} onClick={onClose}>
{card.name}
</Link>
{' from '}
2022-08-04 13:31:14 +02:00
{activity.data.fromList.name}
2019-08-31 04:07:25 +05:00
{' to '}
2022-08-04 13:31:14 +02:00
{activity.data.toList.name}
2019-08-31 04:07:25 +05:00
</Trans>
);
2022-11-20 15:30:07 +01:00
case ActivityTypes.COMMENT_CARD: {
const commentText = truncate(activity.data.text);
2019-08-31 04:07:25 +05:00
return (
<Trans
i18nKey="common.userLeftNewCommentToCard"
values={{
2022-08-04 13:31:14 +02:00
user: activity.user.name,
2022-11-20 15:30:07 +01:00
comment: commentText,
2019-08-31 04:07:25 +05:00
card: card.name,
}}
>
2022-08-04 13:31:14 +02:00
{activity.user.name}
2022-11-20 15:30:07 +01:00
{` left a new comment «${commentText}» to `}
2019-08-31 04:07:25 +05:00
<Link to={Paths.CARDS.replace(':id', card.id)} onClick={onClose}>
{card.name}
</Link>
</Trans>
);
2022-11-20 15:30:07 +01:00
}
2019-08-31 04:07:25 +05:00
default:
}
return null;
},
[onClose],
);
return (
<>
2022-08-23 21:06:50 +02:00
<Popup.Header>
{t('common.notifications', {
context: 'title',
})}
</Popup.Header>
2019-08-31 04:07:25 +05:00
<Popup.Content>
2022-11-20 15:30:07 +01:00
{items.length > 0 ? (
<div className={styles.wrapper}>
{items.map((item) => (
<div key={item.id} className={styles.item}>
2022-08-04 13:31:14 +02:00
{item.card && item.activity ? (
<>
<User
2022-08-04 13:31:14 +02:00
name={item.activity.user.name}
avatarUrl={item.activity.user.avatarUrl}
size="large"
/>
2022-11-20 15:30:07 +01:00
<span className={styles.itemContent}>{renderItemContent(item)}</span>
</>
) : (
2022-11-20 15:30:07 +01:00
<div className={styles.itemDeleted}>{t('common.cardOrActionAreDeleted')}</div>
)}
<Button
type="button"
2022-11-20 15:30:07 +01:00
icon="trash alternate outline"
className={styles.itemButton}
onClick={() => handleDelete(item.id)}
/>
</div>
2022-11-20 15:30:07 +01:00
))}
</div>
) : (
t('common.noUnreadNotifications')
)}
2019-08-31 04:07:25 +05:00
</Popup.Content>
</>
);
});
NotificationsStep.propTypes = {
items: PropTypes.array.isRequired, // eslint-disable-line react/forbid-prop-types
onDelete: PropTypes.func.isRequired,
onClose: PropTypes.func.isRequired,
};
export default withPopup(NotificationsStep, {
position: 'bottom right',
});