Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 1s
145 lines
4.3 KiB
TypeScript
145 lines
4.3 KiB
TypeScript
import type { IRoom } from '@rocket.chat/core-typings';
|
|
import { useLocalStorage } from '@rocket.chat/fuselage-hooks';
|
|
import type { ChannelMention, UserMention } from '@rocket.chat/gazzodown';
|
|
import { MarkupInteractionContext } from '@rocket.chat/gazzodown';
|
|
import { escapeRegExp } from '@rocket.chat/tools';
|
|
import { useLayout, useRouter, useUserPreference, useUserId, useUserCard } from '@rocket.chat/ui-contexts';
|
|
import type { UIEvent, ReactNode } from 'react';
|
|
import { useCallback, memo, useMemo } from 'react';
|
|
|
|
import { normalizeUsername } from '../../lib/utils/normalizeUsername';
|
|
import { detectEmoji } from '../lib/utils/detectEmoji';
|
|
import { fireGlobalEvent } from '../lib/utils/fireGlobalEvent';
|
|
import { useMessageListHighlights, useMessageListShowRealName } from './message/list/MessageListContext';
|
|
import { useGoToRoom } from '../views/room/hooks/useGoToRoom';
|
|
|
|
export type GazzodownTextProps = {
|
|
children: ReactNode;
|
|
mentions?: {
|
|
type?: 'user' | 'team';
|
|
_id: string;
|
|
username?: string;
|
|
name?: string;
|
|
}[];
|
|
channels?: Pick<IRoom, '_id' | 'name'>[];
|
|
searchText?: string;
|
|
};
|
|
|
|
const GazzodownText = ({ mentions, channels, searchText, children }: GazzodownTextProps) => {
|
|
const [userLanguage] = useLocalStorage('userLanguage', 'en');
|
|
|
|
const highlights = useMessageListHighlights();
|
|
const { triggerProps, openUserCard } = useUserCard();
|
|
|
|
const highlightRegex = useMemo(() => {
|
|
if (!highlights?.length) {
|
|
return;
|
|
}
|
|
|
|
// Due to unnecessary escaping in escapeRegExp, we need to remove the escape character for the following characters: - = ! :
|
|
// This is necessary because it was crashing the client due to Invalid regular expression error.
|
|
const alternatives = highlights.map(({ highlight }) => escapeRegExp(highlight).replace(/\\([-=!:])/g, '$1')).join('|');
|
|
const expression = `(?<=^|[\\p{P}\\p{Z}])(${alternatives})(?=$|[\\p{P}\\p{Z}])`;
|
|
|
|
return (): RegExp => new RegExp(expression, 'gmiu');
|
|
}, [highlights]);
|
|
|
|
const markRegex = useMemo(() => {
|
|
if (!searchText) {
|
|
return;
|
|
}
|
|
|
|
return (): RegExp => new RegExp(`(${searchText})(?![^<]*>)`, 'gi');
|
|
}, [searchText]);
|
|
|
|
const convertAsciiToEmoji = useUserPreference<boolean>('convertAsciiEmoji', true);
|
|
const useEmoji = useUserPreference<boolean>('useEmojis', true);
|
|
const useRealName = useMessageListShowRealName();
|
|
const ownUserId = useUserId();
|
|
const showMentionSymbol = Boolean(useUserPreference<boolean>('mentionsWithSymbol'));
|
|
|
|
const resolveUserMention = useCallback(
|
|
(mention: string) => {
|
|
if (mention === 'all' || mention === 'here') {
|
|
return undefined;
|
|
}
|
|
|
|
const filterUser = ({ username, type }: UserMention) => {
|
|
if (!username || type === 'team') return false;
|
|
return normalizeUsername(username) === normalizeUsername(mention);
|
|
};
|
|
const filterTeam = ({ name, type }: UserMention) => type === 'team' && name === mention;
|
|
|
|
return mentions?.find((mention) => filterUser(mention) || filterTeam(mention));
|
|
},
|
|
[mentions],
|
|
);
|
|
|
|
const onUserMentionClick = useCallback(
|
|
({ username }: UserMention) => {
|
|
if (!username) {
|
|
return;
|
|
}
|
|
|
|
return (event: UIEvent): void => {
|
|
event.stopPropagation();
|
|
openUserCard(event, username);
|
|
};
|
|
},
|
|
[openUserCard],
|
|
);
|
|
|
|
const goToRoom = useGoToRoom();
|
|
|
|
const { isEmbedded, isMobile } = useLayout();
|
|
|
|
const resolveChannelMention = useCallback((mention: string) => channels?.find(({ name }) => name === mention), [channels]);
|
|
|
|
const router = useRouter();
|
|
|
|
const onChannelMentionClick = useCallback(
|
|
({ _id: rid }: ChannelMention) =>
|
|
(event: UIEvent): void => {
|
|
if (isEmbedded) {
|
|
fireGlobalEvent('click-mention-link', {
|
|
path: router.buildRoutePath({
|
|
pattern: '/channel/:name/:tab?/:context?',
|
|
params: { name: rid },
|
|
}),
|
|
channel: rid,
|
|
});
|
|
}
|
|
|
|
event.stopPropagation();
|
|
goToRoom(rid);
|
|
},
|
|
[router, isEmbedded, goToRoom],
|
|
);
|
|
|
|
return (
|
|
<MarkupInteractionContext.Provider
|
|
value={{
|
|
detectEmoji,
|
|
highlightRegex,
|
|
markRegex,
|
|
resolveUserMention,
|
|
onUserMentionClick,
|
|
resolveChannelMention,
|
|
onChannelMentionClick,
|
|
convertAsciiToEmoji,
|
|
useEmoji,
|
|
useRealName,
|
|
isMobile,
|
|
ownUserId,
|
|
showMentionSymbol,
|
|
triggerProps,
|
|
language: userLanguage,
|
|
}}
|
|
>
|
|
{children}
|
|
</MarkupInteractionContext.Provider>
|
|
);
|
|
};
|
|
|
|
export default memo(GazzodownText);
|