diff --git a/.changeset/000_merge_upstream.md b/.changeset/000_merge_upstream.md new file mode 100644 index 000000000..d04b7f251 --- /dev/null +++ b/.changeset/000_merge_upstream.md @@ -0,0 +1,5 @@ +--- +sable: minor +--- + +Merge Voice Call updates from upstream. diff --git a/package-lock.json b/package-lock.json index d82546355..85a16648f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/src/app/components/CallEmbedProvider.tsx b/src/app/components/CallEmbedProvider.tsx new file mode 100644 index 000000000..0fc084f55 --- /dev/null +++ b/src/app/components/CallEmbedProvider.tsx @@ -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(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 ( + + {callEmbed && } + {children} +
+ + ); +} diff --git a/src/app/components/create-room/CreateRoomTypeSelector.tsx b/src/app/components/create-room/CreateRoomTypeSelector.tsx index ff40c9836..42468a03d 100644 --- a/src/app/components/create-room/CreateRoomTypeSelector.tsx +++ b/src/app/components/create-room/CreateRoomTypeSelector.tsx @@ -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={} after={value === CreateRoomType.TextRoom && } > - Text - - Send text messages, videos and GIFs. - + + + Chat Room + + + - Messages, photos, and videos. + + } after={value === CreateRoomType.VoiceRoom && } > - Voice - - A room optimized for voice calls. - + + + Voice Room + + + - Live audio and video conversations. + + + diff --git a/src/app/components/editor/autocomplete/RoomMentionAutocomplete.tsx b/src/app/components/editor/autocomplete/RoomMentionAutocomplete.tsx index 0d2e9693f..e02467122 100644 --- a/src/app/components/editor/autocomplete/RoomMentionAutocomplete.tsx +++ b/src/app/components/editor/autocomplete/RoomMentionAutocomplete.tsx @@ -169,6 +169,7 @@ export function RoomMentionAutocomplete({ )} diff --git a/src/app/components/sequence-card/SequenceCard.tsx b/src/app/components/sequence-card/SequenceCard.tsx index 65d80dcee..7738d93fb 100644 --- a/src/app/components/sequence-card/SequenceCard.tsx +++ b/src/app/components/sequence-card/SequenceCard.tsx @@ -17,6 +17,7 @@ export const SequenceCard = as< firstChild, lastChild, outlined, + mergeBorder, ...props }, ref @@ -24,7 +25,7 @@ export const SequenceCard = as< ( + ({ size, variant, className, ...props }, ref) => ( + + ) +); diff --git a/src/app/components/stacked-avatar/index.ts b/src/app/components/stacked-avatar/index.ts new file mode 100644 index 000000000..63d9f56d0 --- /dev/null +++ b/src/app/components/stacked-avatar/index.ts @@ -0,0 +1 @@ +export * from './StackedAvatar'; diff --git a/src/app/components/stacked-avatar/styles.css.ts b/src/app/components/stacked-avatar/styles.css.ts new file mode 100644 index 000000000..3bbf7271a --- /dev/null +++ b/src/app/components/stacked-avatar/styles.css.ts @@ -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; diff --git a/src/app/components/user-profile/UserChips.tsx b/src/app/components/user-profile/UserChips.tsx index 27f962d13..1ce3b26c1 100644 --- a/src/app/components/user-profile/UserChips.tsx +++ b/src/app/components/user-profile/UserChips.tsx @@ -332,7 +332,7 @@ export function MutualRoomsChip({ userId }: { userId: string }) { )} /> ) : ( - + )} } diff --git a/src/app/features/add-existing/AddExisting.tsx b/src/app/features/add-existing/AddExisting.tsx index c6ba336ad..80ace407b 100644 --- a/src/app/features/add-existing/AddExisting.tsx +++ b/src/app/features/add-existing/AddExisting.tsx @@ -287,7 +287,11 @@ export function AddExistingModal({ parentId, space, requestClose }: AddExistingM )} /> ) : ( - + )} } diff --git a/src/app/features/call-status/CallControl.tsx b/src/app/features/call-status/CallControl.tsx new file mode 100644 index 000000000..5b7625e85 --- /dev/null +++ b/src/app/features/call-status/CallControl.tsx @@ -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; +}; +function MicrophoneButton({ enabled, onToggle }: MicrophoneButtonProps) { + return ( + + {enabled ? 'Turn Off Microphone' : 'Turn On Microphone'} + + } + > + {(anchorRef) => ( + onToggle()} + outlined={!enabled} + > + + + )} + + ); +} + +type SoundButtonProps = { + enabled: boolean; + onToggle: () => void; +}; +function SoundButton({ enabled, onToggle }: SoundButtonProps) { + return ( + + {enabled ? 'Turn Off Sound' : 'Turn On Sound'} + + } + > + {(anchorRef) => ( + onToggle()} + outlined={!enabled} + > + + + )} + + ); +} + +type VideoButtonProps = { + enabled: boolean; + onToggle: () => Promise; +}; +function VideoButton({ enabled, onToggle }: VideoButtonProps) { + return ( + + {enabled ? 'Stop Camera' : 'Start Camera'} + + } + > + {(anchorRef) => ( + onToggle()} + outlined={enabled} + > + + + )} + + ); +} + +function ScreenShareButton() { + const [enabled, setEnabled] = useState(false); + + return ( + + {enabled ? 'Stop Screenshare' : 'Start Screenshare'} + + } + > + {(anchorRef) => ( + setEnabled(!enabled)} + outlined={enabled} + > + + + )} + + ); +} + +export function CallControl({ callEmbed }: { callEmbed: CallEmbed }) { + const { microphone, video, sound } = useCallControlState(callEmbed.control); + + return ( + + + callEmbed.control.toggleMicrophone()} + /> + callEmbed.control.toggleSound()} /> + + + + callEmbed.control.toggleVideo()} /> + {false && } + + + } + outlined + onClick={() => callEmbed.hangup()} + > + + End + + + + ); +} diff --git a/src/app/features/call-status/CallRoomName.tsx b/src/app/features/call-status/CallRoomName.tsx new file mode 100644 index 000000000..e2cca3b1b --- /dev/null +++ b/src/app/features/call-status/CallRoomName.tsx @@ -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 ( + + } + onClick={() => navigateRoom(room.roomId)} + > + + {name} + {!dm && perfectOrphanParent && ( + + {' •'} {getRoom(perfectOrphanParent)?.name ?? perfectOrphanParent} + + )} + + + ); +} diff --git a/src/app/features/call-status/CallStatus.tsx b/src/app/features/call-status/CallStatus.tsx new file mode 100644 index 000000000..33e3a5f26 --- /dev/null +++ b/src/app/features/call-status/CallStatus.tsx @@ -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 ( + + + {callJoined && callMembers.length > 0 ? ( + + + + + ) : ( + + )} + + + + + + + + ); +} diff --git a/src/app/features/call-status/LiveChip.tsx b/src/app/features/call-status/LiveChip.tsx new file mode 100644 index 000000000..3806fd0e0 --- /dev/null +++ b/src/app/features/call-status/LiveChip.tsx @@ -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(); + + const handleOpenMenu: MouseEventHandler = (evt) => { + setCords(evt.currentTarget.getBoundingClientRect()); + }; + + return ( + setCords(undefined), + clickOutsideDeactivates: true, + isKeyForward: (evt: KeyboardEvent) => evt.key === 'ArrowDown', + isKeyBackward: (evt: KeyboardEvent) => evt.key === 'ArrowUp', + escapeDeactivates: stopPropagation, + }} + > + + + + + {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 ( + + openUserProfile( + room.roomId, + undefined, + userId, + getMouseEventCords(evt.nativeEvent), + 'Right' + ) + } + before={ + + } + /> + + } + > + + {name} + + + ); + })} + + + + + + } + > + } + after={} + radii="Pill" + onClick={handleOpenMenu} + > + + {count} Live + + + + ); +} diff --git a/src/app/features/call-status/MemberGlance.tsx b/src/app/features/call-status/MemberGlance.tsx new file mode 100644 index 000000000..3773ff3a3 --- /dev/null +++ b/src/app/features/call-status/MemberGlance.tsx @@ -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 ( + + {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 ( + + openUserProfile( + room.roomId, + undefined, + userId, + getMouseEventCords(evt.nativeEvent), + 'Top' + ) + } + > + } + /> + + ); + })} + {remainingCount > 0 && ( + + +{remainingCount} + + )} + + ); +} diff --git a/src/app/features/call-status/components.tsx b/src/app/features/call-status/components.tsx new file mode 100644 index 000000000..29eb7e77f --- /dev/null +++ b/src/app/features/call-status/components.tsx @@ -0,0 +1,8 @@ +import { Line } from 'folds'; +import * as css from './styles.css'; + +export function StatusDivider() { + return ( + + ); +} diff --git a/src/app/features/call-status/index.ts b/src/app/features/call-status/index.ts new file mode 100644 index 000000000..99accf8bf --- /dev/null +++ b/src/app/features/call-status/index.ts @@ -0,0 +1 @@ +export * from './CallStatus'; diff --git a/src/app/features/call-status/styles.css.ts b/src/app/features/call-status/styles.css.ts new file mode 100644 index 000000000..26b08f0be --- /dev/null +++ b/src/app/features/call-status/styles.css.ts @@ -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), +}); diff --git a/src/app/features/call/CallMemberCard.tsx b/src/app/features/call/CallMemberCard.tsx new file mode 100644 index 000000000..7d03ac77b --- /dev/null +++ b/src/app/features/call/CallMemberCard.tsx @@ -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 ( + + openUserProfile( + room.roomId, + undefined, + userId, + getMouseEventCords(evt.nativeEvent), + 'Right' + ) + } + > + + + } + /> + + + + {name} + + + {audioOnly && } + + + ); +} + +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) => ( + + ))} + {members.length > max && ( + setViewMore(!viewMore)} + > + + {viewMore ? ( + + Collapse + + ) : ( + + {remaining === 0 ? `+${remaining} Other` : `+${remaining} Others`} + + )} + + + + )} + + ); +} diff --git a/src/app/features/call/CallView.css.ts b/src/app/features/call/CallView.css.ts deleted file mode 100644 index 5a976829d..000000000 --- a/src/app/features/call/CallView.css.ts +++ /dev/null @@ -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', - }, -}); diff --git a/src/app/features/call/CallView.tsx b/src/app/features/call/CallView.tsx index c845e80ce..969554667 100644 --- a/src/app/features/call/CallView.tsx +++ b/src/app/features/call/CallView.tsx @@ -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 ( - - {children} - + + Voice chat's empty — Be the first to hop in! + ); } -export function CallView({ room }: { room: Room }) { - const callIframeRef = useContext(CallRefContext); - const iframeHostRef = useRef(null); +function NoPermissionMessage() { + return ( + + You don't have permission to join! + + ); +} - const originalIframeStylesRef = useRef(null); +function AlreadyInCallMessage() { + return ( + + Already in another call — End the current call to join! + + ); +} + +export function CallView() { const mx = useMatrixClient(); + const room = useRoom(); - const [visibleCallNames, setVisibleCallNames] = useState(''); + const callViewRef = useRef(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).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 = (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 ( - -
- - - {callMembers.slice(0, 6).map((callMember) => ( - - ))} - - - - - {roomName} - - - {visibleCallNames !== '' ? visibleCallNames : 'No one'}{' '} - {memberDisplayNames.length > 1 ? 'are' : 'is'} currently in voice - - - - + + + )} ); } diff --git a/src/app/features/call/CallViewUser.tsx b/src/app/features/call/CallViewUser.tsx deleted file mode 100644 index 2de556b56..000000000 --- a/src/app/features/call/CallViewUser.tsx +++ /dev/null @@ -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) => ( - - ) -); - -export const CallViewUserBase = as<'div'>(({ className, ...props }, 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 = (evt) => { - openProfile(room.roomId, space?.roomId, userId, evt.currentTarget.getBoundingClientRect()); - }; - - return ( - - - - - } - /> - - - {getName} - - - - - ); -} diff --git a/src/app/features/call/CinnyWidget.ts b/src/app/features/call/CinnyWidget.ts deleted file mode 100644 index e5dfaec76..000000000 --- a/src/app/features/call/CinnyWidget.ts +++ /dev/null @@ -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); - } -} diff --git a/src/app/features/call/Controls.tsx b/src/app/features/call/Controls.tsx new file mode 100644 index 000000000..0e0a07cda --- /dev/null +++ b/src/app/features/call/Controls.tsx @@ -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 ( + + ); +} + +type MicrophoneButtonProps = { + enabled: boolean; + onToggle: () => void; +}; +export function MicrophoneButton({ enabled, onToggle }: MicrophoneButtonProps) { + return ( + + {enabled ? 'Turn Off Microphone' : 'Turn On Microphone'} + + } + > + {(anchorRef) => ( + onToggle()} + outlined + > + + + )} + + ); +} + +type SoundButtonProps = { + enabled: boolean; + onToggle: () => void; +}; +export function SoundButton({ enabled, onToggle }: SoundButtonProps) { + return ( + + {enabled ? 'Turn Off Sound' : 'Turn On Sound'} + + } + > + {(anchorRef) => ( + onToggle()} + outlined + > + + + )} + + ); +} + +type VideoButtonProps = { + enabled: boolean; + onToggle: () => void; +}; +export function VideoButton({ enabled, onToggle }: VideoButtonProps) { + return ( + + {enabled ? 'Stop Camera' : 'Start Camera'} + + } + > + {(anchorRef) => ( + onToggle()} + outlined + > + + + )} + + ); +} + +export function ChatButton() { + const [chat, setChat] = useAtom(callChatAtom); + + return ( + + {chat ? 'Close Chat' : 'Open Chat'} + + } + > + {(anchorRef) => ( + setChat(!chat)} + outlined + > + + + )} + + ); +} diff --git a/src/app/features/call/PrescreenControls.tsx b/src/app/features/call/PrescreenControls.tsx new file mode 100644 index 000000000..705a4d1e6 --- /dev/null +++ b/src/app/features/call/PrescreenControls.tsx @@ -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 ( + + + + + + + + + + + + + + + ); +} diff --git a/src/app/features/call/SmallWidget.ts b/src/app/features/call/SmallWidget.ts deleted file mode 100644 index 63186862e..000000000 --- a/src/app/features/call/SmallWidget.ts +++ /dev/null @@ -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 = {}; // room ID to event ID - - private readonly eventsToFeed = new WeakSet(); - - private stickyPromise?: () => Promise; - - 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[] = []; - 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) => { - 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 => { - 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(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 -}); diff --git a/src/app/features/call/SmallWidgetDriver.ts b/src/app/features/call/SmallWidgetDriver.ts deleted file mode 100644 index 467adbb54..000000000 --- a/src/app/features/call/SmallWidgetDriver.ts +++ /dev/null @@ -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; - - 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): Promise> { - // Stubbed under the assumption voice calls will be valid thru element-call - return requested; - } - - public async sendEvent( - eventType: K, - content: StateEvents[K], - stateKey: string | null, - targetRoomId: string | null - ): Promise; - - public async sendEvent( - eventType: K, - content: TimelineEvents[K], - stateKey: null, - targetRoomId: string | null - ): Promise; - - public async sendEvent( - eventType: string, - content: IContent, - stateKey: string | null = null, - targetRoomId: string | null = null - ): Promise { - 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( - delay: number | null, - parentDelayId: string | null, - eventType: K, - content: StateEvents[K], - stateKey: string | null, - targetRoomId: string | null - ): Promise; - - /** - * @experimental Part of MSC4140 & MSC4157 - */ - public async sendDelayedEvent( - delay: number | null, - parentDelayId: string | null, - eventType: K, - content: TimelineEvents[K], - stateKey: null, - targetRoomId: string | null - ): Promise; - - public async sendDelayedEvent( - delay: number | null, - parentDelayId: string | null, - eventType: string, - content: IContent, - stateKey: string | null = null, - targetRoomId: string | null = null - ): Promise { - 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 { - 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 { - 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 { - 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> - ): Promise { - 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 = {}; - - // 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} 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 { - 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): Promise { - 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} Resolves to the events representing the - * current values of the room state entries. - */ - public async readRoomState( - roomId: string, - eventType: string, - stateKey: string | undefined - ): Promise { - 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 { - navigateToPermalink(uri); - } - */ - - public async readEventRelations( - eventId: string, - roomId?: string, - relationType?: string, - eventType?: string, - from?: string, - to?: string, - limit?: number, - direction?: 'f' | 'b' - ): Promise { - 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 { - 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 { - 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; - } -} diff --git a/src/app/features/call/styles.css.ts b/src/app/features/call/styles.css.ts new file mode 100644 index 000000000..1ee2892a2 --- /dev/null +++ b/src/app/features/call/styles.css.ts @@ -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, +}); diff --git a/src/app/features/create-room/CreateRoomModal.tsx b/src/app/features/create-room/CreateRoomModal.tsx index d17922c05..ad613a520 100644 --- a/src/app/features/create-room/CreateRoomModal.tsx +++ b/src/app/features/create-room/CreateRoomModal.tsx @@ -54,7 +54,9 @@ function CreateRoomModal({ state }: CreateRoomModalProps) { }} > - New {type === CreateRoomType.VoiceRoom && 'Voice '}Room + + {type === CreateRoomType.VoiceRoom ? 'New Voice Room' : 'New Chat Room'} + diff --git a/src/app/features/lobby/SpaceItem.tsx b/src/app/features/lobby/SpaceItem.tsx index 61422d41e..f43af5d72 100644 --- a/src/app/features/lobby/SpaceItem.tsx +++ b/src/app/features/lobby/SpaceItem.tsx @@ -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)} > - New Room + Chat Room + + handleCreateRoom(CreateRoomType.VoiceRoom)} + after={} + > + Voice Room ( - + )} /> diff --git a/src/app/features/room-nav/RoomCallNavStatus.css.ts b/src/app/features/room-nav/RoomCallNavStatus.css.ts deleted file mode 100644 index 059afd5ad..000000000 --- a/src/app/features/room-nav/RoomCallNavStatus.css.ts +++ /dev/null @@ -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, -}); diff --git a/src/app/features/room-nav/RoomCallNavStatus.tsx b/src/app/features/room-nav/RoomCallNavStatus.tsx deleted file mode 100644 index 63073d4d8..000000000 --- a/src/app/features/room-nav/RoomCallNavStatus.tsx +++ /dev/null @@ -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 ( - - - - - {hasActiveCall && ( - - Go to Room - - } - > - {(triggerRef) => ( - - {isConnected ? ( - - ) : ( - - )} - - {isConnected ? 'Connected' : 'Connecting'} - - - )} - - )} - - {hasActiveCall && ( - - Hang Up - - } - > - {(triggerRef) => ( - - - - )} - - )} - - {!isAudioEnabled ? 'Unmute' : 'Mute'} - - } - > - {(triggerRef) => ( - - - - )} - - - {!isVideoEnabled ? 'Video On' : 'Video Off'} - - } - > - {(triggerRef) => ( - - - - )} - - - - ); -} diff --git a/src/app/features/room-nav/RoomNavItem.tsx b/src/app/features/room-nav/RoomNavItem.tsx index 9ba8b0f83..474522cdf 100644 --- a/src/app/features/room-nav/RoomNavItem.tsx +++ b/src/app/features/room-nav/RoomNavItem.tsx @@ -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( ({ 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(); + 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 = (evt) => { evt.preventDefault(); setMenuAnchor({ @@ -316,16 +317,17 @@ export function RoomNavItem({ const handleNavItemClick: MouseEventHandler = (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) => { 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 && ( + + + {callMembers.length} Live + + + )} {optionsVisible && ( + {room.isCallRoom() && ( + + {isChatOpen ? 'Hide Chat' : 'Show Chat'} + + } + > + {(triggerRef) => ( + + + + )} + + )} } > - {room.isCallRoom() && ( - - {isChatOpen ? 'Hide Chat' : 'Show Chat'} - - } - > - {(triggerRef) => ( - - - - )} - - )} )} + {room.isCallRoom() && ( - {callMemberships.map((callMembership) => ( + {callMembers.map((callMembership) => ( void; }; + export function RoomSettings({ initialPage, requestClose }: RoomSettingsProps) { const room = useRoom(); const mx = useMatrixClient(); @@ -118,6 +119,7 @@ export function RoomSettings({ initialPage, requestClose }: RoomSettingsProps) { renderFallback={() => ( diff --git a/src/app/features/room/CallChatView.tsx b/src/app/features/room/CallChatView.tsx new file mode 100644 index 000000000..b51b97cae --- /dev/null +++ b/src/app/features/room/CallChatView.tsx @@ -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 ( + + + + + + Chat + + + + + Close + + } + > + {(triggerRef) => ( + + + + )} + + + + + + + + + ); +} diff --git a/src/app/features/room/Room.tsx b/src/app/features/room/Room.tsx index f2619e6ad..c960d431b 100644 --- a/src/app/features/room/Room.tsx +++ b/src/app/features/room/Room.tsx @@ -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 ( - - - - - {room.isCallRoom() && screenSize === ScreenSize.Desktop && isChatOpen && ( + {callView && (screenSize === ScreenSize.Desktop || !chat) && ( + + + + + + + )} + {!callView && ( + + + + + + + )} + + {callView && chat && ( + <> + {screenSize === ScreenSize.Desktop && ( )} - {(!room.isCallRoom() || isChatOpen) && } - - - {screenSize === ScreenSize.Desktop && isDrawer && ( + + + )} + {!callView && screenSize === ScreenSize.Desktop && isDrawer && ( <> diff --git a/src/app/features/room/RoomTimeline.tsx b/src/app/features/room/RoomTimeline.tsx index d07c62a3c..74f60593f 100644 --- a/src/app/features/room/RoomTimeline.tsx +++ b/src/app/features/room/RoomTimeline.tsx @@ -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({ ); }, + [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().application; + + const timeJSX = ( + + } + /> + + ); + }, }, (mEventId, mEvent, item) => { if (!showHiddenEvents) return null; diff --git a/src/app/features/room/RoomView.tsx b/src/app/features/room/RoomView.tsx index f166f1224..15f64e936 100644 --- a/src/app/features/room/RoomView.tsx +++ b/src/app/features/room/RoomView.tsx @@ -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(null); const roomViewRef = useRef(null); const [hideReads] = useSetting(settingsAtom, 'hideReads'); const screenSize = useScreenSizeContext(); + const room = useRoom(); const { roomId } = room; const editor = useEditor(); diff --git a/src/app/features/room/RoomViewHeader.tsx b/src/app/features/room/RoomViewHeader.tsx index 0490350fc..84b67cce9 100644 --- a/src/app/features/room/RoomViewHeader.tsx +++ b/src/app/features/room/RoomViewHeader.tsx @@ -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(({ 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(); 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 ( - {(!room.isCallRoom() || isChatOpen) && ( + {(!room.isCallRoom() || chat) && ( <> {!encryptedRoom && ( )} - {screenSize === ScreenSize.Desktop && ( {(triggerRef) => ( - setPeopleDrawer((drawer) => !drawer)} - > + )} )} - {room.isCallRoom() && !direct && ( + {callView && ( - {isChatOpen ? 'Hide Chat' : 'Show Chat'} + {chat ? 'Hide Chat' : 'Show Chat'} } > @@ -668,10 +675,10 @@ export function RoomViewHeader() { fill="None" ref={triggerRef} onClick={() => { - toggleChat(); + setChat(!chat); }} > - + )} diff --git a/src/app/hooks/useCall.ts b/src/app/hooks/useCall.ts new file mode 100644 index 000000000..a4374fcd7 --- /dev/null +++ b/src/app/hooks/useCall.ts @@ -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( + 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; +}; diff --git a/src/app/hooks/useCallEmbed.ts b/src/app/hooks/useCallEmbed.ts new file mode 100644 index 000000000..e04182dd7 --- /dev/null +++ b/src/app/hooks/useCallEmbed.ts @@ -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(undefined); + +export const CallEmbedContextProvider = CallEmbedContext.Provider; + +export const useCallEmbed = (): CallEmbed | undefined => { + const callEmbed = useContext(CallEmbedContext); + + return callEmbed; +}; + +const CallEmbedRefContext = createContext | undefined>(undefined); +export const CallEmbedRefContextProvider = CallEmbedRefContext.Provider; +export const useCallEmbedRef = (): RefObject => { + 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): 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]) + ); +}; diff --git a/src/app/hooks/useCallMemberships.ts b/src/app/hooks/useCallMemberships.ts deleted file mode 100644 index 9d1cd0889..000000000 --- a/src/app/hooks/useCallMemberships.ts +++ /dev/null @@ -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([]); - 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; -}; diff --git a/src/app/pages/CallStatusRenderer.tsx b/src/app/pages/CallStatusRenderer.tsx new file mode 100644 index 000000000..344c67be4 --- /dev/null +++ b/src/app/pages/CallStatusRenderer.tsx @@ -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 ; +} diff --git a/src/app/pages/Router.tsx b/src/app/pages/Router.tsx index f54734633..d4684322a 100644 --- a/src/app/pages/Router.tsx +++ b/src/app/pages/Router.tsx @@ -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) - + @@ -160,11 +160,10 @@ export const createRouter = (clientConfig: ClientConfig, screenSize: ScreenSize) } > - - - + - + + diff --git a/src/app/pages/client/ClientInitStorageAtom.tsx b/src/app/pages/client/ClientInitStorageAtom.tsx index 80b263e94..90f6d914f 100644 --- a/src/app/pages/client/ClientInitStorageAtom.tsx +++ b/src/app/pages/client/ClientInitStorageAtom.tsx @@ -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 ( - {children} + + {children} + diff --git a/src/app/pages/client/call/CallProvider.tsx b/src/app/pages/client/call/CallProvider.tsx deleted file mode 100644 index e484a0bf5..000000000 --- a/src/app/pages/client/call/CallProvider.tsx +++ /dev/null @@ -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: ( - action: WidgetApiToWidgetAction | string, - data: T - ) => Promise; - isAudioEnabled: boolean; - isVideoEnabled: boolean; - isChatOpen: boolean; - isActiveCallReady: boolean; - resetActiveCallReady: () => void; - toggleAudio: () => Promise; - toggleVideo: () => Promise; - toggleChat: () => Promise; -} - -const CallContext = createContext(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(null); - const [viewedCallRoomId, setViewedCallRoomIdState] = useState(null); - - const [activeClientWidgetApi, setActiveClientWidgetApiState] = useState( - null - ); - const [activeClientWidget, setActiveClientWidget] = useState(null); - const [activeClientWidgetApiRoomId, setActiveClientWidgetApiRoomId] = useState( - null - ); - const [activeClientWidgetIframeRef, setActiveClientWidgetIframeRef] = - useState(null); - - const [isAudioEnabled, setIsAudioEnabledState] = useState(DEFAULT_AUDIO_ENABLED); - const [isVideoEnabled, setIsVideoEnabledState] = useState(DEFAULT_VIDEO_ENABLED); - const [isChatOpen, setIsChatOpenState] = useState(DEFAULT_CHAT_OPENED); - const [isActiveCallReady, setIsActiveCallReady] = useState(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 ( - action: WidgetApiToWidgetAction | string, - data: T - ): Promise => { - 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) => { - 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( - () => ({ - 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 {children}; -} - -export function useCallState(): CallContextState { - const context = useContext(CallContext); - if (context === undefined) { - throw new Error('useCallState must be used within a CallProvider'); - } - return context; -} diff --git a/src/app/pages/client/call/PersistentCallContainer.tsx b/src/app/pages/client/call/PersistentCallContainer.tsx deleted file mode 100644 index f6548bc76..000000000 --- a/src/app/pages/client/call/PersistentCallContainer.tsx +++ /dev/null @@ -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 | null>( - null -); - -export function PersistentCallContainer({ children }: PersistentCallContainerProps) { - const callIframeRef = useRef(null); - const callWidgetApiRef = useRef(null); - const callSmallWidgetRef = useRef(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, - smallWidgetRef: MutableRefObject, - iframeRef: MutableRefObject, - 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 ( - - - - -