2023-03-01 16:05:39 +01:00
|
|
|
import {
|
|
|
|
Box,
|
|
|
|
Typography,
|
|
|
|
styled,
|
|
|
|
Avatar,
|
|
|
|
ListItemButton,
|
|
|
|
useTheme,
|
|
|
|
} from '@mui/material';
|
2023-02-27 13:04:09 +01:00
|
|
|
import {
|
|
|
|
NotificationsSchemaItem,
|
|
|
|
NotificationsSchemaItemNotificationType,
|
|
|
|
} from 'openapi';
|
|
|
|
import { ReactComponent as ChangesAppliedIcon } from 'assets/icons/merge.svg';
|
|
|
|
import TimeAgo from 'react-timeago';
|
|
|
|
import { ToggleOffOutlined } from '@mui/icons-material';
|
2023-03-01 16:05:39 +01:00
|
|
|
import { flexRow } from 'themes/themeStyles';
|
2023-02-27 13:04:09 +01:00
|
|
|
|
|
|
|
const StyledContainerBox = styled(Box, {
|
2023-10-02 14:25:46 +02:00
|
|
|
shouldForwardProp: (prop) => prop !== 'readAt',
|
2023-02-27 13:04:09 +01:00
|
|
|
})<{ readAt: boolean }>(({ theme, readAt }) => ({
|
|
|
|
padding: theme.spacing(0.5),
|
2023-03-22 15:37:40 +01:00
|
|
|
marginRight: theme.spacing(1.5),
|
2023-02-27 13:04:09 +01:00
|
|
|
backgroundColor: readAt
|
|
|
|
? theme.palette.neutral.light
|
|
|
|
: theme.palette.secondary.light,
|
|
|
|
width: '30px',
|
|
|
|
height: '30px',
|
|
|
|
display: 'flex',
|
|
|
|
justifyContent: 'center',
|
|
|
|
alignItems: 'center',
|
|
|
|
borderRadius: `${theme.shape.borderRadius}px`,
|
|
|
|
top: 3,
|
|
|
|
left: 7,
|
|
|
|
}));
|
|
|
|
|
2023-03-01 14:28:05 +01:00
|
|
|
const StyledListItemButton = styled(ListItemButton)(({ theme }) => ({
|
2023-02-27 13:04:09 +01:00
|
|
|
position: 'relative',
|
|
|
|
cursor: 'pointer',
|
2023-03-22 15:37:40 +01:00
|
|
|
padding: theme.spacing(2.5, 2, 3, 2),
|
|
|
|
borderRadius: theme.shape.borderRadiusMedium,
|
2023-02-27 13:04:09 +01:00
|
|
|
width: '100%',
|
2023-03-22 15:37:40 +01:00
|
|
|
'&:after': {
|
|
|
|
content: '""',
|
|
|
|
width: `calc(100% - ${theme.spacing(4)})`,
|
|
|
|
height: '1px',
|
|
|
|
position: 'absolute',
|
|
|
|
bottom: 0,
|
|
|
|
backgroundColor: theme.palette.divider,
|
|
|
|
},
|
2023-02-27 13:04:09 +01:00
|
|
|
}));
|
|
|
|
|
|
|
|
const StyledNotificationMessageBox = styled(Box)(({ theme }) => ({
|
|
|
|
display: 'flex',
|
|
|
|
flexDirection: 'column',
|
|
|
|
width: '100%',
|
|
|
|
}));
|
|
|
|
|
|
|
|
const StyledSecondaryInfoBox = styled(Box)(({ theme }) => ({
|
|
|
|
display: 'flex',
|
2023-03-01 16:05:39 +01:00
|
|
|
justifyContent: 'space-between',
|
|
|
|
alignItems: 'center',
|
2023-03-22 15:37:40 +01:00
|
|
|
margin: theme.spacing(1, 0, 0, 5.25),
|
2023-02-27 13:04:09 +01:00
|
|
|
}));
|
|
|
|
|
|
|
|
const StyledMessageTypography = styled(Typography, {
|
2023-10-02 14:25:46 +02:00
|
|
|
shouldForwardProp: (prop) => prop !== 'readAt',
|
2023-02-27 13:04:09 +01:00
|
|
|
})<{ readAt: boolean }>(({ theme, readAt }) => ({
|
2023-03-22 15:37:40 +01:00
|
|
|
display: 'flex',
|
|
|
|
alignItems: 'center',
|
2023-02-27 13:04:09 +01:00
|
|
|
fontSize: theme.fontSizes.smallBody,
|
|
|
|
fontWeight: readAt ? 'normal' : 'bold',
|
|
|
|
textDecoration: 'none',
|
|
|
|
color: 'inherit',
|
2023-03-28 12:27:47 +02:00
|
|
|
wordBreak: 'break-all',
|
2023-02-27 13:04:09 +01:00
|
|
|
}));
|
|
|
|
|
|
|
|
const StyledTimeAgoTypography = styled(Typography)(({ theme }) => ({
|
|
|
|
fontSize: theme.fontSizes.smallerBody,
|
|
|
|
color: theme.palette.neutral.main,
|
|
|
|
}));
|
|
|
|
|
2023-03-01 16:05:39 +01:00
|
|
|
const StyledUserContainer = styled(Box)(({ theme }) => ({
|
|
|
|
...flexRow,
|
|
|
|
}));
|
|
|
|
|
|
|
|
const StyledAvatar = styled(Avatar)(({ theme }) => ({
|
|
|
|
width: '18px',
|
|
|
|
height: '18px',
|
|
|
|
}));
|
|
|
|
|
|
|
|
const StyledCreatedBy = styled(Typography)(({ theme }) => ({
|
2023-03-22 15:37:40 +01:00
|
|
|
fontSize: theme.fontSizes.smallBody,
|
2023-03-01 16:05:39 +01:00
|
|
|
color: theme.palette.neutral.main,
|
|
|
|
marginLeft: theme.spacing(1),
|
|
|
|
}));
|
|
|
|
|
2023-02-27 13:04:09 +01:00
|
|
|
interface INotificationProps {
|
|
|
|
notification: NotificationsSchemaItem;
|
|
|
|
onNotificationClick: (notification: NotificationsSchemaItem) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const Notification = ({
|
|
|
|
notification,
|
|
|
|
onNotificationClick,
|
|
|
|
}: INotificationProps) => {
|
|
|
|
const theme = useTheme();
|
|
|
|
const { readAt } = notification;
|
|
|
|
|
|
|
|
const resolveIcon = (type: NotificationsSchemaItemNotificationType) => {
|
|
|
|
if (type === 'change-request') {
|
|
|
|
return (
|
|
|
|
<StyledContainerBox readAt={Boolean(readAt)}>
|
|
|
|
<ChangesAppliedIcon
|
|
|
|
color={
|
|
|
|
notification.readAt
|
|
|
|
? theme.palette.neutral.main
|
|
|
|
: theme.palette.primary.main
|
|
|
|
}
|
|
|
|
style={{ transform: 'scale(0.8)' }}
|
|
|
|
/>
|
|
|
|
</StyledContainerBox>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === 'toggle') {
|
|
|
|
return (
|
|
|
|
<StyledContainerBox readAt={Boolean(readAt)}>
|
|
|
|
<ToggleOffOutlined
|
2023-10-02 14:25:46 +02:00
|
|
|
sx={(theme) => ({
|
2023-02-27 13:04:09 +01:00
|
|
|
height: '20px',
|
|
|
|
width: '20px',
|
2023-10-02 14:25:46 +02:00
|
|
|
color: readAt
|
2023-02-27 13:04:09 +01:00
|
|
|
? theme.palette.neutral.main
|
|
|
|
: theme.palette.primary.main,
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
</StyledContainerBox>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2023-03-01 14:28:05 +01:00
|
|
|
<StyledListItemButton onClick={() => onNotificationClick(notification)}>
|
2023-02-27 13:04:09 +01:00
|
|
|
<StyledNotificationMessageBox>
|
|
|
|
<StyledMessageTypography readAt={Boolean(readAt)}>
|
2023-03-22 15:37:40 +01:00
|
|
|
{resolveIcon(notification.notificationType)}{' '}
|
2023-02-27 13:04:09 +01:00
|
|
|
{notification.message}
|
|
|
|
</StyledMessageTypography>
|
|
|
|
<StyledSecondaryInfoBox>
|
2023-03-01 16:05:39 +01:00
|
|
|
<StyledUserContainer>
|
|
|
|
<StyledAvatar
|
|
|
|
src={notification.createdBy.imageUrl || ''}
|
|
|
|
/>
|
|
|
|
<StyledCreatedBy>
|
|
|
|
Created by {notification.createdBy.username}
|
|
|
|
</StyledCreatedBy>
|
|
|
|
</StyledUserContainer>
|
|
|
|
|
2023-02-27 13:04:09 +01:00
|
|
|
<StyledTimeAgoTypography>
|
2023-08-15 13:35:38 +02:00
|
|
|
<TimeAgo
|
|
|
|
date={new Date(notification.createdAt)}
|
|
|
|
minPeriod={60}
|
|
|
|
/>
|
2023-02-27 13:04:09 +01:00
|
|
|
</StyledTimeAgoTypography>
|
|
|
|
</StyledSecondaryInfoBox>
|
|
|
|
</StyledNotificationMessageBox>
|
2023-03-01 14:28:05 +01:00
|
|
|
</StyledListItemButton>
|
2023-02-27 13:04:09 +01:00
|
|
|
);
|
|
|
|
};
|