2022-03-23 20:17:57 +00:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2023-03-17 12:58:41 +13:00
|
|
|
Copyright 2023 The Matrix.org Foundation C.I.C.
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2022 Michael Telatynski <7t3chguy@gmail.com>
|
2022-03-23 20:17:57 +00:00
|
|
|
|
2025-01-06 11:18:54 +00: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.
|
2022-03-23 20:17:57 +00:00
|
|
|
*/
|
|
|
|
|
|
2025-02-05 13:25:06 +00:00
|
|
|
import React, { createContext, type ReactNode, useState } from "react";
|
|
|
|
|
import { type Room } from "matrix-js-sdk/src/matrix";
|
2022-03-23 20:17:57 +00:00
|
|
|
import classNames from "classnames";
|
|
|
|
|
|
2025-02-05 13:25:06 +00:00
|
|
|
import { _t, type TranslationKey } from "../../../../languageHandler";
|
|
|
|
|
import { type XOR } from "../../../../@types/common";
|
|
|
|
|
import { type Tool } from "../DevtoolsDialog";
|
2022-03-23 20:17:57 +00:00
|
|
|
|
|
|
|
|
export interface IDevtoolsProps {
|
|
|
|
|
onBack(): void;
|
2023-10-11 23:21:03 +01:00
|
|
|
setTool(label: TranslationKey, tool: Tool): void;
|
2022-03-23 20:17:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface IMinProps extends Pick<IDevtoolsProps, "onBack"> {
|
|
|
|
|
className?: string;
|
2023-03-08 13:28:07 +00:00
|
|
|
children?: ReactNode;
|
2023-07-07 09:16:11 +01:00
|
|
|
extraButton?: ReactNode;
|
2022-03-23 20:17:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface IProps extends IMinProps {
|
2023-10-11 23:21:03 +01:00
|
|
|
actionLabel: TranslationKey;
|
2022-03-23 20:17:57 +00:00
|
|
|
onAction(): Promise<string | void>;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-07 09:16:11 +01:00
|
|
|
const BaseTool: React.FC<XOR<IMinProps, IProps>> = ({
|
|
|
|
|
className,
|
|
|
|
|
actionLabel,
|
|
|
|
|
onBack,
|
|
|
|
|
onAction,
|
|
|
|
|
children,
|
|
|
|
|
extraButton,
|
|
|
|
|
}) => {
|
2023-03-17 12:58:41 +13:00
|
|
|
const [message, setMessage] = useState<string | null>(null);
|
2022-03-23 20:17:57 +00:00
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
const onBackClick = (): void => {
|
2022-03-23 20:17:57 +00:00
|
|
|
if (message) {
|
|
|
|
|
setMessage(null);
|
|
|
|
|
} else {
|
|
|
|
|
onBack();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-03-17 12:58:41 +13:00
|
|
|
let actionButton: ReactNode = null;
|
2022-03-23 20:17:57 +00:00
|
|
|
if (message) {
|
|
|
|
|
children = message;
|
2024-03-19 17:55:06 +00:00
|
|
|
} else if (onAction && actionLabel) {
|
2023-01-12 13:25:14 +00:00
|
|
|
const onActionClick = (): void => {
|
2022-03-23 20:17:57 +00:00
|
|
|
onAction().then((msg) => {
|
|
|
|
|
if (typeof msg === "string") {
|
|
|
|
|
setMessage(msg);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2023-10-11 23:21:03 +01:00
|
|
|
actionButton = <button onClick={onActionClick}>{_t(actionLabel)}</button>;
|
2022-03-23 20:17:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<div className={classNames("mx_DevTools_content", className)}>{children}</div>
|
|
|
|
|
<div className="mx_Dialog_buttons">
|
2023-07-07 09:16:11 +01:00
|
|
|
{extraButton}
|
2023-08-23 11:57:22 +01:00
|
|
|
<button onClick={onBackClick}>{_t("action|back")}</button>
|
2022-03-23 20:17:57 +00:00
|
|
|
{actionButton}
|
|
|
|
|
</div>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default BaseTool;
|
|
|
|
|
|
|
|
|
|
interface IContext {
|
|
|
|
|
room: Room;
|
2023-07-07 08:40:25 -06:00
|
|
|
threadRootId?: string | null;
|
2022-03-23 20:17:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const DevtoolsContext = createContext<IContext>({} as IContext);
|