Files
ThreadNet-Web/src/components/structures/ToastContainer.tsx
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

91 lines
2.8 KiB
TypeScript
Raw Normal View History

2019-11-20 16:18:28 +01:00
/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
2020-01-16 20:23:47 +00:00
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
2019-11-20 16:18:28 +01:00
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
2024-09-09 14:57:16 +01:00
Please see LICENSE files in the repository root for full details.
2019-11-20 16:18:28 +01:00
*/
2025-03-19 10:39:52 +00:00
import React from "react";
2019-11-20 16:18:28 +01:00
import classNames from "classnames";
2024-07-30 13:57:15 +01:00
import { Text } from "@vector-im/compound-web";
2025-02-05 13:25:06 +00:00
import { type EmptyObject } from "matrix-js-sdk/src/matrix";
2021-10-22 17:23:32 -05:00
2025-02-05 13:25:06 +00:00
import ToastStore, { type IToast } from "../../stores/ToastStore";
2019-11-20 16:18:28 +01:00
interface IState {
toasts: IToast<any>[];
}
export default class ToastContainer extends React.Component<EmptyObject, IState> {
public constructor(props: EmptyObject) {
2023-02-13 11:39:16 +00:00
super(props);
this.state = {
toasts: ToastStore.sharedInstance().getToasts(),
};
}
2019-11-20 16:18:28 +01:00
public componentDidMount(): void {
ToastStore.sharedInstance().on("update", this.onToastStoreUpdate);
this.onToastStoreUpdate();
2019-11-20 16:18:28 +01:00
}
public componentWillUnmount(): void {
ToastStore.sharedInstance().removeListener("update", this.onToastStoreUpdate);
2019-11-20 16:18:28 +01:00
}
private onToastStoreUpdate = (): void => {
this.setState({
toasts: ToastStore.sharedInstance().getToasts(),
});
2019-11-20 16:18:28 +01:00
};
public render(): React.ReactNode {
2019-11-20 16:18:28 +01:00
const totalCount = this.state.toasts.length;
const isStacked = totalCount > 1;
let toast;
2021-05-20 16:11:33 +01:00
let containerClasses;
if (totalCount !== 0) {
const topToast = this.state.toasts[0];
const { title, icon, key, component, className, bodyClassName, props } = topToast;
2021-07-26 12:21:58 +02:00
const bodyClasses = classNames("mx_Toast_body", bodyClassName);
const toastClasses = classNames("mx_Toast_toast", className, {
mx_Toast_hasIcon: !!icon,
2021-07-26 12:21:58 +02:00
});
const toastProps = Object.assign({}, props, {
key,
2020-01-16 20:23:47 +00:00
toastKey: key,
});
2021-07-24 13:04:06 +02:00
const content = React.createElement(component, toastProps);
2021-07-26 12:21:58 +02:00
let titleElement;
if (title) {
titleElement = (
2021-07-24 13:04:06 +02:00
<div className="mx_Toast_title">
2024-07-30 13:57:15 +01:00
<Text size="lg" weight="semibold" as="h2">
{title}
</Text>
2021-07-24 13:04:06 +02:00
</div>
2021-07-26 12:21:58 +02:00
);
}
toast = (
<div className={toastClasses}>
{icon}
2021-07-26 12:21:58 +02:00
{titleElement}
<div className={bodyClasses}>{content}</div>
</div>
);
2021-05-20 16:11:33 +01:00
containerClasses = classNames("mx_ToastContainer", {
mx_ToastContainer_stacked: isStacked,
});
}
2021-05-20 16:11:33 +01:00
return toast ? (
<div className={containerClasses} role="alert">
{toast}
2021-05-20 16:11:33 +01:00
</div>
) : null;
2019-11-20 16:18:28 +01:00
}
}