1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/dashboard/client/components/notifications/notifications.tsx
Jari Kolehmainen db4dca3005 lens app source code
Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
2020-03-15 09:46:21 +02:00

97 lines
2.6 KiB
TypeScript

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, INotification, 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) {
notificationsStore.add({
message: message,
timeout: 2500,
status: "ok"
})
}
static error(message: IMessage) {
notificationsStore.add({
message: message,
timeout: 10000,
status: "error"
});
}
static info(message: IMessage) {
return notificationsStore.add({
message: message,
timeout: 0,
status: "info"
});
}
componentDidMount() {
disposeOnUnmount(this, [
reaction(() => notificationsStore.notifications.length, () => {
this.scrollToLastNotification();
}, { delay: 250 }),
]);
}
scrollToLastNotification() {
if (!this.elem) {
return;
}
this.elem.scrollTo({
top: this.elem.scrollHeight,
behavior: "smooth"
})
}
getMessage(notification: INotification) {
let { message } = notification;
if (message instanceof JsonApiErrorParsed) {
message = message.toString();
}
return React.Children.toArray(message);
}
render() {
const { notifications, remove, addAutoHideTimer, removeAutoHideTimer } = notificationsStore;
return (
<div className="Notifications flex column align-flex-end" ref={e => this.elem = e}>
{notifications.map(notification => {
const { id, status } = notification;
const msgText = this.getMessage(notification);
return (
<Animate key={id}>
<div
className={cssNames("notification flex align-center", status)}
onMouseLeave={() => addAutoHideTimer(notification)}
onMouseEnter={() => removeAutoHideTimer(notification)}>
<div className="box center">
<Icon material="info_outline"/>
</div>
<div className="message box grow">{msgText}</div>
<div className="box center">
<Icon
material="close" className="close"
onClick={prevDefault(() => remove(notification))}
/>
</div>
</div>
</Animate>
)
})}
</div>
)
}
}