2025-04-14 04:28:08 -05:00
|
|
|
import React, { useCallback, useRef, useEffect } from 'react';
|
|
|
|
|
import { Box, Text, config } from 'folds'; // Assuming 'folds' is a UI library
|
2025-04-14 09:43:36 -05:00
|
|
|
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';
|
2025-04-14 09:43:36 -05:00
|
|
|
import { ClientWidgetApi } from 'matrix-widget-api';
|
|
|
|
|
import { logger } from 'matrix-js-sdk/lib/logger';
|
|
|
|
|
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';
|
2024-05-31 19:49:46 +05:30
|
|
|
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';
|
2025-04-14 09:43:36 -05:00
|
|
|
import { Page } from '../../components/page';
|
2024-05-31 19:49:46 +05:30
|
|
|
import { RoomViewHeader } from './RoomViewHeader';
|
2025-04-14 09:43:36 -05:00
|
|
|
import { useKeyDown } from '../../hooks/useKeyDown';
|
|
|
|
|
import { editableActiveElement } from '../../utils/dom';
|
|
|
|
|
import navigation from '../../../client/state/navigation';
|
|
|
|
|
import { settingsAtom } from '../../state/settings';
|
|
|
|
|
import { useSetting } from '../../state/hooks/settings';
|
|
|
|
|
import { useAccessibleTagColors, usePowerLevelTags } from '../../hooks/usePowerLevelTags';
|
|
|
|
|
import { useTheme } from '../../hooks/useTheme';
|
|
|
|
|
import { createVirtualWidget, Edget, getWidgetData, getWidgetUrl } from './SmallWidget';
|
2024-07-18 18:50:20 +05:30
|
|
|
|
2025-04-14 04:28:08 -05:00
|
|
|
// --- Constants ---
|
2025-02-26 21:42:42 +11:00
|
|
|
const FN_KEYS_REGEX = /^F\d+$/;
|
2025-04-14 04:28:08 -05:00
|
|
|
|
|
|
|
|
// --- Helper Functions ---
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determines if a keyboard event should trigger focusing the message input field.
|
|
|
|
|
* @param evt - The KeyboardEvent.
|
|
|
|
|
* @returns True if the input should be focused, false otherwise.
|
|
|
|
|
*/
|
2024-07-18 18:50:20 +05:30
|
|
|
const shouldFocusMessageField = (evt: KeyboardEvent): boolean => {
|
|
|
|
|
const { code } = evt;
|
2025-04-14 04:28:08 -05:00
|
|
|
// Ignore if modifier keys are pressed
|
2024-07-18 18:50:20 +05:30
|
|
|
if (evt.metaKey || evt.altKey || evt.ctrlKey) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2024-08-04 11:06:42 +05:30
|
|
|
|
2025-04-14 04:28:08 -05:00
|
|
|
// Ignore function keys (F1, F2, etc.)
|
2025-02-26 21:42:42 +11:00
|
|
|
if (FN_KEYS_REGEX.test(code)) return false;
|
2024-07-18 18:50:20 +05:30
|
|
|
|
2025-04-14 04:28:08 -05:00
|
|
|
// Ignore specific control/navigation keys
|
2024-07-18 18:50:20 +05:30
|
|
|
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' ||
|
2025-04-14 04:28:08 -05:00
|
|
|
code === 'Space' || // Allow space if needed elsewhere, but not for focusing input
|
|
|
|
|
code === 'Enter' || // Allow enter if needed elsewhere
|
2024-07-18 18:50:20 +05:30
|
|
|
code === 'NumLock' ||
|
|
|
|
|
code === 'ScrollLock'
|
|
|
|
|
) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-14 04:28:08 -05:00
|
|
|
// If none of the above conditions met, it's likely a character key
|
2024-07-18 18:50:20 +05:30
|
|
|
return true;
|
|
|
|
|
};
|
2024-05-31 19:49:46 +05:30
|
|
|
|
2025-04-14 04:28:08 -05:00
|
|
|
// --- RoomView Component ---
|
2025-04-12 03:40:13 -05:00
|
|
|
|
2024-05-31 19:49:46 +05:30
|
|
|
export function RoomView({ room, eventId }: { room: Room; eventId?: string }) {
|
2025-04-14 04:28:08 -05:00
|
|
|
// Refs
|
2024-07-18 18:50:20 +05:30
|
|
|
const roomInputRef = useRef<HTMLDivElement>(null);
|
2025-04-14 04:28:08 -05:00
|
|
|
const roomViewRef = useRef<HTMLDivElement>(null); // Ref for the main Page container
|
|
|
|
|
const iframeRef = useRef<HTMLIFrameElement>(null); // Ref for the iframe element
|
|
|
|
|
const widgetApiRef = useRef<ClientWidgetApi | null>(null); // Ref to store the widget API instance
|
|
|
|
|
const edgetRef = useRef<Edget | null>(null); // Ref to store the Edget instance
|
2024-05-31 19:49:46 +05:30
|
|
|
|
2025-04-14 04:28:08 -05:00
|
|
|
// State & Hooks
|
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();
|
2025-04-14 09:43:36 -05:00
|
|
|
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
|
|
|
|
|
|
2025-04-14 04:28:08 -05:00
|
|
|
// Effect for focusing input on key press (for non-call rooms)
|
2024-07-18 18:50:20 +05:30
|
|
|
useKeyDown(
|
|
|
|
|
window,
|
|
|
|
|
useCallback(
|
|
|
|
|
(evt) => {
|
2025-04-14 04:28:08 -05:00
|
|
|
// Don't focus if an editable element already has focus
|
2024-07-18 18:50:20 +05:30
|
|
|
if (editableActiveElement()) return;
|
2025-04-14 04:28:08 -05:00
|
|
|
// Don't focus if a modal is likely open
|
|
|
|
|
if (document.querySelector('.ReactModalPortal > *') || navigation.isRawModalVisible) {
|
2025-04-14 09:43:36 -05:00
|
|
|
return;
|
2024-07-18 18:50:20 +05:30
|
|
|
}
|
2025-04-14 04:28:08 -05:00
|
|
|
// Don't focus if in a call view (no text editor)
|
|
|
|
|
if (isCall) return;
|
2025-04-12 03:40:13 -05:00
|
|
|
|
2025-04-14 04:28:08 -05:00
|
|
|
// Check if the key pressed should trigger focus or is paste hotkey
|
2024-07-21 11:13:33 +05:30
|
|
|
if (shouldFocusMessageField(evt) || isKeyHotkey('mod+v', evt)) {
|
2025-04-14 04:28:08 -05:00
|
|
|
if (editor) {
|
2025-04-14 09:43:36 -05:00
|
|
|
ReactEditor.focus(editor);
|
2025-04-12 03:40:13 -05:00
|
|
|
}
|
2024-07-18 18:50:20 +05:30
|
|
|
}
|
|
|
|
|
},
|
2025-04-14 04:28:08 -05:00
|
|
|
[editor, isCall] // Dependencies
|
2024-07-18 18:50:20 +05:30
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
2025-04-14 04:28:08 -05:00
|
|
|
// Effect to setup and cleanup the widget API for call rooms
|
2025-04-12 03:40:13 -05:00
|
|
|
useEffect(() => {
|
2025-04-14 04:28:08 -05:00
|
|
|
// Only run if it's a call room
|
|
|
|
|
if (isCall) {
|
|
|
|
|
const iframeElement = iframeRef.current;
|
|
|
|
|
// Ensure iframe element exists before proceeding
|
|
|
|
|
if (!iframeElement) {
|
|
|
|
|
logger.warn(`Iframe element not found for room ${roomId}, cannot initialize widget.`);
|
|
|
|
|
return;
|
2025-04-12 03:40:13 -05:00
|
|
|
}
|
|
|
|
|
|
2025-04-14 04:28:08 -05:00
|
|
|
logger.info(`Setting up Element Call widget for room ${roomId}`);
|
|
|
|
|
const userId = mx.getUserId() ?? ''; // Ensure userId is not null
|
|
|
|
|
const url = getWidgetUrl(mx, roomId); // Generate the widget URL
|
2025-04-12 03:40:13 -05:00
|
|
|
|
2025-04-14 04:28:08 -05:00
|
|
|
// 1. Create the virtual widget definition
|
2025-04-13 16:00:41 -05:00
|
|
|
const app = createVirtualWidget(
|
|
|
|
|
mx,
|
2025-04-14 04:28:08 -05:00
|
|
|
`element-call-${roomId}`,
|
2025-04-13 16:00:41 -05:00
|
|
|
userId,
|
2025-04-14 04:28:08 -05:00
|
|
|
'Element Call',
|
|
|
|
|
'm.call', // Widget type
|
|
|
|
|
url,
|
|
|
|
|
false, // waitForIframeLoad - false as we manually control src loading
|
2025-04-14 09:43:36 -05:00
|
|
|
getWidgetData(
|
|
|
|
|
// Widget data
|
2025-04-13 16:00:41 -05:00
|
|
|
mx,
|
|
|
|
|
roomId,
|
2025-04-14 04:28:08 -05:00
|
|
|
{}, // Initial data (can be fetched if needed)
|
2025-04-14 09:43:36 -05:00
|
|
|
{
|
|
|
|
|
// Overwrite/specific data
|
2025-04-14 04:28:08 -05:00
|
|
|
skipLobby: true, // Example configuration
|
|
|
|
|
preload: false, // Set preload based on whether you want background loading
|
|
|
|
|
returnToLobby: false, // Example configuration
|
|
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
roomId
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 2. Instantiate Edget to manage widget communication
|
|
|
|
|
const edget = new Edget(app);
|
|
|
|
|
edgetRef.current = edget; // Store instance in ref
|
|
|
|
|
|
|
|
|
|
// 3. Start the widget messaging *before* setting the iframe src
|
|
|
|
|
try {
|
|
|
|
|
const widgetApi = edget.startMessaging(iframeElement);
|
|
|
|
|
widgetApiRef.current = widgetApi; // Store API instance
|
|
|
|
|
|
|
|
|
|
// Listen for the 'ready' event from the widget API
|
2025-04-14 09:43:36 -05:00
|
|
|
widgetApi.once('ready', () => {
|
|
|
|
|
logger.info(`Element Call widget is ready for room ${roomId}.`);
|
|
|
|
|
// Perform actions needed once the widget confirms it's ready
|
|
|
|
|
// Example: widgetApi.transport.send("action", { data: "..." });
|
2025-04-14 04:28:08 -05:00
|
|
|
});
|
|
|
|
|
|
2025-04-14 09:43:36 -05:00
|
|
|
widgetApi.on('action:im.vector.hangup', () => {
|
|
|
|
|
logger.info(`Received hangup action from widget in room ${roomId}.`);
|
|
|
|
|
// Handle hangup logic (e.g., navigate away, update room state)
|
2025-04-14 04:28:08 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Add other necessary event listeners from the widget API
|
|
|
|
|
// widgetApi.on("action:some_other_action", (ev) => { ... });
|
|
|
|
|
|
|
|
|
|
// 4. Set the iframe src *after* messaging is initialized
|
|
|
|
|
logger.info(`Setting iframe src for room ${roomId}: ${url.toString()}`);
|
|
|
|
|
iframeElement.src = url.toString();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error(`Error initializing widget messaging for room ${roomId}:`, error);
|
|
|
|
|
// Handle initialization error (e.g., show an error message to the user)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 5. Return cleanup function
|
2025-04-12 03:40:13 -05:00
|
|
|
return () => {
|
2025-04-14 04:28:08 -05:00
|
|
|
logger.info(`Cleaning up Element Call widget for room ${roomId}`);
|
|
|
|
|
// Stop messaging and clean up resources
|
|
|
|
|
if (edgetRef.current) {
|
|
|
|
|
edgetRef.current.stopMessaging();
|
|
|
|
|
edgetRef.current = null;
|
|
|
|
|
}
|
|
|
|
|
widgetApiRef.current = null; // Clear API ref
|
|
|
|
|
|
|
|
|
|
// Clear iframe src to stop activity and free resources
|
2025-04-12 03:40:13 -05:00
|
|
|
if (iframeRef.current) {
|
2025-04-14 09:43:36 -05:00
|
|
|
iframeRef.current.src = 'about:blank';
|
|
|
|
|
logger.info(`Cleared iframe src for room ${roomId}`);
|
2025-04-12 03:40:13 -05:00
|
|
|
}
|
|
|
|
|
};
|
2025-04-14 04:28:08 -05:00
|
|
|
} else {
|
|
|
|
|
// If not a call room, ensure any previous call state is cleaned up
|
|
|
|
|
// (This might be redundant if component unmounts/remounts correctly, but safe)
|
|
|
|
|
if (widgetApiRef.current && iframeRef.current) {
|
|
|
|
|
logger.info(`Room ${roomId} is no longer a call room, ensuring cleanup.`);
|
2025-04-14 09:43:36 -05:00
|
|
|
if (edgetRef.current) {
|
2025-04-14 04:28:08 -05:00
|
|
|
edgetRef.current.stopMessaging();
|
|
|
|
|
edgetRef.current = null;
|
|
|
|
|
}
|
|
|
|
|
widgetApiRef.current = null;
|
|
|
|
|
iframeRef.current.src = 'about:blank';
|
|
|
|
|
}
|
2025-04-12 03:40:13 -05:00
|
|
|
}
|
|
|
|
|
|
2025-04-14 04:28:08 -05:00
|
|
|
// Explicitly return undefined if not a call room or no cleanup needed initially
|
2025-04-12 03:40:13 -05:00
|
|
|
return undefined;
|
2025-04-14 04:28:08 -05:00
|
|
|
}, [isCall, mx, roomId, editor]); // Dependencies: run effect if these change
|
2025-04-12 03:40:13 -05:00
|
|
|
|
2025-04-14 04:28:08 -05:00
|
|
|
// --- Render Logic ---
|
|
|
|
|
|
2025-04-12 03:40:13 -05:00
|
|
|
// Render Call View
|
|
|
|
|
if (isCall) {
|
2025-04-14 04:28:08 -05:00
|
|
|
// Initial src is set to about:blank. The useEffect hook will set the actual src later.
|
2025-04-12 03:40:13 -05:00
|
|
|
return (
|
2025-04-14 09:43:36 -05:00
|
|
|
<Page
|
|
|
|
|
ref={roomViewRef}
|
|
|
|
|
style={{ display: 'flex', flexDirection: 'column', height: '100%', overflow: 'hidden' }}
|
|
|
|
|
>
|
2025-04-12 03:40:13 -05:00
|
|
|
<RoomViewHeader />
|
2025-04-14 04:28:08 -05:00
|
|
|
{/* Box grows to fill available space */}
|
|
|
|
|
<Box grow="Yes" style={{ flex: 1, overflow: 'hidden', minHeight: 0 }}>
|
2025-04-14 09:43:36 -05:00
|
|
|
<iframe
|
|
|
|
|
ref={iframeRef}
|
|
|
|
|
src="about:blank" // Start with a blank page
|
|
|
|
|
style={{ width: '100%', height: '100%', border: 'none', display: 'block' }}
|
|
|
|
|
title={`Element Call - ${room.name || roomId}`}
|
|
|
|
|
// Sandbox attributes for security. Adjust as needed by Element Call.
|
|
|
|
|
//sandbox="allow-forms allow-scripts allow-same-origin allow-popups allow-modals allow-downloads"
|
|
|
|
|
// Permissions policy for features like camera, microphone.
|
|
|
|
|
allow="microphone; camera; display-capture; autoplay; clipboard-write;"
|
|
|
|
|
/>
|
2025-04-12 03:40:13 -05:00
|
|
|
</Box>
|
2025-04-14 09:43:36 -05:00
|
|
|
{/* Optional: Minimal footer or status indicators */}
|
2025-04-12 03:40:13 -05:00
|
|
|
</Page>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Render Standard Text/Timeline Room View
|
2024-05-31 19:49:46 +05:30
|
|
|
return (
|
|
|
|
|
<Page ref={roomViewRef}>
|
|
|
|
|
<RoomViewHeader />
|
2025-04-14 04:28:08 -05:00
|
|
|
{/* Main timeline area */}
|
|
|
|
|
<Box grow="Yes" direction="Column" style={{ flex: 1, overflow: 'hidden', minHeight: 0 }}>
|
2024-05-31 19:49:46 +05:30
|
|
|
<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>
|
2025-04-14 04:28:08 -05:00
|
|
|
{/* Input area and potentially other footer elements */}
|
2024-05-31 19:49:46 +05:30
|
|
|
<Box shrink="No" direction="Column">
|
2025-04-14 09:43:36 -05:00
|
|
|
<div style={{ padding: `0 ${config.space.S400}` }}>
|
|
|
|
|
{' '}
|
|
|
|
|
{/* Use theme spacing */}
|
2024-05-31 19:49:46 +05:30
|
|
|
{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-14 04:28:08 -05:00
|
|
|
fileDropContainerRef={roomViewRef} // Pass the Page ref for file drops
|
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-04-14 04:28:08 -05:00
|
|
|
{/* Following/Activity Feed */}
|
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-14 04:28:08 -05:00
|
|
|
}
|