Files
cinny/src/app/hooks/useMentionClickHandler.ts
T

48 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-07-30 17:48:59 +05:30
import { ReactEventHandler, useCallback } from 'react';
import { useNavigate } from 'react-router-dom';
import { useRoomNavigate } from './useRoomNavigate';
import { useMatrixClient } from './useMatrixClient';
import { isRoomId, isUserId } from '../utils/matrix';
import { getHomeRoomPath, withSearchParam } from '../pages/pathUtils';
import { _RoomSearchParams } from '../pages/paths';
2025-08-09 17:46:10 +05:30
import { useOpenUserRoomProfile } from '../state/hooks/userRoomProfile';
import { useSpaceOptionally } from './useSpace';
2024-07-30 17:48:59 +05:30
export const useMentionClickHandler = (roomId: string): ReactEventHandler<HTMLElement> => {
const mx = useMatrixClient();
const { navigateRoom, navigateSpace } = useRoomNavigate();
const navigate = useNavigate();
2025-08-09 17:46:10 +05:30
const openProfile = useOpenUserRoomProfile();
const space = useSpaceOptionally();
2024-07-30 17:48:59 +05:30
const handleClick: ReactEventHandler<HTMLElement> = useCallback(
(evt) => {
evt.stopPropagation();
2024-07-30 17:48:59 +05:30
evt.preventDefault();
const target = evt.currentTarget;
const mentionId = target.getAttribute('data-mention-id');
if (typeof mentionId !== 'string') return;
if (isUserId(mentionId)) {
2025-08-09 17:46:10 +05:30
openProfile(roomId, space?.roomId, mentionId, target.getBoundingClientRect());
2024-07-30 17:48:59 +05:30
return;
}
const eventId = target.getAttribute('data-mention-event-id') || undefined;
if (isRoomId(mentionId) && mx.getRoom(mentionId)) {
if (mx.getRoom(mentionId)?.isSpaceRoom()) navigateSpace(mentionId);
else navigateRoom(mentionId, eventId);
return;
}
const viaServers = target.getAttribute('data-mention-via') || undefined;
const path = getHomeRoomPath(mentionId, eventId);
navigate(viaServers ? withSearchParam<_RoomSearchParams>(path, { viaServers }) : path);
},
2025-08-09 17:46:10 +05:30
[mx, navigate, navigateRoom, navigateSpace, roomId, space, openProfile]
2024-07-30 17:48:59 +05:30
);
return handleClick;
};