Skip to content

Commit

Permalink
Merge pull request #56549 from Expensify/cmartins-useTranslateLive
Browse files Browse the repository at this point in the history
Cmartins use translate live
  • Loading branch information
roryabraham authored Feb 7, 2025
2 parents af3bb87 + 1e735f1 commit 74485da
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/components/SelectionList/ChatListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import variables from '@styles/variables';
import CONST from '@src/CONST';
import BaseListItem from './BaseListItem';
import type {ChatListItemProps, ListItem, ReportActionListItemType} from './types';

Check failure on line 19 in src/components/SelectionList/ChatListItem.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Delete `';⏎import·useTranslateLive·from·'@hooks/useTranslateLive`

Check failure on line 19 in src/components/SelectionList/ChatListItem.tsx

View workflow job for this annotation

GitHub Actions / ESLint check

Delete `';⏎import·useTranslateLive·from·'@hooks/useTranslateLive`
import useTranslateLive from '@hooks/useTranslateLive';

Check failure on line 20 in src/components/SelectionList/ChatListItem.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

`@hooks/useTranslateLive` import should occur before import of `./BaseListItem`

Check failure on line 20 in src/components/SelectionList/ChatListItem.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

'useTranslateLive' is defined but never used

Check failure on line 20 in src/components/SelectionList/ChatListItem.tsx

View workflow job for this annotation

GitHub Actions / ESLint check

`@hooks/useTranslateLive` import should occur before import of `./BaseListItem`

Check failure on line 20 in src/components/SelectionList/ChatListItem.tsx

View workflow job for this annotation

GitHub Actions / ESLint check

'useTranslateLive' is defined but never used

function ChatListItem<TItem extends ListItem>({
item,
Expand Down Expand Up @@ -140,6 +141,8 @@ function ChatListItem<TItem extends ListItem>({
{reportActionItem.message.map((fragment, index) => (
<ReportActionItemFragment
// eslint-disable-next-line react/no-array-index-key
reportID={item.reportID}
reportAction={reportActionItem}

Check failure on line 145 in src/components/SelectionList/ChatListItem.tsx

View workflow job for this annotation

GitHub Actions / typecheck

Type 'ReportActionListItemType' is not assignable to type 'string | undefined'.
key={`actionFragment-${reportActionItem.reportActionID}-${index}`}

Check failure on line 146 in src/components/SelectionList/ChatListItem.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Do not use Array index in keys

Check failure on line 146 in src/components/SelectionList/ChatListItem.tsx

View workflow job for this annotation

GitHub Actions / ESLint check

Do not use Array index in keys
fragment={fragment}
actionName={reportActionItem.actionName}
Expand Down
39 changes: 39 additions & 0 deletions src/hooks/useTranslateLive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import ONYXKEYS from '@src/ONYXKEYS';

Check failure on line 1 in src/hooks/useTranslateLive.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Delete `import·ONYXKEYS·from·'@src/ONYXKEYS';⏎`

Check failure on line 1 in src/hooks/useTranslateLive.ts

View workflow job for this annotation

GitHub Actions / ESLint check

Delete `import·ONYXKEYS·from·'@src/ONYXKEYS';⏎`
import {useEffect} from 'react';
import Onyx, {useOnyx} from 'react-native-onyx';
import type {OnyxEntry, ReportAction} from 'react-native-onyx';

Check failure on line 4 in src/hooks/useTranslateLive.ts

View workflow job for this annotation

GitHub Actions / typecheck

Module '"react-native-onyx"' has no exported member 'ReportAction'. Did you mean to use 'import ReportAction from "react-native-onyx"' instead?
import * as API from '@libs/API';

Check failure on line 5 in src/hooks/useTranslateLive.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Namespace imports from @libs are not allowed. Use named imports instead. Example: import { method } from "@libs/module"

Check failure on line 5 in src/hooks/useTranslateLive.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Do not call API directly outside of actions methods. Only actions should make API requests

Check failure on line 5 in src/hooks/useTranslateLive.ts

View workflow job for this annotation

GitHub Actions / ESLint check

Do not call API directly outside of actions methods. Only actions should make API requests
import useLocalize from './useLocalize';

Check failure on line 6 in src/hooks/useTranslateLive.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Insert `ONYXKEYS·from·'@src/ONYXKEYS';⏎import·`

Check failure on line 6 in src/hooks/useTranslateLive.ts

View workflow job for this annotation

GitHub Actions / ESLint check

Insert `ONYXKEYS·from·'@src/ONYXKEYS';⏎import·`

export default function useTranslateLive(reportID: string, reportAction: OnyxEntry<ReportAction>) {
const {preferredLocale} = useLocalize();
const [session] = useOnyx(ONYXKEYS.SESSION);

useEffect(() => {
if (reportAction.message.length > 1) {

Check failure on line 13 in src/hooks/useTranslateLive.ts

View workflow job for this annotation

GitHub Actions / ESLint check

Unsafe member access .message on an `any` value
return;
}

if (reportAction.actorAccountID === session?.accountID) {

Check failure on line 17 in src/hooks/useTranslateLive.ts

View workflow job for this annotation

GitHub Actions / ESLint check

Unsafe member access .actorAccountID on an `any` value
return;
}

if (!reportAction?.message?.[0]?.html) {
return;
}

console.log('over here API call')
API.makeRequestWithSideEffects('Translate', {

Check failure on line 26 in src/hooks/useTranslateLive.ts

View workflow job for this annotation

GitHub Actions / typecheck

Argument of type '"Translate"' is not assignable to parameter of type 'SideEffectRequestCommand'.
type: 'live',
textToTranslate: reportAction?.message?.[0]?.html,
targetLanguage: preferredLocale,
}).then((response) => {
console.log('over here', response)
Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, {
[reportAction.reportActionID]: {
message: [{...reportAction?.message[0], translatedText: response.translation}]

Check failure on line 34 in src/hooks/useTranslateLive.ts

View workflow job for this annotation

GitHub Actions / typecheck

Property 'translation' does not exist on type 'void | Response'.
}
});
});
}, [reportID, reportAction.reportActionID]);

Check warning on line 38 in src/hooks/useTranslateLive.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

React Hook useEffect has missing dependencies: 'preferredLocale', 'reportAction.actorAccountID', 'reportAction.message', and 'session?.accountID'. Either include them or remove the dependency array

Check warning on line 38 in src/hooks/useTranslateLive.ts

View workflow job for this annotation

GitHub Actions / ESLint check

React Hook useEffect has missing dependencies: 'preferredLocale', 'reportAction.actorAccountID', 'reportAction.message', and 'session?.accountID'. Either include them or remove the dependency array
}
16 changes: 12 additions & 4 deletions src/pages/home/report/ReportActionItemFragment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import type ReportActionName from '@src/types/onyx/ReportActionName';
import AttachmentCommentFragment from './comment/AttachmentCommentFragment';
import TextCommentFragment from './comment/TextCommentFragment';
import ReportActionItemMessageHeaderSender from './ReportActionItemMessageHeaderSender';
import useTranslateLive from '@hooks/useTranslateLive';
import type {ReportAction} from '@src/types/onyx';

type ReportActionItemFragmentProps = {
/** Users accountID */
Expand Down Expand Up @@ -63,6 +65,9 @@ type ReportActionItemFragmentProps = {
actionName?: ReportActionName;

moderationDecision?: DecisionName;

reportAction?: string;
reportID?: string;
};

const MUTED_ACTIONS = [
Expand All @@ -76,6 +81,8 @@ const MUTED_ACTIONS = [
] as ReportActionName[];

function ReportActionItemFragment({
reportID,
reportAction,
pendingAction,
actionName,
fragment,
Expand All @@ -96,6 +103,7 @@ function ReportActionItemFragment({
const styles = useThemeStyles();
const {isOffline} = useNetwork();
const {translate} = useLocalize();
useTranslateLive(reportID, reportAction);

switch (fragment?.type) {
case 'COMMENT': {
Expand All @@ -117,7 +125,7 @@ function ReportActionItemFragment({
return (
<AttachmentCommentFragment
source={source}
html={fragment?.html ?? ''}
html={fragment?.translatedText ?? fragment?.html ?? ''}
addExtraMargin={!displayAsGroup}
styleAsDeleted={!!(isOffline && isPendingDelete)}
/>
Expand All @@ -143,7 +151,7 @@ function ReportActionItemFragment({
numberOfLines={isSingleLine ? 1 : undefined}
style={[styles.chatItemMessage, styles.colorMuted]}
>
{isFragmentContainingDisplayName ? convertToLTR(fragment?.text ?? '') : fragment?.text}
{isFragmentContainingDisplayName ? convertToLTR(fragment?.translatedText ?? fragment?.text ?? '') : fragment?.text}
</Text>
);
}
Expand All @@ -154,7 +162,7 @@ function ReportActionItemFragment({
numberOfLines={isSingleLine ? 1 : undefined}
style={[styles.chatItemMessage]}
>
{isFragmentContainingDisplayName ? convertToLTR(fragment?.text ?? '') : fragment?.text}
{isFragmentContainingDisplayName ? convertToLTR(fragment?.translatedText ?? fragment?.text ?? '') : fragment?.text}
</Text>
);
}
Expand All @@ -163,7 +171,7 @@ function ReportActionItemFragment({
<ReportActionItemMessageHeaderSender
accountID={accountID}
delegateAccountID={delegateAccountID}
fragmentText={fragment.text}
fragmentText={fragment?.translatedText ?? fragment.text}
actorIcon={actorIcon}
isSingleLine={isSingleLine}
/>
Expand Down
2 changes: 2 additions & 0 deletions src/pages/home/report/ReportActionItemMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ function ReportActionItemMessage({action, displayAsGroup, reportID, style, isHid
<ReportActionItemFragment
/* eslint-disable-next-line react/no-array-index-key */
key={`actionFragment-${action.reportActionID}-${index}`}
reportID={reportID}
reportAction={action}
fragment={fragment}
iouMessage={iouMessage}
isThreadParentMessage={isThreadParentMessage(action, reportID)}
Expand Down
2 changes: 2 additions & 0 deletions src/pages/home/report/ReportActionItemSingle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ function ReportActionItemSingle({
{personArray?.map((fragment, index) => (
<ReportActionItemFragment
// eslint-disable-next-line react/no-array-index-key
reportID={reportID}
reportAction={action}
key={`person-${action?.reportActionID}-${index}`}
accountID={Number(delegatePersonalDetails && !isWorkspaceActor ? actorAccountID : icon.id ?? CONST.DEFAULT_NUMBER_ID)}
fragment={{...fragment, type: fragment.type ?? '', text: fragment.text ?? ''}}
Expand Down
9 changes: 5 additions & 4 deletions src/pages/home/report/comment/TextCommentFragment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ type TextCommentFragmentProps = {
function TextCommentFragment({fragment, styleAsDeleted, styleAsMuted = false, source, style, displayAsGroup, iouMessage = ''}: TextCommentFragmentProps) {
const theme = useTheme();
const styles = useThemeStyles();
const {html = ''} = fragment ?? {};
const text = ReportActionsUtils.getTextFromHtml(html);
const {translatedText = null, html = ''} = fragment ?? {};
const displayText = translatedText ?? html;
const text = ReportActionsUtils.getTextFromHtml(displayText);
const {translate} = useLocalize();
const {shouldUseNarrowLayout} = useResponsiveLayout();

Expand All @@ -67,9 +68,9 @@ function TextCommentFragment({fragment, styleAsDeleted, styleAsMuted = false, so
// on other device, only render it as text if the only difference is <br /> tag
const containsOnlyEmojis = EmojiUtils.containsOnlyEmojis(text ?? '');
const containsEmojis = CONST.REGEX.ALL_EMOJIS.test(text ?? '');
if (!shouldRenderAsText(html, text ?? '') && !(containsOnlyEmojis && styleAsDeleted)) {
if (!shouldRenderAsText(displayText, text ?? '') && !(containsOnlyEmojis && styleAsDeleted)) {
const editedTag = fragment?.isEdited ? `<edited ${styleAsDeleted ? 'deleted' : ''}></edited>` : '';
const htmlWithDeletedTag = styleAsDeleted ? `<del>${html}</del>` : html;
const htmlWithDeletedTag = styleAsDeleted ? `<del>${displayText}</del>` : displayText;

let htmlContent = htmlWithDeletedTag;
if (containsOnlyEmojis) {
Expand Down
3 changes: 3 additions & 0 deletions src/types/onyx/ReportAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ type Message = {
/** The html content of the fragment. */
html?: string;

/** Translate text */
translatedText?: string;

/** Used to apply additional styling. Style refers to a predetermined constant and not a class name. e.g. 'normal'
* or 'strong'
*/
Expand Down

0 comments on commit 74485da

Please sign in to comment.