Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Mentions v2] Parse <mention-report> when displaying reportAction #39906

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ function BaseHTMLEngineProvider({textSelectable = false, children, enableExperim
contentModel: HTMLContentModel.textual,
}),
'mention-user': HTMLElementModel.fromCustomModel({tagName: 'mention-user', contentModel: HTMLContentModel.textual}),
'mention-report': HTMLElementModel.fromCustomModel({tagName: 'mention-report', contentModel: HTMLContentModel.textual}),
'mention-here': HTMLElementModel.fromCustomModel({tagName: 'mention-here', contentModel: HTMLContentModel.textual}),
'next-step': HTMLElementModel.fromCustomModel({
tagName: 'next-step',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import isEmpty from 'lodash/isEmpty';
import React from 'react';
import type {TextStyle} from 'react-native';
import {StyleSheet} from 'react-native';
import type {OnyxCollection} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import type {CustomRendererProps, TPhrasing, TText} from 'react-native-render-html';
import {ShowContextMenuContext} from '@components/ShowContextMenuContext';
import Text from '@components/Text';
import useCurrentReportID from '@hooks/useCurrentReportID';
import useStyleUtils from '@hooks/useStyleUtils';
import useThemeStyles from '@hooks/useThemeStyles';
import Navigation from '@navigation/Navigation';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Route} from '@src/ROUTES';

Check failure on line 16 in src/components/HTMLEngineProvider/HTMLRenderers/MentionRoomRenderer.tsx

View workflow job for this annotation

GitHub Actions / Run ESLint

'Route' is defined but never used
import ROUTES from '@src/ROUTES';
import type {Report} from '@src/types/onyx';
import {isEmptyObject} from '@src/types/utils/EmptyObject';

type RoomMentionOnyxProps = {
/** All reports shared with the user */
reports: OnyxCollection<Report>;
};

type MentionRoomRendererProps = RoomMentionOnyxProps & CustomRendererProps<TText | TPhrasing>;

function MentionRoomRenderer({style, tnode, TDefaultRenderer, reports, ...defaultRendererProps}: MentionRoomRendererProps) {
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const htmlAttributeReportID = tnode.attributes.reportid;
const currentReportID = useCurrentReportID();

let reportID: number | undefined;
let mentionDisplayText: string;

const removeLeadingLTRAndHash = (value: string) => value.replace(CONST.UNICODE.LTR, '').slice(1);
war-in marked this conversation as resolved.
Show resolved Hide resolved

if (!isEmpty(htmlAttributeReportID)) {
const report = reports?.['report_'.concat(htmlAttributeReportID)];

reportID = report?.reportID ? parseInt(report.reportID, 10) : undefined;
war-in marked this conversation as resolved.
Show resolved Hide resolved
mentionDisplayText = report?.reportName ?? report?.displayName ?? htmlAttributeReportID;
} else if ('data' in tnode && !isEmptyObject(tnode.data)) {
mentionDisplayText = removeLeadingLTRAndHash(tnode.data);

const currentReport = reports?.['report_'.concat(currentReportID?.currentReportID ?? '')];

// eslint-disable-next-line rulesdir/prefer-early-return
Object.values(reports ?? {}).forEach((report) => {
if (report?.policyID === currentReport?.policyID && removeLeadingLTRAndHash(report?.reportName ?? '') === mentionDisplayText) {
reportID = Number(report?.reportID);
}
});
} else {
return null;
}
war-in marked this conversation as resolved.
Show resolved Hide resolved

const navigationRoute = reportID ? ROUTES.REPORT_WITH_ID.getRoute(String(reportID)) : undefined;
const isCurrentRoomMention = String(reportID) === currentReportID?.currentReportID;

const flattenStyle = StyleSheet.flatten(style as TextStyle);
const {color, ...styleWithoutColor} = flattenStyle;

return (
<ShowContextMenuContext.Consumer>
{() => (
<Text
// eslint-disable-next-line react/jsx-props-no-spreading
{...defaultRendererProps}
style={[styles.link, styleWithoutColor, StyleUtils.getMentionStyle(isCurrentRoomMention), {color: StyleUtils.getMentionTextColor(isCurrentRoomMention)}]}
suppressHighlighting
onPress={(event) => {
event.preventDefault();

if (navigationRoute) {
Navigation.navigate(navigationRoute);
}
}}
role={CONST.ROLE.LINK}
accessibilityLabel={`/${navigationRoute}`}
>
#{mentionDisplayText}
</Text>
)}
</ShowContextMenuContext.Consumer>
);
}

MentionRoomRenderer.displayName = 'MentionRoomRenderer';

export default withOnyx<MentionRoomRendererProps, RoomMentionOnyxProps>({
reports: {
key: ONYXKEYS.COLLECTION.REPORT,
},
war-in marked this conversation as resolved.
Show resolved Hide resolved
})(MentionRoomRenderer);
2 changes: 2 additions & 0 deletions src/components/HTMLEngineProvider/HTMLRenderers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import EditedRenderer from './EditedRenderer';
import EmojiRenderer from './EmojiRenderer';
import ImageRenderer from './ImageRenderer';
import MentionHereRenderer from './MentionHereRenderer';
import MentionRoomRenderer from './MentionRoomRenderer';
import MentionUserRenderer from './MentionUserRenderer';
import NextStepEmailRenderer from './NextStepEmailRenderer';
import PreRenderer from './PreRenderer';
Expand All @@ -25,6 +26,7 @@ const HTMLEngineProviderComponentList: CustomTagRendererRecord = {
pre: PreRenderer,
/* eslint-disable @typescript-eslint/naming-convention */
'mention-user': MentionUserRenderer,
'mention-report': MentionRoomRenderer,
'mention-here': MentionHereRenderer,
emoji: EmojiRenderer,
'next-step-email': NextStepEmailRenderer,
Expand Down
Loading