108 lines
3.4 KiB
TypeScript
108 lines
3.4 KiB
TypeScript
/*
|
|||
|
|
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 React, {useContext, useEffect} from "react";
|
||
|
|
import {Room} from "matrix-js-sdk/src/models/room";
|
||
|
|
|
||
|
|
import MatrixClientContext from "../../../contexts/MatrixClientContext";
|
||
|
|
import BaseCard from "./BaseCard";
|
||
|
|
import WidgetUtils from "../../../utils/WidgetUtils";
|
||
|
|
import AccessibleButton from "../elements/AccessibleButton";
|
||
|
|
import AppTile from "../elements/AppTile";
|
||
|
|
import {_t} from "../../../languageHandler";
|
||
|
|
import {useWidgets} from "./RoomSummaryCard";
|
||
|
|
import {RightPanelPhases} from "../../../stores/RightPanelStorePhases";
|
||
|
|
import defaultDispatcher from "../../../dispatcher/dispatcher";
|
||
|
|
import {SetRightPanelPhasePayload} from "../../../dispatcher/payloads/SetRightPanelPhasePayload";
|
||
|
|
import {Action} from "../../../dispatcher/actions";
|
||
|
|
import {WidgetStore} from "../../../stores/WidgetStore";
|
||
|
|
|
||
|
|
interface IProps {
|
||
|
|
room: Room;
|
||
|
|
widgetId: string;
|
||
|
|
onClose(): void;
|
||
|
|
}
|
||
|
|
|
||
|
|
const onFinished = () => {
|
||
|
|
defaultDispatcher.dispatch<SetRightPanelPhasePayload>({
|
||
|
|
action: Action.SetRightPanelPhase,
|
||
|
|
phase: RightPanelPhases.RoomSummary,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
const WidgetCard: React.FC<IProps> = ({ room, widgetId, onClose }) => {
|
||
|
|
const cli = useContext(MatrixClientContext);
|
||
|
|
|
||
|
|
const apps = useWidgets(room);
|
||
|
|
const app = apps.find(a => a.id === widgetId);
|
||
|
|
const isPinned = app && WidgetStore.instance.isPinned(app.id);
|
||
|
|
|
||
|
|
useEffect(() => {
|
||
|
|
if (!app || isPinned) {
|
||
|
|
// TODO maybe we should do this in the ActiveWidgetStore instead
|
||
|
|
onFinished();
|
||
|
|
}
|
||
|
|
}, [app, isPinned]);
|
||
|
|
|
||
|
|
// Don't render anything as we are about to transition
|
||
|
|
if (!app || isPinned) return null;
|
||
|
|
|
||
|
|
const header = <React.Fragment>
|
||
|
|
<h2>{ WidgetUtils.getWidgetName(app) }</h2>
|
||
|
|
</React.Fragment>;
|
||
|
|
|
||
|
|
const onPinClick = () => {
|
||
|
|
WidgetStore.instance.pinWidget(app.id);
|
||
|
|
};
|
||
|
|
|
||
|
|
const onEditClick = () => {
|
||
|
|
WidgetUtils.editWidget(room, app);
|
||
|
|
};
|
||
|
|
|
||
|
|
const footer = <React.Fragment>
|
||
|
|
<AccessibleButton kind="secondary" onClick={onPinClick}>
|
||
|
|
{ _t("Pin to room") }
|
||
|
|
</AccessibleButton>
|
||
|
|
<AccessibleButton kind="secondary" onClick={onEditClick}>
|
||
|
|
{ _t("Edit") }
|
||
|
|
</AccessibleButton>
|
||
|
|
</React.Fragment>;
|
||
|
|
|
||
|
|
return <BaseCard
|
||
|
|
header={header}
|
||
|
|
footer={footer}
|
||
|
|
className="mx_WidgetCard"
|
||
|
|
onClose={onClose}
|
||
|
|
previousPhase={RightPanelPhases.RoomSummary}
|
||
|
|
withoutScrollContainer
|
||
|
|
>
|
||
|
|
<AppTile
|
||
|
|
app={app}
|
||
|
|
fullWidth
|
||
|
|
show
|
||
|
|
showMenubar={false}
|
||
|
|
room={room}
|
||
|
|
userId={cli.getUserId()}
|
||
|
|
creatorUserId={app.creatorUserId}
|
||
|
|
widgetPageTitle={WidgetUtils.getWidgetDataTitle(app)}
|
||
|
|
waitForIframeLoad={app.waitForIframeLoad}
|
||
|
|
whitelistCapabilities={WidgetUtils.getCapWhitelistForAppTypeInRoomId(app.type, room.roomId)}
|
||
|
|
/>
|
||
|
|
</BaseCard>;
|
||
|
|
};
|
||
|
|
|
||
|
|
export default WidgetCard;
|