Introduce a concept of "non-urgent" toasts
This is somewhat expected to be temporary.
This commit is contained in:
parent
6a9fe35fa8
commit
14757cacd5
7 changed files with 159 additions and 3 deletions
50
src/stores/NonUrgentToastStore.ts
Normal file
50
src/stores/NonUrgentToastStore.ts
Normal file
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
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";
|
||||
import { ComponentClass } from "../@types/common";
|
||||
import { UPDATE_EVENT } from "./AsyncStore";
|
||||
|
||||
export type ToastReference = symbol;
|
||||
|
||||
export default class NonUrgentToastStore extends EventEmitter {
|
||||
private static _instance: NonUrgentToastStore;
|
||||
|
||||
private toasts = new Map<ToastReference, ComponentClass>();
|
||||
|
||||
public static get instance(): NonUrgentToastStore {
|
||||
if (!NonUrgentToastStore._instance) {
|
||||
NonUrgentToastStore._instance = new NonUrgentToastStore();
|
||||
}
|
||||
return NonUrgentToastStore._instance;
|
||||
}
|
||||
|
||||
public get components(): ComponentClass[] {
|
||||
return Array.from(this.toasts.values());
|
||||
}
|
||||
|
||||
public addToast(c: ComponentClass): ToastReference {
|
||||
const ref: ToastReference = Symbol();
|
||||
this.toasts.set(ref, c);
|
||||
this.emit(UPDATE_EVENT);
|
||||
return ref;
|
||||
}
|
||||
|
||||
public removeToast(ref: ToastReference) {
|
||||
this.toasts.delete(ref);
|
||||
this.emit(UPDATE_EVENT);
|
||||
}
|
||||
}
|
|
@ -15,9 +15,10 @@ limitations under the License.
|
|||
*/
|
||||
|
||||
import EventEmitter from "events";
|
||||
import React, {JSXElementConstructor} from "react";
|
||||
import React from "react";
|
||||
import { ComponentClass } from "../@types/common";
|
||||
|
||||
export interface IToast<C extends keyof JSX.IntrinsicElements | JSXElementConstructor<any>> {
|
||||
export interface IToast<C extends ComponentClass> {
|
||||
key: string;
|
||||
// higher priority number will be shown on top of lower priority
|
||||
priority: number;
|
||||
|
@ -55,7 +56,7 @@ export default class ToastStore extends EventEmitter {
|
|||
*
|
||||
* @param {object} newToast The new toast
|
||||
*/
|
||||
addOrReplaceToast<C extends keyof JSX.IntrinsicElements | JSXElementConstructor<any>>(newToast: IToast<C>) {
|
||||
addOrReplaceToast<C extends ComponentClass>(newToast: IToast<C>) {
|
||||
const oldIndex = this.toasts.findIndex(t => t.key === newToast.key);
|
||||
if (oldIndex === -1) {
|
||||
let newIndex = this.toasts.length;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue