A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
// SPDX-License-Identifier: AGPL-3.0-or-later
import {Logger} from '@app/features/platform/utils/AppLogger';
import {randomUuid} from '@app/features/platform/utils/RandomUuid';
import {getActivePortalHost} from '@app/features/ui/overlay/PortalHostContext';
import type {ToastProps} from '@app/features/ui/toast';
import {makeAutoObservable, observableRef} from 'mobx';
const logger = new Logger('Toast');
interface ToastEntry {
id: string;
data: ToastProps;
ownerDocument: Document;
}
class Toast {
currentToast: ToastEntry | null = null;
constructor() {
makeAutoObservable(
this,
{
currentToast: observableRef,
},
{autoBind: true},
);
}
createToast(data: ToastProps): string {
const id = randomUuid();
logger.debug(`Creating toast: ${id}, type: ${data.type}`);
this.currentToast = {id, data, ownerDocument: getActivePortalHost()?.ownerDocument ?? document};
return id;
}
destroyToast(id: string): void {
if (this.currentToast?.id === id) {
logger.debug(`Destroying toast: ${id}`);
this.currentToast = null;
}
}
success(message: string): string {
return this.createToast({type: 'success', children: message, timeout: 3000});
}
error(message: string): string {
return this.createToast({type: 'error', children: message, timeout: 5000});
}
}
export default new Toast();