Skip to content

Commit

Permalink
Merge pull request #29012 from kubabutkiewicz/ts-migration/ReportUtil…
Browse files Browse the repository at this point in the history
…s-lib

[TS Migration] Migrate 'ReportUtils.js' lib to TypeScript
  • Loading branch information
nkuoch authored Nov 27, 2023
2 parents 090dc3a + aac1519 commit 40544eb
Show file tree
Hide file tree
Showing 21 changed files with 1,515 additions and 1,645 deletions.
6 changes: 3 additions & 3 deletions src/languages/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,17 @@ type DidSplitAmountMessageParams = {formattedAmount: string; comment: string};

type AmountEachParams = {amount: number};

type PayerOwesAmountParams = {payer: string; amount: number};
type PayerOwesAmountParams = {payer: string; amount: number | string};

type PayerOwesParams = {payer: string};

type PayerPaidAmountParams = {payer: string; amount: number};
type PayerPaidAmountParams = {payer: string; amount: number | string};

type ManagerApprovedParams = {manager: string};

type PayerPaidParams = {payer: string};

type PayerSettledParams = {amount: number};
type PayerSettledParams = {amount: number | string};

type WaitingOnBankAccountParams = {submitterDisplayName: string};

Expand Down
3 changes: 2 additions & 1 deletion src/libs/GroupChatUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ Onyx.connect({
/**
* Returns the report name if the report is a group chat
*/
function getGroupChatName(report: Report): string {
function getGroupChatName(report: Report): string | undefined {
const participants = report.participantAccountIDs ?? [];
const isMultipleParticipantReport = participants.length > 1;
const participantPersonalDetails = OptionsListUtils.getPersonalDetailsForAccountIDs(participants, allPersonalDetails ?? {});
// @ts-expect-error Error will gone when OptionsListUtils will be migrated to Typescript
const displayNamesWithTooltips = ReportUtils.getDisplayNamesWithTooltips(participantPersonalDetails, isMultipleParticipantReport);
return ReportUtils.getDisplayNamesStringFromTooltips(displayNamesWithTooltips);
}
Expand Down
5 changes: 3 additions & 2 deletions src/libs/IOUUtils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {OnyxEntry} from 'react-native-onyx';
import CONST from '@src/CONST';
import {Report, Transaction} from '@src/types/onyx';
import * as CurrencyUtils from './CurrencyUtils';
Expand Down Expand Up @@ -35,8 +36,8 @@ function calculateAmount(numberOfParticipants: number, total: number, currency:
*
* @param isDeleting - whether the user is deleting the request
*/
function updateIOUOwnerAndTotal(iouReport: Report, actorAccountID: number, amount: number, currency: string, isDeleting = false): Report {
if (currency !== iouReport.currency) {
function updateIOUOwnerAndTotal(iouReport: OnyxEntry<Report>, actorAccountID: number, amount: number, currency: string, isDeleting = false): OnyxEntry<Report> {
if (currency !== iouReport?.currency) {
return iouReport;
}

Expand Down
25 changes: 13 additions & 12 deletions src/libs/Permissions.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
import {OnyxEntry} from 'react-native-onyx';
import CONST from '@src/CONST';
import Beta from '@src/types/onyx/Beta';

function canUseAllBetas(betas: Beta[]): boolean {
return betas?.includes(CONST.BETAS.ALL);
function canUseAllBetas(betas: OnyxEntry<Beta[]>): boolean {
return !!betas?.includes(CONST.BETAS.ALL);
}

function canUseChronos(betas: Beta[]): boolean {
return betas?.includes(CONST.BETAS.CHRONOS_IN_CASH) || canUseAllBetas(betas);
function canUseChronos(betas: OnyxEntry<Beta[]>): boolean {
return !!betas?.includes(CONST.BETAS.CHRONOS_IN_CASH) || canUseAllBetas(betas);
}

function canUseDefaultRooms(betas: Beta[]): boolean {
return betas?.includes(CONST.BETAS.DEFAULT_ROOMS) || canUseAllBetas(betas);
function canUseDefaultRooms(betas: OnyxEntry<Beta[]>): boolean {
return !!betas?.includes(CONST.BETAS.DEFAULT_ROOMS) || canUseAllBetas(betas);
}

function canUseCommentLinking(betas: Beta[]): boolean {
return betas?.includes(CONST.BETAS.BETA_COMMENT_LINKING) || canUseAllBetas(betas);
function canUseCommentLinking(betas: OnyxEntry<Beta[]>): boolean {
return !!betas?.includes(CONST.BETAS.BETA_COMMENT_LINKING) || canUseAllBetas(betas);
}

/**
* We're requiring you to be added to the policy rooms beta on dev,
* since contributors have been reporting a number of false issues related to the feature being under development.
* See https://expensify.slack.com/archives/C01GTK53T8Q/p1641921996319400?thread_ts=1641598356.166900&cid=C01GTK53T8Q
*/
function canUsePolicyRooms(betas: Beta[]): boolean {
return betas?.includes(CONST.BETAS.POLICY_ROOMS) || canUseAllBetas(betas);
function canUsePolicyRooms(betas: OnyxEntry<Beta[]>): boolean {
return !!betas?.includes(CONST.BETAS.POLICY_ROOMS) || canUseAllBetas(betas);
}

function canUseViolations(betas: Beta[]): boolean {
return betas?.includes(CONST.BETAS.VIOLATIONS) || canUseAllBetas(betas);
function canUseViolations(betas: OnyxEntry<Beta[]>): boolean {
return !!betas?.includes(CONST.BETAS.VIOLATIONS) || canUseAllBetas(betas);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/libs/PolicyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import Str from 'expensify-common/lib/str';
import {OnyxCollection, OnyxEntry} from 'react-native-onyx';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import {PersonalDetails, Policy, PolicyMembers, PolicyTags} from '@src/types/onyx';
import {PersonalDetails, Policy, PolicyMembers, PolicyTag, PolicyTags} from '@src/types/onyx';
import {EmptyObject, isEmptyObject} from '@src/types/utils/EmptyObject';

type MemberEmailsToAccountIDs = Record<string, number>;
type PersonalDetailsList = Record<string, PersonalDetails>;
Expand Down Expand Up @@ -157,8 +158,8 @@ function getIneligibleInvitees(policyMembers: OnyxEntry<PolicyMembers>, personal
/**
* Gets the tag from policy tags, defaults to the first if no key is provided.
*/
function getTag(policyTags: OnyxEntry<PolicyTags>, tagKey?: keyof typeof policyTags) {
if (Object.keys(policyTags ?? {})?.length === 0) {
function getTag(policyTags: OnyxEntry<PolicyTags>, tagKey?: keyof typeof policyTags): PolicyTag | undefined | EmptyObject {
if (isEmptyObject(policyTags)) {
return {};
}

Expand Down
8 changes: 7 additions & 1 deletion src/libs/ReportActionsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import ONYXKEYS from '@src/ONYXKEYS';
import {ActionName} from '@src/types/onyx/OriginalMessage';
import Report from '@src/types/onyx/Report';
import ReportAction, {ReportActions} from '@src/types/onyx/ReportAction';
import {EmptyObject, isEmptyObject} from '@src/types/utils/EmptyObject';
import * as CollectionUtils from './CollectionUtils';
import * as Environment from './Environment/Environment';
import isReportMessageAttachment from './isReportMessageAttachment';
Expand Down Expand Up @@ -72,7 +73,10 @@ function isReversedTransaction(reportAction: OnyxEntry<ReportAction>) {
return (reportAction?.message?.[0]?.isReversedTransaction ?? false) && (reportAction?.childVisibleActionCount ?? 0) > 0;
}

function isPendingRemove(reportAction: OnyxEntry<ReportAction>): boolean {
function isPendingRemove(reportAction: OnyxEntry<ReportAction> | EmptyObject): boolean {
if (isEmptyObject(reportAction)) {
return false;
}
return reportAction?.message?.[0]?.moderationDecision?.decision === CONST.MODERATION.MODERATOR_DECISION_PENDING_REMOVE;
}

Expand Down Expand Up @@ -695,3 +699,5 @@ export {
getFirstVisibleReportActionID,
isChannelLogMemberAction,
};

export type {LastVisibleMessage};
Loading

0 comments on commit 40544eb

Please sign in to comment.