2020-05-22 13:47:40 +02:00
|
|
|
/*
|
2024-09-09 15:57:16 +02:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2020-05-22 13:47:40 +02:00
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 15:57:16 +02:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2020-05-22 13:47:40 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
import EventEmitter from "events";
|
2023-04-28 16:56:26 +02:00
|
|
|
import React from "react";
|
2021-10-23 00:23:32 +02:00
|
|
|
|
2020-07-29 20:43:35 +02:00
|
|
|
import { ComponentClass } from "../@types/common";
|
2020-05-22 13:47:40 +02:00
|
|
|
|
2020-07-29 20:43:35 +02:00
|
|
|
export interface IToast<C extends ComponentClass> {
|
2020-05-22 13:47:40 +02:00
|
|
|
key: string;
|
|
|
|
// higher priority number will be shown on top of lower priority
|
|
|
|
priority: number;
|
2021-07-24 13:04:06 +02:00
|
|
|
title?: string;
|
2020-05-22 13:47:40 +02:00
|
|
|
icon?: string;
|
|
|
|
component: C;
|
2020-11-09 15:21:45 +01:00
|
|
|
className?: string;
|
2021-07-26 12:21:58 +02:00
|
|
|
bodyClassName?: string;
|
2020-06-29 12:34:58 +02:00
|
|
|
props?: Omit<React.ComponentProps<C>, "toastKey">; // toastKey is injected by ToastContainer
|
2020-05-22 13:47:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Holds the active toasts
|
|
|
|
*/
|
|
|
|
export default class ToastStore extends EventEmitter {
|
|
|
|
private toasts: IToast<any>[] = [];
|
2020-05-22 15:28:01 +02: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 15:32:41 +02:00
|
|
|
private countSeen = 0;
|
2020-05-22 13:47:40 +02:00
|
|
|
|
2023-01-12 14:25:14 +01:00
|
|
|
public static sharedInstance(): ToastStore {
|
2020-07-20 21:43:49 +02:00
|
|
|
if (!window.mxToastStore) window.mxToastStore = new ToastStore();
|
|
|
|
return window.mxToastStore;
|
2020-05-22 13:47:40 +02:00
|
|
|
}
|
|
|
|
|
2023-01-12 14:25:14 +01:00
|
|
|
public reset(): void {
|
2020-05-22 13:47:40 +02:00
|
|
|
this.toasts = [];
|
2020-05-22 15:28:01 +02:00
|
|
|
this.countSeen = 0;
|
2020-05-22 13:47:40 +02: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
|
|
|
|
*/
|
2023-01-12 14:25:14 +01:00
|
|
|
public addOrReplaceToast<C extends ComponentClass>(newToast: IToast<C>): void {
|
2022-12-12 12:24:14 +01:00
|
|
|
const oldIndex = this.toasts.findIndex((t) => t.key === newToast.key);
|
2020-05-22 13:47:40 +02:00
|
|
|
if (oldIndex === -1) {
|
|
|
|
let newIndex = this.toasts.length;
|
2020-05-23 10:02:35 +02:00
|
|
|
while (newIndex > 0 && this.toasts[newIndex - 1].priority < newToast.priority) --newIndex;
|
2020-05-22 13:47:40 +02:00
|
|
|
this.toasts.splice(newIndex, 0, newToast);
|
|
|
|
} else {
|
|
|
|
this.toasts[oldIndex] = newToast;
|
|
|
|
}
|
2022-12-12 12:24:14 +01:00
|
|
|
this.emit("update");
|
2020-05-22 13:47:40 +02:00
|
|
|
}
|
|
|
|
|
2023-01-12 14:25:14 +01:00
|
|
|
public dismissToast(key: string): void {
|
2020-05-27 12:48:48 +02:00
|
|
|
if (this.toasts[0] && this.toasts[0].key === key) {
|
|
|
|
this.countSeen++;
|
|
|
|
}
|
|
|
|
|
2020-05-22 13:47:40 +02:00
|
|
|
const length = this.toasts.length;
|
2022-12-12 12:24:14 +01:00
|
|
|
this.toasts = this.toasts.filter((t) => t.key !== key);
|
2020-05-22 13:47:40 +02:00
|
|
|
if (length !== this.toasts.length) {
|
2020-05-22 15:28:01 +02:00
|
|
|
if (this.toasts.length === 0) {
|
|
|
|
this.countSeen = 0;
|
|
|
|
}
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
this.emit("update");
|
2020-05-22 13:47:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-12 14:25:14 +01:00
|
|
|
public getToasts(): IToast<any>[] {
|
2020-05-22 13:47:40 +02:00
|
|
|
return this.toasts;
|
|
|
|
}
|
2020-05-22 15:28:01 +02:00
|
|
|
|
2023-01-12 14:25:14 +01:00
|
|
|
public getCountSeen(): number {
|
2020-05-22 15:28:01 +02:00
|
|
|
return this.countSeen;
|
|
|
|
}
|
2020-05-22 13:47:40 +02:00
|
|
|
}
|