import './notifications.scss'; import React from 'react'; import { reaction } from "mobx"; import { disposeOnUnmount, observer } from "mobx-react"; import { JsonApiErrorParsed } from "../../api/json-api"; import { cssNames, prevDefault } from "../../utils"; import { IMessage, Notification, notificationsStore } from "./notifications.store"; import { Animate } from "../animate"; import { Icon } from "../icon"; @observer export class Notifications extends React.Component { public elem: HTMLElement; static ok(message: IMessage): void { notificationsStore.add({ message: message, timeout: 2500, status: "ok" }); } static error(message: IMessage): void { notificationsStore.add({ message: message, timeout: 10000, status: "error" }); } static info(message: IMessage): void { return notificationsStore.add({ message: message, timeout: 0, status: "info" }); } componentDidMount(): void { disposeOnUnmount(this, [ reaction(() => notificationsStore.notifications.length, () => { this.scrollToLastNotification(); }, { delay: 250 }), ]); } scrollToLastNotification(): void { if (!this.elem) { return; } this.elem.scrollTo({ top: this.elem.scrollHeight, behavior: "smooth" }); } getMessage(notification: Notification): React.ReactNode[] { let { message } = notification; if (message instanceof JsonApiErrorParsed) { message = message.toString(); } return React.Children.toArray(message); } render(): JSX.Element { const { notifications, remove, addAutoHideTimer, removeAutoHideTimer } = notificationsStore; return (
{ this.elem = e; }}> {notifications.map(notification => { const { id, status } = notification; const msgText = this.getMessage(notification); return (
addAutoHideTimer(notification)} onMouseEnter={(): void => removeAutoHideTimer(notification)}>
{msgText}
remove(notification))} />
); })}
); } }