2025-04-12 03:40:13 -05:00
|
|
|
import React, { useCallback, useRef, useEffect } from 'react'; // Added useEffect
|
2024-05-31 19:49:46 +05:30
|
|
|
import { Box, Text, config } from 'folds';
|
|
|
|
|
import { EventType, Room } from 'matrix-js-sdk';
|
2024-07-18 18:50:20 +05:30
|
|
|
import { ReactEditor } from 'slate-react';
|
|
|
|
|
import { isKeyHotkey } from 'is-hotkey';
|
2024-05-31 19:49:46 +05:30
|
|
|
import { useStateEvent } from '../../hooks/useStateEvent';
|
|
|
|
|
import { StateEvent } from '../../../types/matrix/room';
|
|
|
|
|
import { usePowerLevelsAPI, usePowerLevelsContext } from '../../hooks/usePowerLevels';
|
|
|
|
|
import { useMatrixClient } from '../../hooks/useMatrixClient';
|
|
|
|
|
import { useEditor } from '../../components/editor';
|
|
|
|
|
import { RoomInputPlaceholder } from './RoomInputPlaceholder';
|
|
|
|
|
import { RoomTimeline } from './RoomTimeline';
|
|
|
|
|
import { RoomViewTyping } from './RoomViewTyping';
|
|
|
|
|
import { RoomTombstone } from './RoomTombstone';
|
|
|
|
|
import { RoomInput } from './RoomInput';
|
2025-02-26 21:44:53 +11:00
|
|
|
import { RoomViewFollowing, RoomViewFollowingPlaceholder } from './RoomViewFollowing';
|
2024-05-31 19:49:46 +05:30
|
|
|
import { Page } from '../../components/page';
|
|
|
|
|
import { RoomViewHeader } from './RoomViewHeader';
|
2024-07-18 18:50:20 +05:30
|
|
|
import { useKeyDown } from '../../hooks/useKeyDown';
|
|
|
|
|
import { editableActiveElement } from '../../utils/dom';
|
|
|
|
|
import navigation from '../../../client/state/navigation';
|
2025-02-26 21:44:53 +11:00
|
|
|
import { settingsAtom } from '../../state/settings';
|
|
|
|
|
import { useSetting } from '../../state/hooks/settings';
|
2025-03-23 22:09:29 +11:00
|
|
|
import { useAccessibleTagColors, usePowerLevelTags } from '../../hooks/usePowerLevelTags';
|
|
|
|
|
import { useTheme } from '../../hooks/useTheme';
|
2025-04-12 03:40:13 -05:00
|
|
|
import { logger } from 'matrix-js-sdk/lib/logger';
|
|
|
|
|
import { ClientWidgetApi, Widget, WidgetKind } from 'matrix-widget-api';
|
|
|
|
|
import { SmallWidgetDriver } from './SmallWidgetDriver';
|
2024-07-18 18:50:20 +05:30
|
|
|
|
2025-02-26 21:42:42 +11:00
|
|
|
const FN_KEYS_REGEX = /^F\d+$/;
|
2024-07-18 18:50:20 +05:30
|
|
|
const shouldFocusMessageField = (evt: KeyboardEvent): boolean => {
|
|
|
|
|
const { code } = evt;
|
|
|
|
|
if (evt.metaKey || evt.altKey || evt.ctrlKey) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2024-08-04 11:06:42 +05:30
|
|
|
|
2024-07-18 18:50:20 +05:30
|
|
|
// do not focus on F keys
|
2025-02-26 21:42:42 +11:00
|
|
|
if (FN_KEYS_REGEX.test(code)) return false;
|
2024-07-18 18:50:20 +05:30
|
|
|
|
|
|
|
|
// do not focus on numlock/scroll lock
|
|
|
|
|
if (
|
|
|
|
|
code.startsWith('OS') ||
|
|
|
|
|
code.startsWith('Meta') ||
|
|
|
|
|
code.startsWith('Shift') ||
|
|
|
|
|
code.startsWith('Alt') ||
|
|
|
|
|
code.startsWith('Control') ||
|
|
|
|
|
code.startsWith('Arrow') ||
|
2024-08-04 11:06:42 +05:30
|
|
|
code.startsWith('Page') ||
|
|
|
|
|
code.startsWith('End') ||
|
|
|
|
|
code.startsWith('Home') ||
|
2024-07-18 18:50:20 +05:30
|
|
|
code === 'Tab' ||
|
|
|
|
|
code === 'Space' ||
|
|
|
|
|
code === 'Enter' ||
|
|
|
|
|
code === 'NumLock' ||
|
|
|
|
|
code === 'ScrollLock'
|
|
|
|
|
) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
};
|
2024-05-31 19:49:46 +05:30
|
|
|
|
2025-04-12 03:40:13 -05:00
|
|
|
// Keep this function to generate the URL
|
|
|
|
|
const getWidgetUrl = (mx, roomId) => {
|
|
|
|
|
const baseUrl = window.location.href;
|
|
|
|
|
const params = new URLSearchParams({
|
|
|
|
|
embed: "true", // We're embedding EC within another application
|
|
|
|
|
widgetId: "test",
|
|
|
|
|
// Template variables are used, so that this can be configured using the data.
|
|
|
|
|
preload: "$preload", // We want it to load in the background.
|
|
|
|
|
// skipLobby: "true", // Skip the lobby in case we show a lobby component of our own.
|
|
|
|
|
returnToLobby: "$returnToLobby", // Returns to the lobby (instead of blank screen) when the call ends. (For video rooms)
|
|
|
|
|
perParticipantE2EE: "$perParticipantE2EE",
|
|
|
|
|
hideHeader: "true", // Hide the header since our room header is enough
|
|
|
|
|
userId: mx.getUserId()!,
|
|
|
|
|
deviceId: mx.getDeviceId()!,
|
|
|
|
|
roomId: roomId,
|
|
|
|
|
baseUrl: window.location.href,
|
|
|
|
|
parentUrl: baseUrl,
|
|
|
|
|
// lang: getCurrentLanguage().replace("_", "-"),
|
|
|
|
|
// fontScale: (FontWatcher.getRootFontSize() / FontWatcher.getBrowserDefaultFontSize()).toString(),
|
|
|
|
|
theme: "$org.matrix.msc2873.client_theme",
|
|
|
|
|
});
|
|
|
|
|
const replacedUrl = params.toString().replace(/%24/g, "$");
|
|
|
|
|
const url= 'https://elementcall.example.quest' + `#?${replacedUrl}`;
|
|
|
|
|
logger.error(url);
|
|
|
|
|
return url;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-05-31 19:49:46 +05:30
|
|
|
export function RoomView({ room, eventId }: { room: Room; eventId?: string }) {
|
2024-07-18 18:50:20 +05:30
|
|
|
const roomInputRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
const roomViewRef = useRef<HTMLDivElement>(null);
|
2025-04-12 03:40:13 -05:00
|
|
|
const iframeRef = useRef<HTMLIFrameElement>(null); // Ref for the iframe
|
2024-05-31 19:49:46 +05:30
|
|
|
|
2025-02-26 21:44:53 +11:00
|
|
|
const [hideActivity] = useSetting(settingsAtom, 'hideActivity');
|
|
|
|
|
|
2024-05-31 19:49:46 +05:30
|
|
|
const { roomId } = room;
|
|
|
|
|
const editor = useEditor();
|
|
|
|
|
|
|
|
|
|
const mx = useMatrixClient();
|
|
|
|
|
|
|
|
|
|
const tombstoneEvent = useStateEvent(room, StateEvent.RoomTombstone);
|
|
|
|
|
const powerLevels = usePowerLevelsContext();
|
|
|
|
|
const { getPowerLevel, canSendEvent } = usePowerLevelsAPI(powerLevels);
|
|
|
|
|
const myUserId = mx.getUserId();
|
|
|
|
|
const canMessage = myUserId
|
|
|
|
|
? canSendEvent(EventType.RoomMessage, getPowerLevel(myUserId))
|
|
|
|
|
: false;
|
|
|
|
|
|
2025-03-23 22:09:29 +11:00
|
|
|
const [powerLevelTags, getPowerLevelTag] = usePowerLevelTags(room, powerLevels);
|
|
|
|
|
const theme = useTheme();
|
|
|
|
|
const accessibleTagColors = useAccessibleTagColors(theme.kind, powerLevelTags);
|
|
|
|
|
|
2025-04-12 03:40:13 -05:00
|
|
|
const isCall = room.isCallRoom(); // Determine if it's a call room
|
|
|
|
|
|
2024-07-18 18:50:20 +05:30
|
|
|
useKeyDown(
|
|
|
|
|
window,
|
|
|
|
|
useCallback(
|
|
|
|
|
(evt) => {
|
|
|
|
|
if (editableActiveElement()) return;
|
2025-04-12 03:40:13 -05:00
|
|
|
// Check modal visibility more robustly if needed
|
|
|
|
|
if (document.querySelector('.ReactModalPortal > *')) { // Simple check if any modal portal has content
|
|
|
|
|
if (navigation.isRawModalVisible) return; // Skip if raw modal is explicitly visible
|
|
|
|
|
// Add other modal checks if necessary
|
2024-07-18 18:50:20 +05:30
|
|
|
}
|
2025-04-12 03:40:13 -05:00
|
|
|
|
2024-07-21 11:13:33 +05:30
|
|
|
if (shouldFocusMessageField(evt) || isKeyHotkey('mod+v', evt)) {
|
2025-04-12 03:40:13 -05:00
|
|
|
// Only focus editor if not in a call view where editor isn't present
|
|
|
|
|
if (!isCall && editor) {
|
|
|
|
|
ReactEditor.focus(editor);
|
|
|
|
|
}
|
2024-07-18 18:50:20 +05:30
|
|
|
}
|
|
|
|
|
},
|
2025-04-12 03:40:13 -05:00
|
|
|
[editor, isCall] // Add isCall dependency
|
2024-07-18 18:50:20 +05:30
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
2025-04-12 03:40:13 -05:00
|
|
|
// Effect to setup the widget API when the iframe is mounted for a call room
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
let widgetApi: ClientWidgetApi | null = null;
|
|
|
|
|
let driver: SmallWidgetDriver | null = null;
|
|
|
|
|
|
|
|
|
|
// Only run setup if it's a call room and the iframe ref is available
|
|
|
|
|
if (isCall && iframeRef.current) {
|
|
|
|
|
const iframe = iframeRef.current;
|
|
|
|
|
const url = getWidgetUrl(mx, roomId);
|
|
|
|
|
|
|
|
|
|
// Update iframe src if necessary (though it's set in JSX, this ensures it if URL changes)
|
|
|
|
|
if (iframe.src !== url) {
|
|
|
|
|
iframe.src = url;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.info(`Setting up widget API for room ${roomId}`);
|
|
|
|
|
|
|
|
|
|
const widget = new Widget({
|
|
|
|
|
id: 'test-call-widget', // Match ID used in URL params
|
|
|
|
|
creatorUserId: mx.getUserId()!,
|
|
|
|
|
type: 'm.custom', // Or appropriate widget type e.g., m.video
|
|
|
|
|
url: url,
|
|
|
|
|
roomId: roomId, // Pass roomId if needed by Widget constructor
|
|
|
|
|
waitForIframeLoad: false,
|
|
|
|
|
// Add other necessary Widget properties
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Ensure driver is correctly instantiated with necessary parameters
|
|
|
|
|
// The second argument `[]` might need adjustment based on SmallWidgetDriver's needs (e.g., allowed capabilities)
|
|
|
|
|
driver = new SmallWidgetDriver(mx, [], widget, WidgetKind.Room, true, roomId);
|
|
|
|
|
|
|
|
|
|
widgetApi = new ClientWidgetApi(widget, iframe, driver);
|
|
|
|
|
// widgetApi.start(); // Start communication if required by your setup
|
|
|
|
|
|
|
|
|
|
// Return a cleanup function
|
|
|
|
|
return () => {
|
|
|
|
|
logger.info(`Cleaning up widget API for room ${roomId}`);
|
|
|
|
|
// Implement proper cleanup for ClientWidgetApi and SmallWidgetDriver
|
|
|
|
|
// This might involve calling stop methods, removing listeners, etc.
|
|
|
|
|
// Example: widgetApi?.stop();
|
|
|
|
|
// Example: driver?.stop();
|
|
|
|
|
widgetApi = null;
|
|
|
|
|
driver = null;
|
|
|
|
|
// Clear iframe src to stop loading/activity
|
|
|
|
|
if (iframeRef.current) {
|
|
|
|
|
iframeRef.current.src = 'about:blank';
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If it's not a call room or the iframe isn't ready, ensure no setup runs/is cleaned up
|
|
|
|
|
return undefined;
|
|
|
|
|
|
|
|
|
|
}, [isCall, mx, roomId]); // Dependencies: run effect if call status, client, or room ID changes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Render Call View
|
|
|
|
|
if (isCall) {
|
|
|
|
|
const url = getWidgetUrl(mx, roomId);
|
|
|
|
|
return (
|
|
|
|
|
// Attach roomViewRef here if <Page> is the main container you want to reference
|
|
|
|
|
<Page ref={roomViewRef} style={{ display: 'flex', flexDirection: 'column', height: '100%' }}>
|
|
|
|
|
<RoomViewHeader />
|
|
|
|
|
{/* Embed the iframe directly. Ensure parent has definite height or use flex grow */}
|
|
|
|
|
<Box grow="Yes" style={{ overflow: 'hidden' }}> {/* Use Box with grow */}
|
|
|
|
|
<iframe
|
|
|
|
|
ref={iframeRef}
|
|
|
|
|
src={url} // Set initial source
|
|
|
|
|
style={{ width: '100%', height: '100%', border: 'none' }}
|
|
|
|
|
title={`Element Call - ${room.name || roomId}`} // Accessible title
|
|
|
|
|
// Add necessary sandbox/allow attributes for WebRTC, etc.
|
|
|
|
|
sandbox="allow-forms allow-scripts allow-same-origin allow-popups allow-downloads"
|
|
|
|
|
allow="microphone; camera; display-capture; autoplay;"
|
|
|
|
|
/>
|
|
|
|
|
</Box>
|
|
|
|
|
{/* You might want a minimal footer or status bar here */}
|
|
|
|
|
</Page>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Render Standard Text/Timeline Room View
|
2024-05-31 19:49:46 +05:30
|
|
|
return (
|
|
|
|
|
<Page ref={roomViewRef}>
|
|
|
|
|
<RoomViewHeader />
|
|
|
|
|
<Box grow="Yes" direction="Column">
|
|
|
|
|
<RoomTimeline
|
2025-04-12 03:40:13 -05:00
|
|
|
key={roomId} // Key helps React reset state when room changes
|
2024-05-31 19:49:46 +05:30
|
|
|
room={room}
|
|
|
|
|
eventId={eventId}
|
|
|
|
|
roomInputRef={roomInputRef}
|
|
|
|
|
editor={editor}
|
2025-03-23 22:09:29 +11:00
|
|
|
getPowerLevelTag={getPowerLevelTag}
|
|
|
|
|
accessibleTagColors={accessibleTagColors}
|
2024-05-31 19:49:46 +05:30
|
|
|
/>
|
|
|
|
|
<RoomViewTyping room={room} />
|
|
|
|
|
</Box>
|
|
|
|
|
<Box shrink="No" direction="Column">
|
|
|
|
|
<div style={{ padding: `0 ${config.space.S400}` }}>
|
|
|
|
|
{tombstoneEvent ? (
|
|
|
|
|
<RoomTombstone
|
|
|
|
|
roomId={roomId}
|
|
|
|
|
body={tombstoneEvent.getContent().body}
|
|
|
|
|
replacementRoomId={tombstoneEvent.getContent().replacement_room}
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
2025-04-12 03:40:13 -05:00
|
|
|
{canMessage ? (
|
2024-05-31 19:49:46 +05:30
|
|
|
<RoomInput
|
|
|
|
|
room={room}
|
|
|
|
|
editor={editor}
|
|
|
|
|
roomId={roomId}
|
2025-04-12 03:40:13 -05:00
|
|
|
fileDropContainerRef={roomViewRef} // Pass the Page ref here if needed
|
2024-05-31 19:49:46 +05:30
|
|
|
ref={roomInputRef}
|
2025-03-23 22:09:29 +11:00
|
|
|
getPowerLevelTag={getPowerLevelTag}
|
|
|
|
|
accessibleTagColors={accessibleTagColors}
|
2024-05-31 19:49:46 +05:30
|
|
|
/>
|
2025-04-12 03:40:13 -05:00
|
|
|
) : (
|
2024-05-31 19:49:46 +05:30
|
|
|
<RoomInputPlaceholder
|
|
|
|
|
style={{ padding: config.space.S200 }}
|
|
|
|
|
alignItems="Center"
|
|
|
|
|
justifyContent="Center"
|
|
|
|
|
>
|
|
|
|
|
<Text align="Center">You do not have permission to post in this room</Text>
|
|
|
|
|
</RoomInputPlaceholder>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2025-02-26 21:44:53 +11:00
|
|
|
{hideActivity ? <RoomViewFollowingPlaceholder /> : <RoomViewFollowing room={room} />}
|
2024-05-31 19:49:46 +05:30
|
|
|
</Box>
|
|
|
|
|
</Page>
|
|
|
|
|
);
|
2025-04-12 03:40:13 -05:00
|
|
|
}
|