mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-29 13:22:47 -08:00
fix: route Matrix URI mentions in profiles (#1736)
<!-- Please read https://github.com/SableClient/Sable/blob/dev/CONTRIBUTING.md before submitting your pull request --> ### Description <!-- Please include a summary of the change. Please also include relevant motivation and context. List any dependencies that are required for this change. --> Fixes # #### Type of change - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] This change requires a documentation update ### Checklist: - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings ### AI disclosure: - [ ] Partially AI assisted (clarify which code was AI assisted and briefly explain what it does). - [ ] Fully AI generated (explain what all the generated code does in moderate detail). <!-- Write any explanation required here, but do not generate the explanation using AI!! You must prove you understand what the code in this PR does. -->
This commit is contained in:
4 files changed
+76
-17
No files matched your search
@@ -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<string | undefined>();
|
||||
const close = useCloseUserRoomProfile();
|
||||
const close = useSetAtom(userRoomProfileAtom);
|
||||
const requestClose = useCallback(() => {
|
||||
close((current) => (current === state ? undefined : current));
|
||||
}, [close, state]);
|
||||
|
||||
if (!room) return null;
|
||||
|
||||
return (
|
||||
<ResponsiveMenu
|
||||
anchor={cords}
|
||||
requestClose={close}
|
||||
requestClose={requestClose}
|
||||
position={position ?? 'Top'}
|
||||
align={cords.y > 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}
|
||||
/>
|
||||
</RoomProvider>
|
||||
</SpaceProvider>
|
||||
@@ -48,7 +54,7 @@ function UserRoomProfileContextMenu({ state }: { state: UserRoomProfileState })
|
||||
}
|
||||
|
||||
export function UserRoomProfileRenderer() {
|
||||
const state = useUserRoomProfileState();
|
||||
const state = useAtomValue(userRoomProfileAtom);
|
||||
|
||||
if (!state) return null;
|
||||
return <UserRoomProfileContextMenu key={`${state.roomId}:${state.userId}`} state={state} />;
|
||||
|
||||
@@ -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<UserProfile>;
|
||||
onSurfaceColorChange?: (color: string) => void;
|
||||
anchor: RectCords;
|
||||
position?: Position;
|
||||
};
|
||||
export function UserRoomProfile({
|
||||
userId,
|
||||
pmp: initialPmp,
|
||||
initialProfile,
|
||||
onSurfaceColorChange,
|
||||
anchor,
|
||||
position,
|
||||
}: Readonly<UserRoomProfileProps>) {
|
||||
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<HTMLElement>) => {
|
||||
e.preventDefault();
|
||||
}, []);
|
||||
const mentionClickHandler = useMentionClickHandler(room.roomId, anchor, position);
|
||||
const settingsLinkBaseUrl = useSettingsLinkBaseUrl();
|
||||
|
||||
const linkifyOpts = useMemo<LinkifyOpts>(
|
||||
@@ -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;
|
||||
|
||||
@@ -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<HTMLElement> => {
|
||||
export const useMentionClickHandler = (
|
||||
roomId: string,
|
||||
profileAnchor?: RectCords,
|
||||
profilePosition?: Position
|
||||
): ReactEventHandler<HTMLElement> => {
|
||||
const mx = useMatrixClient();
|
||||
const { navigateRoom, navigateSpace } = useRoomNavigate();
|
||||
const navigate = useNavigate();
|
||||
@@ -38,7 +43,14 @@ export const useMentionClickHandler = (roomId: string): ReactEventHandler<HTMLEl
|
||||
if (typeof mentionId !== 'string') return;
|
||||
|
||||
if (isUserId(mentionId)) {
|
||||
openProfile(roomId, space?.roomId, mentionId, undefined, target.getBoundingClientRect());
|
||||
openProfile(
|
||||
roomId,
|
||||
space?.roomId,
|
||||
mentionId,
|
||||
undefined,
|
||||
profileAnchor ?? target.getBoundingClientRect(),
|
||||
profilePosition
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -54,7 +66,18 @@ export const useMentionClickHandler = (roomId: string): ReactEventHandler<HTMLEl
|
||||
|
||||
navigate(viaServers ? withSearchParam(path, { viaServers }) : path);
|
||||
},
|
||||
[mx, navigate, navigateRoom, navigateSpace, openProfile, openSettings, roomId, space]
|
||||
[
|
||||
mx,
|
||||
navigate,
|
||||
navigateRoom,
|
||||
navigateSpace,
|
||||
openProfile,
|
||||
openSettings,
|
||||
profileAnchor,
|
||||
profilePosition,
|
||||
roomId,
|
||||
space,
|
||||
]
|
||||
);
|
||||
|
||||
return handleClick;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import { fireEvent, render, screen } from '@testing-library/react';
|
||||
import type { ReactEventHandler } from 'react';
|
||||
import parse from 'html-react-parser';
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import * as customHtmlCss from '$styles/CustomHtml.css';
|
||||
@@ -493,7 +494,7 @@ describe('react custom html parser', () => {
|
||||
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<ReactEventHandler<HTMLElement>>();
|
||||
const parserOptions = getReactCustomHtmlParser(roomMx(), '!room:example.com', {
|
||||
settingsLinkBaseUrl,
|
||||
linkifyOpts: LINKIFY_OPTS,
|
||||
handleMentionClick,
|
||||
});
|
||||
|
||||
render(<div>{parse(`<a href="${href}">${href}</a>`, parserOptions)}</div>);
|
||||
|
||||
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(
|
||||
'<a href="matrix:roomid/room:example.org?via=elsewhere.ca">matrix:roomid/room:example.org?via=elsewhere.ca</a>',
|
||||
|
||||
Reference in new issue
Block a user