import Linkify from 'linkify-react';
import { ErrorBoundary } from 'react-error-boundary';
import * as css from '../styles/CustomHtml.css';
-import { getMxIdLocalPart, getCanonicalAliasRoomId, isRoomAlias, mxcUrlToHttp } from '../utils/matrix';
+import {
+ getMxIdLocalPart,
+ getCanonicalAliasRoomId,
+ isRoomAlias,
+ mxcUrlToHttp,
+} from '../utils/matrix';
import { getMemberDisplayName } from '../utils/room';
import { EMOJI_PATTERN, URL_NEG_LB } from '../utils/regex';
import { getHexcodeForEmoji, getShortcodeFor } from './emoji';
};
export const makeMentionCustomProps = (
- handleMentionClick?: ReactEventHandler<HTMLElement>
+ handleMentionClick?: ReactEventHandler<HTMLElement>,
+ content?: string
): ComponentPropsWithoutRef<'a'> => ({
style: { cursor: 'pointer' },
target: '_blank',
tabIndex: handleMentionClick ? 0 : -1,
onKeyDown: handleMentionClick ? onEnterOrSpace(handleMentionClick) : undefined,
onClick: handleMentionClick,
+ children: content,
});
export const renderMatrixMention = (
className={css.Mention({ highlight: mx.getUserId() === userId })}
data-mention-id={userId}
>
- {`@${(currentRoom && getMemberDisplayName(currentRoom, userId)) ?? getMxIdLocalPart(userId)
- }`}
+ {`@${
+ (currentRoom && getMemberDisplayName(currentRoom, userId)) ?? getMxIdLocalPart(userId)
+ }`}
</a>
);
}
isRoomAlias(roomIdOrAlias) ? getCanonicalAliasRoomId(mx, roomIdOrAlias) : roomIdOrAlias
);
+ const fallbackContent = mentionRoom ? `#${mentionRoom.name}` : roomIdOrAlias;
+
return (
<a
href={href}
data-mention-id={mentionRoom?.roomId ?? roomIdOrAlias}
data-mention-via={viaServers?.join(',')}
>
- {mentionRoom ? `#${mentionRoom.name}` : roomIdOrAlias}
+ {customProps.children ? customProps.children : fallbackContent}
</a>
);
}
data-mention-event-id={eventId}
data-mention-via={viaServers?.join(',')}
>
- Message: {mentionRoom ? `#${mentionRoom.name}` : roomIdOrAlias}
+ {customProps.children
+ ? customProps.children
+ : `Message: ${mentionRoom ? `#${mentionRoom.name}` : roomIdOrAlias}`}
</a>
);
}
}
if (name === 'a' && testMatrixTo(tryDecodeURIComponent(props.href))) {
+ const content = children.find((child) => !(child instanceof DOMText))
+ ? undefined
+ : children.map((c) => (c instanceof DOMText ? c.data : '')).join();
+
const mention = renderMatrixMention(
mx,
roomId,
tryDecodeURIComponent(props.href),
- makeMentionCustomProps(params.handleMentionClick)
+ makeMentionCustomProps(params.handleMentionClick, content)
);
+
if (mention) return mention;
}