Merge pull request #228 from 7w1/merge-upstream

Merge upstream
This commit is contained in:
7w1 authored and GitHub committed 2026-03-07 12:48:02 -06:00
commit dd57be2c7b
66 files changed
+2749 -2313

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
sable: minor
---
Merge Voice Call updates from upstream.
+1 -1
View File
@@ -12778,7 +12778,7 @@
"jwt-decode": "^4.0.0",
"loglevel": "^1.9.2",
"matrix-events-sdk": "0.0.1",
"matrix-widget-api": "^1.10.0",
"matrix-widget-api": "1.13.0",
"oidc-client-ts": "^3.0.1",
"p-retry": "7",
"sdp-transform": "^2.14.1",
+63
View File
@@ -0,0 +1,63 @@
import { ReactNode, useCallback, useRef } from 'react';
import { useAtomValue, useSetAtom } from 'jotai';
import {
CallEmbedContextProvider,
CallEmbedRefContextProvider,
useCallHangupEvent,
useCallJoined,
useCallThemeSync,
} from '../hooks/useCallEmbed';
import { callChatAtom, callEmbedAtom } from '../state/callEmbed';
import { CallEmbed } from '../plugins/call';
import { useSelectedRoom } from '../hooks/router/useSelectedRoom';
import { ScreenSize, useScreenSizeContext } from '../hooks/useScreenSize';
function CallUtils({ embed }: { embed: CallEmbed }) {
const setCallEmbed = useSetAtom(callEmbedAtom);
useCallThemeSync(embed);
useCallHangupEvent(
embed,
useCallback(() => {
setCallEmbed(undefined);
}, [setCallEmbed])
);
return null;
}
type CallEmbedProviderProps = {
children?: ReactNode;
};
export function CallEmbedProvider({ children }: CallEmbedProviderProps) {
const callEmbed = useAtomValue(callEmbedAtom);
const callEmbedRef = useRef<HTMLDivElement>(null);
const joined = useCallJoined(callEmbed);
const selectedRoom = useSelectedRoom();
const chat = useAtomValue(callChatAtom);
const screenSize = useScreenSizeContext();
const chatOnlyView = chat && screenSize !== ScreenSize.Desktop;
const callVisible = callEmbed && selectedRoom === callEmbed.roomId && joined && !chatOnlyView;
return (
<CallEmbedContextProvider value={callEmbed}>
{callEmbed && <CallUtils embed={callEmbed} />}
<CallEmbedRefContextProvider value={callEmbedRef}>{children}</CallEmbedRefContextProvider>
<div
data-call-embed-container
style={{
visibility: callVisible ? undefined : 'hidden',
position: 'fixed',
top: 0,
left: 0,
width: '100%',
height: '50%',
}}
ref={callEmbedRef}
/>
</CallEmbedContextProvider>
);
}
@@ -1,6 +1,7 @@
import { Box, Text, Icon, Icons, config, IconSrc } from 'folds';
import { SequenceCard } from '$components/sequence-card';
import { SettingTile } from '$components/setting-tile';
import { BetaNoticeBadge } from '$components/BetaNoticeBadge';
import { CreateRoomType } from './types';
type CreateRoomTypeSelectorProps = {
@@ -32,10 +33,14 @@ export function CreateRoomTypeSelector({
before={<Icon size="400" src={getIcon(CreateRoomType.TextRoom)} />}
after={value === CreateRoomType.TextRoom && <Icon src={Icons.Check} />}
>
<Text size="H6">Text</Text>
<Text size="T300" priority="300">
Send text messages, videos and GIFs.
</Text>
<Box gap="200" alignItems="Baseline">
<Text size="H6" style={{ flexShrink: 0 }}>
Chat Room
</Text>
<Text size="T300" priority="300" truncate>
- Messages, photos, and videos.
</Text>
</Box>
</SettingTile>
</SequenceCard>
<SequenceCard
@@ -53,10 +58,15 @@ export function CreateRoomTypeSelector({
before={<Icon size="400" src={getIcon(CreateRoomType.VoiceRoom)} />}
after={value === CreateRoomType.VoiceRoom && <Icon src={Icons.Check} />}
>
<Text size="H6">Voice</Text>
<Text size="T300" priority="300">
A room optimized for voice calls.
</Text>
<Box gap="200" alignItems="Baseline">
<Text size="H6" style={{ flexShrink: 0 }}>
Voice Room
</Text>
<Text size="T300" priority="300" truncate>
- Live audio and video conversations.
</Text>
<BetaNoticeBadge />
</Box>
</SettingTile>
</SequenceCard>
</Box>
@@ -169,6 +169,7 @@ export function RoomMentionAutocomplete({
<RoomIcon
size="50"
joinRule={room.getJoinRule() ?? JoinRule.Restricted}
roomType={room.getType()}
filled
/>
)}
@@ -17,6 +17,7 @@ export const SequenceCard = as<
firstChild,
lastChild,
outlined,
mergeBorder,
...props
},
ref
@@ -24,7 +25,7 @@ export const SequenceCard = as<
<Box
as={AsSequenceCard}
className={classNames(
css.SequenceCard({ radii, outlined }),
css.SequenceCard({ radii, outlined, mergeBorder }),
ContainerColor({ variant }),
className
)}
+11 -2
View File
@@ -11,7 +11,7 @@ export const SequenceCard = recipe({
},
borderStyle: 'solid',
borderWidth: outlinedWidth,
borderBottomWidth: 0,
selectors: {
'&:first-child, :not(&) + &': {
borderTopLeftRadius: [radii],
@@ -20,7 +20,6 @@ export const SequenceCard = recipe({
'&:last-child, &:not(:has(+&))': {
borderBottomLeftRadius: [radii],
borderBottomRightRadius: [radii],
borderBottomWidth: outlinedWidth,
},
[`&[data-first-child="true"]`]: {
borderTopLeftRadius: [radii],
@@ -74,6 +73,16 @@ export const SequenceCard = recipe({
},
},
},
mergeBorder: {
true: {
borderBottomWidth: 0,
selectors: {
'&:last-child, &:not(:has(+&))': {
borderBottomWidth: outlinedWidth,
},
},
},
},
},
defaultVariants: {
radii: '400',
@@ -0,0 +1,17 @@
import { as, Avatar } from 'folds';
import classNames from 'classnames';
import * as css from './styles.css';
type StackedAvatarProps = {
radii?: '0' | '300' | '400' | '500' | 'Pill' | 'Inherit' | undefined;
};
export const StackedAvatar = as<'span', css.StackedAvatarVariants & StackedAvatarProps>(
({ size, variant, className, ...props }, ref) => (
<Avatar
size={size}
className={classNames(css.StackedAvatar({ size, variant }), className)}
{...props}
ref={ref}
/>
)
);
@@ -0,0 +1 @@
export * from './StackedAvatar';
@@ -0,0 +1,59 @@
import { ComplexStyleRule } from '@vanilla-extract/css';
import { recipe, RecipeVariants } from '@vanilla-extract/recipes';
import { color, config, ContainerColor, toRem } from 'folds';
const getVariant = (variant: ContainerColor): ComplexStyleRule => ({
outlineColor: color[variant].Container,
});
export const StackedAvatar = recipe({
base: {
backgroundColor: color.Surface.Container,
outlineStyle: 'solid',
selectors: {
'&:first-child': {
marginLeft: 0,
},
'button&': {
cursor: 'pointer',
},
},
},
variants: {
size: {
'200': {
marginLeft: toRem(-6),
outlineWidth: config.borderWidth.B300,
},
'300': {
marginLeft: toRem(-9),
outlineWidth: config.borderWidth.B400,
},
'400': {
marginLeft: toRem(-10.5),
outlineWidth: config.borderWidth.B500,
},
'500': {
marginLeft: toRem(-13),
outlineWidth: config.borderWidth.B600,
},
},
variant: {
Background: getVariant('Background'),
Surface: getVariant('Surface'),
SurfaceVariant: getVariant('SurfaceVariant'),
Primary: getVariant('Primary'),
Secondary: getVariant('Secondary'),
Success: getVariant('Success'),
Warning: getVariant('Warning'),
Critical: getVariant('Critical'),
},
},
defaultVariants: {
size: '400',
variant: 'Surface',
},
});
export type StackedAvatarVariants = RecipeVariants<typeof StackedAvatar>;
@@ -332,7 +332,7 @@ export function MutualRoomsChip({ userId }: { userId: string }) {
)}
/>
) : (
<RoomIcon size="100" joinRule={room.getJoinRule()} />
<RoomIcon size="100" joinRule={room.getJoinRule()} roomType={room.getType()} />
)}
</Avatar>
}
@@ -287,7 +287,11 @@ export function AddExistingModal({ parentId, space, requestClose }: AddExistingM
)}
/>
) : (
<RoomIcon size="200" joinRule={room.getJoinRule()} />
<RoomIcon
size="200"
joinRule={room.getJoinRule()}
roomType={room.getType()}
/>
)}
</Avatar>
}
@@ -0,0 +1,168 @@
import { Box, Chip, Icon, IconButton, Icons, Text, Tooltip, TooltipProvider } from 'folds';
import { useState } from 'react';
import { StatusDivider } from './components';
import { CallEmbed, useCallControlState } from '../../plugins/call';
type MicrophoneButtonProps = {
enabled: boolean;
onToggle: () => Promise<unknown>;
};
function MicrophoneButton({ enabled, onToggle }: MicrophoneButtonProps) {
return (
<TooltipProvider
position="Top"
tooltip={
<Tooltip>
<Text size="T200">{enabled ? 'Turn Off Microphone' : 'Turn On Microphone'}</Text>
</Tooltip>
}
>
{(anchorRef) => (
<IconButton
ref={anchorRef}
variant={enabled ? 'Background' : 'Warning'}
fill="Soft"
radii="300"
size="300"
onClick={() => onToggle()}
outlined={!enabled}
>
<Icon size="100" src={enabled ? Icons.Mic : Icons.MicMute} filled={!enabled} />
</IconButton>
)}
</TooltipProvider>
);
}
type SoundButtonProps = {
enabled: boolean;
onToggle: () => void;
};
function SoundButton({ enabled, onToggle }: SoundButtonProps) {
return (
<TooltipProvider
position="Top"
tooltip={
<Tooltip>
<Text size="T200">{enabled ? 'Turn Off Sound' : 'Turn On Sound'}</Text>
</Tooltip>
}
>
{(anchorRef) => (
<IconButton
ref={anchorRef}
variant={enabled ? 'Background' : 'Warning'}
fill="Soft"
radii="300"
size="300"
onClick={() => onToggle()}
outlined={!enabled}
>
<Icon
size="100"
src={enabled ? Icons.Headphone : Icons.HeadphoneMute}
filled={!enabled}
/>
</IconButton>
)}
</TooltipProvider>
);
}
type VideoButtonProps = {
enabled: boolean;
onToggle: () => Promise<unknown>;
};
function VideoButton({ enabled, onToggle }: VideoButtonProps) {
return (
<TooltipProvider
position="Top"
tooltip={
<Tooltip>
<Text size="T200">{enabled ? 'Stop Camera' : 'Start Camera'}</Text>
</Tooltip>
}
>
{(anchorRef) => (
<IconButton
ref={anchorRef}
variant={enabled ? 'Success' : 'Background'}
fill="Soft"
radii="300"
size="300"
onClick={() => onToggle()}
outlined={enabled}
>
<Icon
size="100"
src={enabled ? Icons.VideoCamera : Icons.VideoCameraMute}
filled={enabled}
/>
</IconButton>
)}
</TooltipProvider>
);
}
function ScreenShareButton() {
const [enabled, setEnabled] = useState(false);
return (
<TooltipProvider
position="Top"
tooltip={
<Tooltip>
<Text size="T200">{enabled ? 'Stop Screenshare' : 'Start Screenshare'}</Text>
</Tooltip>
}
>
{(anchorRef) => (
<IconButton
ref={anchorRef}
variant={enabled ? 'Success' : 'Background'}
fill="Soft"
radii="300"
size="300"
onClick={() => setEnabled(!enabled)}
outlined={enabled}
>
<Icon size="100" src={Icons.ScreenShare} filled={enabled} />
</IconButton>
)}
</TooltipProvider>
);
}
export function CallControl({ callEmbed }: { callEmbed: CallEmbed }) {
const { microphone, video, sound } = useCallControlState(callEmbed.control);
return (
<Box shrink="No" alignItems="Center" gap="300">
<Box alignItems="Inherit" gap="200">
<MicrophoneButton
enabled={microphone}
onToggle={() => callEmbed.control.toggleMicrophone()}
/>
<SoundButton enabled={sound} onToggle={() => callEmbed.control.toggleSound()} />
</Box>
<StatusDivider />
<Box alignItems="Inherit" gap="200">
<VideoButton enabled={video} onToggle={() => callEmbed.control.toggleVideo()} />
{false && <ScreenShareButton />}
</Box>
<StatusDivider />
<Chip
variant="Critical"
radii="300"
fill="Soft"
before={<Icon size="50" src={Icons.PhoneDown} filled />}
outlined
onClick={() => callEmbed.hangup()}
>
<Text as="span" size="L400">
End
</Text>
</Chip>
</Box>
);
}
@@ -0,0 +1,54 @@
import { Room } from 'matrix-js-sdk';
import { Chip, Text } from 'folds';
import { useAtomValue } from 'jotai';
import { useRoomName } from '../../hooks/useRoomMeta';
import { RoomIcon } from '../../components/room-avatar';
import { roomToParentsAtom } from '../../state/room/roomToParents';
import { getAllParents, guessPerfectParent } from '../../utils/room';
import { useOrphanSpaces } from '../../state/hooks/roomList';
import { useMatrixClient } from '../../hooks/useMatrixClient';
import { allRoomsAtom } from '../../state/room-list/roomList';
import { mDirectAtom } from '../../state/mDirectList';
import { useAllJoinedRoomsSet, useGetRoom } from '../../hooks/useGetRoom';
import { useRoomNavigate } from '../../hooks/useRoomNavigate';
type CallRoomNameProps = {
room: Room;
};
export function CallRoomName({ room }: CallRoomNameProps) {
const mx = useMatrixClient();
const name = useRoomName(room);
const roomToParents = useAtomValue(roomToParentsAtom);
const orphanSpaces = useOrphanSpaces(mx, allRoomsAtom, roomToParents);
const mDirects = useAtomValue(mDirectAtom);
const dm = mDirects.has(room.roomId);
const allRoomsSet = useAllJoinedRoomsSet();
const getRoom = useGetRoom(allRoomsSet);
const allParents = getAllParents(roomToParents, room.roomId);
const orphanParents = allParents && orphanSpaces.filter((o) => allParents.has(o));
const perfectOrphanParent = orphanParents && guessPerfectParent(mx, room.roomId, orphanParents);
const { navigateRoom } = useRoomNavigate();
return (
<Chip
variant="Background"
radii="Pill"
before={
<RoomIcon size="200" joinRule={room.getJoinRule()} roomType={room.getType()} filled />
}
onClick={() => navigateRoom(room.roomId)}
>
<Text size="L400" truncate>
{name}
{!dm && perfectOrphanParent && (
<Text as="span" size="T200" priority="300">
{' •'} <b>{getRoom(perfectOrphanParent)?.name ?? perfectOrphanParent}</b>
</Text>
)}
</Text>
</Chip>
);
}
@@ -0,0 +1,51 @@
import { Box, Spinner } from 'folds';
import classNames from 'classnames';
import { LiveChip } from './LiveChip';
import * as css from './styles.css';
import { CallRoomName } from './CallRoomName';
import { CallControl } from './CallControl';
import { ContainerColor } from '../../styles/ContainerColor.css';
import { useCallMembers, useCallSession } from '../../hooks/useCall';
import { ScreenSize, useScreenSize } from '../../hooks/useScreenSize';
import { MemberGlance } from './MemberGlance';
import { StatusDivider } from './components';
import { CallEmbed } from '../../plugins/call/CallEmbed';
import { useCallJoined } from '../../hooks/useCallEmbed';
type CallStatusProps = {
callEmbed: CallEmbed;
};
export function CallStatus({ callEmbed }: CallStatusProps) {
const { room } = callEmbed;
const callSession = useCallSession(room);
const callMembers = useCallMembers(room, callSession);
const screenSize = useScreenSize();
const callJoined = useCallJoined(callEmbed);
return (
<Box
className={classNames(css.CallStatus, ContainerColor({ variant: 'Background' }))}
shrink="No"
gap="400"
alignItems="Center"
direction={screenSize === ScreenSize.Mobile ? 'Column' : 'Row'}
>
<Box grow="Yes" alignItems="Inherit" gap="200">
{callJoined && callMembers.length > 0 ? (
<Box shrink="No" gap="Inherit" alignItems="Inherit">
<MemberGlance room={room} members={callMembers} />
<LiveChip count={callMembers.length} room={room} members={callMembers} />
</Box>
) : (
<Spinner variant="Secondary" size="200" />
)}
<StatusDivider />
<CallRoomName room={room} />
</Box>
<Box shrink="No" alignItems="Inherit" gap="Inherit">
<CallControl callEmbed={callEmbed} />
</Box>
</Box>
);
}
+137
View File
@@ -0,0 +1,137 @@
import { MouseEventHandler, useState } from 'react';
import {
Avatar,
Badge,
Box,
Chip,
config,
Icon,
Icons,
Menu,
MenuItem,
PopOut,
RectCords,
Scroll,
Text,
toRem,
} from 'folds';
import { CallMembership } from 'matrix-js-sdk/lib/matrixrtc/CallMembership';
import FocusTrap from 'focus-trap-react';
import { Room } from 'matrix-js-sdk';
import * as css from './styles.css';
import { stopPropagation } from '../../utils/keyboard';
import { getMemberAvatarMxc, getMemberDisplayName } from '../../utils/room';
import { getMxIdLocalPart, mxcUrlToHttp } from '../../utils/matrix';
import { useMatrixClient } from '../../hooks/useMatrixClient';
import { useMediaAuthentication } from '../../hooks/useMediaAuthentication';
import { UserAvatar } from '../../components/user-avatar';
import { useOpenUserRoomProfile } from '../../state/hooks/userRoomProfile';
import { getMouseEventCords } from '../../utils/dom';
type LiveChipProps = {
room: Room;
members: CallMembership[];
count: number;
};
export function LiveChip({ count, room, members }: LiveChipProps) {
const mx = useMatrixClient();
const useAuthentication = useMediaAuthentication();
const openUserProfile = useOpenUserRoomProfile();
const [cords, setCords] = useState<RectCords>();
const handleOpenMenu: MouseEventHandler<HTMLButtonElement> = (evt) => {
setCords(evt.currentTarget.getBoundingClientRect());
};
return (
<PopOut
anchor={cords}
position="Top"
align="Start"
content={
<FocusTrap
focusTrapOptions={{
initialFocus: false,
onDeactivate: () => setCords(undefined),
clickOutsideDeactivates: true,
isKeyForward: (evt: KeyboardEvent) => evt.key === 'ArrowDown',
isKeyBackward: (evt: KeyboardEvent) => evt.key === 'ArrowUp',
escapeDeactivates: stopPropagation,
}}
>
<Menu
style={{
maxHeight: '75vh',
maxWidth: toRem(300),
display: 'flex',
}}
>
<Box grow="Yes">
<Scroll size="0" hideTrack visibility="Hover">
<Box direction="Column" style={{ padding: config.space.S100 }}>
{members.map((callMember) => {
const userId = callMember.sender;
if (!userId) return null;
const name =
getMemberDisplayName(room, userId) ?? getMxIdLocalPart(userId) ?? userId;
const avatarMxc = getMemberAvatarMxc(room, userId);
const avatarUrl = avatarMxc
? (mxcUrlToHttp(mx, avatarMxc, useAuthentication, 96, 96) ?? undefined)
: undefined;
return (
<MenuItem
key={callMember.membershipID}
size="400"
variant="Surface"
radii="300"
style={{ paddingLeft: config.space.S200 }}
onClick={(evt) =>
openUserProfile(
room.roomId,
undefined,
userId,
getMouseEventCords(evt.nativeEvent),
'Right'
)
}
before={
<Avatar size="200" radii="400">
<UserAvatar
userId={userId}
src={avatarUrl}
alt={name}
renderFallback={() => <Icon size="50" src={Icons.User} filled />}
/>
</Avatar>
}
>
<Text size="T300" truncate>
{name}
</Text>
</MenuItem>
);
})}
</Box>
</Scroll>
</Box>
</Menu>
</FocusTrap>
}
>
<Chip
variant="Surface"
fill="Soft"
before={<Badge variant="Critical" fill="Solid" size="200" />}
after={<Icon size="50" src={cords ? Icons.ChevronBottom : Icons.ChevronTop} />}
radii="Pill"
onClick={handleOpenMenu}
>
<Text className={css.LiveChipText} as="span" size="L400">
{count} Live
</Text>
</Chip>
</PopOut>
);
}
@@ -0,0 +1,71 @@
import { Box, config, Icon, Icons, Text } from 'folds';
import { CallMembership } from 'matrix-js-sdk/lib/matrixrtc/CallMembership';
import { Room } from 'matrix-js-sdk';
import { UserAvatar } from '../../components/user-avatar';
import { getMemberAvatarMxc, getMemberDisplayName } from '../../utils/room';
import { getMxIdLocalPart, mxcUrlToHttp } from '../../utils/matrix';
import { useMatrixClient } from '../../hooks/useMatrixClient';
import { useMediaAuthentication } from '../../hooks/useMediaAuthentication';
import { StackedAvatar } from '../../components/stacked-avatar';
import { useOpenUserRoomProfile } from '../../state/hooks/userRoomProfile';
import { getMouseEventCords } from '../../utils/dom';
type MemberGlanceProps = {
room: Room;
members: CallMembership[];
max?: number;
};
export function MemberGlance({ room, members, max = 6 }: MemberGlanceProps) {
const mx = useMatrixClient();
const useAuthentication = useMediaAuthentication();
const openUserProfile = useOpenUserRoomProfile();
const visibleMembers = members.slice(0, max);
const remainingCount = max && members.length > max ? members.length - max : 0;
return (
<Box alignItems="Center">
{visibleMembers.map((callMember) => {
const userId = callMember.sender;
if (!userId) return null;
const name = getMemberDisplayName(room, userId) ?? getMxIdLocalPart(userId) ?? userId;
const avatarMxc = getMemberAvatarMxc(room, userId);
const avatarUrl = avatarMxc
? (mxcUrlToHttp(mx, avatarMxc, useAuthentication, 96, 96) ?? undefined)
: undefined;
return (
<StackedAvatar
key={callMember.membershipID}
title={name}
as="button"
variant="Background"
size="200"
radii="Pill"
onClick={(evt) =>
openUserProfile(
room.roomId,
undefined,
userId,
getMouseEventCords(evt.nativeEvent),
'Top'
)
}
>
<UserAvatar
userId={userId}
src={avatarUrl}
alt={name}
renderFallback={() => <Icon size="50" src={Icons.User} filled />}
/>
</StackedAvatar>
);
})}
{remainingCount > 0 && (
<Text size="L400" style={{ paddingLeft: config.space.S100 }}>
+{remainingCount}
</Text>
)}
</Box>
);
}
@@ -0,0 +1,8 @@
import { Line } from 'folds';
import * as css from './styles.css';
export function StatusDivider() {
return (
<Line variant="Background" size="300" direction="Vertical" className={css.ControlDivider} />
);
}
+1
View File
@@ -0,0 +1 @@
export * from './CallStatus';
@@ -0,0 +1,17 @@
import { style } from '@vanilla-extract/css';
import { color, config, toRem } from 'folds';
export const LiveChipText = style({
color: color.Critical.Main,
});
export const CallStatus = style([
{
padding: `${toRem(6)} ${config.space.S200}`,
borderTop: `${config.borderWidth.B300} solid ${color.Background.ContainerLine}`,
},
]);
export const ControlDivider = style({
height: toRem(16),
});
+121
View File
@@ -0,0 +1,121 @@
import { CallMembership, SessionMembershipData } from 'matrix-js-sdk/lib/matrixrtc/CallMembership';
import { useState } from 'react';
import { Avatar, Box, Icon, Icons, Text } from 'folds';
import { useMatrixClient } from '../../hooks/useMatrixClient';
import { useMediaAuthentication } from '../../hooks/useMediaAuthentication';
import { useOpenUserRoomProfile } from '../../state/hooks/userRoomProfile';
import { SequenceCard } from '../../components/sequence-card';
import { getMemberAvatarMxc, getMemberDisplayName } from '../../utils/room';
import { useRoom } from '../../hooks/useRoom';
import { getMxIdLocalPart, mxcUrlToHttp } from '../../utils/matrix';
import { UserAvatar } from '../../components/user-avatar';
import { getMouseEventCords } from '../../utils/dom';
import * as css from './styles.css';
interface MemberWithMembershipData {
membershipData?: SessionMembershipData & {
'm.call.intent': 'video' | 'audio';
};
}
type CallMemberCardProps = {
member: CallMembership;
};
export function CallMemberCard({ member }: CallMemberCardProps) {
const mx = useMatrixClient();
const useAuthentication = useMediaAuthentication();
const room = useRoom();
const openUserProfile = useOpenUserRoomProfile();
const userId = member.sender;
if (!userId) return null;
const name = getMemberDisplayName(room, userId) ?? getMxIdLocalPart(userId) ?? userId;
const avatarMxc = getMemberAvatarMxc(room, userId);
const avatarUrl = avatarMxc
? (mxcUrlToHttp(mx, avatarMxc, useAuthentication, 96, 96) ?? undefined)
: undefined;
const audioOnly =
(member as unknown as MemberWithMembershipData).membershipData?.['m.call.intent'] === 'audio';
return (
<SequenceCard
as="button"
key={member.membershipID}
className={css.CallMemberCard}
variant="SurfaceVariant"
radii="500"
onClick={(evt: any) =>
openUserProfile(
room.roomId,
undefined,
userId,
getMouseEventCords(evt.nativeEvent),
'Right'
)
}
>
<Box grow="Yes" gap="300" alignItems="Center">
<Avatar size="200" radii="400">
<UserAvatar
userId={userId}
src={avatarUrl}
alt={name}
renderFallback={() => <Icon size="50" src={Icons.User} filled />}
/>
</Avatar>
<Box grow="Yes">
<Text size="L400" truncate>
{name}
</Text>
</Box>
{audioOnly && <Icon src={Icons.VideoCameraMute} size="100" />}
</Box>
</SequenceCard>
);
}
export function CallMemberRenderer({
members,
max = 4,
}: {
members: CallMembership[];
max?: number;
}) {
const [viewMore, setViewMore] = useState(false);
const truncatedMembers = viewMore ? members : members.slice(0, 4);
const remaining = members.length - truncatedMembers.length;
return (
<>
{truncatedMembers.map((member) => (
<CallMemberCard key={member.membershipID} member={member} />
))}
{members.length > max && (
<SequenceCard
as="button"
className={css.CallMemberCard}
variant="SurfaceVariant"
radii="500"
onClick={() => setViewMore(!viewMore)}
>
<Box grow="Yes" gap="300" alignItems="Center">
{viewMore ? (
<Text size="L400" truncate>
Collapse
</Text>
) : (
<Text size="L400" truncate>
{remaining === 0 ? `+${remaining} Other` : `+${remaining} Others`}
</Text>
)}
</Box>
<Icon src={viewMore ? Icons.ChevronTop : Icons.ChevronBottom} size="100" />
</SequenceCard>
)}
</>
);
}
-37
View File
@@ -1,37 +0,0 @@
import { style } from '@vanilla-extract/css';
import { DefaultReset, config } from 'folds';
import { ContainerColor } from '$styles/ContainerColor.css';
export const CallViewUserGrid = style({
display: 'flex',
flexWrap: 'wrap',
justifyContent: 'center',
alignItems: 'center',
marginInline: '20px',
gap: config.space.S400,
});
export const CallViewUser = style([
DefaultReset,
ContainerColor({ variant: 'SurfaceVariant' }),
{
height: '90px',
width: '150px',
borderRadius: config.radii.R500,
},
]);
export const UserLink = style({
color: 'inherit',
minWidth: 0,
cursor: 'pointer',
flexGrow: 0,
transition: 'all ease-out 200ms',
':hover': {
transform: 'translateY(-3px)',
textDecoration: 'unset',
},
':focus': {
outline: 'none',
},
});
+80 -264
View File
@@ -1,289 +1,105 @@
import { EventType, Room } from '$types/matrix-sdk';
import {
Children,
useContext,
useCallback,
useEffect,
useRef,
MouseEventHandler,
useState,
ReactNode,
} from 'react';
import { Box, Button, config, Spinner, Text } from 'folds';
import { useCallState } from '$pages/client/call/CallProvider';
import { useCallMembers } from '$hooks/useCallMemberships';
import { CallRefContext } from '$pages/client/call/PersistentCallContainer';
import { ScreenSize, useScreenSizeContext } from '$hooks/useScreenSize';
import { useDebounce } from '$hooks/useDebounce';
import { useMatrixClient } from '$hooks/useMatrixClient';
import { useRoomNavigate } from '$hooks/useRoomNavigate';
import { getMemberDisplayName } from '$utils/room';
import { getMxIdLocalPart } from '$utils/matrix';
import { useRoomPermissions } from '$hooks/useRoomPermissions';
import { useRoomCreators } from '$hooks/useRoomCreators';
import { useRef } from 'react';
import { Badge, Box, color, Header, Scroll, Text, toRem } from 'folds';
import { EventType } from '$types/matrix-sdk';
import { useCallEmbed, useCallJoined, useSyncCallEmbedPlacement } from '$hooks/useCallEmbed';
import { ContainerColor } from '$styles/ContainerColor.css';
import { usePowerLevelsContext } from '$hooks/usePowerLevels';
import { useRoomName } from '$hooks/useRoomMeta';
import { useAtomValue } from 'jotai';
import { nicknamesAtom } from '$state/nicknames';
import { settingsAtom } from '$state/settings';
import { useSetting } from '$state/hooks/settings';
import * as css from './CallView.css';
import { CallViewUser } from './CallViewUser';
import { useRoom } from '$hooks/useRoom';
import { useRoomCreators } from '$hooks/useRoomCreators';
import { useRoomPermissions } from '$hooks/useRoomPermissions';
import { useMatrixClient } from '$hooks/useMatrixClient';
import { useCallMembers, useCallSession } from '$hooks/useCall';
import { PrescreenControls } from './PrescreenControls';
import { CallMemberRenderer } from './CallMemberCard';
import * as css from './styles.css';
type OriginalStyles = {
position?: string;
top?: string;
left?: string;
width?: string;
height?: string;
zIndex?: string;
display?: string;
visibility?: string;
pointerEvents?: string;
border?: string;
};
function JoinMessage({ hasParticipant }: { hasParticipant?: boolean }) {
if (hasParticipant) return null;
export function CallViewUserGrid({ children }: { children: ReactNode }) {
return (
<Box
className={css.CallViewUserGrid}
style={{
maxWidth: Children.count(children) === 4 ? '336px' : '503px',
}}
>
{children}
</Box>
<Text style={{ margin: 'auto' }} size="L400" align="Center">
Voice chat&apos;s empty — Be the first to hop in!
</Text>
);
}
export function CallView({ room }: { room: Room }) {
const callIframeRef = useContext(CallRefContext);
const iframeHostRef = useRef<HTMLDivElement>(null);
function NoPermissionMessage() {
return (
<Text style={{ margin: 'auto' }} size="L400" align="Center">
You don&apos;t have permission to join!
</Text>
);
}
const originalIframeStylesRef = useRef<OriginalStyles | null>(null);
function AlreadyInCallMessage() {
return (
<Text style={{ margin: 'auto', color: color.Warning.Main }} size="L400" align="Center">
Already in another call — End the current call to join!
</Text>
);
}
export function CallView() {
const mx = useMatrixClient();
const room = useRoom();
const [visibleCallNames, setVisibleCallNames] = useState('');
const callViewRef = useRef<HTMLDivElement>(null);
useSyncCallEmbedPlacement(callViewRef);
const powerLevels = usePowerLevelsContext();
const creators = useRoomCreators(room);
const roomName = useRoomName(room);
const permissions = useRoomPermissions(creators, powerLevels);
const canJoin = permissions.event(EventType.GroupCallMemberPrefix, mx.getSafeUserId());
const {
isActiveCallReady,
activeCallRoomId,
isChatOpen,
setActiveCallRoomId,
hangUp,
setViewedCallRoomId,
} = useCallState();
const callSession = useCallSession(room);
const callMembers = useCallMembers(room, callSession);
const hasParticipant = callMembers.length > 0;
const isActiveCallRoom = activeCallRoomId === room.roomId;
const callIsCurrentAndReady = isActiveCallRoom && isActiveCallReady;
const callMembers = useCallMembers(mx, room.roomId);
const callEmbed = useCallEmbed();
const callJoined = useCallJoined(callEmbed);
const inOtherCall = callEmbed && callEmbed.roomId !== room.roomId;
const nicknames = useAtomValue(nicknamesAtom);
const getName = (userId: string) =>
getMemberDisplayName(room, userId, nicknames) ?? getMxIdLocalPart(userId);
const memberDisplayNames = callMembers.map((callMembership) =>
getName(callMembership.sender ?? '')
);
const { navigateRoom } = useRoomNavigate();
const screenSize = useScreenSizeContext();
const isMobile = screenSize === ScreenSize.Mobile;
const activeIframeDisplayRef = callIframeRef;
const applyFixedPositioningToIframe = useCallback(() => {
const iframeElement = activeIframeDisplayRef?.current;
const hostElement = iframeHostRef?.current;
if (iframeElement && hostElement) {
if (!originalIframeStylesRef.current) {
const computed = window.getComputedStyle(iframeElement);
originalIframeStylesRef.current = {
position: iframeElement.style.position || computed.position,
top: iframeElement.style.top || computed.top,
left: iframeElement.style.left || computed.left,
width: iframeElement.style.width || computed.width,
height: iframeElement.style.height || computed.height,
zIndex: iframeElement.style.zIndex || computed.zIndex,
display: iframeElement.style.display || computed.display,
visibility: iframeElement.style.visibility || computed.visibility,
pointerEvents: iframeElement.style.pointerEvents || computed.pointerEvents,
border: iframeElement.style.border || computed.border,
};
}
const hostRect = hostElement.getBoundingClientRect();
iframeElement.style.position = 'fixed';
iframeElement.style.top = `${hostRect.top}px`;
iframeElement.style.left = `${hostRect.left}px`;
iframeElement.style.width = `${hostRect.width}px`;
iframeElement.style.height = `${hostRect.height}px`;
iframeElement.style.border = 'none';
iframeElement.style.zIndex = '1000';
iframeElement.style.display = room.isCallRoom() ? 'block' : 'none';
iframeElement.style.visibility = 'visible';
iframeElement.style.pointerEvents = 'auto';
}
}, [activeIframeDisplayRef, room]);
const debouncedApplyFixedPositioning = useDebounce(applyFixedPositioningToIframe, {
wait: 50,
immediate: false,
});
const [allowPipVideos] = useSetting(settingsAtom, 'allowPipVideos');
useEffect(() => {
const iframeElement = activeIframeDisplayRef?.current;
const hostElement = iframeHostRef?.current;
if (allowPipVideos) {
const removeDisablePictureInPicture = (mutated: any) => {
mutated.forEach((event: any) => {
Array.from(event.target.getElementsByTagName('video')).forEach((video: any) => {
video.removeAttribute('disablepictureinpicture');
});
});
};
const pipObserver = new MutationObserver(removeDisablePictureInPicture);
pipObserver.observe(iframeElement?.contentDocument!, { subtree: true, childList: true });
}
if (room.isCallRoom() || (callIsCurrentAndReady && iframeElement && hostElement)) {
applyFixedPositioningToIframe();
const resizeObserver = new ResizeObserver(debouncedApplyFixedPositioning);
if (hostElement) resizeObserver.observe(hostElement);
window.addEventListener('scroll', debouncedApplyFixedPositioning, true);
return () => {
resizeObserver.disconnect();
window.removeEventListener('scroll', debouncedApplyFixedPositioning, true);
if (iframeElement && originalIframeStylesRef.current) {
const originalStyles = originalIframeStylesRef.current;
(Object.keys(originalStyles) as Array<keyof OriginalStyles>).forEach((key) => {
if (key in iframeElement.style) {
iframeElement.style[key as any] = originalStyles[key] || '';
}
});
}
originalIframeStylesRef.current = null;
};
}
return undefined;
}, [
activeIframeDisplayRef,
applyFixedPositioningToIframe,
debouncedApplyFixedPositioning,
callIsCurrentAndReady,
room,
allowPipVideos,
]);
const handleJoinVCClick: MouseEventHandler<HTMLElement> = (evt) => {
if (!canJoin) return;
if (isMobile) {
evt.stopPropagation();
setViewedCallRoomId(room.roomId);
navigateRoom(room.roomId);
}
if (!callIsCurrentAndReady) {
hangUp();
setActiveCallRoomId(room.roomId);
}
};
const isCallViewVisible = room.isCallRoom() && (screenSize === ScreenSize.Desktop || !isChatOpen);
useEffect(() => {
if (memberDisplayNames.length <= 2) {
setVisibleCallNames(memberDisplayNames.join(' and '));
} else {
const visible = memberDisplayNames.slice(0, 2);
const remaining = memberDisplayNames.length - 2;
setVisibleCallNames(
`${visible.join(', ')}, and ${remaining} other${remaining > 1 ? 's' : ''}`
);
}
}, [memberDisplayNames]);
const currentJoined = callEmbed?.roomId === room.roomId && callJoined;
return (
<Box grow="Yes" direction="Column" style={{ display: isCallViewVisible ? 'flex' : 'none' }}>
<div
ref={iframeHostRef}
style={{
width: '100%',
height: '100%',
position: 'relative',
pointerEvents: 'none',
display: callIsCurrentAndReady ? 'flex' : 'none',
}}
/>
<Box
grow="Yes"
justifyContent="Center"
alignItems="Center"
direction="Column"
gap="300"
style={{
display: callIsCurrentAndReady ? 'none' : 'flex',
}}
>
<CallViewUserGrid>
{callMembers.slice(0, 6).map((callMember) => (
<CallViewUser key={callMember.membershipID} room={room} callMembership={callMember} />
))}
</CallViewUserGrid>
<Box
direction="Column"
alignItems="Center"
style={{
paddingBlock: config.space.S200,
}}
>
<Text
size="H1"
style={{
paddingBottom: config.space.S300,
}}
>
{roomName}
</Text>
<Text size="T200">
{visibleCallNames !== '' ? visibleCallNames : 'No one'}{' '}
{memberDisplayNames.length > 1 ? 'are' : 'is'} currently in voice
</Text>
</Box>
<Button
variant="Secondary"
disabled={!canJoin || isActiveCallRoom}
onClick={handleJoinVCClick}
>
{isActiveCallRoom ? (
<Box justifyContent="Center" alignItems="Center" gap="200">
<Spinner />
<Text size="B500">{activeCallRoomId === room.roomId ? `Joining` : 'Join Voice'}</Text>
<Box
ref={callViewRef}
className={ContainerColor({ variant: 'Surface' })}
style={{ minWidth: toRem(280) }}
grow="Yes"
>
{!currentJoined && (
<Scroll variant="Surface" hideTrack>
<Box className={css.CallViewContent} alignItems="Center" justifyContent="Center">
<Box style={{ maxWidth: toRem(382), width: '100%' }} direction="Column" gap="100">
{hasParticipant && (
<Header size="300">
<Box grow="Yes" alignItems="Center">
<Text size="L400">Participant</Text>
</Box>
<Badge variant="Critical" fill="Solid" size="400">
<Text as="span" size="L400" truncate>
{callMembers.length} Live
</Text>
</Badge>
</Header>
)}
<CallMemberRenderer members={callMembers} />
<PrescreenControls canJoin={canJoin} />
<Header size="300">
{!inOtherCall &&
(canJoin ? (
<JoinMessage hasParticipant={hasParticipant} />
) : (
<NoPermissionMessage />
))}
{inOtherCall && <AlreadyInCallMessage />}
</Header>
</Box>
) : (
<Text size="B500">{canJoin ? 'Join Voice' : 'Channel Locked'}</Text>
)}
</Button>
</Box>
</Box>
</Scroll>
)}
</Box>
);
}
-73
View File
@@ -1,73 +0,0 @@
import { as, Avatar, Box, Icon, Icons, Text } from 'folds';
import { MouseEventHandler } from 'react';
import classNames from 'classnames';
import { Room, CallMembership } from '$types/matrix-sdk';
import { UserAvatar } from '$components/user-avatar';
import { useMatrixClient } from '$hooks/useMatrixClient';
import { getMxIdLocalPart } from '$utils/matrix';
import { getMemberAvatarMxc, getMemberDisplayName } from '$utils/room';
import { useAtomValue } from 'jotai';
import { nicknamesAtom } from '$state/nicknames';
import { useMediaAuthentication } from '$hooks/useMediaAuthentication';
import { useOpenUserRoomProfile } from '$state/hooks/userRoomProfile';
import { useSpaceOptionally } from '$hooks/useSpace';
import * as css from './CallView.css';
type CallViewUserProps = {
room: Room;
callMembership: CallMembership;
};
export const UserProfileButton = as<'button'>(
({ as: AsUserProfileButton = 'button', className, ...props }, ref) => (
<AsUserProfileButton className={classNames(css.UserLink, className)} {...props} ref={ref} />
)
);
export const CallViewUserBase = as<'div'>(({ className, ...props }, ref) => (
<Box
direction="Column"
gap="300"
className={classNames(css.CallViewUser, className)}
{...props}
ref={ref}
/>
));
export function CallViewUser({ room, callMembership }: CallViewUserProps) {
const mx = useMatrixClient();
const useAuthentication = useMediaAuthentication();
const openProfile = useOpenUserRoomProfile();
const space = useSpaceOptionally();
const userId = callMembership.sender ?? '';
const avatarMxcUrl = getMemberAvatarMxc(room, userId);
const avatarUrl = avatarMxcUrl
? mx.mxcUrlToHttp(avatarMxcUrl, 32, 32, 'crop', undefined, false, useAuthentication)
: undefined;
const nicknames = useAtomValue(nicknamesAtom);
const getName = getMemberDisplayName(room, userId, nicknames) ?? getMxIdLocalPart(userId);
const handleUserClick: MouseEventHandler<HTMLButtonElement> = (evt) => {
openProfile(room.roomId, space?.roomId, userId, evt.currentTarget.getBoundingClientRect());
};
return (
<UserProfileButton onClick={handleUserClick} aria-label={getName}>
<CallViewUserBase>
<Box direction="Column" grow="Yes" alignItems="Center" gap="200" justifyContent="Center">
<Avatar size="200">
<UserAvatar
userId={userId}
src={avatarUrl ?? undefined}
alt={getName}
renderFallback={() => <Icon size="50" src={Icons.User} filled />}
/>
</Avatar>
<Text size="B400" priority="300" truncate>
{getName}
</Text>
</Box>
</CallViewUserBase>
</UserProfileButton>
);
}
-9
View File
@@ -1,9 +0,0 @@
import { Widget } from 'matrix-widget-api';
import type { IApp } from './SmallWidget';
// Wrapper class for the widget definition
export class CinnyWidget extends Widget {
public constructor(private rawDefinition: IApp) {
super(rawDefinition);
}
}
+144
View File
@@ -0,0 +1,144 @@
import { Icon, IconButton, Icons, Line, Text, Tooltip, TooltipProvider } from 'folds';
import { useAtom } from 'jotai';
import * as css from './styles.css';
import { callChatAtom } from '../../state/callEmbed';
export function ControlDivider() {
return (
<Line variant="SurfaceVariant" size="300" direction="Vertical" className={css.ControlDivider} />
);
}
type MicrophoneButtonProps = {
enabled: boolean;
onToggle: () => void;
};
export function MicrophoneButton({ enabled, onToggle }: MicrophoneButtonProps) {
return (
<TooltipProvider
position="Top"
delay={500}
tooltip={
<Tooltip>
<Text size="T200">{enabled ? 'Turn Off Microphone' : 'Turn On Microphone'}</Text>
</Tooltip>
}
>
{(anchorRef) => (
<IconButton
ref={anchorRef}
variant={enabled ? 'Surface' : 'Warning'}
fill="Soft"
radii="400"
size="400"
onClick={() => onToggle()}
outlined
>
<Icon size="400" src={enabled ? Icons.Mic : Icons.MicMute} filled={!enabled} />
</IconButton>
)}
</TooltipProvider>
);
}
type SoundButtonProps = {
enabled: boolean;
onToggle: () => void;
};
export function SoundButton({ enabled, onToggle }: SoundButtonProps) {
return (
<TooltipProvider
position="Top"
delay={500}
tooltip={
<Tooltip>
<Text size="T200">{enabled ? 'Turn Off Sound' : 'Turn On Sound'}</Text>
</Tooltip>
}
>
{(anchorRef) => (
<IconButton
ref={anchorRef}
variant={enabled ? 'Surface' : 'Warning'}
fill="Soft"
radii="400"
size="400"
onClick={() => onToggle()}
outlined
>
<Icon
size="400"
src={enabled ? Icons.Headphone : Icons.HeadphoneMute}
filled={!enabled}
/>
</IconButton>
)}
</TooltipProvider>
);
}
type VideoButtonProps = {
enabled: boolean;
onToggle: () => void;
};
export function VideoButton({ enabled, onToggle }: VideoButtonProps) {
return (
<TooltipProvider
position="Top"
delay={500}
tooltip={
<Tooltip>
<Text size="T200">{enabled ? 'Stop Camera' : 'Start Camera'}</Text>
</Tooltip>
}
>
{(anchorRef) => (
<IconButton
ref={anchorRef}
variant={enabled ? 'Success' : 'Surface'}
fill="Soft"
radii="400"
size="400"
onClick={() => onToggle()}
outlined
>
<Icon
size="400"
src={enabled ? Icons.VideoCamera : Icons.VideoCameraMute}
filled={enabled}
/>
</IconButton>
)}
</TooltipProvider>
);
}
export function ChatButton() {
const [chat, setChat] = useAtom(callChatAtom);
return (
<TooltipProvider
position="Top"
delay={500}
tooltip={
<Tooltip>
<Text size="T200">{chat ? 'Close Chat' : 'Open Chat'}</Text>
</Tooltip>
}
>
{(anchorRef) => (
<IconButton
ref={anchorRef}
variant={chat ? 'Success' : 'Surface'}
fill="Soft"
radii="400"
size="400"
onClick={() => setChat(!chat)}
outlined
>
<Icon size="400" src={Icons.Message} filled={chat} />
</IconButton>
)}
</TooltipProvider>
);
}
@@ -0,0 +1,67 @@
import { Box, Button, Icon, Icons, Spinner, Text } from 'folds';
import { SequenceCard } from '../../components/sequence-card';
import * as css from './styles.css';
import { ChatButton, ControlDivider, MicrophoneButton, SoundButton, VideoButton } from './Controls';
import { useIsDirectRoom, useRoom } from '../../hooks/useRoom';
import { useCallEmbed, useCallJoined, useCallStart } from '../../hooks/useCallEmbed';
import { useCallPreferences } from '../../state/hooks/callPreferences';
import { CallControlState } from '../../plugins/call/CallControlState';
type PrescreenControlsProps = {
canJoin?: boolean;
};
export function PrescreenControls({ canJoin }: PrescreenControlsProps) {
const room = useRoom();
const callEmbed = useCallEmbed();
const callJoined = useCallJoined(callEmbed);
const direct = useIsDirectRoom();
const inOtherCall = callEmbed && callEmbed.roomId !== room.roomId;
const startCall = useCallStart(direct);
const joining = callEmbed?.roomId === room.roomId && !callJoined;
const disabled = inOtherCall || !canJoin;
const { microphone, video, sound, toggleMicrophone, toggleVideo, toggleSound } =
useCallPreferences();
return (
<SequenceCard
className={css.ControlCard}
variant="SurfaceVariant"
gap="400"
radii="500"
alignItems="Center"
justifyContent="SpaceBetween"
wrap="Wrap"
>
<Box shrink="No" alignItems="Inherit" justifyContent="SpaceBetween" gap="200">
<MicrophoneButton enabled={microphone} onToggle={toggleMicrophone} />
<SoundButton enabled={sound} onToggle={toggleSound} />
</Box>
<ControlDivider />
<Box shrink="No" alignItems="Inherit" justifyContent="SpaceBetween" gap="200">
<VideoButton enabled={video} onToggle={toggleVideo} />
<ChatButton />
</Box>
<Box grow="Yes" direction="Column">
<Button
variant={disabled ? 'Secondary' : 'Success'}
fill={disabled ? 'Soft' : 'Solid'}
onClick={() => startCall(room, new CallControlState(microphone, video, sound))}
disabled={disabled || joining}
before={
joining ? (
<Spinner variant="Success" fill="Solid" size="200" />
) : (
<Icon src={Icons.Phone} size="200" filled />
)
}
>
<Text size="B400">Join</Text>
</Button>
</Box>
</SequenceCard>
);
}
-439
View File
@@ -1,439 +0,0 @@
/*
* Copyright 2024 New Vector Ltd.
* Copyright 2020-2023 The Matrix.org Foundation C.I.C.
*
* SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
* Please see LICENSE files in the repository root for full details.
*/
import EventEmitter from 'events';
import {
ClientEvent,
Direction,
IEvent,
KnownMembership,
MatrixClient,
MatrixEvent,
MatrixEventEvent,
Room,
RoomStateEvent,
} from '$types/matrix-sdk';
import {
ClientWidgetApi,
IRoomEvent,
IStickyActionRequest,
IWidget,
IWidgetData,
MatrixCapabilities,
WidgetApiFromWidgetAction,
WidgetKind,
} from 'matrix-widget-api';
import { createLogger } from '$utils/debug';
import { CinnyWidget } from './CinnyWidget';
import { SmallWidgetDriver } from './SmallWidgetDriver';
const log = createLogger('SmallWidget');
/**
* Returns the appropriate EC `intent` and `callIntent` URL params for a room.
* - Voice rooms (`isCallRoom()`): `join_existing` intent, `audio` callType (persistent channel).
* - DM rooms (≤2 joined members): `start_call` intent, `video` callType (1:1 video call).
* - Group rooms: `start_call` intent, `audio` callType.
*/
export function getCallIntentParams(room: Room | null | undefined): {
intent: 'join_existing' | 'start_call';
callIntentParam: 'audio' | 'video';
} {
const isVoiceRoom = room?.isCallRoom() ?? false;
// Require room to be non-null: unknown/missing room should not be treated as a DM.
const isDmRoom = room != null && !isVoiceRoom && room.currentState.getJoinedMemberCount() <= 2;
return {
intent: isVoiceRoom ? 'join_existing' : 'start_call',
callIntentParam: isDmRoom ? 'video' : 'audio',
};
}
/**
* Generates the URL for the Element Call widget.
* @param mx - The MatrixClient instance.
* @param roomId - The ID of the room.
* @returns The generated URL object.
*/
export const getWidgetUrl = (
mx: MatrixClient,
roomId: string,
elementCallUrl: string,
widgetId: string,
setParams: any
): URL => {
const baseUrl = window.location.origin;
const url = elementCallUrl
? new URL(`${elementCallUrl}/room`)
: new URL('/public/element-call/index.html#', baseUrl);
const params = new URLSearchParams({
embed: 'true',
widgetId,
appPrompt: 'false',
skipLobby: setParams.skipLobby ?? 'true', // TODO: skipLobby is deprecated, use intent instead (intent doesn't produce the same effect?)
returnToLobby: setParams.returnToLobby ?? 'true',
perParticipantE2EE: setParams.perParticipantE2EE ?? 'true',
callIntent: setParams.callIntent ?? 'video',
header: 'none',
confineToRoom: 'true',
theme: setParams.theme ?? 'dark',
userId: mx.getUserId()!,
deviceId: mx.getDeviceId()!,
roomId,
baseUrl: mx.baseUrl,
parentUrl: window.location.origin,
});
const replacedParams = params.toString().replace(/%24/g, '$');
url.search = `?${replacedParams}`;
return url;
};
export interface IApp extends IWidget {
client: MatrixClient;
roomId: string;
eventId?: string;
avatar_url?: string;
sender: string;
'io.element.managed_hybrid'?: boolean;
}
export class SmallWidget extends EventEmitter {
private client: MatrixClient;
private messaging: ClientWidgetApi | null = null;
private mockWidget: CinnyWidget;
public roomId?: string;
public url?: string;
public iframe: HTMLIFrameElement | null = null;
private type: string; // Type of the widget (e.g., 'm.call')
private readUpToMap: Record<string, string> = {}; // room ID to event ID
private readonly eventsToFeed = new WeakSet<MatrixEvent>();
private stickyPromise?: () => Promise<void>;
constructor(private iapp: IApp) {
super();
this.client = iapp.client;
this.roomId = iapp.roomId;
this.url = iapp.url;
this.type = iapp.type;
this.mockWidget = new CinnyWidget(iapp);
}
/**
* Initializes the widget messaging API.
* @param iframe - The HTMLIFrameElement to bind to.
* @returns The initialized ClientWidgetApi instance.
*/
startMessaging(iframe: HTMLIFrameElement): ClientWidgetApi {
// Ensure the driver is correctly instantiated
// The capabilities array might need adjustment based on required permissions
const driver = new SmallWidgetDriver(
this.client,
[],
this.mockWidget,
WidgetKind.Room,
true,
this.roomId
);
this.iframe = iframe;
this.messaging = new ClientWidgetApi(this.mockWidget, iframe, driver);
this.messaging.setViewedRoomId(this.roomId ?? null);
// Emit events during the widget lifecycle
this.messaging.on('preparing', () => this.emit('preparing'));
this.messaging.on('error:preparing', (err: unknown) => this.emit('error:preparing', err));
this.messaging.once('ready', () => this.emit('ready'));
// this.messaging.on("capabilitiesNotified", () => this.emit("capabilitiesNotified")); // Uncomment if needed
// Populate the map of "read up to" events for this widget with the current event in every room.
// This is a bit inefficient, but should be okay. We do this for all rooms in case the widget
// requests timeline capabilities in other rooms down the road. It's just easier to manage here.
// eslint-disable-next-line no-restricted-syntax
for (const room of this.client.getRooms()) {
// Timelines are most recent last
const events = room.getLiveTimeline()?.getEvents() || [];
const roomEvent = events[events.length - 1];
// force later code to think the room is fresh
if (roomEvent) {
const eventId = roomEvent.getId();
if (eventId) this.readUpToMap[room.roomId] = eventId;
}
}
this.messaging.on('action:org.matrix.msc2876.read_events', (ev: CustomEvent) => {
const room = this.client.getRoom(this.roomId);
const events: Partial<IEvent>[] = [];
const { type } = ev.detail.data;
ev.preventDefault();
if (room === null) {
return this.messaging?.transport.reply(ev.detail, { events });
}
const state = room.getLiveTimeline().getState(Direction.Forward);
if (state === undefined) {
return this.messaging?.transport.reply(ev.detail, { events });
}
const stateEvents = state.events?.get(type);
Array.from(stateEvents?.values() ?? []).forEach((eventObject) => {
events.push(eventObject.event);
});
return this.messaging?.transport.reply(ev.detail, { events });
});
this.client.on(ClientEvent.Event, this.onEvent);
this.client.on(MatrixEventEvent.Decrypted, this.onEventDecrypted);
this.client.on(RoomStateEvent.Events, this.onStateUpdate);
this.client.on(ClientEvent.ToDeviceEvent, this.onToDeviceEvent);
this.messaging.on(
`action:${WidgetApiFromWidgetAction.UpdateAlwaysOnScreen}`,
async (ev: CustomEvent<IStickyActionRequest>) => {
if (this.messaging?.hasCapability(MatrixCapabilities.AlwaysOnScreen)) {
ev.preventDefault();
if (ev.detail.data.value) {
// If the widget wants to become sticky we wait for the stickyPromise to resolve
if (this.stickyPromise) await this.stickyPromise();
this.messaging.transport.reply(ev.detail, {});
}
// Stop being persistent can be done instantly
// MAKE PERSISTENT HERE
// Send the ack after the widget actually has become sticky.
}
}
);
return this.messaging;
}
private onEvent = (ev: MatrixEvent): void => {
this.client.decryptEventIfNeeded(ev);
if (!ev.isState()) this.feedEvent(ev);
};
private onEventDecrypted = (ev: MatrixEvent): void => {
if (!ev.isState()) this.feedEvent(ev);
};
private onReadEvent = (ev: MatrixEvent): void => {
this.feedEvent(ev);
};
private onStateUpdate = (ev: MatrixEvent): void => {
if (this.messaging === null || !ev.isState()) return;
const raw = ev.getEffectiveEvent();
this.messaging.feedStateUpdate(raw as IRoomEvent).catch((err) => {
log.warn('Failed to feed widget state update:', err);
});
};
private onToDeviceEvent = async (ev: MatrixEvent): Promise<void> => {
await this.client.decryptEventIfNeeded(ev);
if (ev.isDecryptionFailure()) return;
await this.messaging?.feedToDevice(ev.getEffectiveEvent() as IRoomEvent, ev.isEncrypted());
};
/**
* Determines whether the event comes from a room that we've been invited to
* (in which case we likely don't have the full timeline).
*/
private isFromInvite(ev: MatrixEvent): boolean {
const room = this.client.getRoom(ev.getRoomId());
return room?.getMyMembership() === KnownMembership.Invite;
}
/**
* Determines whether the event has a relation to an unknown parent.
*/
private relatesToUnknown(ev: MatrixEvent): boolean {
// Replies to unknown events don't count
if (!ev.relationEventId || ev.replyEventId) return false;
const room = this.client.getRoom(ev.getRoomId());
return room === null || !room.findEventById(ev.relationEventId);
}
// eslint-disable-next-line class-methods-use-this
private arrayFastClone<T>(a: T[]): T[] {
return a.slice(0, a.length);
}
private advanceReadUpToMarker(ev: MatrixEvent): boolean {
const evId = ev.getId();
if (evId === undefined) return false;
const roomId = ev.getRoomId();
if (roomId === undefined) return false;
const room = this.client.getRoom(roomId);
if (room === null) return false;
const upToEventId = this.readUpToMap[ev.getRoomId()!];
if (!upToEventId) {
// There's no marker yet; start it at this event
this.readUpToMap[roomId] = evId;
return true;
}
// Small optimization for exact match (skip the search)
if (upToEventId === evId) return false;
// Timelines are most recent last, so reverse the order and limit ourselves to 100 events
// to avoid overusing the CPU.
const timeline = room.getLiveTimeline();
const events = this.arrayFastClone(timeline.getEvents()).reverse().slice(0, 100);
let advanced = false;
events.some((timelineEvent) => {
const id = timelineEvent.getId();
if (id === upToEventId) {
// The event must be somewhere before the "read up to" marker
return true;
}
if (id === evId) {
// The event is after the marker; advance it
this.readUpToMap[roomId] = evId;
advanced = true;
return true;
}
// We can't say for sure whether the widget has seen the event; let's
// just assume that it has
return false;
});
return advanced;
}
private feedEvent(ev: MatrixEvent): void {
if (this.messaging === null) return;
if (
// If we had decided earlier to feed this event to the widget, but
// it just wasn't ready, give it another try
this.eventsToFeed.delete(ev) ||
// Skip marker timeline check for events with relations to unknown parent because these
// events are not added to the timeline here and will be ignored otherwise:
// https://github.com/matrix-org/matrix-js-sdk/blob/d3dfcd924201d71b434af3d77343b5229b6ed75e/src/models/room.ts#L2207-L2213
this.relatesToUnknown(ev) ||
// Skip marker timeline check for rooms where membership is
// 'invite', otherwise the membership event from the invitation room
// will advance the marker and new state events will not be
// forwarded to the widget.
this.isFromInvite(ev) ||
// Check whether this event would be before or after our "read up to" marker. If it's
// before, or we can't decide, then we assume the widget will have already seen the event.
// If the event is after, or we don't have a marker for the room, then the marker will advance and we'll
// send it through.
// This approach of "read up to" prevents widgets receiving decryption spam from startup or
// receiving ancient events from backfill and such.
this.advanceReadUpToMarker(ev)
) {
// If the event is still being decrypted, remember that we want to
// feed it to the widget (even if not strictly in the order given by
// the timeline) and get back to it later
if (ev.isBeingDecrypted() || ev.isDecryptionFailure()) {
this.eventsToFeed.add(ev);
} else {
const raw = ev.getEffectiveEvent();
this.messaging.feedEvent(raw as IRoomEvent).catch((err) => {
log.warn('Failed to feed widget event:', err);
});
}
}
}
/**
* Stops the widget messaging and cleans up resources.
*/
stopMessaging() {
if (this.messaging) {
this.messaging.stop(); // Example if a stop method exists
this.messaging.removeAllListeners(); // Remove listeners attached by SmallWidget
this.messaging = null;
}
this.client.off(ClientEvent.Event, this.onEvent);
this.client.off(MatrixEventEvent.Decrypted, this.onEventDecrypted);
this.client.off(RoomStateEvent.Events, this.onStateUpdate);
this.client.off(ClientEvent.ToDeviceEvent, this.onToDeviceEvent);
}
}
/**
* Creates the data object for the widget.
* @param client - The MatrixClient instance.
* @param roomId - The ID of the room.
* @param currentData - Existing widget data.
* @param overwriteData - Data to merge or overwrite.
* @returns The final widget data object.
*/
export const getWidgetData = (
client: MatrixClient,
roomId: string,
currentData: object,
overwriteData: object
): IWidgetData => {
// Example: Determine E2EE based on room state if needed
const perParticipantE2EE = true; // Default or based on logic
// const roomEncryption = client.getRoom(roomId)?.currentState.getStateEvents(EventType.RoomEncryption, "");
// if (roomEncryption) perParticipantE2EE = true; // Simplified example
return {
...currentData,
...overwriteData,
perParticipantE2EE,
};
};
/**
* Creates a virtual widget definition (IApp).
* @param client - MatrixClient instance.
* @param id - Widget ID.
* @param creatorUserId - User ID of the creator.
* @param name - Widget display name.
* @param type - Widget type (e.g., 'm.call').
* @param url - Widget URL.
* @param waitForIframeLoad - Whether to wait for iframe load signal.
* @param data - Widget data.
* @param roomId - Room ID.
* @returns The IApp widget definition.
*/
export const createVirtualWidget = (
client: MatrixClient,
id: string,
creatorUserId: string,
name: string,
type: string,
url: URL,
waitForIframeLoad: boolean,
data: IWidgetData,
roomId: string
): IApp => ({
client,
id,
creatorUserId,
name,
type,
url: url.toString(), // Store URL as string in the definition
waitForIframeLoad,
data,
roomId,
// Add other required fields from IWidget if necessary
sender: creatorUserId, // Example: Assuming sender is the creator
});
-560
View File
@@ -1,560 +0,0 @@
/* eslint-disable no-param-reassign */
/* eslint-disable no-continue */
/* eslint-disable class-methods-use-this */
/*
* Copyright 2024 New Vector Ltd.
* Copyright 2020-2023 The Matrix.org Foundation C.I.C.
*
* SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
* Please see LICENSE files in the repository root for full details.
*/
import {
type Capability,
EventDirection,
type ISendDelayedEventDetails,
type ISendEventDetails,
type IReadEventRelationsResult,
type IRoomEvent,
MatrixCapabilities,
type Widget,
WidgetDriver,
WidgetEventCapability,
WidgetKind,
type IWidgetApiErrorResponseDataDetails,
type ISearchUserDirectoryResult,
type IGetMediaConfigResult,
UpdateDelayedEventAction,
OpenIDRequestState,
SimpleObservable,
IOpenIDUpdate,
} from 'matrix-widget-api';
import {
EventType,
type IContent,
MatrixError,
type MatrixEvent,
Direction,
type SendDelayedEventResponse,
type StateEvents,
type TimelineEvents,
MatrixClient,
} from '$types/matrix-sdk';
export class SmallWidgetDriver extends WidgetDriver {
private allowedCapabilities: Set<Capability>;
private readonly mxClient: MatrixClient; // Store the client instance
public constructor(
mx: MatrixClient,
allowedCapabilities: Capability[],
private forWidget: Widget,
private forWidgetKind: WidgetKind,
virtual: boolean, // Assuming 'virtual' might be needed later, kept for consistency
private inRoomId?: string
) {
super();
this.mxClient = mx; // Store the passed instance
this.allowedCapabilities = new Set([
...allowedCapabilities,
MatrixCapabilities.Screenshots,
// Add other base capabilities as needed, e.g., ElementWidgetCapabilities.RequiresClient
]);
// --- Capabilities specific to Element Call (or similar trusted widgets) ---
// This is a trusted Element Call widget that we control (adjust if not Element Call)
this.allowedCapabilities.add(MatrixCapabilities.AlwaysOnScreen);
this.allowedCapabilities.add(MatrixCapabilities.MSC3846TurnServers);
this.allowedCapabilities.add(MatrixCapabilities.MSC4157SendDelayedEvent);
this.allowedCapabilities.add(MatrixCapabilities.MSC4157UpdateDelayedEvent);
// Capability to access the room timeline (MSC2762)
this.allowedCapabilities.add(`org.matrix.msc2762.timeline:${inRoomId}`);
// Capability to read room state (MSC2762)
this.allowedCapabilities.add(`org.matrix.msc2762.state:${inRoomId}`);
this.allowedCapabilities.add(
WidgetEventCapability.forStateEvent(EventDirection.Receive, EventType.RoomMember).raw
);
this.allowedCapabilities.add(
WidgetEventCapability.forStateEvent(EventDirection.Receive, 'org.matrix.msc3401.call').raw
);
this.allowedCapabilities.add(
WidgetEventCapability.forStateEvent(EventDirection.Receive, EventType.RoomEncryption).raw
);
const clientUserId = this.mxClient.getSafeUserId();
// For the legacy membership type
this.allowedCapabilities.add(
WidgetEventCapability.forStateEvent(
EventDirection.Send,
'org.matrix.msc3401.call.member',
clientUserId
).raw
);
const clientDeviceId = this.mxClient.getDeviceId();
if (clientDeviceId !== null) {
// For the session membership type compliant with MSC4143
this.allowedCapabilities.add(
WidgetEventCapability.forStateEvent(
EventDirection.Send,
'org.matrix.msc3401.call.member',
`_${clientUserId}_${clientDeviceId}`
).raw
);
// Version with no leading underscore, for room versions whose auth rules allow it
this.allowedCapabilities.add(
WidgetEventCapability.forStateEvent(
EventDirection.Send,
'org.matrix.msc3401.call.member',
`${clientUserId}_${clientDeviceId}`
).raw
);
}
this.allowedCapabilities.add(
WidgetEventCapability.forStateEvent(EventDirection.Receive, 'org.matrix.msc3401.call.member')
.raw
);
// for determining auth rules specific to the room version
this.allowedCapabilities.add(
WidgetEventCapability.forStateEvent(EventDirection.Receive, EventType.RoomCreate).raw
);
const sendRecvRoomEvents = [
'io.element.call.encryption_keys',
'org.matrix.rageshake_request',
EventType.Reaction,
EventType.RoomRedaction,
'io.element.call.reaction',
];
// eslint-disable-next-line no-restricted-syntax
for (const eventType of sendRecvRoomEvents) {
this.allowedCapabilities.add(
WidgetEventCapability.forRoomEvent(EventDirection.Send, eventType).raw
);
this.allowedCapabilities.add(
WidgetEventCapability.forRoomEvent(EventDirection.Receive, eventType).raw
);
}
const sendRecvToDevice = [
EventType.CallInvite,
EventType.CallCandidates,
EventType.CallAnswer,
EventType.CallHangup,
EventType.CallReject,
EventType.CallSelectAnswer,
EventType.CallNegotiate,
EventType.CallSDPStreamMetadataChanged,
EventType.CallSDPStreamMetadataChangedPrefix,
EventType.CallReplaces,
EventType.CallEncryptionKeysPrefix,
];
// eslint-disable-next-line no-restricted-syntax
for (const eventType of sendRecvToDevice) {
this.allowedCapabilities.add(
WidgetEventCapability.forToDeviceEvent(EventDirection.Send, eventType).raw
);
this.allowedCapabilities.add(
WidgetEventCapability.forToDeviceEvent(EventDirection.Receive, eventType).raw
);
}
}
public async validateCapabilities(requested: Set<Capability>): Promise<Set<Capability>> {
// Stubbed under the assumption voice calls will be valid thru element-call
return requested;
}
public async sendEvent<K extends keyof StateEvents>(
eventType: K,
content: StateEvents[K],
stateKey: string | null,
targetRoomId: string | null
): Promise<ISendEventDetails>;
public async sendEvent<K extends keyof TimelineEvents>(
eventType: K,
content: TimelineEvents[K],
stateKey: null,
targetRoomId: string | null
): Promise<ISendEventDetails>;
public async sendEvent(
eventType: string,
content: IContent,
stateKey: string | null = null,
targetRoomId: string | null = null
): Promise<ISendEventDetails> {
const client = this.mxClient;
const roomId = targetRoomId || this.inRoomId;
if (!client || !roomId) throw new Error('Not in a room or not attached to a client');
let r: { event_id: string } | null;
if (stateKey !== null) {
// state event
r = await client.sendStateEvent(
roomId,
eventType as keyof StateEvents,
content as StateEvents[keyof StateEvents],
stateKey
);
} else if (eventType === EventType.RoomRedaction) {
// special case: extract the `redacts` property and call redact
r = await client.redactEvent(roomId, content.redacts);
} else {
// message event
r = await client.sendEvent(
roomId,
eventType as keyof TimelineEvents,
content as TimelineEvents[keyof TimelineEvents]
);
}
return { roomId, eventId: r.event_id };
}
/**
* @experimental Part of MSC4140 & MSC4157
* @see {@link WidgetDriver#sendDelayedEvent}
*/
public async sendDelayedEvent<K extends keyof StateEvents>(
delay: number | null,
parentDelayId: string | null,
eventType: K,
content: StateEvents[K],
stateKey: string | null,
targetRoomId: string | null
): Promise<ISendDelayedEventDetails>;
/**
* @experimental Part of MSC4140 & MSC4157
*/
public async sendDelayedEvent<K extends keyof TimelineEvents>(
delay: number | null,
parentDelayId: string | null,
eventType: K,
content: TimelineEvents[K],
stateKey: null,
targetRoomId: string | null
): Promise<ISendDelayedEventDetails>;
public async sendDelayedEvent(
delay: number | null,
parentDelayId: string | null,
eventType: string,
content: IContent,
stateKey: string | null = null,
targetRoomId: string | null = null
): Promise<ISendDelayedEventDetails> {
const client = this.mxClient;
const roomId = targetRoomId || this.inRoomId;
if (!client || !roomId) throw new Error('Not in a room or not attached to a client');
let delayOpts;
if (delay !== null) {
delayOpts = {
delay,
...(parentDelayId !== null && { parent_delay_id: parentDelayId }),
};
} else if (parentDelayId !== null) {
delayOpts = {
parent_delay_id: parentDelayId,
};
} else {
throw new Error('Must provide at least one of delay or parentDelayId');
}
let r: SendDelayedEventResponse | null;
if (stateKey !== null) {
// state event
r = await client._unstable_sendDelayedStateEvent(
roomId,
delayOpts,
eventType as keyof StateEvents,
content as StateEvents[keyof StateEvents],
stateKey
);
} else {
// message event
r = await client._unstable_sendDelayedEvent(
roomId,
delayOpts,
null,
eventType as keyof TimelineEvents,
content as TimelineEvents[keyof TimelineEvents]
);
}
return {
roomId,
delayId: r.delay_id,
};
}
/**
* @experimental Part of MSC4140 & MSC4157
* ClientWidgetApi dispatches three specific actions; the base WidgetDriver.updateDelayedEvent
* is never called. Implementing all three prevents 'Failed to override function' errors and
* the resulting 'Connection lost' in Element Call's MembershipManager.
*/
public async cancelScheduledDelayedEvent(delayId: string): Promise<void> {
const client = this.mxClient;
if (!client) throw new Error('Not in a room or not attached to a client');
await client._unstable_updateDelayedEvent(delayId, UpdateDelayedEventAction.Cancel);
}
public async restartScheduledDelayedEvent(delayId: string): Promise<void> {
const client = this.mxClient;
if (!client) throw new Error('Not in a room or not attached to a client');
await client._unstable_updateDelayedEvent(delayId, UpdateDelayedEventAction.Restart);
}
public async sendScheduledDelayedEvent(delayId: string): Promise<void> {
const client = this.mxClient;
if (!client) throw new Error('Not in a room or not attached to a client');
await client._unstable_updateDelayedEvent(delayId, UpdateDelayedEventAction.Send);
}
/**
* Implements {@link WidgetDriver#sendToDevice}
*/
public async sendToDevice(
eventType: string,
encrypted: boolean,
contentMap: Record<string, Record<string, object>>
): Promise<void> {
const client = this.mxClient;
if (encrypted) {
const crypto = client.getCrypto();
if (!crypto) throw new Error('E2EE not enabled');
// attempt to re-batch these up into a single request
const invertedContentMap: Record<string, { userId: string; deviceId: string }[]> = {};
// eslint-disable-next-line no-restricted-syntax
for (const userId of Object.keys(contentMap)) {
const userContentMap = contentMap[userId];
// eslint-disable-next-line no-restricted-syntax
for (const deviceId of Object.keys(userContentMap)) {
const content = userContentMap[deviceId];
const stringifiedContent = JSON.stringify(content);
invertedContentMap[stringifiedContent] = invertedContentMap[stringifiedContent] || [];
invertedContentMap[stringifiedContent].push({ userId, deviceId });
}
}
await Promise.all(
Object.entries(invertedContentMap).map(async ([stringifiedContent, recipients]) => {
const batch = await crypto.encryptToDeviceMessages(
eventType,
recipients,
JSON.parse(stringifiedContent)
);
await client.queueToDevice(batch);
})
);
} else {
await client.queueToDevice({
eventType,
batch: Object.entries(contentMap).flatMap(([userId, userContentMap]) =>
Object.entries(userContentMap).map(([deviceId, content]) => ({
userId,
deviceId,
payload: content,
}))
),
});
}
}
/**
* Reads all events of the given type, and optionally `msgtype` (if applicable/defined),
* the user has access to. The widget API will have already verified that the widget is
* capable of receiving the events. Less events than the limit are allowed to be returned,
* but not more.
* @param roomId The ID of the room to look within.
* @param eventType The event type to be read.
* @param msgtype The msgtype of the events to be read, if applicable/defined.
* @param stateKey The state key of the events to be read, if applicable/defined.
* @param limit The maximum number of events to retrieve. Will be zero to denote "as many as
* possible".
* @param since When null, retrieves the number of events specified by the "limit" parameter.
* Otherwise, the event ID at which only subsequent events will be returned, as many as specified
* in "limit".
* @returns {Promise<IRoomEvent[]>} Resolves to the room events, or an empty array.
*/
public async readRoomTimeline(
roomId: string,
eventType: string,
msgtype: string | undefined,
stateKey: string | undefined,
limit: number,
since: string | undefined
): Promise<IRoomEvent[]> {
limit = limit > 0 ? Math.min(limit, Number.MAX_SAFE_INTEGER) : Number.MAX_SAFE_INTEGER; // relatively arbitrary
const room = this.mxClient.getRoom(roomId);
if (room === null) return [];
const results: MatrixEvent[] = [];
const events = room.getLiveTimeline().getEvents(); // timelines are most recent last
for (let i = events.length - 1; i >= 0; i -= 1) {
const ev = events[i];
if (results.length >= limit) break;
if (since !== undefined && ev.getId() === since) break;
if (ev.getType() !== eventType || ev.isState()) continue;
if (eventType === EventType.RoomMessage && msgtype && msgtype !== ev.getContent().msgtype)
continue;
if (ev.getStateKey() !== undefined && stateKey !== undefined && ev.getStateKey() !== stateKey)
continue;
results.push(ev);
}
return results.map((e) => e.getEffectiveEvent() as IRoomEvent);
}
public async askOpenID(observer: SimpleObservable<IOpenIDUpdate>): Promise<void> {
return observer.update({
state: OpenIDRequestState.Allowed,
token: await this.mxClient.getOpenIdToken(),
});
}
/**
* Reads the current values of all matching room state entries.
* @param roomId The ID of the room.
* @param eventType The event type of the entries to be read.
* @param stateKey The state key of the entry to be read. If undefined,
* all room state entries with a matching event type should be returned.
* @returns {Promise<IRoomEvent[]>} Resolves to the events representing the
* current values of the room state entries.
*/
public async readRoomState(
roomId: string,
eventType: string,
stateKey: string | undefined
): Promise<IRoomEvent[]> {
const room = this.mxClient.getRoom(roomId);
if (room === null) return [];
const state = room.getLiveTimeline().getState(Direction.Forward);
if (state === undefined) return [];
if (stateKey === undefined)
return state.getStateEvents(eventType).map((e) => e.getEffectiveEvent() as IRoomEvent);
const event = state.getStateEvents(eventType, stateKey);
return event === null ? [] : [event.getEffectiveEvent() as IRoomEvent];
}
/*
public async navigate(uri: string): Promise<void> {
navigateToPermalink(uri);
}
*/
public async readEventRelations(
eventId: string,
roomId?: string,
relationType?: string,
eventType?: string,
from?: string,
to?: string,
limit?: number,
direction?: 'f' | 'b'
): Promise<IReadEventRelationsResult> {
const client = this.mxClient;
const dir = direction as Direction;
roomId = roomId ?? this.inRoomId ?? undefined;
if (typeof roomId !== 'string') {
throw new Error('Error while reading the current room');
}
const { events, nextBatch, prevBatch } = await client.relations(
roomId,
eventId,
relationType ?? null,
eventType ?? null,
{ from, to, limit, dir }
);
return {
chunk: events.map((e) => e.getEffectiveEvent() as IRoomEvent),
nextBatch: nextBatch ?? undefined,
prevBatch: prevBatch ?? undefined,
};
}
public async searchUserDirectory(
searchTerm: string,
limit?: number
): Promise<ISearchUserDirectoryResult> {
const client = this.mxClient;
const { limited, results } = await client.searchUserDirectory({ term: searchTerm, limit });
return {
limited,
results: results.map((r) => ({
userId: r.user_id,
displayName: r.display_name,
avatarUrl: r.avatar_url,
})),
};
}
public async getMediaConfig(): Promise<IGetMediaConfigResult> {
const client = this.mxClient;
return client.getMediaConfig();
}
public async uploadFile(file: XMLHttpRequestBodyInit): Promise<{ contentUri: string }> {
const client = this.mxClient;
const uploadResult = await client.uploadContent(file);
return { contentUri: uploadResult.content_uri };
}
/**
* Download a file from the media repository on the homeserver.
*
* @param contentUri - the MXC URI of the file to download
* @returns an object with: file - response contents as Blob
*/
/*
public async downloadFile(contentUri: string): Promise<{ file: XMLHttpRequestBodyInit }> {
const client = this.mxClient;
const media = mediaFromMxc(contentUri, client);
const response = await media.downloadSource();
const blob = await response.blob();
return { file: blob };
}
*/
/**
* Gets the IDs of all joined or invited rooms currently known to the
* client.
* @returns The room IDs.
*/
public getKnownRooms(): string[] {
return this.mxClient.getVisibleRooms().map((r) => r.roomId);
}
/**
* Expresses a {@link MatrixError} as a JSON payload
* for use by Widget API error responses.
* @param error The error to handle.
* @returns The error expressed as a JSON payload,
* or undefined if it is not a {@link MatrixError}.
*/
public processError(error: unknown): IWidgetApiErrorResponseDataDetails | undefined {
return error instanceof MatrixError
? { matrix_api_error: error.asWidgetApiErrorData() }
: undefined;
}
}
+20
View File
@@ -0,0 +1,20 @@
import { style } from '@vanilla-extract/css';
import { config, toRem } from 'folds';
export const CallViewContent = style({
padding: config.space.S400,
paddingRight: 0,
minHeight: '100%',
});
export const ControlCard = style({
padding: config.space.S300,
});
export const ControlDivider = style({
height: toRem(24),
});
export const CallMemberCard = style({
padding: config.space.S300,
});
@@ -54,7 +54,9 @@ function CreateRoomModal({ state }: CreateRoomModalProps) {
}}
>
<Box grow="Yes">
<Text size="H4">New {type === CreateRoomType.VoiceRoom && 'Voice '}Room</Text>
<Text size="H4">
{type === CreateRoomType.VoiceRoom ? 'New Voice Room' : 'New Chat Room'}
</Text>
</Box>
<Box shrink="No">
<IconButton size="300" radii="300" onClick={closeDialog}>
+12 -1
View File
@@ -33,6 +33,7 @@ import { useOpenCreateRoomModal } from '$state/hooks/createRoomModal';
import { useOpenCreateSpaceModal } from '$state/hooks/createSpaceModal';
import { CreateRoomType } from '$components/create-room/types';
import { AddExistingModal } from '$features/add-existing';
import { BetaNoticeBadge } from '$components/BetaNoticeBadge';
import { useDraggableItem } from './DnD';
import * as styleCss from './style.css';
import * as css from './SpaceItem.css';
@@ -283,7 +284,17 @@ function AddRoomButton({ item }: { item: HierarchyItem }) {
fill="None"
onClick={() => handleCreateRoom(CreateRoomType.TextRoom)}
>
<Text size="T300">New Room</Text>
<Text size="T300">Chat Room</Text>
</MenuItem>
<MenuItem
size="300"
radii="300"
variant="Primary"
fill="None"
onClick={() => handleCreateRoom(CreateRoomType.VoiceRoom)}
after={<BetaNoticeBadge />}
>
<Text size="T300">Voice Room</Text>
</MenuItem>
<MenuItem
size="300"
@@ -214,7 +214,12 @@ export function SearchResultGroup({
src={getRoomAvatarUrl(mx, room, 96, useAuthentication)}
alt={room.name}
renderFallback={() => (
<RoomIcon size="50" joinRule={room.getJoinRule() ?? JoinRule.Restricted} filled />
<RoomIcon
size="50"
roomType={room.getType()}
joinRule={room.getJoinRule() ?? JoinRule.Restricted}
filled
/>
)}
/>
</Avatar>
@@ -1,21 +0,0 @@
import { style } from '@vanilla-extract/css';
import { config } from 'folds';
export const Actions = style({
padding: config.space.S200,
});
export const RoomButtonWrap = style({
minWidth: 0,
});
export const RoomButton = style({
width: '100%',
minWidth: 0,
padding: `0 ${config.space.S200}`,
});
export const RoomName = style({
flexGrow: 1,
minWidth: 0,
});
@@ -1,128 +0,0 @@
import {
Box,
Chip,
Icon,
IconButton,
Icons,
Line,
Spinner,
Text,
Tooltip,
TooltipProvider,
color,
} from 'folds';
import { useCallState } from '$pages/client/call/CallProvider';
import { useRoomNavigate } from '$hooks/useRoomNavigate';
import * as css from './RoomCallNavStatus.css';
export function CallNavStatus() {
const {
activeCallRoomId,
isActiveCallReady,
isAudioEnabled,
isVideoEnabled,
toggleAudio,
toggleVideo,
hangUp,
} = useCallState();
const { navigateRoom } = useRoomNavigate();
const hasActiveCall = Boolean(activeCallRoomId);
const isConnected = hasActiveCall && isActiveCallReady;
const handleGoToCallRoom = () => {
if (activeCallRoomId) {
navigateRoom(activeCallRoomId);
}
};
return (
<Box direction="Column" shrink="No">
<Line variant="Surface" size="300" />
<Box className={css.Actions} direction="Row" alignItems="Center" gap="100">
<Box className={css.RoomButtonWrap} grow="Yes">
{hasActiveCall && (
<TooltipProvider
position="Top"
offset={4}
tooltip={
<Tooltip>
<Text>Go to Room</Text>
</Tooltip>
}
>
{(triggerRef) => (
<Chip
size="500"
fill="Soft"
as="button"
onClick={handleGoToCallRoom}
ref={triggerRef}
className={css.RoomButton}
>
{isConnected ? (
<Icon size="300" src={Icons.VolumeHigh} style={{ color: color.Success.Main }} />
) : (
<Spinner size="300" variant="Secondary" />
)}
<Text
as="span"
size="L400"
style={{ color: isConnected ? color.Success.Main : color.Warning.Main }}
>
{isConnected ? 'Connected' : 'Connecting'}
</Text>
</Chip>
)}
</TooltipProvider>
)}
</Box>
{hasActiveCall && (
<TooltipProvider
position="Top"
offset={4}
tooltip={
<Tooltip>
<Text>Hang Up</Text>
</Tooltip>
}
>
{(triggerRef) => (
<IconButton fill="None" size="300" ref={triggerRef} onClick={hangUp}>
<Icon src={Icons.PhoneDown} />
</IconButton>
)}
</TooltipProvider>
)}
<TooltipProvider
position="Top"
offset={4}
tooltip={
<Tooltip>
<Text>{!isAudioEnabled ? 'Unmute' : 'Mute'}</Text>
</Tooltip>
}
>
{(triggerRef) => (
<IconButton fill="None" size="300" ref={triggerRef} onClick={toggleAudio}>
<Icon src={!isAudioEnabled ? Icons.MicMute : Icons.Mic} />
</IconButton>
)}
</TooltipProvider>
<TooltipProvider
position="Top"
offset={4}
tooltip={
<Tooltip>
<Text>{!isVideoEnabled ? 'Video On' : 'Video Off'}</Text>
</Tooltip>
}
>
{(triggerRef) => (
<IconButton fill="None" size="300" ref={triggerRef} onClick={toggleVideo}>
<Icon src={!isVideoEnabled ? Icons.VideoCameraMute : Icons.VideoCamera} />
</IconButton>
)}
</TooltipProvider>
</Box>
</Box>
);
}
+70 -60
View File
@@ -1,5 +1,5 @@
import { MouseEventHandler, forwardRef, useState, MouseEvent, useEffect } from 'react';
import { EventType, Room, RoomEvent as RoomEventEnum } from '$types/matrix-sdk';
import { Room, RoomEvent as RoomEventEnum } from '$types/matrix-sdk';
import {
Avatar,
Box,
@@ -21,6 +21,7 @@ import {
} from 'folds';
import { useFocusWithin, useHover } from 'react-aria';
import FocusTrap from 'focus-trap-react';
import { useAtom, useAtomValue } from 'jotai';
import { useNavigate } from 'react-router-dom';
import { NavButton, NavItem, NavItemContent, NavItemOptions } from '$components/nav';
import { UnreadBadge, UnreadBadgeCenter } from '$components/unread-badge';
@@ -54,13 +55,17 @@ import { RoomNotificationModeSwitcher } from '$components/RoomNotificationSwitch
import { useRoomCreators } from '$hooks/useRoomCreators';
import { useRoomPermissions } from '$hooks/useRoomPermissions';
import { InviteUserPrompt } from '$components/invite-user-prompt';
import { useCallState } from '$pages/client/call/CallProvider';
import { useCallMembers } from '$hooks/useCallMemberships';
import { useRoomNavigate } from '$hooks/useRoomNavigate';
import { ScreenSize, useScreenSizeContext } from '$hooks/useScreenSize';
import { useRoomName } from '$hooks/useRoomMeta';
import { useAtomValue } from 'jotai';
import { nicknamesAtom } from '$state/nicknames';
import { useRoomNavigate } from '$hooks/useRoomNavigate';
// Upstream Call Hooks
import { useCallMembers, useCallSession } from '$hooks/useCall';
import { useCallEmbed, useCallStart } from '$hooks/useCallEmbed';
import { callChatAtom } from '$state/callEmbed';
import { useCallPreferencesAtom } from '$state/hooks/callPreferences';
import { CallControlState } from '$plugins/call/CallControlState';
import { RoomNavUser } from './RoomNavUser';
/**
@@ -90,6 +95,7 @@ type RoomNavItemMenuProps = {
requestClose: () => void;
notificationMode?: RoomNotificationMode;
};
const RoomNavItemMenu = forwardRef<HTMLDivElement, RoomNavItemMenuProps>(
({ room, requestClose, notificationMode }, ref) => {
const mx = useMatrixClient();
@@ -250,6 +256,7 @@ type RoomNavItemProps = {
showAvatar?: boolean;
direct?: boolean;
};
export function RoomNavItem({
room,
selected,
@@ -264,41 +271,35 @@ export function RoomNavItem({
const { hoverProps } = useHover({ onHoverChange: setHover });
const { focusWithinProps } = useFocusWithin({ onFocusWithinChange: setHover });
const [menuAnchor, setMenuAnchor] = useState<RectCords>();
const unread = useRoomUnread(room.roomId, roomToUnreadAtom);
const hasRoomUnread = useRoomHasUnread(room);
const typingMember = useRoomTypingMember(room.roomId).filter(
(receipt) => receipt.userId !== mx.getUserId()
);
const {
isActiveCallReady,
activeCallRoomId,
setActiveCallRoomId,
setViewedCallRoomId,
isChatOpen,
toggleChat,
hangUp,
} = useCallState();
const isActiveCall = isActiveCallReady && activeCallRoomId === room.roomId;
const callMemberships = useCallMembers(mx, room.roomId);
const powerLevels = usePowerLevels(room);
const creators = useRoomCreators(room);
// Name Resolution
const nicknames = useAtomValue(nicknamesAtom);
const dmUserId = direct ? room.getAvatarFallbackMember()?.userId : undefined;
const matrixRoomName = useRoomName(room);
const roomName = (dmUserId && nicknames[dmUserId]) || matrixRoomName;
const permissions = useRoomPermissions(creators, powerLevels);
const canJoinCall = permissions.event(EventType.GroupCallMemberPrefix, mx.getSafeUserId());
// Navigation & Context
const { navigateRoom } = useRoomNavigate();
const navigate = useNavigate();
const screenSize = useScreenSizeContext();
const isMobile = screenSize === ScreenSize.Mobile;
// Call Hooks (Merged from upstream)
const callSession = useCallSession(room);
const callMembers = useCallMembers(room, callSession);
const startCall = useCallStart(direct);
const callEmbed = useCallEmbed();
const callPref = useAtomValue(useCallPreferencesAtom());
const [isChatOpen, setChatOpen] = useAtom(callChatAtom);
const isActiveCall = callEmbed?.roomId === room.roomId;
const handleContextMenu: MouseEventHandler<HTMLElement> = (evt) => {
evt.preventDefault();
setMenuAnchor({
@@ -316,16 +317,17 @@ export function RoomNavItem({
const handleNavItemClick: MouseEventHandler<HTMLElement> = (evt) => {
if (room.isCallRoom()) {
if (!isMobile) {
if (!isActiveCall && canJoinCall) {
hangUp();
setActiveCallRoomId(room.roomId);
if (!isActiveCall && !callEmbed) {
startCall(
room,
new CallControlState(callPref.microphone, callPref.video, callPref.sound)
);
} else {
navigateRoom(room.roomId);
}
} else {
evt.stopPropagation();
if (isChatOpen) toggleChat();
setViewedCallRoomId(room.roomId);
if (isChatOpen) setChatOpen(false);
navigateRoom(room.roomId);
}
} else {
@@ -335,8 +337,7 @@ export function RoomNavItem({
const handleChatButtonClick = (evt: MouseEvent<HTMLButtonElement>) => {
evt.stopPropagation();
toggleChat();
setViewedCallRoomId(room.roomId);
setChatOpen(!isChatOpen);
navigate(linkPath);
};
@@ -345,13 +346,14 @@ export function RoomNavItem({
if (unread) {
unreadCount = unread.highlight > 0 ? unread.highlight : unread.total;
}
const ariaLabel = [
roomName,
room.isCallRoom()
? [
'Call Room',
isActiveCall && 'Currently in Call',
callMemberships.length && `${callMemberships.length} in Call`,
callMembers.length && `${callMembers.length} in Call`,
]
: 'Text Room',
unread?.total && `${unread.total} Messages`,
@@ -438,11 +440,45 @@ export function RoomNavItem({
aria-label={notificationMode}
/>
)}
{room.isCallRoom() && callMembers.length > 0 && !optionsVisible && (
<Badge variant="Critical" fill="Solid" size="400">
<Text as="span" size="L400" truncate>
{callMembers.length} Live
</Text>
</Badge>
)}
</Box>
</NavItemContent>
</NavButton>
{optionsVisible && (
<NavItemOptions>
{room.isCallRoom() && (
<TooltipProvider
position="Bottom"
offset={4}
tooltip={
<Tooltip>
<Text>{isChatOpen ? 'Hide Chat' : 'Show Chat'}</Text>
</Tooltip>
}
>
{(triggerRef) => (
<IconButton
ref={triggerRef}
data-testid="chat-button"
onClick={handleChatButtonClick}
aria-pressed={isChatOpen && selected}
aria-label="Open Chat"
variant="Background"
fill="None"
size="300"
radii="300"
>
<Icon size="50" src={Icons.Message} filled={isChatOpen} />
</IconButton>
)}
</TooltipProvider>
)}
<PopOut
id={`menu-${room.roomId}`}
aria-expanded={!!menuAnchor}
@@ -471,33 +507,6 @@ export function RoomNavItem({
</FocusTrap>
}
>
{room.isCallRoom() && (
<TooltipProvider
position="Bottom"
offset={4}
tooltip={
<Tooltip>
<Text>{isChatOpen ? 'Hide Chat' : 'Show Chat'}</Text>
</Tooltip>
}
>
{(triggerRef) => (
<IconButton
ref={triggerRef}
data-testid="chat-button"
onClick={handleChatButtonClick}
aria-pressed={isChatOpen && selected}
aria-label="Open Chat"
variant="Background"
fill="None"
size="300"
radii="300"
>
<Icon size="50" src={Icons.Message} />
</IconButton>
)}
</TooltipProvider>
)}
<IconButton
onClick={handleOpenMenu}
aria-pressed={!!menuAnchor}
@@ -514,9 +523,10 @@ export function RoomNavItem({
</NavItemOptions>
)}
</NavItem>
{room.isCallRoom() && (
<Box direction="Column" style={{ paddingLeft: config.space.S200 }}>
{callMemberships.map((callMembership) => (
{callMembers.map((callMembership) => (
<RoomNavUser
key={callMembership.membershipID}
room={room}
+7 -4
View File
@@ -1,29 +1,32 @@
import { Avatar, Box, Icon, Icons, Text } from 'folds';
import { MouseEventHandler } from 'react';
import { useAtomValue } from 'jotai';
import { Room, CallMembership } from '$types/matrix-sdk';
import { NavButton, NavItem, NavItemContent } from '$components/nav';
import { UserAvatar } from '$components/user-avatar';
import { useMatrixClient } from '$hooks/useMatrixClient';
import { useCallState } from '$pages/client/call/CallProvider';
import { getMxIdLocalPart } from '$utils/matrix';
import { getMemberAvatarMxc, getMemberDisplayName } from '$utils/room';
import { useMediaAuthentication } from '$hooks/useMediaAuthentication';
import { useOpenUserRoomProfile } from '$state/hooks/userRoomProfile';
import { useSpaceOptionally } from '$hooks/useSpace';
import { useAtomValue } from 'jotai';
import { nicknamesAtom } from '$state/nicknames';
import { useCallEmbed } from '$hooks/useCallEmbed';
type RoomNavUserProps = {
room: Room;
callMembership: CallMembership;
};
export function RoomNavUser({ room, callMembership }: RoomNavUserProps) {
const mx = useMatrixClient();
const useAuthentication = useMediaAuthentication();
const openProfile = useOpenUserRoomProfile();
const space = useSpaceOptionally();
const { isActiveCallReady, activeCallRoomId } = useCallState();
const isActiveCall = isActiveCallReady && activeCallRoomId === room.roomId;
const callEmbed = useCallEmbed();
const isActiveCall = callEmbed?.roomId === room.roomId;
const userId = callMembership.sender ?? '';
const avatarMxcUrl = getMemberAvatarMxc(room, userId);
const avatarUrl = avatarMxcUrl
@@ -67,6 +67,7 @@ type RoomSettingsProps = {
initialPage?: RoomSettingsPage;
requestClose: () => void;
};
export function RoomSettings({ initialPage, requestClose }: RoomSettingsProps) {
const room = useRoom();
const mx = useMatrixClient();
@@ -118,6 +119,7 @@ export function RoomSettings({ initialPage, requestClose }: RoomSettingsProps) {
renderFallback={() => (
<RoomIcon
size="50"
roomType={room.getType()}
joinRule={joinRuleContent?.join_rule ?? JoinRule.Invite}
filled
/>
+56
View File
@@ -0,0 +1,56 @@
import { useSetAtom } from 'jotai';
import { useParams } from 'react-router-dom';
import { Box, Text, TooltipProvider, Tooltip, Icon, Icons, IconButton, toRem } from 'folds';
import { Page, PageHeader } from '../../components/page';
import { callChatAtom } from '../../state/callEmbed';
import { RoomView } from './RoomView';
import { ScreenSize, useScreenSizeContext } from '../../hooks/useScreenSize';
export function CallChatView() {
const { eventId } = useParams();
const setChat = useSetAtom(callChatAtom);
const screenSize = useScreenSizeContext();
const handleClose = () => setChat(false);
return (
<Page
style={{
width: screenSize === ScreenSize.Desktop ? toRem(456) : '100%',
flexShrink: 0,
flexGrow: 0,
}}
>
<PageHeader>
<Box grow="Yes" alignItems="Center" gap="200">
<Box grow="Yes">
<Text size="H5" truncate>
Chat
</Text>
</Box>
<Box shrink="No" alignItems="Center">
<TooltipProvider
position="Bottom"
align="End"
offset={4}
tooltip={
<Tooltip>
<Text>Close</Text>
</Tooltip>
}
>
{(triggerRef) => (
<IconButton ref={triggerRef} variant="Surface" onClick={handleClose}>
<Icon src={Icons.Cross} />
</IconButton>
)}
</TooltipProvider>
</Box>
</Box>
</PageHeader>
<Box grow="Yes" direction="Column">
<RoomView eventId={eventId} />
</Box>
</Page>
);
}
+30 -11
View File
@@ -12,11 +12,13 @@ import { markAsRead } from '$utils/notifications';
import { useMatrixClient } from '$hooks/useMatrixClient';
import { useRoomMembers } from '$hooks/useRoomMembers';
import { CallView } from '$features/call/CallView';
import { useCallState } from '$pages/client/call/CallProvider';
import { WidgetsDrawer } from '$features/widgets/WidgetsDrawer';
import { useAtomValue } from 'jotai';
import { callChatAtom } from '$state/callEmbed';
import { RoomViewHeader } from './RoomViewHeader';
import { MembersDrawer } from './MembersDrawer';
import { RoomView } from './RoomView';
import { CallChatView } from './CallChatView';
export function Room() {
const { eventId } = useParams();
@@ -29,7 +31,7 @@ export function Room() {
const screenSize = useScreenSizeContext();
const powerLevels = usePowerLevels(room);
const members = useRoomMembers(mx, room.roomId);
const { isChatOpen } = useCallState();
const chat = useAtomValue(callChatAtom);
useKeyDown(
window,
@@ -43,20 +45,37 @@ export function Room() {
)
);
const callView = room.isCallRoom();
return (
<PowerLevelsContextProvider value={powerLevels}>
<Box grow="Yes">
<Box grow="Yes" direction="Column">
<RoomViewHeader />
<Box grow="Yes">
<CallView room={room} />
{room.isCallRoom() && screenSize === ScreenSize.Desktop && isChatOpen && (
{callView && (screenSize === ScreenSize.Desktop || !chat) && (
<Box grow="Yes" direction="Column">
<RoomViewHeader callView />
<Box grow="Yes">
<CallView />
</Box>
</Box>
)}
{!callView && (
<Box grow="Yes" direction="Column">
<RoomViewHeader />
<Box grow="Yes">
<RoomView eventId={eventId} />
</Box>
</Box>
)}
{callView && chat && (
<>
{screenSize === ScreenSize.Desktop && (
<Line variant="Background" direction="Vertical" size="300" />
)}
{(!room.isCallRoom() || isChatOpen) && <RoomView room={room} eventId={eventId} />}
</Box>
</Box>
{screenSize === ScreenSize.Desktop && isDrawer && (
<CallChatView />
</>
)}
{!callView && screenSize === ScreenSize.Desktop && isDrawer && (
<>
<Line variant="Background" direction="Vertical" size="300" />
<MembersDrawer key={room.roomId} room={room} members={members} />
+47
View File
@@ -32,6 +32,7 @@ import { HTMLReactParserOptions } from 'html-react-parser';
import classNames from 'classnames';
import { ReactEditor } from 'slate-react';
import { Editor } from 'slate';
import { SessionMembershipData } from 'matrix-js-sdk/lib/matrixrtc/CallMembership';
import to from 'await-to-js';
import { useAtomValue, useSetAtom } from 'jotai';
import {
@@ -1822,6 +1823,52 @@ export function RoomTimeline({
</Event>
);
},
[StateEvent.GroupCallMemberPrefix]: (mEventId, mEvent, item) => {
const highlighted = focusItem?.index === item && focusItem.highlight;
const senderId = mEvent.getSender() ?? '';
const senderName = getMemberDisplayName(room, senderId) || getMxIdLocalPart(senderId);
const callJoined = mEvent.getContent<SessionMembershipData>().application;
const timeJSX = (
<Time
ts={mEvent.getTs()}
compact={messageLayout === MessageLayout.Compact}
hour24Clock={hour24Clock}
dateFormatString={dateFormatString}
/>
);
return (
<Event
key={mEvent.getId()}
data-message-item={item}
data-message-id={mEventId}
room={room}
mEvent={mEvent}
highlight={highlighted}
messageSpacing={messageSpacing}
canDelete={canRedact || mEvent.getSender() === mx.getUserId()}
hideReadReceipts={hideReads}
onReplyClick={handleReplyClick}
showDeveloperTools={showDeveloperTools}
>
<EventContent
messageLayout={messageLayout}
time={timeJSX}
iconSrc={callJoined ? Icons.Phone : Icons.PhoneDown}
content={
<Box grow="Yes" direction="Column">
<Text size="T300" priority="300">
<b>{senderName}</b>
{callJoined ? ' joined the call' : ' ended the call'}
</Text>
</Box>
}
/>
</Event>
);
},
},
(mEventId, mEvent, item) => {
if (!showHiddenEvents) return null;
+4 -2
View File
@@ -2,7 +2,7 @@ import { useCallback, useRef, useState } from 'react';
import { useAtomValue } from 'jotai';
import { Transforms } from 'slate';
import { Box, Text, config, toRem } from 'folds';
import { EventType, Room } from '$types/matrix-sdk';
import { EventType } from '$types/matrix-sdk';
import { ReactEditor } from 'slate-react';
import { isKeyHotkey } from 'is-hotkey';
import { useStateEvent } from '$hooks/useStateEvent';
@@ -26,6 +26,7 @@ import { RoomSettingsPage } from '$state/roomSettings';
import { GlobalModalManager } from '$components/message/modals/GlobalModalManager';
import { useDelayedEventsSupport } from '$hooks/useDelayedEventsSupport';
import { delayedEventsSupportedAtom } from '$state/scheduledMessages';
import { useRoom } from '$hooks/useRoom';
import { RoomViewFollowing, RoomViewFollowingPlaceholder } from './RoomViewFollowing';
import { RoomInput } from './RoomInput';
import { RoomTombstone } from './RoomTombstone';
@@ -65,13 +66,14 @@ const shouldFocusMessageField = (evt: KeyboardEvent): boolean => {
return true;
};
export function RoomView({ room, eventId }: { room: Room; eventId?: string }) {
export function RoomView({ eventId }: { eventId?: string }) {
const roomInputRef = useRef<HTMLDivElement>(null);
const roomViewRef = useRef<HTMLDivElement>(null);
const [hideReads] = useSetting(settingsAtom, 'hideReads');
const screenSize = useScreenSizeContext();
const room = useRoom();
const { roomId } = room;
const editor = useEditor();
+22 -15
View File
@@ -1,5 +1,6 @@
import { MouseEventHandler, forwardRef, useCallback, useEffect, useState } from 'react';
import FocusTrap from 'focus-trap-react';
import { useAtom, useAtomValue } from 'jotai';
import {
Box,
Avatar,
@@ -70,14 +71,14 @@ import { useRoomNavigate } from '$hooks/useRoomNavigate';
import { useRoomCreators } from '$hooks/useRoomCreators';
import { useRoomPermissions } from '$hooks/useRoomPermissions';
import { InviteUserPrompt } from '$components/invite-user-prompt';
import { useCallState } from '$pages/client/call/CallProvider';
import { ContainerColor } from '$styles/ContainerColor.css';
import { useRoomWidgets } from '$hooks/useRoomWidgets';
import { AccountDataEvent } from '$types/matrix/accountData';
import { DirectInvitePrompt } from '$components/direct-invite-prompt';
import { AsyncStatus, useAsyncCallback } from '$hooks/useAsyncCallback';
import { useAtomValue } from 'jotai';
import { mDirectAtom } from '$state/mDirectList';
import { callChatAtom } from '$state/callEmbed';
import { RoomSettingsPage } from '$state/roomSettings';
import { JumpToTime } from './jump-to-time';
import { RoomPinMenu } from './room-pin-menu';
import * as css from './RoomViewHeader.css';
@@ -329,7 +330,7 @@ const RoomMenu = forwardRef<HTMLDivElement, RoomMenuProps>(({ room, requestClose
});
RoomMenu.displayName = 'RoomMenu';
export function RoomViewHeader() {
export function RoomViewHeader({ callView }: { callView?: boolean }) {
const navigate = useNavigate();
const mx = useMatrixClient();
const useAuthentication = useMediaAuthentication();
@@ -340,7 +341,8 @@ export function RoomViewHeader() {
const [pinMenuAnchor, setPinMenuAnchor] = useState<RectCords>();
const direct = useIsDirectRoom();
const { isChatOpen, toggleChat } = useCallState();
const [chat, setChat] = useAtom(callChatAtom);
const encryptionEvent = useStateEvent(room, StateEvent.RoomEncryption);
const encryptedRoom = !!encryptionEvent;
const avatarMxc = useRoomAvatar(room, direct);
@@ -433,6 +435,16 @@ export function RoomViewHeader() {
});
};
const openSettings = useOpenRoomSettings();
const parentSpace = useSpaceOptionally();
const handleMemberToggle = () => {
if (callView) {
openSettings(room.roomId, parentSpace?.roomId, RoomSettingsPage.MembersPage);
return;
}
setPeopleDrawer(!peopleDrawer);
};
return (
<PageHeader
className={ContainerColor({ variant: 'Surface' })}
@@ -508,7 +520,7 @@ export function RoomViewHeader() {
</Box>
<Box shrink="No">
{(!room.isCallRoom() || isChatOpen) && (
{(!room.isCallRoom() || chat) && (
<>
{!encryptedRoom && (
<TooltipProvider
@@ -630,7 +642,6 @@ export function RoomViewHeader() {
)}
</TooltipProvider>
)}
{screenSize === ScreenSize.Desktop && (
<TooltipProvider
position="Bottom"
@@ -642,24 +653,20 @@ export function RoomViewHeader() {
}
>
{(triggerRef) => (
<IconButton
fill="None"
ref={triggerRef}
onClick={() => setPeopleDrawer((drawer) => !drawer)}
>
<IconButton fill="None" ref={triggerRef} onClick={handleMemberToggle}>
<Icon size="400" src={Icons.User} filled={peopleDrawer} />
</IconButton>
)}
</TooltipProvider>
)}
{room.isCallRoom() && !direct && (
{callView && (
<TooltipProvider
position="Bottom"
offset={4}
tooltip={
<Tooltip>
<Text>{isChatOpen ? 'Hide Chat' : 'Show Chat'}</Text>
<Text>{chat ? 'Hide Chat' : 'Show Chat'}</Text>
</Tooltip>
}
>
@@ -668,10 +675,10 @@ export function RoomViewHeader() {
fill="None"
ref={triggerRef}
onClick={() => {
toggleChat();
setChat(!chat);
}}
>
<Icon size="400" src={Icons.Message} filled={isChatOpen} />
<Icon size="400" src={Icons.Message} filled={chat} />
</IconButton>
)}
</TooltipProvider>
+55
View File
@@ -0,0 +1,55 @@
import { Room } from 'matrix-js-sdk';
import {
MatrixRTCSession,
MatrixRTCSessionEvent,
} from 'matrix-js-sdk/lib/matrixrtc/MatrixRTCSession';
import { CallMembership } from 'matrix-js-sdk/lib/matrixrtc/CallMembership';
import { useEffect, useState } from 'react';
import { MatrixRTCSessionManagerEvents } from 'matrix-js-sdk/lib/matrixrtc/MatrixRTCSessionManager';
import { useMatrixClient } from './useMatrixClient';
export const useCallSession = (room: Room): MatrixRTCSession => {
const mx = useMatrixClient();
const [session, setSession] = useState(mx.matrixRTC.getRoomSession(room));
useEffect(() => {
const start = (roomId: string) => {
if (roomId !== room.roomId) return;
setSession(mx.matrixRTC.getRoomSession(room));
};
const end = (roomId: string) => {
if (roomId !== room.roomId) return;
setSession(mx.matrixRTC.getRoomSession(room));
};
mx.matrixRTC.on(MatrixRTCSessionManagerEvents.SessionStarted, start);
mx.matrixRTC.on(MatrixRTCSessionManagerEvents.SessionEnded, end);
return () => {
mx.matrixRTC.off(MatrixRTCSessionManagerEvents.SessionStarted, start);
mx.matrixRTC.off(MatrixRTCSessionManagerEvents.SessionEnded, end);
};
}, [mx, room]);
return session;
};
export const useCallMembers = (room: Room, session: MatrixRTCSession): CallMembership[] => {
const [memberships, setMemberships] = useState<CallMembership[]>(
MatrixRTCSession.sessionMembershipsForRoom(room, session.sessionDescription)
);
useEffect(() => {
const updateMemberships = () => {
setMemberships(MatrixRTCSession.sessionMembershipsForRoom(room, session.sessionDescription));
};
updateMemberships();
session.on(MatrixRTCSessionEvent.MembershipsChanged, updateMemberships);
return () => {
session.removeListener(MatrixRTCSessionEvent.MembershipsChanged, updateMemberships);
};
}, [session, room]);
return memberships;
};
+130
View File
@@ -0,0 +1,130 @@
import { createContext, RefObject, useCallback, useContext, useEffect, useState } from 'react';
import { MatrixRTCSession } from 'matrix-js-sdk/lib/matrixrtc/MatrixRTCSession';
import { MatrixClient, Room } from 'matrix-js-sdk';
import { useSetAtom } from 'jotai';
import {
CallEmbed,
ElementCallThemeKind,
ElementWidgetActions,
useClientWidgetApiEvent,
} from '../plugins/call';
import { useMatrixClient } from './useMatrixClient';
import { ThemeKind, useTheme } from './useTheme';
import { callEmbedAtom } from '../state/callEmbed';
import { useResizeObserver } from './useResizeObserver';
import { CallControlState } from '../plugins/call/CallControlState';
const CallEmbedContext = createContext<CallEmbed | undefined>(undefined);
export const CallEmbedContextProvider = CallEmbedContext.Provider;
export const useCallEmbed = (): CallEmbed | undefined => {
const callEmbed = useContext(CallEmbedContext);
return callEmbed;
};
const CallEmbedRefContext = createContext<RefObject<HTMLDivElement> | undefined>(undefined);
export const CallEmbedRefContextProvider = CallEmbedRefContext.Provider;
export const useCallEmbedRef = (): RefObject<HTMLDivElement> => {
const ref = useContext(CallEmbedRefContext);
if (!ref) {
throw new Error('CallEmbedRef is not provided!');
}
return ref;
};
export const createCallEmbed = (
mx: MatrixClient,
room: Room,
dm: boolean,
themeKind: ElementCallThemeKind,
container: HTMLElement,
controlState?: CallControlState
): CallEmbed => {
const rtcSession = mx.matrixRTC.getRoomSession(room);
const ongoing =
MatrixRTCSession.sessionMembershipsForRoom(room, rtcSession.sessionDescription).length > 0;
const intent = CallEmbed.getIntent(dm, ongoing);
const widget = CallEmbed.getWidget(mx, room, intent, themeKind);
const embed = new CallEmbed(mx, room, widget, container, controlState);
return embed;
};
export const useCallStart = (dm = false) => {
const mx = useMatrixClient();
const theme = useTheme();
const setCallEmbed = useSetAtom(callEmbedAtom);
const callEmbedRef = useCallEmbedRef();
const startCall = useCallback(
(room: Room, controlState?: CallControlState) => {
const container = callEmbedRef.current;
if (!container) {
throw new Error('Failed to start call, No embed container element found!');
}
const callEmbed = createCallEmbed(mx, room, dm, theme.kind, container, controlState);
setCallEmbed(callEmbed);
},
[mx, dm, theme, setCallEmbed, callEmbedRef]
);
return startCall;
};
export const useCallJoined = (embed?: CallEmbed): boolean => {
const [joined, setJoined] = useState(embed?.joined ?? false);
useClientWidgetApiEvent(
embed?.call,
ElementWidgetActions.JoinCall,
useCallback(() => {
setJoined(true);
}, [])
);
useEffect(() => {
if (!embed) {
setJoined(false);
}
}, [embed]);
return joined;
};
export const useCallHangupEvent = (embed: CallEmbed, callback: () => void) => {
useClientWidgetApiEvent(embed.call, ElementWidgetActions.HangupCall, callback);
};
export const useCallThemeSync = (embed: CallEmbed) => {
const theme = useTheme();
useEffect(() => {
const name: ElementCallThemeKind = theme.kind === ThemeKind.Dark ? 'dark' : 'light';
embed.setTheme(name);
}, [theme.kind, embed]);
};
export const useSyncCallEmbedPlacement = (containerViewRef: RefObject<HTMLDivElement>): void => {
const callEmbedRef = useCallEmbedRef();
const syncCallEmbedPlacement = useCallback(() => {
const embedEl = callEmbedRef.current;
const container = containerViewRef.current;
if (!embedEl || !container) return;
embedEl.style.top = `${container.offsetTop}px`;
embedEl.style.left = `${container.offsetLeft}px`;
embedEl.style.width = `${container.clientWidth}px`;
embedEl.style.height = `${container.clientHeight}px`;
}, [callEmbedRef, containerViewRef]);
useResizeObserver(
syncCallEmbedPlacement,
useCallback(() => containerViewRef.current, [containerViewRef])
);
};
-34
View File
@@ -1,34 +0,0 @@
import {
MatrixClient,
MatrixRTCSession,
MatrixRTCSessionEvent,
CallMembership,
} from '$types/matrix-sdk';
import { useEffect, useState } from 'react';
export const useCallMembers = (mx: MatrixClient, roomId: string): CallMembership[] => {
const [memberships, setMemberships] = useState<CallMembership[]>([]);
const room = mx.getRoom(roomId);
useEffect(() => {
if (!room) {
setMemberships([]);
return undefined;
}
const mxr = mx.matrixRTC.getRoomSession(room);
const updateMemberships = () => {
if (!room.isCallRoom()) return;
setMemberships(MatrixRTCSession.callMembershipsForRoom(room));
};
updateMemberships();
mxr.on(MatrixRTCSessionEvent.MembershipsChanged, updateMemberships);
return () => {
mxr.removeListener(MatrixRTCSessionEvent.MembershipsChanged, updateMemberships);
};
}, [mx, room, roomId]);
return memberships;
};
+10
View File
@@ -0,0 +1,10 @@
import { useCallEmbed } from '../hooks/useCallEmbed';
import { CallStatus } from '../features/call-status';
export function CallStatusRenderer() {
const callEmbed = useCallEmbed();
if (!callEmbed) return null;
return <CallStatus callEmbed={callEmbed} />;
}
+6 -7
View File
@@ -24,6 +24,7 @@ import { getLocalStorageItem } from '$state/utils/atomWithLocalStorage';
import { NotificationJumper } from '$hooks/useNotificationJumper';
import { SearchModalRenderer } from '$features/search';
import { GlobalKeyboardShortcuts } from '$components/GlobalKeyboardShortcuts';
import { CallEmbedProvider } from '$components/CallEmbedProvider';
import { AuthLayout, Login, Register, ResetPassword } from './auth';
import {
DIRECT_PATH,
@@ -71,9 +72,8 @@ import { AuthRouteThemeManager, UnAuthRouteThemeManager } from './ThemeManager';
import { ClientRoomsNotificationPreferences } from './client/ClientRoomsNotificationPreferences';
import { HomeCreateRoom } from './client/home/CreateRoom';
import { Create } from './client/create';
import { CallProvider } from './client/call/CallProvider';
import { PersistentCallContainer } from './client/call/PersistentCallContainer';
import { ToRoomEvent } from './client/ToRoomEvent';
import { CallStatusRenderer } from './CallStatusRenderer';
/**
* Returns true if there is at least one stored session.
@@ -152,7 +152,7 @@ export const createRouter = (clientConfig: ClientConfig, screenSize: ScreenSize)
<ClientBindAtoms>
<ClientNonUIFeatures>
<NotificationJumper />
<CallProvider>
<CallEmbedProvider>
<ClientLayout
nav={
<MobileFriendlyClientNav>
@@ -160,11 +160,10 @@ export const createRouter = (clientConfig: ClientConfig, screenSize: ScreenSize)
</MobileFriendlyClientNav>
}
>
<PersistentCallContainer>
<Outlet />
</PersistentCallContainer>
<Outlet />
</ClientLayout>
</CallProvider>
<CallStatusRenderer />
</CallEmbedProvider>
<SearchModalRenderer />
<UserRoomProfileRenderer />
<CreateRoomModalRenderer />
@@ -8,6 +8,8 @@ import { makeNavToActivePathAtom } from '$state/navToActivePath';
import { NavToActivePathProvider } from '$state/hooks/navToActivePath';
import { makeOpenedSidebarFolderAtom } from '$state/openedSidebarFolder';
import { OpenedSidebarFolderProvider } from '$state/hooks/openedSidebarFolder';
import { makeCallPreferencesAtom } from '$state/callPreferences';
import { CallPreferencesProvider } from '$state/hooks/callPreferences';
type ClientInitStorageAtomProps = {
children: ReactNode;
@@ -24,12 +26,16 @@ export function ClientInitStorageAtom({ children }: ClientInitStorageAtomProps)
const openedSidebarFolderAtom = useMemo(() => makeOpenedSidebarFolderAtom(userId), [userId]);
const callPreferencesAtom = useMemo(() => makeCallPreferencesAtom(userId), [userId]);
return (
<ClosedNavCategoriesProvider value={closedNavCategoriesAtom}>
<ClosedLobbyCategoriesProvider value={closedLobbyCategoriesAtom}>
<NavToActivePathProvider value={navToActivePathAtom}>
<OpenedSidebarFolderProvider value={openedSidebarFolderAtom}>
{children}
<CallPreferencesProvider value={callPreferencesAtom}>
{children}
</CallPreferencesProvider>
</OpenedSidebarFolderProvider>
</NavToActivePathProvider>
</ClosedLobbyCategoriesProvider>
-343
View File
@@ -1,343 +0,0 @@
import {
createContext,
useState,
useContext,
useMemo,
useCallback,
ReactNode,
useEffect,
} from 'react';
import { WidgetApiToWidgetAction, ClientWidgetApi, IWidgetApiRequestData } from 'matrix-widget-api';
import { useParams } from 'react-router-dom';
import { SmallWidget } from '$features/call/SmallWidget';
interface MediaStatePayload {
data?: {
audio_enabled?: boolean;
video_enabled?: boolean;
};
}
const WIDGET_MEDIA_STATE_UPDATE_ACTION = 'io.element.device_mute';
const WIDGET_HANGUP_ACTION = 'im.vector.hangup';
const WIDGET_ON_SCREEN_ACTION = 'set_always_on_screen';
const WIDGET_JOIN_ACTION = 'io.element.join';
const WIDGET_TILE_UPDATE = 'io.element.tile_layout';
interface CallContextState {
activeCallRoomId: string | null;
setActiveCallRoomId: (roomId: string | null) => void;
viewedCallRoomId: string | null;
setViewedCallRoomId: (roomId: string | null) => void;
hangUp: () => void;
activeClientWidgetApi: ClientWidgetApi | null;
activeClientWidget: SmallWidget | null;
registerActiveClientWidgetApi: (
roomId: string | null,
clientWidgetApi: ClientWidgetApi | null,
clientWidget: SmallWidget | null,
activeClientIframeRef: HTMLIFrameElement | null
) => void;
sendWidgetAction: <T extends IWidgetApiRequestData = IWidgetApiRequestData>(
action: WidgetApiToWidgetAction | string,
data: T
) => Promise<void>;
isAudioEnabled: boolean;
isVideoEnabled: boolean;
isChatOpen: boolean;
isActiveCallReady: boolean;
resetActiveCallReady: () => void;
toggleAudio: () => Promise<void>;
toggleVideo: () => Promise<void>;
toggleChat: () => Promise<void>;
}
const CallContext = createContext<CallContextState | undefined>(undefined);
interface CallProviderProps {
children: ReactNode;
}
const DEFAULT_AUDIO_ENABLED = true;
const DEFAULT_VIDEO_ENABLED = false;
const DEFAULT_CHAT_OPENED = false;
export function CallProvider({ children }: CallProviderProps) {
const [activeCallRoomId, setActiveCallRoomIdState] = useState<string | null>(null);
const [viewedCallRoomId, setViewedCallRoomIdState] = useState<string | null>(null);
const [activeClientWidgetApi, setActiveClientWidgetApiState] = useState<ClientWidgetApi | null>(
null
);
const [activeClientWidget, setActiveClientWidget] = useState<SmallWidget | null>(null);
const [activeClientWidgetApiRoomId, setActiveClientWidgetApiRoomId] = useState<string | null>(
null
);
const [activeClientWidgetIframeRef, setActiveClientWidgetIframeRef] =
useState<HTMLIFrameElement | null>(null);
const [isAudioEnabled, setIsAudioEnabledState] = useState<boolean>(DEFAULT_AUDIO_ENABLED);
const [isVideoEnabled, setIsVideoEnabledState] = useState<boolean>(DEFAULT_VIDEO_ENABLED);
const [isChatOpen, setIsChatOpenState] = useState<boolean>(DEFAULT_CHAT_OPENED);
const [isActiveCallReady, setIsActiveCallReady] = useState<boolean>(false);
const { roomIdOrAlias: viewedRoomId } = useParams<{ roomIdOrAlias: string }>();
const setActiveCallRoomId = useCallback((roomId: string | null) => {
setActiveCallRoomIdState(roomId);
}, []);
const setViewedCallRoomId = useCallback(
(roomId: string | null) => {
setViewedCallRoomIdState(roomId);
},
[setViewedCallRoomIdState]
);
const setActiveClientWidgetApi = useCallback(
(
clientWidgetApi: ClientWidgetApi | null,
clientWidget: SmallWidget | null,
roomId: string | null,
clientWidgetIframeRef: HTMLIFrameElement | null
) => {
setActiveClientWidgetApiState(clientWidgetApi);
setActiveClientWidget(clientWidget);
setActiveClientWidgetApiRoomId(roomId);
setActiveClientWidgetIframeRef(clientWidgetIframeRef);
},
[]
);
const registerActiveClientWidgetApi = useCallback(
(
roomId: string | null,
clientWidgetApi: ClientWidgetApi | null,
clientWidget: SmallWidget | null,
clientWidgetIframeRef: HTMLIFrameElement | null
) => {
if (roomId && clientWidgetApi) {
setActiveClientWidgetApi(clientWidgetApi, clientWidget, roomId, clientWidgetIframeRef);
} else if (roomId === activeClientWidgetApiRoomId || roomId === null) {
setActiveClientWidgetApi(null, null, null, null);
}
},
[activeClientWidgetApiRoomId, setActiveClientWidgetApi]
);
const hangUp = useCallback(() => {
setActiveClientWidgetApi(null, null, null, null);
setActiveCallRoomIdState(null);
activeClientWidgetApi?.transport.send(WIDGET_HANGUP_ACTION, {});
setIsActiveCallReady(false);
}, [activeClientWidgetApi?.transport, setActiveClientWidgetApi]);
const sendWidgetAction = useCallback(
async <T extends IWidgetApiRequestData = IWidgetApiRequestData>(
action: WidgetApiToWidgetAction | string,
data: T
): Promise<void> => {
if (!activeClientWidgetApi) {
return Promise.reject(new Error('No active call clientWidgetApi'));
}
if (!activeClientWidgetApiRoomId || activeClientWidgetApiRoomId !== activeCallRoomId) {
return Promise.reject(new Error('Mismatched active call clientWidgetApi'));
}
await activeClientWidgetApi.transport.send(action, data);
return Promise.resolve();
},
[activeClientWidgetApi, activeCallRoomId, activeClientWidgetApiRoomId]
);
const toggleAudio = useCallback(async () => {
const newState = !isAudioEnabled;
setIsAudioEnabledState(newState);
if (isActiveCallReady) {
try {
await sendWidgetAction(WIDGET_MEDIA_STATE_UPDATE_ACTION, {
audio_enabled: newState,
video_enabled: isVideoEnabled,
});
} catch (error) {
setIsAudioEnabledState(!newState);
throw error;
}
}
}, [isAudioEnabled, isVideoEnabled, sendWidgetAction, isActiveCallReady]);
const toggleVideo = useCallback(async () => {
const newState = !isVideoEnabled;
setIsVideoEnabledState(newState);
if (isActiveCallReady) {
try {
await sendWidgetAction(WIDGET_MEDIA_STATE_UPDATE_ACTION, {
audio_enabled: isAudioEnabled,
video_enabled: newState,
});
} catch (error) {
setIsVideoEnabledState(!newState);
throw error;
}
}
}, [isVideoEnabled, isAudioEnabled, sendWidgetAction, isActiveCallReady]);
useEffect(() => {
if (!activeCallRoomId && !viewedCallRoomId) {
return;
}
if (!activeClientWidgetApi) {
return;
}
const handleHangup = (ev: CustomEvent) => {
ev.preventDefault();
if (isActiveCallReady && ev.detail.widgetId === activeClientWidgetApi.widget.id) {
activeClientWidgetApi.transport.reply(ev.detail, {});
}
};
const handleMediaStateUpdate = (ev: CustomEvent<MediaStatePayload>) => {
if (!isActiveCallReady) return;
ev.preventDefault();
const { audio_enabled: audioEnabled, video_enabled: videoEnabled } = ev.detail.data ?? {};
if (typeof audioEnabled === 'boolean' && audioEnabled !== isAudioEnabled) {
setIsAudioEnabledState(audioEnabled);
}
if (typeof videoEnabled === 'boolean' && videoEnabled !== isVideoEnabled) {
setIsVideoEnabledState(videoEnabled);
}
};
const handleOnScreenStateUpdate = (ev: CustomEvent) => {
ev.preventDefault();
activeClientWidgetApi.transport.reply(ev.detail, {});
};
const handleOnTileLayout = (ev: CustomEvent) => {
ev.preventDefault();
activeClientWidgetApi.transport.reply(ev.detail, {});
};
const handleJoin = (ev: CustomEvent) => {
ev.preventDefault();
activeClientWidgetApi.transport.reply(ev.detail, {});
const iframeDoc =
activeClientWidgetIframeRef?.contentWindow?.document ||
activeClientWidgetIframeRef?.contentDocument;
if (iframeDoc) {
const observer = new MutationObserver(() => {
const button = iframeDoc.querySelector('[data-testid="incall_leave"]');
if (button) {
button.addEventListener('click', () => {
hangUp();
});
}
observer.disconnect();
});
observer.observe(iframeDoc, { childList: true, subtree: true });
}
setIsActiveCallReady(true);
};
sendWidgetAction(WIDGET_MEDIA_STATE_UPDATE_ACTION, {
audio_enabled: isAudioEnabled,
video_enabled: isVideoEnabled,
}).catch(() => {
// Widget transport may reject while call/session setup is still in progress.
});
activeClientWidgetApi.on(`action:${WIDGET_HANGUP_ACTION}`, handleHangup);
activeClientWidgetApi.on(`action:${WIDGET_MEDIA_STATE_UPDATE_ACTION}`, handleMediaStateUpdate);
activeClientWidgetApi.on(`action:${WIDGET_TILE_UPDATE}`, handleOnTileLayout);
activeClientWidgetApi.on(`action:${WIDGET_ON_SCREEN_ACTION}`, handleOnScreenStateUpdate);
activeClientWidgetApi.on(`action:${WIDGET_JOIN_ACTION}`, handleJoin);
}, [
activeClientWidgetIframeRef,
activeClientWidgetApi,
activeCallRoomId,
activeClientWidgetApiRoomId,
hangUp,
isChatOpen,
isAudioEnabled,
isVideoEnabled,
isActiveCallReady,
viewedRoomId,
viewedCallRoomId,
setViewedCallRoomId,
activeClientWidget?.iframe?.contentDocument,
activeClientWidget?.iframe?.contentWindow?.document,
sendWidgetAction,
]);
const resetActiveCallReady = useCallback(() => {
setIsActiveCallReady(false);
}, []);
const toggleChat = useCallback(async () => {
const newState = !isChatOpen;
setIsChatOpenState(newState);
}, [isChatOpen]);
const contextValue = useMemo<CallContextState>(
() => ({
activeCallRoomId,
setActiveCallRoomId,
viewedCallRoomId,
setViewedCallRoomId,
hangUp,
activeClientWidgetApi,
registerActiveClientWidgetApi,
activeClientWidget,
sendWidgetAction,
isChatOpen,
isAudioEnabled,
isVideoEnabled,
isActiveCallReady,
resetActiveCallReady,
toggleAudio,
toggleVideo,
toggleChat,
}),
[
activeCallRoomId,
setActiveCallRoomId,
viewedCallRoomId,
setViewedCallRoomId,
hangUp,
activeClientWidgetApi,
registerActiveClientWidgetApi,
activeClientWidget,
sendWidgetAction,
isChatOpen,
isAudioEnabled,
isVideoEnabled,
isActiveCallReady,
resetActiveCallReady,
toggleAudio,
toggleVideo,
toggleChat,
]
);
return <CallContext.Provider value={contextValue}>{children}</CallContext.Provider>;
}
export function useCallState(): CallContextState {
const context = useContext(CallContext);
if (context === undefined) {
throw new Error('useCallState must be used within a CallProvider');
}
return context;
}
@@ -1,266 +0,0 @@
import {
createContext,
MutableRefObject,
ReactNode,
useCallback,
useEffect,
useMemo,
useRef,
} from 'react';
import { ClientWidgetApi } from 'matrix-widget-api';
import { MatrixRTCSession } from 'matrix-js-sdk/lib/matrixrtc/MatrixRTCSession';
import { Box } from 'folds';
import {
createVirtualWidget,
SmallWidget,
getWidgetData,
getWidgetUrl,
getCallIntentParams,
} from '$features/call/SmallWidget';
import { useMatrixClient } from '$hooks/useMatrixClient';
import { useClientConfig } from '$hooks/useClientConfig';
import { ScreenSize, useScreenSizeContext } from '$hooks/useScreenSize';
import { ThemeKind, useTheme } from '$hooks/useTheme';
import { useCallState } from './CallProvider';
interface PersistentCallContainerProps {
children: ReactNode;
}
export const CallRefContext = createContext<MutableRefObject<HTMLIFrameElement | null> | null>(
null
);
export function PersistentCallContainer({ children }: PersistentCallContainerProps) {
const callIframeRef = useRef<HTMLIFrameElement | null>(null);
const callWidgetApiRef = useRef<ClientWidgetApi | null>(null);
const callSmallWidgetRef = useRef<SmallWidget | null>(null);
// After any lobby join, reload EC with join_existing for proper in-call view.
const hasReloadedAfterLobbyRef = useRef(false);
const postLobbyIntentRef = useRef<'join_existing' | null>(null);
const {
activeCallRoomId,
viewedCallRoomId,
isChatOpen,
isActiveCallReady,
registerActiveClientWidgetApi,
activeClientWidget,
resetActiveCallReady,
hangUp,
} = useCallState();
const mx = useMatrixClient();
const clientConfig = useClientConfig();
const screenSize = useScreenSizeContext();
const theme = useTheme();
const isMobile = screenSize === ScreenSize.Mobile;
/* eslint-disable no-param-reassign */
const setupWidget = useCallback(
(
widgetApiRef: MutableRefObject<ClientWidgetApi | null>,
smallWidgetRef: MutableRefObject<SmallWidget | null>,
iframeRef: MutableRefObject<HTMLIFrameElement | null>,
skipLobby: boolean,
themeKind: ThemeKind | null,
intentOverride?: 'join_existing'
) => {
if (mx?.getUserId()) {
if (activeCallRoomId && !isActiveCallReady) {
const roomIdToSet = activeCallRoomId;
const room = mx.getRoom(roomIdToSet);
const { intent: intentParam, callIntentParam } = getCallIntentParams(room);
const effectiveIntent = intentOverride ?? intentParam;
const widgetId = `element-call-${roomIdToSet}-${Date.now()}`;
const newUrl = getWidgetUrl(
mx,
roomIdToSet,
clientConfig.elementCallUrl ?? '',
widgetId,
{
skipLobby: (intentOverride === 'join_existing' ? true : skipLobby).toString(),
returnToLobby: 'true',
perParticipantE2EE: 'true',
theme: themeKind,
intent: effectiveIntent,
callIntent: callIntentParam,
}
);
if (
callSmallWidgetRef.current?.roomId &&
activeClientWidget?.roomId &&
activeClientWidget.roomId === callSmallWidgetRef.current?.roomId
) {
return;
}
if (
iframeRef.current &&
(!iframeRef.current.src || iframeRef.current.src !== newUrl.toString())
) {
iframeRef.current.src = newUrl.toString();
}
const iframeElement = iframeRef.current;
if (!iframeElement) {
return;
}
const userId = mx.getUserId() ?? '';
const app = createVirtualWidget(
mx,
widgetId,
userId,
'Element Call',
'm.call',
newUrl,
false,
getWidgetData(
mx,
roomIdToSet,
{},
{ skipLobby: true, intent: effectiveIntent, callIntent: callIntentParam }
),
roomIdToSet
);
const smallWidget = new SmallWidget(app);
smallWidgetRef.current = smallWidget;
const widgetApiInstance = smallWidget.startMessaging(iframeElement);
widgetApiRef.current = widgetApiInstance;
registerActiveClientWidgetApi(
roomIdToSet,
widgetApiRef.current,
smallWidget,
iframeElement
);
}
}
},
[
mx,
activeCallRoomId,
isActiveCallReady,
clientConfig.elementCallUrl,
activeClientWidget,
registerActiveClientWidgetApi,
]
);
// After any lobby join, poll until EC's call member state event has propagated to the room,
// then reload EC with intent=join_existing + skipLobby=true so it auto-joins the existing
// session and shows the full in-call grid. Hangs up if the session never appears.
// Applies to all room types: DM/group (start_call) and voice rooms (join_existing) both
// hit the same timing issue where the in-call grid is not shown after the first join.
useEffect(() => {
if (!activeCallRoomId) {
hasReloadedAfterLobbyRef.current = false;
return undefined;
}
if (isActiveCallReady && !hasReloadedAfterLobbyRef.current) {
const room = mx?.getRoom(activeCallRoomId);
if (room) {
hasReloadedAfterLobbyRef.current = true;
const POLL_INTERVAL_MS = 200;
const TIMEOUT_MS = 10000;
const startTime = Date.now();
const pollTimer = setInterval(() => {
if (MatrixRTCSession.callMembershipsForRoom(room).length > 0) {
clearInterval(pollTimer);
callSmallWidgetRef.current?.stopMessaging();
callWidgetApiRef.current = null;
callSmallWidgetRef.current = null;
registerActiveClientWidgetApi(activeCallRoomId, null, null, null);
postLobbyIntentRef.current = 'join_existing';
resetActiveCallReady();
} else if (Date.now() - startTime >= TIMEOUT_MS) {
clearInterval(pollTimer);
hangUp();
}
}, POLL_INTERVAL_MS);
return () => clearInterval(pollTimer);
}
}
return undefined;
}, [
isActiveCallReady,
activeCallRoomId,
mx,
registerActiveClientWidgetApi,
resetActiveCallReady,
hangUp,
]);
useEffect(() => {
if (activeCallRoomId) {
const intentOverride = postLobbyIntentRef.current ?? undefined;
postLobbyIntentRef.current = null;
setupWidget(
callWidgetApiRef,
callSmallWidgetRef,
callIframeRef,
true,
theme.kind,
intentOverride
);
}
}, [
theme,
setupWidget,
callWidgetApiRef,
callSmallWidgetRef,
callIframeRef,
registerActiveClientWidgetApi,
activeCallRoomId,
viewedCallRoomId,
isActiveCallReady,
]);
const memoizedIframeRef = useMemo(() => callIframeRef, [callIframeRef]);
return (
<CallRefContext.Provider value={memoizedIframeRef}>
<Box grow="No">
<Box
direction="Column"
style={{
position: 'relative',
zIndex: 0,
display: isMobile && isChatOpen ? 'none' : 'flex',
width: isMobile && isChatOpen ? '0%' : '100%',
height: isMobile && isChatOpen ? '0%' : '100%',
}}
>
<Box
grow="Yes"
style={{
position: 'relative',
}}
>
<iframe
ref={callIframeRef}
style={{
position: 'absolute',
top: 0,
left: 0,
display: 'flex',
width: '100%',
height: '100%',
border: 'none',
}}
title="Persistent Element Call"
sandbox="allow-forms allow-scripts allow-same-origin allow-popups allow-modals allow-downloads"
allow="microphone; camera; display-capture; autoplay; clipboard-write;"
src="about:blank"
/>
</Box>
</Box>
</Box>
{children}
</CallRefContext.Provider>
);
}
-2
View File
@@ -50,7 +50,6 @@ import {
useRoomsNotificationPreferencesContext,
} from '$hooks/useRoomsNotificationPreferences';
import { useDirectCreateSelected } from '$hooks/router/useDirectSelected';
import { CallNavStatus } from '$features/room-nav/RoomCallNavStatus';
import { useDirectRooms } from './useDirectRooms';
type DirectMenuProps = {
@@ -280,7 +279,6 @@ export function Direct() {
</Box>
</PageNavContent>
)}
<CallNavStatus />
</PageNav>
);
}
-2
View File
@@ -61,7 +61,6 @@ import {
import { UseStateProvider } from '$components/UseStateProvider';
import { JoinAddressPrompt } from '$components/join-address-prompt';
import { RoomSearchParams } from '$pages/paths';
import { CallNavStatus } from '$features/room-nav/RoomCallNavStatus';
import { useHomeRooms } from './useHomeRooms';
type HomeMenuProps = {
@@ -359,7 +358,6 @@ export function Home() {
</Box>
</PageNavContent>
)}
<CallNavStatus />
</PageNav>
);
}
+6 -1
View File
@@ -427,7 +427,12 @@ function RoomNotificationsGroupComp({
src={getRoomAvatarUrl(mx, room, 96, useAuthentication)}
alt={room.name}
renderFallback={() => (
<RoomIcon size="50" joinRule={room.getJoinRule() ?? JoinRule.Restricted} filled />
<RoomIcon
size="50"
roomType={room.getType()}
joinRule={room.getJoinRule() ?? JoinRule.Restricted}
filled
/>
)}
/>
</Avatar>
+7 -17
View File
@@ -19,8 +19,9 @@ import {
toRem,
} from 'folds';
import { useVirtualizer } from '@tanstack/react-virtual';
import { JoinRule, Room, RoomJoinRulesEventContent } from '$types/matrix-sdk';
import FocusTrap from 'focus-trap-react';
import { useNavigate } from 'react-router-dom';
import { JoinRule, Room, RoomJoinRulesEventContent } from '$types/matrix-sdk';
import { useMatrixClient } from '$hooks/useMatrixClient';
import { mDirectAtom } from '$state/mDirectList';
import { NavCategory, NavCategoryHeader, NavItem, NavItemContent, NavLink } from '$components/nav';
@@ -67,17 +68,16 @@ import { ContainerColor } from '$styles/ContainerColor.css';
import { AsyncStatus, useAsyncCallback } from '$hooks/useAsyncCallback';
import { BreakWord } from '$styles/Text.css';
import { InviteUserPrompt } from '$components/invite-user-prompt';
import { CallNavStatus } from '$features/room-nav/RoomCallNavStatus';
import { mobileOrTablet } from '$utils/user-agent';
import { useNavigate } from 'react-router-dom';
import { lastVisitedRoomIdAtom } from '$state/room/lastRoom';
import { SwipeableOverlayWrapper } from '$components/SwipeableOverlayWrapper';
import { useCallState } from '$pages/client/call/CallProvider';
import { useCallEmbed } from '$hooks/useCallEmbed';
type SpaceMenuProps = {
room: Room;
requestClose: () => void;
};
const SpaceMenu = forwardRef<HTMLDivElement, SpaceMenuProps>(({ room, requestClose }, ref) => {
const mx = useMatrixClient();
const [hideReads] = useSetting(settingsAtom, 'hideReads');
@@ -379,7 +379,7 @@ export function Space() {
const selectedRoomId = useSelectedRoom();
const lobbySelected = useSpaceLobbySelected(spaceIdOrAlias);
const searchSelected = useSpaceSearchSelected(spaceIdOrAlias);
const { isActiveCallReady, activeCallRoomId } = useCallState();
const callEmbed = useCallEmbed();
const [closedCategories, setClosedCategories] = useAtom(useClosedNavCategoriesAtom());
@@ -404,19 +404,10 @@ export function Space() {
const unread = roomToUnread.get(roomId);
const hasUnread = !!unread && (unread.total > 0 || unread.highlight > 0);
const showRoomAnyway =
hasUnread ||
roomId === selectedRoomId ||
(isActiveCallReady && activeCallRoomId === roomId);
hasUnread || roomId === selectedRoomId || callEmbed?.roomId === roomId;
return !showRoomAnyway;
},
[
space.roomId,
closedCategories,
roomToUnread,
selectedRoomId,
activeCallRoomId,
isActiveCallReady,
]
[space.roomId, closedCategories, roomToUnread, selectedRoomId, callEmbed]
),
useCallback(
(sId) => closedCategories.has(makeNavCategoryId(space.roomId, sId)),
@@ -554,7 +545,6 @@ export function Space() {
</NavCategory>
</Box>
</PageNavContent>
<CallNavStatus />
</SwipeableOverlayWrapper>
</PageNav>
);
+117
View File
@@ -0,0 +1,117 @@
import { ClientWidgetApi } from 'matrix-widget-api';
import EventEmitter from 'events';
import { CallControlState } from './CallControlState';
import { ElementMediaStateDetail, ElementMediaStatePayload, ElementWidgetActions } from './types';
export enum CallControlEvent {
StateUpdate = 'state_update',
}
export class CallControl extends EventEmitter implements CallControlState {
private state: CallControlState;
private call: ClientWidgetApi;
private iframe: HTMLIFrameElement;
constructor(state: CallControlState, call: ClientWidgetApi, iframe: HTMLIFrameElement) {
super();
this.state = state;
this.call = call;
this.iframe = iframe;
}
public getState(): CallControlState {
return this.state;
}
public get microphone(): boolean {
return this.state.microphone;
}
public get video(): boolean {
return this.state.video;
}
public get sound(): boolean {
return this.state.sound;
}
public async applyState() {
await this.setMediaState({
audio_enabled: this.microphone,
video_enabled: this.video,
});
this.setSound(this.sound);
this.emitStateUpdate();
}
private setMediaState(state: ElementMediaStatePayload) {
return this.call.transport.send(ElementWidgetActions.DeviceMute, state);
}
private setSound(sound: boolean): void {
const callDocument = this.iframe.contentDocument ?? this.iframe.contentWindow?.document;
if (callDocument) {
callDocument.querySelectorAll('audio').forEach((el) => {
if (el instanceof HTMLAudioElement) {
// eslint-disable-next-line no-param-reassign
el.muted = !sound;
}
});
}
}
public onMediaState(evt: CustomEvent<ElementMediaStateDetail>) {
const { data } = evt.detail;
if (!data) return;
const state = new CallControlState(
data.audio_enabled ?? this.microphone,
data.video_enabled ?? this.video,
this.sound
);
this.state = state;
this.emitStateUpdate();
if (this.microphone && !this.sound) {
this.toggleSound();
}
}
public toggleMicrophone() {
const payload: ElementMediaStatePayload = {
audio_enabled: !this.microphone,
video_enabled: this.video,
};
return this.setMediaState(payload);
}
public toggleVideo() {
const payload: ElementMediaStatePayload = {
audio_enabled: this.microphone,
video_enabled: !this.video,
};
return this.setMediaState(payload);
}
public toggleSound() {
const sound = !this.sound;
this.setSound(sound);
const state = new CallControlState(this.microphone, this.video, sound);
this.state = state;
this.emitStateUpdate();
if (!this.sound && this.microphone) {
this.toggleMicrophone();
}
}
private emitStateUpdate() {
this.emit(CallControlEvent.StateUpdate);
}
}
+13
View File
@@ -0,0 +1,13 @@
export class CallControlState {
public readonly microphone: boolean;
public readonly video: boolean;
public readonly sound: boolean;
constructor(microphone: boolean, video: boolean, sound: boolean) {
this.microphone = microphone;
this.video = video;
this.sound = sound;
}
}
+358
View File
@@ -0,0 +1,358 @@
import {
ClientEvent,
KnownMembership,
MatrixClient,
MatrixEvent,
MatrixEventEvent,
Room,
RoomStateEvent,
} from 'matrix-js-sdk';
import {
ClientWidgetApi,
IRoomEvent,
IWidget,
Widget,
WidgetApiToWidgetAction,
WidgetDriver,
} from 'matrix-widget-api';
import { CallWidgetDriver } from './CallWidgetDriver';
import { trimTrailingSlash } from '../../utils/common';
import {
ElementCallIntent,
ElementCallThemeKind,
ElementMediaStateDetail,
ElementWidgetActions,
} from './types';
import { CallControl } from './CallControl';
import { CallControlState } from './CallControlState';
export class CallEmbed {
private mx: MatrixClient;
public readonly call: ClientWidgetApi;
public readonly iframe: HTMLIFrameElement;
public readonly room: Room;
public joined = false;
public readonly control: CallControl;
private readonly container: HTMLElement;
private readUpToMap: Record<string, string> = {}; // room ID to event ID
private eventsToFeed = new WeakSet<MatrixEvent>();
private readonly disposables: Array<() => void> = [];
static getIntent(dm: boolean, ongoing: boolean): ElementCallIntent {
if (ongoing) {
return dm ? ElementCallIntent.JoinExistingDM : ElementCallIntent.JoinExisting;
}
return dm ? ElementCallIntent.StartCallDM : ElementCallIntent.StartCall;
}
static getWidget(
mx: MatrixClient,
room: Room,
intent: ElementCallIntent,
themeKind: ElementCallThemeKind
): Widget {
const userId = mx.getSafeUserId();
const deviceId = mx.getDeviceId() ?? '';
const clientOrigin = window.location.origin;
const widgetId = 'call-embed';
const params = new URLSearchParams({
widgetId,
parentUrl: clientOrigin,
baseUrl: mx.baseUrl,
roomId: room.roomId,
userId,
deviceId,
intent,
skipLobby: 'true',
confineToRoom: 'true',
appPrompt: 'false',
perParticipantE2EE: room.hasEncryptionStateEvent().toString(),
lang: 'en-EN',
theme: themeKind,
});
const widgetUrl = new URL(
`${trimTrailingSlash(import.meta.env.BASE_URL)}/public/element-call/index.html`,
window.location.origin
);
widgetUrl.search = params.toString();
const options: IWidget = {
id: widgetId,
creatorUserId: userId,
name: 'Call',
type: 'm.call',
url: widgetUrl.href,
waitForIframeLoad: false,
data: {},
};
const widget: Widget = new Widget(options);
return widget;
}
static getIframe(url: string): HTMLIFrameElement {
const iframe = document.createElement('iframe');
iframe.title = 'Call Embed';
iframe.sandbox =
'allow-forms allow-scripts allow-same-origin allow-popups allow-modals allow-downloads';
iframe.allow = 'microphone; camera; display-capture; autoplay; clipboard-write;';
iframe.src = url;
iframe.style.width = '100%';
iframe.style.height = '100%';
iframe.style.border = 'none';
return iframe;
}
constructor(
mx: MatrixClient,
room: Room,
widget: Widget,
container: HTMLElement,
initialControlState?: CallControlState
) {
const iframe = CallEmbed.getIframe(
widget.getCompleteUrl({ currentUserId: mx.getSafeUserId() })
);
container.append(iframe);
const callWidgetDriver: WidgetDriver = new CallWidgetDriver(mx, room.roomId);
const call: ClientWidgetApi = new ClientWidgetApi(widget, iframe, callWidgetDriver);
this.mx = mx;
this.call = call;
this.room = room;
this.iframe = iframe;
this.container = container;
const controlState = initialControlState ?? new CallControlState(true, false, true);
this.control = new CallControl(controlState, call, iframe);
let initialMediaEvent = true;
this.disposables.push(
this.listenEvent<ElementMediaStateDetail>(ElementWidgetActions.DeviceMute, (evt) => {
if (initialMediaEvent) {
initialMediaEvent = false;
this.control.applyState();
return;
}
this.control.onMediaState(evt);
})
);
this.start();
}
get roomId(): string {
return this.room.roomId;
}
public setTheme(theme: ElementCallThemeKind) {
return this.call.transport.send(WidgetApiToWidgetAction.ThemeChange, {
name: theme,
});
}
public hangup() {
return this.call.transport.send(ElementWidgetActions.HangupCall, {});
}
public listenEvent<T>(type: string, callback: (event: CustomEvent<T>) => void) {
this.call.on(`action:${type}`, callback);
return () => {
this.call.off(`action:${type}`, callback);
};
}
private start() {
// Room widgets get locked to the room they were added in
this.call.setViewedRoomId(this.roomId);
this.disposables.push(
this.listenEvent(ElementWidgetActions.JoinCall, this.onCallJoined.bind(this))
);
// Populate the map of "read up to" events for this widget with the current event in every room.
// This is a bit inefficient, but should be okay. We do this for all rooms in case the widget
// requests timeline capabilities in other rooms down the road. It's just easier to manage here.
this.mx.getRooms().forEach((room) => {
// Timelines are most recent last
const events = room.getLiveTimeline()?.getEvents() || [];
const roomEvent = events[events.length - 1];
if (!roomEvent) return; // force later code to think the room is fresh
this.readUpToMap[room.roomId] = roomEvent.getId()!;
});
// Attach listeners for feeding events - the underlying widget classes handle permissions for us
this.mx.on(ClientEvent.Event, this.onEvent.bind(this));
this.mx.on(MatrixEventEvent.Decrypted, this.onEventDecrypted.bind(this));
this.mx.on(RoomStateEvent.Events, this.onStateUpdate.bind(this));
this.mx.on(ClientEvent.ToDeviceEvent, this.onToDeviceEvent.bind(this));
}
/**
* Stops the widget messaging for if it is started. Skips stopping if it is an active
* widget.
* @param opts
*/
public dispose(): void {
this.disposables.forEach((disposable) => {
disposable();
});
this.call.stop();
this.container.removeChild(this.iframe);
this.mx.off(ClientEvent.Event, this.onEvent.bind(this));
this.mx.off(MatrixEventEvent.Decrypted, this.onEventDecrypted.bind(this));
this.mx.off(RoomStateEvent.Events, this.onStateUpdate.bind(this));
this.mx.off(ClientEvent.ToDeviceEvent, this.onToDeviceEvent.bind(this));
// Clear internal state
this.readUpToMap = {};
this.eventsToFeed = new WeakSet<MatrixEvent>();
}
private onCallJoined(): void {
this.joined = true;
}
private onEvent(ev: MatrixEvent): void {
this.mx.decryptEventIfNeeded(ev);
this.feedEvent(ev);
}
private onEventDecrypted(ev: MatrixEvent): void {
this.feedEvent(ev);
}
private onStateUpdate(ev: MatrixEvent): void {
if (this.call === null) return;
const raw = ev.getEffectiveEvent();
this.call.feedStateUpdate(raw as IRoomEvent).catch((e) => {
console.error('Error sending state update to widget: ', e);
});
}
private async onToDeviceEvent(ev: MatrixEvent): Promise<void> {
await this.mx.decryptEventIfNeeded(ev);
if (ev.isDecryptionFailure()) return;
await this.call?.feedToDevice(ev.getEffectiveEvent() as IRoomEvent, ev.isEncrypted());
}
/**
* Determines whether the event has a relation to an unknown parent.
*/
private relatesToUnknown(ev: MatrixEvent): boolean {
// Replies to unknown events don't count
if (!ev.relationEventId || ev.replyEventId) return false;
const room = this.mx.getRoom(ev.getRoomId());
return room === null || !room.findEventById(ev.relationEventId);
}
/**
* Advances the "read up to" marker for a room to a certain event. No-ops if
* the event is before the marker.
* @returns Whether the "read up to" marker was advanced.
*/
private advanceReadUpToMarker(ev: MatrixEvent): boolean {
const evId = ev.getId();
if (evId === undefined) return false;
const roomId = ev.getRoomId();
if (roomId === undefined) return false;
const room = this.mx.getRoom(roomId);
if (room === null) return false;
const upToEventId = this.readUpToMap[ev.getRoomId()!];
if (!upToEventId) {
// There's no marker yet; start it at this event
this.readUpToMap[roomId] = evId;
return true;
}
// Small optimization for exact match (skip the search)
if (upToEventId === evId) return false;
// Timelines are most recent last, so reverse the order and limit ourselves to 100 events
// to avoid overusing the CPU.
const timeline = room.getLiveTimeline();
const events = [...timeline.getEvents()].reverse().slice(0, 100);
function isRelevantTimelineEvent(timelineEvent: MatrixEvent): boolean {
return timelineEvent.getId() === upToEventId || timelineEvent.getId() === ev.getId();
}
const possibleMarkerEv = events.find(isRelevantTimelineEvent);
if (possibleMarkerEv?.getId() === upToEventId) {
// The event must be somewhere before the "read up to" marker
return false;
}
if (possibleMarkerEv?.getId() === ev.getId()) {
// The event is after the marker; advance it
this.readUpToMap[roomId] = evId;
return true;
}
// We can't say for sure whether the widget has seen the event; let's
// just assume that it has
return false;
}
/**
* Determines whether the event comes from a room that we've been invited to
* (in which case we likely don't have the full timeline).
*/
private isFromInvite(ev: MatrixEvent): boolean {
const room = this.mx.getRoom(ev.getRoomId());
return room?.getMyMembership() === KnownMembership.Invite;
}
private feedEvent(ev: MatrixEvent): void {
if (this.call === null) return;
if (
// If we had decided earlier to feed this event to the widget, but
// it just wasn't ready, give it another try
this.eventsToFeed.delete(ev) ||
// Skip marker timeline check for events with relations to unknown parent because these
// events are not added to the timeline here and will be ignored otherwise:
// https://github.com/matrix-org/matrix-js-sdk/blob/d3dfcd924201d71b434af3d77343b5229b6ed75e/src/models/room.ts#L2207-L2213
this.relatesToUnknown(ev) ||
// Skip marker timeline check for rooms where membership is
// 'invite', otherwise the membership event from the invitation room
// will advance the marker and new state events will not be
// forwarded to the widget.
this.isFromInvite(ev) ||
// Check whether this event would be before or after our "read up to" marker. If it's
// before, or we can't decide, then we assume the widget will have already seen the event.
// If the event is after, or we don't have a marker for the room,
// then the marker will advance and we'll send it through.
// This approach of "read up to" prevents widgets receiving decryption spam from startup or
// receiving ancient events from backfill and such.
this.advanceReadUpToMarker(ev)
) {
// If the event is still being decrypted, remember that we want to
// feed it to the widget (even if not strictly in the order given by
// the timeline) and get back to it later
if (ev.isBeingDecrypted() || ev.isDecryptionFailure()) {
this.eventsToFeed.add(ev);
} else {
const raw = ev.getEffectiveEvent();
this.call.feedEvent(raw as IRoomEvent).catch((e) => {
console.error('Error sending event to widget: ', e);
});
}
}
}
}
+343
View File
@@ -0,0 +1,343 @@
import {
type Capability,
type ISendDelayedEventDetails,
type ISendEventDetails,
type IReadEventRelationsResult,
type IRoomEvent,
WidgetDriver,
type IWidgetApiErrorResponseDataDetails,
type ISearchUserDirectoryResult,
type IGetMediaConfigResult,
type UpdateDelayedEventAction,
OpenIDRequestState,
SimpleObservable,
IOpenIDUpdate,
} from 'matrix-widget-api';
import {
EventType,
type IContent,
MatrixError,
type MatrixEvent,
Direction,
type SendDelayedEventResponse,
type StateEvents,
type TimelineEvents,
MatrixClient,
} from 'matrix-js-sdk';
import { getCallCapabilities } from './utils';
import { downloadMedia, mxcUrlToHttp } from '../../utils/matrix';
export class CallWidgetDriver extends WidgetDriver {
private allowedCapabilities: Set<Capability>;
private readonly mx: MatrixClient;
public constructor(
mx: MatrixClient,
private inRoomId: string
) {
super();
this.mx = mx;
const deviceId = mx.getDeviceId();
if (!deviceId) throw new Error('Failed to initialize CallWidgetDriver! Device ID not found.');
this.allowedCapabilities = getCallCapabilities(inRoomId, mx.getSafeUserId(), deviceId);
}
public async validateCapabilities(requested: Set<Capability>): Promise<Set<Capability>> {
const allow = Array.from(requested).filter((cap) => this.allowedCapabilities.has(cap));
return new Set(allow);
}
public async sendEvent(
eventType: string,
content: IContent,
stateKey: string | null = null,
targetRoomId: string | null = null
): Promise<ISendEventDetails> {
const client = this.mx;
const roomId = targetRoomId || this.inRoomId;
if (!client || !roomId) throw new Error('Not in a room or not attached to a client');
let r: { event_id: string } | null;
if (typeof stateKey === 'string') {
r = await client.sendStateEvent(
roomId,
eventType as keyof StateEvents,
content as StateEvents[keyof StateEvents],
stateKey
);
} else if (eventType === EventType.RoomRedaction) {
// special case: extract the `redacts` property and call redact
r = await client.redactEvent(roomId, content.redacts);
} else {
r = await client.sendEvent(
roomId,
eventType as keyof TimelineEvents,
content as TimelineEvents[keyof TimelineEvents]
);
}
return { roomId, eventId: r.event_id };
}
public async sendDelayedEvent(
delay: number | null,
parentDelayId: string | null,
eventType: string,
content: IContent,
stateKey: string | null = null,
targetRoomId: string | null = null
): Promise<ISendDelayedEventDetails> {
const client = this.mx;
const roomId = targetRoomId || this.inRoomId;
if (!client || !roomId) throw new Error('Not in a room or not attached to a client');
let delayOpts;
if (delay !== null) {
delayOpts = {
delay,
...(parentDelayId !== null && { parent_delay_id: parentDelayId }),
};
} else if (parentDelayId !== null) {
delayOpts = {
parent_delay_id: parentDelayId,
};
} else {
throw new Error('Must provide at least one of delay or parentDelayId');
}
let r: SendDelayedEventResponse | null;
if (stateKey !== null) {
// state event
r = await client._unstable_sendDelayedStateEvent(
roomId,
delayOpts,
eventType as keyof StateEvents,
content as StateEvents[keyof StateEvents],
stateKey
);
} else {
// message event
r = await client._unstable_sendDelayedEvent(
roomId,
delayOpts,
null,
eventType as keyof TimelineEvents,
content as TimelineEvents[keyof TimelineEvents]
);
}
return {
roomId,
delayId: r.delay_id,
};
}
public async updateDelayedEvent(
delayId: string,
action: UpdateDelayedEventAction
): Promise<void> {
const client = this.mx;
if (!client) throw new Error('Not in a room or not attached to a client');
await client._unstable_updateDelayedEvent(delayId, action);
}
public async sendToDevice(
eventType: string,
encrypted: boolean,
contentMap: Record<string, Record<string, object>>
): Promise<void> {
const client = this.mx;
if (encrypted) {
const crypto = client.getCrypto();
if (!crypto) throw new Error('E2EE not enabled');
// attempt to re-batch these up into a single request
const invertedContentMap: Record<string, { userId: string; deviceId: string }[]> = {};
// eslint-disable-next-line no-restricted-syntax
for (const userId of Object.keys(contentMap)) {
const userContentMap = contentMap[userId];
// eslint-disable-next-line no-restricted-syntax
for (const deviceId of Object.keys(userContentMap)) {
const content = userContentMap[deviceId];
const stringifiedContent = JSON.stringify(content);
invertedContentMap[stringifiedContent] = invertedContentMap[stringifiedContent] || [];
invertedContentMap[stringifiedContent].push({ userId, deviceId });
}
}
await Promise.all(
Object.entries(invertedContentMap).map(async ([stringifiedContent, recipients]) => {
const batch = await crypto.encryptToDeviceMessages(
eventType,
recipients,
JSON.parse(stringifiedContent)
);
await client.queueToDevice(batch);
})
);
} else {
await client.queueToDevice({
eventType,
batch: Object.entries(contentMap).flatMap(([userId, userContentMap]) =>
Object.entries(userContentMap).map(([deviceId, content]) => ({
userId,
deviceId,
payload: content,
}))
),
});
}
}
public async readRoomTimeline(
roomId: string,
eventType: string,
msgtype: string | undefined,
stateKey: string | undefined,
limit: number,
since: string | undefined
): Promise<IRoomEvent[]> {
const safeLimit =
limit > 0 ? Math.min(limit, Number.MAX_SAFE_INTEGER) : Number.MAX_SAFE_INTEGER; // relatively arbitrary
const room = this.mx.getRoom(roomId);
if (room === null) return [];
const results: MatrixEvent[] = [];
const events = room.getLiveTimeline().getEvents();
for (let i = events.length - 1; i >= 0; i -= 1) {
const ev = events[i];
if (results.length >= safeLimit) break;
if (since !== undefined && ev.getId() === since) break;
if (
ev.getType() === eventType &&
!ev.isState() &&
(eventType !== EventType.RoomMessage || !msgtype || msgtype === ev.getContent().msgtype) &&
(ev.getStateKey() === undefined || stateKey === undefined || ev.getStateKey() === stateKey)
) {
results.push(ev);
}
}
return results.map((e) => e.getEffectiveEvent() as IRoomEvent);
}
public async askOpenID(observer: SimpleObservable<IOpenIDUpdate>): Promise<void> {
return observer.update({
state: OpenIDRequestState.Allowed,
token: await this.mx.getOpenIdToken(),
});
}
public async readRoomState(
roomId: string,
eventType: string,
stateKey: string | undefined
): Promise<IRoomEvent[]> {
const room = this.mx.getRoom(roomId);
if (room === null) return [];
const state = room.getLiveTimeline().getState(Direction.Forward);
if (state === undefined) return [];
if (stateKey === undefined)
return state.getStateEvents(eventType).map((e) => e.getEffectiveEvent() as IRoomEvent);
const event = state.getStateEvents(eventType, stateKey);
return event === null ? [] : [event.getEffectiveEvent() as IRoomEvent];
}
public async readEventRelations(
eventId: string,
roomId?: string,
relationType?: string,
eventType?: string,
from?: string,
to?: string,
limit?: number,
direction?: 'f' | 'b'
): Promise<IReadEventRelationsResult> {
const client = this.mx;
const dir = direction as Direction;
const targetRoomId = roomId ?? this.inRoomId ?? undefined;
if (typeof targetRoomId !== 'string') {
throw new Error('Error while reading the current room');
}
const { events, nextBatch, prevBatch } = await client.relations(
targetRoomId,
eventId,
relationType ?? null,
eventType ?? null,
{ from, to, limit, dir }
);
return {
chunk: events.map((e) => e.getEffectiveEvent() as IRoomEvent),
nextBatch: nextBatch ?? undefined,
prevBatch: prevBatch ?? undefined,
};
}
public async searchUserDirectory(
searchTerm: string,
limit?: number
): Promise<ISearchUserDirectoryResult> {
const client = this.mx;
const { limited, results } = await client.searchUserDirectory({ term: searchTerm, limit });
return {
limited,
results: results.map((r) => ({
userId: r.user_id,
displayName: r.display_name,
avatarUrl: r.avatar_url,
})),
};
}
public async getMediaConfig(): Promise<IGetMediaConfigResult> {
const client = this.mx;
return client.getMediaConfig();
}
public async uploadFile(file: XMLHttpRequestBodyInit): Promise<{ contentUri: string }> {
const client = this.mx;
const uploadResult = await client.uploadContent(file);
return { contentUri: uploadResult.content_uri };
}
public async downloadFile(contentUri: string): Promise<{ file: XMLHttpRequestBodyInit }> {
const httpUrl = mxcUrlToHttp(this.mx, contentUri, true);
if (!httpUrl) {
throw new Error('Call widget failed to download file! No http url!');
}
const blob = await downloadMedia(httpUrl);
return { file: blob };
}
public getKnownRooms(): string[] {
return this.mx.getVisibleRooms().map((r) => r.roomId);
}
// eslint-disable-next-line class-methods-use-this
public processError(error: unknown): IWidgetApiErrorResponseDataDetails | undefined {
return error instanceof MatrixError
? { matrix_api_error: error.asWidgetApiErrorData() }
: undefined;
}
}
+49
View File
@@ -0,0 +1,49 @@
import {
ClientWidgetApi,
IWidgetApiAcknowledgeResponseData,
IWidgetApiRequestData,
} from 'matrix-widget-api';
import { useCallback, useEffect, useState } from 'react';
import { CallControl, CallControlEvent } from './CallControl';
import { CallControlState } from './CallControlState';
export const useClientWidgetApiEvent = <T>(
api: ClientWidgetApi | undefined,
type: string,
callback: (event: CustomEvent<T>) => void
) => {
useEffect(() => {
api?.on(`action:${type}`, callback);
return () => {
api?.off(`action:${type}`, callback);
};
}, [api, type, callback]);
};
export const useSendClientWidgetApiAction = (api: ClientWidgetApi) => {
const sendWidgetAction = useCallback(
async <T extends IWidgetApiRequestData = IWidgetApiRequestData>(
action: string,
data: T
): Promise<IWidgetApiAcknowledgeResponseData> => api.transport.send(action, data),
[api]
);
return sendWidgetAction;
};
export const useCallControlState = (control: CallControl): CallControlState => {
const [state, setState] = useState(control.getState());
useEffect(() => {
const handleUpdate = () => {
setState(control.getState());
};
control.on(CallControlEvent.StateUpdate, handleUpdate);
return () => {
control.off(CallControlEvent.StateUpdate, handleUpdate);
};
}, [control]);
return state;
};
+3
View File
@@ -0,0 +1,3 @@
export * from './CallEmbed';
export * from './hooks';
export * from './types';
+25
View File
@@ -0,0 +1,25 @@
export enum ElementCallIntent {
StartCall = 'start_call',
JoinExisting = 'join_existing',
StartCallDM = 'start_call_dm',
JoinExistingDM = 'join_existing_dm',
StartCallDMVoice = 'start_call_dm_voice',
JoinExistingDMVoice = 'join_existing_dm_voice',
}
export type ElementCallThemeKind = 'light' | 'dark';
export type ElementMediaStatePayload = {
audio_enabled?: boolean;
video_enabled?: boolean;
};
export type ElementMediaStateDetail = {
data?: ElementMediaStatePayload;
};
export enum ElementWidgetActions {
JoinCall = 'io.element.join',
HangupCall = 'im.vector.hangup',
Close = 'io.element.close',
DeviceMute = 'io.element.device_mute',
}
+118
View File
@@ -0,0 +1,118 @@
import {
type Capability,
EventDirection,
MatrixCapabilities,
WidgetEventCapability,
} from 'matrix-widget-api';
import { EventType } from 'matrix-js-sdk';
export function getCallCapabilities(
roomId: string,
userId: string,
deviceId: string
): Set<Capability> {
const capabilities: Set<Capability> = new Set();
capabilities.add(MatrixCapabilities.Screenshots);
capabilities.add(MatrixCapabilities.AlwaysOnScreen);
capabilities.add(MatrixCapabilities.MSC3846TurnServers);
capabilities.add(MatrixCapabilities.MSC4157SendDelayedEvent);
capabilities.add(MatrixCapabilities.MSC4157UpdateDelayedEvent);
capabilities.add(`org.matrix.msc2762.timeline:${roomId}`);
capabilities.add(`org.matrix.msc2762.state:${roomId}`);
capabilities.add(
WidgetEventCapability.forStateEvent(EventDirection.Receive, EventType.RoomMember).raw
);
capabilities.add(
WidgetEventCapability.forStateEvent(EventDirection.Receive, 'org.matrix.msc3401.call').raw
);
capabilities.add(
WidgetEventCapability.forStateEvent(EventDirection.Receive, EventType.RoomEncryption).raw
);
capabilities.add(
WidgetEventCapability.forStateEvent(EventDirection.Receive, EventType.RoomName).raw
);
capabilities.add(
WidgetEventCapability.forStateEvent(
EventDirection.Send,
'org.matrix.msc3401.call.member',
userId
).raw
);
capabilities.add(
WidgetEventCapability.forStateEvent(
EventDirection.Send,
'org.matrix.msc3401.call.member',
`_${userId}_${deviceId}_m.call`
).raw
);
capabilities.add(
WidgetEventCapability.forStateEvent(
EventDirection.Send,
'org.matrix.msc3401.call.member',
`${userId}_${deviceId}_m.call`
).raw
);
capabilities.add(
WidgetEventCapability.forStateEvent(
EventDirection.Send,
'org.matrix.msc3401.call.member',
`_${userId}_${deviceId}`
).raw
);
capabilities.add(
WidgetEventCapability.forStateEvent(
EventDirection.Send,
'org.matrix.msc3401.call.member',
`${userId}_${deviceId}`
).raw
);
capabilities.add(
WidgetEventCapability.forStateEvent(EventDirection.Receive, 'org.matrix.msc3401.call.member')
.raw
);
capabilities.add(
WidgetEventCapability.forStateEvent(EventDirection.Receive, EventType.RoomCreate).raw
);
capabilities.add(
WidgetEventCapability.forRoomEvent(
EventDirection.Receive,
'org.matrix.msc4075.rtc.notification'
).raw
);
[
'io.element.call.encryption_keys',
'org.matrix.rageshake_request',
EventType.Reaction,
EventType.RoomRedaction,
'io.element.call.reaction',
'org.matrix.msc4310.rtc.decline',
].forEach((type) => {
capabilities.add(WidgetEventCapability.forRoomEvent(EventDirection.Send, type).raw);
capabilities.add(WidgetEventCapability.forRoomEvent(EventDirection.Receive, type).raw);
});
[
EventType.CallInvite,
EventType.CallCandidates,
EventType.CallAnswer,
EventType.CallHangup,
EventType.CallReject,
EventType.CallSelectAnswer,
EventType.CallNegotiate,
EventType.CallSDPStreamMetadataChanged,
EventType.CallSDPStreamMetadataChangedPrefix,
EventType.CallReplaces,
EventType.CallEncryptionKeysPrefix,
].forEach((type) => {
capabilities.add(WidgetEventCapability.forToDeviceEvent(EventDirection.Send, type).raw);
capabilities.add(WidgetEventCapability.forToDeviceEvent(EventDirection.Receive, type).raw);
});
return capabilities;
}
+20
View File
@@ -0,0 +1,20 @@
import { atom } from 'jotai';
import { CallEmbed } from '../plugins/call';
const baseCallEmbedAtom = atom<CallEmbed | undefined>(undefined);
export const callEmbedAtom = atom<CallEmbed | undefined, [CallEmbed | undefined], void>(
(get) => get(baseCallEmbedAtom),
(get, set, callEmbed) => {
const prevCallEmbed = get(baseCallEmbedAtom);
if (callEmbed === prevCallEmbed) return;
if (prevCallEmbed) {
prevCallEmbed.dispose();
}
set(baseCallEmbedAtom, callEmbed);
}
);
export const callChatAtom = atom<boolean>(false);
+39
View File
@@ -0,0 +1,39 @@
import { WritableAtom } from 'jotai';
import {
atomWithLocalStorage,
getLocalStorageItem,
setLocalStorageItem,
} from './utils/atomWithLocalStorage';
export type CallPreferences = {
microphone: boolean;
video: boolean;
sound: boolean;
};
const CALL_PREFERENCES = 'callPreferences';
const DEFAULT_PREFERENCES: CallPreferences = {
microphone: true,
video: false,
sound: true,
};
export type CallPreferencesAtom = WritableAtom<CallPreferences, [CallPreferences], undefined>;
export const makeCallPreferencesAtom = (userId: string): CallPreferencesAtom => {
const storeKey = `${CALL_PREFERENCES}${userId}`;
const callPreferencesAtom = atomWithLocalStorage<CallPreferences>(
storeKey,
(key) => {
const v = getLocalStorageItem<CallPreferences>(key, DEFAULT_PREFERENCES);
return v;
},
(key, value) => {
setLocalStorageItem(key, value);
}
);
return callPreferencesAtom;
};
+61
View File
@@ -0,0 +1,61 @@
import { createContext, useCallback, useContext } from 'react';
import { useAtom } from 'jotai';
import { CallPreferences, CallPreferencesAtom } from '../callPreferences';
const CallPreferencesAtomContext = createContext<CallPreferencesAtom | null>(null);
export const CallPreferencesProvider = CallPreferencesAtomContext.Provider;
export const useCallPreferencesAtom = (): CallPreferencesAtom => {
const atom = useContext(CallPreferencesAtomContext);
if (!atom) {
throw new Error('CallPreferencesAtom not provided!');
}
return atom;
};
export const useCallPreferences = (): CallPreferences & {
toggleMicrophone: () => void;
toggleVideo: () => void;
toggleSound: () => void;
} => {
const callPrefAtom = useCallPreferencesAtom();
const [pref, setPref] = useAtom(callPrefAtom);
const toggleMicrophone = useCallback(() => {
const microphone = !pref.microphone;
setPref({
microphone,
video: pref.video,
sound: !pref.sound && microphone ? true : pref.sound,
});
}, [setPref, pref]);
const toggleVideo = useCallback(() => {
const video = !pref.video;
setPref({
microphone: pref.microphone,
video,
sound: pref.sound,
});
}, [setPref, pref]);
const toggleSound = useCallback(() => {
const sound = !pref.sound;
setPref({
microphone: !sound ? false : pref.microphone,
video: pref.video,
sound,
});
}, [setPref, pref]);
return {
...pref,
toggleMicrophone,
toggleVideo,
toggleSound,
};
};