diff --git a/src/app/components/UserRoomProfileRenderer.tsx b/src/app/components/UserRoomProfileRenderer.tsx index 9bbdcc99a..96caa8b69 100644 --- a/src/app/components/UserRoomProfileRenderer.tsx +++ b/src/app/components/UserRoomProfileRenderer.tsx @@ -1,6 +1,7 @@ import { Menu, toRem } from 'folds'; -import { useState } from 'react'; -import { useCloseUserRoomProfile, useUserRoomProfileState } from '$state/hooks/userRoomProfile'; +import { useCallback, useState } from 'react'; +import { useAtomValue, useSetAtom } from 'jotai'; +import { userRoomProfileAtom } from '$state/userRoomProfile'; import type { UserRoomProfileState } from '$state/userRoomProfile'; import { useAllJoinedRoomsSet, useGetRoom } from '$hooks/useGetRoom'; import { SpaceProvider } from '$hooks/useSpace'; @@ -16,14 +17,17 @@ function UserRoomProfileContextMenu({ state }: { state: UserRoomProfileState }) const space = spaceId ? getRoom(spaceId) : undefined; const [surfaceColor, setSurfaceColor] = useState(); - const close = useCloseUserRoomProfile(); + const close = useSetAtom(userRoomProfileAtom); + const requestClose = useCallback(() => { + close((current) => (current === state ? undefined : current)); + }, [close, state]); if (!room) return null; return ( window.innerHeight / 2 ? 'End' : 'Start'} returnFocusOnDeactivate @@ -38,6 +42,8 @@ function UserRoomProfileContextMenu({ state }: { state: UserRoomProfileState }) initialProfile={initialProfile} onSurfaceColorChange={setSurfaceColor} pmp={pmp} + anchor={cords} + position={position} /> @@ -48,7 +54,7 @@ function UserRoomProfileContextMenu({ state }: { state: UserRoomProfileState }) } export function UserRoomProfileRenderer() { - const state = useUserRoomProfileState(); + const state = useAtomValue(userRoomProfileAtom); if (!state) return null; return ; diff --git a/src/app/components/user-profile/UserRoomProfile.tsx b/src/app/components/user-profile/UserRoomProfile.tsx index c6fa0f0e2..ed8f8c6be 100644 --- a/src/app/components/user-profile/UserRoomProfile.tsx +++ b/src/app/components/user-profile/UserRoomProfile.tsx @@ -1,6 +1,7 @@ import { Box, Button, color, config, Menu, MenuItem, Scroll, Text, toRem } from 'folds'; -import type { CSSProperties, SyntheticEvent } from 'react'; -import { useCallback, useLayoutEffect, useMemo, useState } from 'react'; +import type { Position, RectCords } from 'folds'; +import type { CSSProperties } from 'react'; +import { useLayoutEffect, useMemo, useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { useAtomValue } from 'jotai'; import type { Opts as LinkifyOpts } from 'linkifyjs'; @@ -61,6 +62,7 @@ import { IgnoredUserAlert, MutualRoomsChip, OptionsChip, ServerChip, ShareChip } import { UserHero, UserHeroName } from './UserHero'; import { KnownMembership } from '$types/matrix-sdk'; import { useRoomMemberHydration } from '$hooks/useRoomMemberHydration'; +import { useMentionClickHandler } from '$hooks/useMentionClickHandler'; import * as css from './styles.css'; import * as prefix from '$unstable/prefixes'; import type { Persona } from '$app/persona'; @@ -415,12 +417,16 @@ type UserRoomProfileProps = { pmp?: Persona; initialProfile?: Partial; onSurfaceColorChange?: (color: string) => void; + anchor: RectCords; + position?: Position; }; export function UserRoomProfile({ userId, pmp: initialPmp, initialProfile, onSurfaceColorChange, + anchor, + position, }: Readonly) { const theme = useTheme(); const mx = useMatrixClient(); @@ -496,10 +502,7 @@ export function UserRoomProfile({ navigate(withSearchParam(getDirectCreatePath(), directSearchParam)); }; - // Todo eventually maybe - const mentionClickHandler = useCallback((e: SyntheticEvent) => { - e.preventDefault(); - }, []); + const mentionClickHandler = useMentionClickHandler(room.roomId, anchor, position); const settingsLinkBaseUrl = useSettingsLinkBaseUrl(); const linkifyOpts = useMemo( @@ -530,8 +533,17 @@ export function UserRoomProfile({ linkifyOpts, useAuthentication, handleSpoilerClick: spoilerClickHandler, + handleMentionClick: mentionClickHandler, }), - [mx, room, linkifyOpts, settingsLinkBaseUrl, useAuthentication, spoilerClickHandler] + [ + mx, + room, + linkifyOpts, + settingsLinkBaseUrl, + useAuthentication, + spoilerClickHandler, + mentionClickHandler, + ] ); const backgroundColor = fetchedProfile.heroColor ?? color.Surface.Container; diff --git a/src/app/hooks/useMentionClickHandler.ts b/src/app/hooks/useMentionClickHandler.ts index 4191cbb58..ebf799365 100644 --- a/src/app/hooks/useMentionClickHandler.ts +++ b/src/app/hooks/useMentionClickHandler.ts @@ -1,6 +1,7 @@ import type { ReactEventHandler } from 'react'; import { useCallback } from 'react'; import { useNavigate } from 'react-router-dom'; +import type { Position, RectCords } from 'folds'; import { isRoomId, isUserId } from '$utils/matrix'; import { getHomeRoomPath, withSearchParam } from '$pages/pathUtils'; import { isSettingsSectionId } from '$features/settings/routes'; @@ -11,7 +12,11 @@ import { useMatrixClient } from './useMatrixClient'; import { useRoomNavigate } from './useRoomNavigate'; import { useSpaceOptionally } from './useSpace'; -export const useMentionClickHandler = (roomId: string): ReactEventHandler => { +export const useMentionClickHandler = ( + roomId: string, + profileAnchor?: RectCords, + profilePosition?: Position +): ReactEventHandler => { const mx = useMatrixClient(); const { navigateRoom, navigateSpace } = useRoomNavigate(); const navigate = useNavigate(); @@ -38,7 +43,14 @@ export const useMentionClickHandler = (roomId: string): ReactEventHandler { describe('matrix: URI mentions', () => { const roomMx = () => createMatrixClient({ - getRoom: () => ({ roomId: '!room:example.org', name: 'Lobby' }), + getRoom: () => ({ roomId: '!room:example.org', name: 'Lobby', getMember: () => undefined }), getRooms: () => [], }); @@ -514,6 +515,23 @@ describe('matrix: URI mentions', () => { ); }); + it.each([ + ['matrix:u/bob:example.org', '@bob'], + ['matrix:roomid/room:example.org', '#Lobby'], + ])('routes a matrix: %s mention through the click handler', (href, label) => { + const handleMentionClick = vi.fn>(); + const parserOptions = getReactCustomHtmlParser(roomMx(), '!room:example.com', { + settingsLinkBaseUrl, + linkifyOpts: LINKIFY_OPTS, + handleMentionClick, + }); + + render(
{parse(`${href}`, parserOptions)}
); + + fireEvent.click(screen.getByRole('link', { name: label })); + expect(handleMentionClick).toHaveBeenCalledOnce(); + }); + it('renders a matrix: room URI with via servers and falls back to the room name', () => { renderParsedHtml( 'matrix:roomid/room:example.org?via=elsewhere.ca',