import React, { useState, useEffect } from 'react'; import { UserPicture, PagePathLabel } from '@growi/ui'; import { IUser } from 'src/interfaces/user'; import { IInAppNotification } from '../../interfaces/in-app-notification'; // import { PageUpdateNotification, PageCommentNotification } from './NotificationContent'; import { apiv3Post } from '../../client/util/apiv3-client'; import FormattedDistanceDate from '../FormattedDistanceDate'; import loggerFactory from '~/utils/logger'; const logger = loggerFactory('growi:InAppNotificationElm'); interface Props { notification: IInAppNotification } const InAppNotificationElm = (props: Props): JSX.Element => { const { notification } = props; const getActionUsers = () => { const latestActionUsers = notification.actionUsers.slice(0, 3); const latestUsers = latestActionUsers.map((user) => { return `@${user.name}`; }); let actionedUsers = ''; const latestUsersCount = latestUsers.length; if (latestUsersCount === 1) { actionedUsers = latestUsers[0]; } else if (notification.actionUsers.length >= 4) { actionedUsers = `${latestUsers.slice(0, 2).join(', ')} and ${notification.actionUsers.length - 2} others`; } else { actionedUsers = latestUsers.join(', '); } return actionedUsers; }; const renderUserPicture = (): JSX.Element => { const actionUsers = notification.actionUsers; if (actionUsers.length < 1) { return <>; } if (actionUsers.length === 1) { return ; } return (
); }; const notificationClickHandler = async() => { try { // set notification status "STATUS_OPEND" await apiv3Post('/in-app-notification/open', { id: notification._id }); // jump to target page window.location.href = notification.target.path; } catch (err) { logger.error(err); } }; const actionUsers = getActionUsers(); const pagePath = { path: props.notification.target.path }; const actionType: string = notification.action; let actionMsg: string; switch (actionType) { case 'PAGE_UPDATE': actionMsg = 'updated on'; break; case 'COMMENT_CREATE': actionMsg = 'commented on'; break; default: actionMsg = ''; } return ( <>
{renderUserPicture()}
notificationClickHandler()}>
{actionUsers} {actionMsg}
); }; export default InAppNotificationElm;