2026-10-20 20:32:45 -04:00
|
|
|
import React, { useEffect, useRef, useSyncExternalStore } from 'react';
|
2026-07-03 13:27:23 -04:00
|
|
|
import { Box, Icon, Icons, Text, color } from 'folds';
|
2026-10-20 20:32:45 -04:00
|
|
|
import { Room } from 'matrix-js-sdk';
|
|
|
|
|
import { Widget } from 'matrix-widget-api';
|
2026-07-03 13:27:23 -04:00
|
|
|
import { useMatrixClient } from '../../../hooks/useMatrixClient';
|
2026-09-24 21:17:18 -04:00
|
|
|
import { WidgetPermissionPrompt } from './WidgetPermissionPrompt';
|
2026-10-20 20:32:45 -04:00
|
|
|
import {
|
|
|
|
|
attachWidgetSession,
|
|
|
|
|
detachWidgetSession,
|
|
|
|
|
getWidgetSession,
|
|
|
|
|
subscribeWidgetSessions,
|
|
|
|
|
widgetSessionKey,
|
|
|
|
|
widgetSessionsVersion,
|
|
|
|
|
} from './widgetSessions';
|
2026-07-03 13:27:23 -04:00
|
|
|
|
|
|
|
|
type RoomWidgetViewProps = {
|
|
|
|
|
room: Room;
|
|
|
|
|
widget: Widget;
|
|
|
|
|
};
|
|
|
|
|
|
2026-10-20 20:32:45 -04:00
|
|
|
// Shows one room widget in the Widgets panel. The widget itself runs in a
|
|
|
|
|
// session (widgetSessions.ts) whose sandboxed iframe lives in an app-level
|
|
|
|
|
// layer and is positioned over this placeholder, so a widget that asked to
|
|
|
|
|
// stay on screen can keep running in a floating window after the panel closes.
|
|
|
|
|
// Display-only access is granted silently; reading/sending in this room is
|
|
|
|
|
// asked for (WidgetPermissionPrompt) and, once granted, the room's live events
|
|
|
|
|
// are fed to the widget. Re-attaches only when the widget id or its (template)
|
|
|
|
|
// URL changes, so viewing a widget doesn't reload it on unrelated state updates.
|
2026-07-03 13:27:23 -04:00
|
|
|
export function RoomWidgetView({ room, widget }: RoomWidgetViewProps) {
|
|
|
|
|
const mx = useMatrixClient();
|
2026-10-20 20:32:45 -04:00
|
|
|
const slotRef = useRef<HTMLDivElement>(null);
|
2026-07-03 13:27:23 -04:00
|
|
|
const widgetRef = useRef(widget);
|
|
|
|
|
widgetRef.current = widget;
|
2026-10-20 20:32:45 -04:00
|
|
|
const key = widgetSessionKey(room.roomId, widget.id);
|
|
|
|
|
useSyncExternalStore(subscribeWidgetSessions, widgetSessionsVersion);
|
|
|
|
|
const session = getWidgetSession(key);
|
2026-07-03 13:27:23 -04:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-10-20 20:32:45 -04:00
|
|
|
const slot = slotRef.current;
|
|
|
|
|
if (!slot) return undefined;
|
|
|
|
|
attachWidgetSession(mx, room, widgetRef.current, slot);
|
|
|
|
|
return () => detachWidgetSession(key, slot);
|
|
|
|
|
}, [mx, room, key, widget.templateUrl]);
|
2026-07-03 13:27:23 -04:00
|
|
|
|
2026-10-20 20:32:45 -04:00
|
|
|
if (session?.blocked) {
|
2026-07-03 13:27:23 -04:00
|
|
|
return (
|
|
|
|
|
<Box direction="Column" grow="Yes" alignItems="Center" justifyContent="Center" gap="200">
|
|
|
|
|
<Icon size="400" src={Icons.Warning} style={{ color: color.Warning.Main }} />
|
|
|
|
|
<Text size="T300" align="Center">
|
|
|
|
|
This widget can't be loaded because its URL is on this app's own origin.
|
|
|
|
|
</Text>
|
|
|
|
|
</Box>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-24 21:17:18 -04:00
|
|
|
const addedBy = widget.creatorUserId
|
|
|
|
|
? (room.getMember(widget.creatorUserId)?.rawDisplayName ?? widget.creatorUserId)
|
|
|
|
|
: undefined;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
2026-10-20 20:32:45 -04:00
|
|
|
<Box ref={slotRef} grow="Yes" style={{ height: '100%', minHeight: 0 }} />
|
|
|
|
|
{session?.pendingAsk && (
|
2026-09-24 21:17:18 -04:00
|
|
|
<WidgetPermissionPrompt
|
|
|
|
|
widgetName={widget.name || 'Widget'}
|
2026-10-20 20:32:45 -04:00
|
|
|
origin={session.pendingAsk.origin}
|
2026-09-24 21:17:18 -04:00
|
|
|
addedBy={addedBy}
|
2026-10-20 20:32:45 -04:00
|
|
|
requests={session.pendingAsk.requests}
|
|
|
|
|
onDone={session.pendingAsk.resolve}
|
2026-09-24 21:17:18 -04:00
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
);
|
2026-07-03 13:27:23 -04:00
|
|
|
}
|