import React, { FC, useRef, } from 'react'; import type { IUser, IPage, HasObjectId } from '@growi/core'; import { UserPicture } from '@growi/ui/dist/components'; import { DropdownItem } from 'reactstrap'; import { IInAppNotificationOpenable } from '~/client/interfaces/in-app-notification-openable'; import { apiv3Post } from '~/client/util/apiv3-client'; import { SupportedTargetModel } from '~/interfaces/activity'; import { IInAppNotification, InAppNotificationStatuses } from '~/interfaces/in-app-notification'; // Change the display for each targetmodel import PageModelNotification from './PageNotification/PageModelNotification'; import UserModelNotification from './PageNotification/UserModelNotification'; interface Props { notification: IInAppNotification & HasObjectId elemClassName?: string, type?: 'button' | 'dropdown-item', } const InAppNotificationElm: FC = (props: Props) => { const { notification } = props; const notificationRef = useRef(null); const clickHandler = async(notification: IInAppNotification & HasObjectId): Promise => { if (notification.status === InAppNotificationStatuses.STATUS_UNOPENED) { // set notification status "OPEND" await apiv3Post('/in-app-notification/open', { id: notification._id }); } const currentInstance = notificationRef.current; if (currentInstance != null) { currentInstance.open(); } }; const renderActionUserPictures = (): JSX.Element => { const actionUsers = notification.actionUsers; if (actionUsers.length < 1) { return <>; } if (actionUsers.length === 1) { return ; } return (
); }; const isDropdownItem = props.type === 'dropdown-item'; const isPageNotification = (notification: IInAppNotification): notification is IInAppNotification => { return notification.targetModel === SupportedTargetModel.MODEL_PAGE; }; const isUserNotification = (notification: IInAppNotification): notification is IInAppNotification => { return notification.targetModel === SupportedTargetModel.MODEL_USER; }; // determine tag const TagElem = isDropdownItem ? DropdownItem // eslint-disable-next-line react/prop-types : props => ; return ( clickHandler(notification)}>
{renderActionUserPictures()} {isPageNotification(notification) && ( )} {isUserNotification(notification) && ( )}
); }; export default InAppNotificationElm;