Refactor: ReportUtils from translateLocal to translate from hook (part 7)#95340
Refactor: ReportUtils from translateLocal to translate from hook (part 7)#95340shubham1206agra wants to merge 2 commits into
Conversation
|
@parasharrajat Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button] |
| * IMPORTANT: keep the English strings here in sync with the `iou.*` entries in `en.ts` and with the branching | ||
| * in {@link getReportPreviewMessage}. | ||
| */ | ||
| function getReportPreviewReportActionMessage(params: GetReportPreviewMessageBaseParams): string { |
There was a problem hiding this comment.
❌ CONSISTENCY-3 (docs)
getReportPreviewReportActionMessage duplicates almost the entire ~200-line body of getReportPreviewMessage. Both functions share identical control flow — the same guards and branches (isSplitBillReportAction, isTrackExpenseAction, isReportApproved, the whole settled/paid block, isWaitingOnBankAccount, the latest-expense amount branch, containsNonReimbursable, and the final "owes" fallback) — and the only real difference is that each translate('iou.*', ...) call is swapped for a hardcoded English string. This is exactly the kind of duplication that rots: the JSDoc itself warns "keep the English strings here in sync with the iou.* entries in en.ts and with the branching in getReportPreviewMessage", meaning every future change to the preview logic must be made twice or the two will silently diverge.
Consolidate the shared branching into a single implementation and vary only how the final string is produced. For example, keep one function that walks the branches and returns a structured result (a translation key + params, e.g. {key: 'iou.payerOwesAmount', params: [...]} / a discriminated union), then have the two public functions render it — the localized one via the injected translate, and the report-action one via a small English-only formatter — so the control flow lives in exactly one place:
type PreviewMessageResult = {key: TranslationPaths; params: unknown[]} | {raw: string};
function resolveReportPreviewMessage(params: GetReportPreviewMessageBaseParams): PreviewMessageResult {
// all the branching lives here, exactly once
}
function getReportPreviewMessage(translate: LocalizedTranslate, params: GetReportPreviewMessageBaseParams): string {
const result = resolveReportPreviewMessage(params);
return 'raw' in result ? result.raw : translate(result.key, ...result.params);
}
function getReportPreviewReportActionMessage(params: GetReportPreviewMessageBaseParams): string {
const result = resolveReportPreviewMessage(params);
return 'raw' in result ? result.raw : formatEnglish(result.key, ...result.params);
}This removes the second copy of the branching logic and the "keep in sync" burden entirely.
Reviewed at: fb69436 | Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.
Codecov Report❌ Looks like you've decreased code coverage for some files. Please write tests to increase, or at least maintain, the existing level of code coverage. See our documentation here for how to interpret this table.
|
Explanation of Change
Fixed Issues
$ #64943
Tests
Offline tests
Same as Tests
QA Steps
Same as Tests
PR Author Checklist
### Fixed Issuessection aboveTestssectionOffline stepssectionQA stepssectioncanBeMissingparam foruseOnyxtoggleReportand notonIconClick)src/languages/*files and using the translation methodSTYLE.md) were followedAvatar, I verified the components usingAvatarare working as expected)StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))npm run compress-svg)Avataris modified, I verified thatAvataris working as expected in all cases)Designlabel and/or tagged@Expensify/designso the design team can review the changes.ScrollViewcomponent to make it scrollable when more elements are added to the page.mainbranch was merged into this PR after a review, I tested again and verified the outcome was still expected according to theTeststeps.Screenshots/Videos
Screen.Recording.2026-07-05.at.11.49.05.PM.mov