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
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import React, { useContext, useMemo, useState } from "react";
|
2025-02-05 13:25:06 +00:00
|
|
|
import { type AccountDataEvents, type IContent, type MatrixEvent } from "matrix-js-sdk/src/matrix";
|
2022-03-23 20:17:57 +00:00
|
|
|
|
2025-02-05 13:25:06 +00:00
|
|
|
import BaseTool, { DevtoolsContext, type IDevtoolsProps } from "./BaseTool";
|
2022-03-23 20:17:57 +00:00
|
|
|
import MatrixClientContext from "../../../../contexts/MatrixClientContext";
|
2025-02-05 13:25:06 +00:00
|
|
|
import { EventEditor, EventViewer, eventTypeField, type IEditorProps, stringify } from "./Event";
|
2022-03-23 20:17:57 +00:00
|
|
|
import FilteredList from "./FilteredList";
|
2026-01-07 11:49:01 +00:00
|
|
|
import { _td } from "../../../../languageHandler";
|
2022-03-23 20:17:57 +00:00
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
export const AccountDataEventEditor: React.FC<IEditorProps> = ({ mxEvent, onBack }) => {
|
2022-03-23 20:17:57 +00:00
|
|
|
const cli = useContext(MatrixClientContext);
|
|
|
|
|
|
|
|
|
|
const fields = useMemo(() => [eventTypeField(mxEvent?.getType())], [mxEvent]);
|
|
|
|
|
|
2024-12-19 22:53:51 +00:00
|
|
|
const onSend = async ([eventType]: Array<keyof AccountDataEvents>, content?: IContent): Promise<void> => {
|
2023-03-17 12:58:41 +13:00
|
|
|
await cli.setAccountData(eventType, content || {});
|
2022-03-23 20:17:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const defaultContent = mxEvent ? stringify(mxEvent.getContent()) : undefined;
|
|
|
|
|
return <EventEditor fieldDefs={fields} defaultContent={defaultContent} onSend={onSend} onBack={onBack} />;
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
export const RoomAccountDataEventEditor: React.FC<IEditorProps> = ({ mxEvent, onBack }) => {
|
2022-03-23 20:17:57 +00:00
|
|
|
const context = useContext(DevtoolsContext);
|
|
|
|
|
const cli = useContext(MatrixClientContext);
|
|
|
|
|
|
|
|
|
|
const fields = useMemo(() => [eventTypeField(mxEvent?.getType())], [mxEvent]);
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
const onSend = async ([eventType]: string[], content?: IContent): Promise<void> => {
|
2023-03-17 12:58:41 +13:00
|
|
|
await cli.setRoomAccountData(context.room.roomId, eventType, content || {});
|
2022-03-23 20:17:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const defaultContent = mxEvent ? stringify(mxEvent.getContent()) : undefined;
|
|
|
|
|
return <EventEditor fieldDefs={fields} defaultContent={defaultContent} onSend={onSend} onBack={onBack} />;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
interface IProps extends IDevtoolsProps {
|
2023-03-28 11:23:16 +01:00
|
|
|
events: Map<string, MatrixEvent>;
|
2022-03-23 20:17:57 +00:00
|
|
|
Editor: React.FC<IEditorProps>;
|
2023-10-11 23:21:03 +01:00
|
|
|
actionLabel: TranslationKey;
|
2022-03-23 20:17:57 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
const BaseAccountDataExplorer: React.FC<IProps> = ({ events, Editor, actionLabel, onBack, setTool }) => {
|
2022-03-23 20:17:57 +00:00
|
|
|
const [query, setQuery] = useState("");
|
2023-03-17 12:58:41 +13:00
|
|
|
const [event, setEvent] = useState<MatrixEvent | null>(null);
|
2022-03-23 20:17:57 +00:00
|
|
|
|
|
|
|
|
if (event) {
|
2023-01-12 13:25:14 +00:00
|
|
|
const onBack = (): void => {
|
2022-03-23 20:17:57 +00:00
|
|
|
setEvent(null);
|
|
|
|
|
};
|
|
|
|
|
return <EventViewer mxEvent={event} onBack={onBack} Editor={Editor} />;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
const onAction = async (): Promise<void> => {
|
2022-03-23 20:17:57 +00:00
|
|
|
setTool(actionLabel, Editor);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<BaseTool onBack={onBack} actionLabel={actionLabel} onAction={onAction}>
|
|
|
|
|
<FilteredList query={query} onChange={setQuery}>
|
2023-03-28 11:23:16 +01:00
|
|
|
{Array.from(events.entries()).map(([eventType, ev]) => {
|
2023-01-12 13:25:14 +00:00
|
|
|
const onClick = (): void => {
|
2022-03-23 20:17:57 +00:00
|
|
|
setEvent(ev);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<button className="mx_DevTools_button" key={eventType} onClick={onClick}>
|
|
|
|
|
{eventType}
|
|
|
|
|
</button>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</FilteredList>
|
|
|
|
|
</BaseTool>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
export const AccountDataExplorer: React.FC<IDevtoolsProps> = ({ onBack, setTool }) => {
|
2022-03-23 20:17:57 +00:00
|
|
|
const cli = useContext(MatrixClientContext);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<BaseAccountDataExplorer
|
|
|
|
|
events={cli.store.accountData}
|
|
|
|
|
Editor={AccountDataEventEditor}
|
2023-10-11 23:21:03 +01:00
|
|
|
actionLabel={_td("devtools|send_custom_account_data_event")}
|
2022-03-23 20:17:57 +00:00
|
|
|
onBack={onBack}
|
|
|
|
|
setTool={setTool}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
export const RoomAccountDataExplorer: React.FC<IDevtoolsProps> = ({ onBack, setTool }) => {
|
2022-03-23 20:17:57 +00:00
|
|
|
const context = useContext(DevtoolsContext);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<BaseAccountDataExplorer
|
|
|
|
|
events={context.room.accountData}
|
|
|
|
|
Editor={RoomAccountDataEventEditor}
|
2023-10-11 23:21:03 +01:00
|
|
|
actionLabel={_td("devtools|send_custom_room_account_data_event")}
|
2022-03-23 20:17:57 +00:00
|
|
|
onBack={onBack}
|
|
|
|
|
setTool={setTool}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
};
|