2020-05-22 11:47:40 +00:00
|
|
|
/*
|
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import EventEmitter from "events";
|
2020-07-29 18:43:35 +00:00
|
|
|
import React from "react";
|
2021-10-22 22:23:32 +00:00
|
|
|
|
2020-07-29 18:43:35 +00:00
|
|
|
import { ComponentClass } from "../@types/common";
|
2020-05-22 11:47:40 +00:00
|
|
|
|
2020-07-29 18:43:35 +00:00
|
|
|
export interface IToast<C extends ComponentClass> {
|
2020-05-22 11:47:40 +00:00
|
|
|
key: string;
|
|
|
|
// higher priority number will be shown on top of lower priority
|
|
|
|
priority: number;
|
2021-07-24 11:04:06 +00:00
|
|
|
title?: string;
|
2020-05-22 11:47:40 +00:00
|
|
|
icon?: string;
|
|
|
|
component: C;
|
2020-11-09 14:21:45 +00:00
|
|
|
className?: string;
|
2021-07-26 10:21:58 +00:00
|
|
|
bodyClassName?: string;
|
2020-06-29 10:34:58 +00:00
|
|
|
props?: Omit<React.ComponentProps<C>, "toastKey">; // toastKey is injected by ToastContainer
|
2020-05-22 11:47:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Holds the active toasts
|
|
|
|
*/
|
|
|
|
export default class ToastStore extends EventEmitter {
|
|
|
|
private toasts: IToast<any>[] = [];
|
2020-05-22 13:28:01 +00:00
|
|
|
// The count of toasts which have been seen & dealt with in this stack
|
|
|
|
// where the count resets when the stack of toasts clears.
|
2020-05-22 13:32:41 +00:00
|
|
|
private countSeen = 0;
|
2020-05-22 11:47:40 +00:00
|
|
|
|
|
|
|
static sharedInstance() {
|
2020-07-20 19:43:49 +00:00
|
|
|
if (!window.mxToastStore) window.mxToastStore = new ToastStore();
|
|
|
|
return window.mxToastStore;
|
2020-05-22 11:47:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reset() {
|
|
|
|
this.toasts = [];
|
2020-05-22 13:28:01 +00:00
|
|
|
this.countSeen = 0;
|
2020-05-22 11:47:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add or replace a toast
|
|
|
|
* If a toast with the same toastKey already exists, the given toast will replace it
|
|
|
|
* Toasts are always added underneath any toasts of the same priority, so existing
|
|
|
|
* toasts stay at the top unless a higher priority one arrives (better to not change the
|
|
|
|
* toast unless necessary).
|
|
|
|
*
|
|
|
|
* @param {object} newToast The new toast
|
|
|
|
*/
|
2020-07-29 18:43:35 +00:00
|
|
|
addOrReplaceToast<C extends ComponentClass>(newToast: IToast<C>) {
|
2020-05-22 11:47:40 +00:00
|
|
|
const oldIndex = this.toasts.findIndex((t) => t.key === newToast.key);
|
|
|
|
if (oldIndex === -1) {
|
|
|
|
let newIndex = this.toasts.length;
|
2020-05-23 08:02:35 +00:00
|
|
|
while (newIndex > 0 && this.toasts[newIndex - 1].priority < newToast.priority) --newIndex;
|
2020-05-22 11:47:40 +00:00
|
|
|
this.toasts.splice(newIndex, 0, newToast);
|
|
|
|
} else {
|
|
|
|
this.toasts[oldIndex] = newToast;
|
|
|
|
}
|
|
|
|
this.emit("update");
|
|
|
|
}
|
|
|
|
|
|
|
|
dismissToast(key) {
|
2020-05-27 10:48:48 +00:00
|
|
|
if (this.toasts[0] && this.toasts[0].key === key) {
|
|
|
|
this.countSeen++;
|
|
|
|
}
|
|
|
|
|
2020-05-22 11:47:40 +00:00
|
|
|
const length = this.toasts.length;
|
|
|
|
this.toasts = this.toasts.filter((t) => t.key !== key);
|
|
|
|
if (length !== this.toasts.length) {
|
2020-05-22 13:28:01 +00:00
|
|
|
if (this.toasts.length === 0) {
|
|
|
|
this.countSeen = 0;
|
|
|
|
}
|
|
|
|
|
2020-05-22 11:47:40 +00:00
|
|
|
this.emit("update");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getToasts() {
|
|
|
|
return this.toasts;
|
|
|
|
}
|
2020-05-22 13:28:01 +00:00
|
|
|
|
|
|
|
getCountSeen() {
|
|
|
|
return this.countSeen;
|
|
|
|
}
|
2020-05-22 11:47:40 +00:00
|
|
|
}
|