Skip to content

Commit

Permalink
Remove isNotEmptyObject in favor of isEmptyObject
Browse files Browse the repository at this point in the history
  • Loading branch information
blazejkustra committed Jan 15, 2024
1 parent 86794fe commit 490236f
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 47 deletions.
6 changes: 3 additions & 3 deletions src/components/OfflineWithFeedback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import shouldRenderOffscreen from '@libs/shouldRenderOffscreen';
import CONST from '@src/CONST';
import type * as OnyxCommon from '@src/types/onyx/OnyxCommon';
import type ChildrenProps from '@src/types/utils/ChildrenProps';
import {isNotEmptyObject} from '@src/types/utils/EmptyObject';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import MessagesRow from './MessagesRow';

/**
Expand Down Expand Up @@ -82,10 +82,10 @@ function OfflineWithFeedback({
const StyleUtils = useStyleUtils();
const {isOffline} = useNetwork();

const hasErrors = isNotEmptyObject(errors ?? {});
const hasErrors = !isEmptyObject(errors ?? {});
// Some errors have a null message. This is used to apply opacity only and to avoid showing redundant messages.
const errorMessages = omitBy(errors, (e) => e === null);
const hasErrorMessages = isNotEmptyObject(errorMessages);
const hasErrorMessages = !isEmptyObject(errorMessages);
const isOfflinePendingAction = !!isOffline && !!pendingAction;
const isUpdateOrDeleteError = hasErrors && (pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE || pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE);
const isAddError = hasErrors && pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD;
Expand Down
48 changes: 24 additions & 24 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import type {Message, ReportActionBase, ReportActions} from '@src/types/onyx/Rep
import type {Receipt, WaypointCollection} from '@src/types/onyx/Transaction';
import type DeepValueOf from '@src/types/utils/DeepValueOf';
import type {EmptyObject} from '@src/types/utils/EmptyObject';
import {isEmptyObject, isNotEmptyObject} from '@src/types/utils/EmptyObject';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import type IconAsset from '@src/types/utils/IconAsset';
import * as CurrencyUtils from './CurrencyUtils';
import DateUtils from './DateUtils';
Expand Down Expand Up @@ -479,7 +479,7 @@ function getRootParentReport(report: OnyxEntry<Report> | undefined | EmptyObject
const parentReport = getReport(report?.parentReportID);

// Runs recursion to iterate a parent report
return getRootParentReport(isNotEmptyObject(parentReport) ? parentReport : null);
return getRootParentReport(!isEmptyObject(parentReport) ? parentReport : null);
}

function getPolicy(policyID: string): Policy | EmptyObject {
Expand Down Expand Up @@ -566,11 +566,11 @@ function isTaskReport(report: OnyxEntry<Report>): boolean {
* In this case, we have added the key to the report itself
*/
function isCanceledTaskReport(report: OnyxEntry<Report> | EmptyObject = {}, parentReportAction: OnyxEntry<ReportAction> | EmptyObject = {}): boolean {
if (isNotEmptyObject(parentReportAction) && (parentReportAction?.message?.[0]?.isDeletedParentAction ?? false)) {
if (!isEmptyObject(parentReportAction) && (parentReportAction?.message?.[0]?.isDeletedParentAction ?? false)) {
return true;
}

if (isNotEmptyObject(report) && report?.isDeletedParentAction) {
if (!isEmptyObject(report) && report?.isDeletedParentAction) {
return true;
}

Expand Down Expand Up @@ -1034,7 +1034,7 @@ function isExpenseRequest(report: OnyxEntry<Report>): boolean {
if (isThread(report)) {
const parentReportAction = ReportActionsUtils.getParentReportAction(report);
const parentReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${report?.parentReportID}`] ?? null;
return isExpenseReport(parentReport) && isNotEmptyObject(parentReportAction) && ReportActionsUtils.isTransactionThread(parentReportAction);
return isExpenseReport(parentReport) && !isEmptyObject(parentReportAction) && ReportActionsUtils.isTransactionThread(parentReportAction);
}
return false;
}
Expand All @@ -1047,7 +1047,7 @@ function isIOURequest(report: OnyxEntry<Report>): boolean {
if (isThread(report)) {
const parentReportAction = ReportActionsUtils.getParentReportAction(report);
const parentReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${report?.parentReportID}`] ?? null;
return isIOUReport(parentReport) && isNotEmptyObject(parentReportAction) && ReportActionsUtils.isTransactionThread(parentReportAction);
return isIOUReport(parentReport) && !isEmptyObject(parentReportAction) && ReportActionsUtils.isTransactionThread(parentReportAction);
}
return false;
}
Expand Down Expand Up @@ -1115,7 +1115,7 @@ function canDeleteReportAction(reportAction: OnyxEntry<ReportAction>, reportID:
// For now, users cannot delete split actions
const isSplitAction = reportAction?.originalMessage?.type === CONST.IOU.REPORT_ACTION_TYPE.SPLIT;

if (isSplitAction || isSettled(String(reportAction?.originalMessage?.IOUReportID)) || (isNotEmptyObject(report) && isReportApproved(report))) {
if (isSplitAction || isSettled(String(reportAction?.originalMessage?.IOUReportID)) || (!isEmptyObject(report) && isReportApproved(report))) {
return false;
}

Expand All @@ -1134,7 +1134,7 @@ function canDeleteReportAction(reportAction: OnyxEntry<ReportAction>, reportID:
}

const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID}`];
const isAdmin = policy?.role === CONST.POLICY.ROLE.ADMIN && isNotEmptyObject(report) && !isDM(report);
const isAdmin = policy?.role === CONST.POLICY.ROLE.ADMIN && !isEmptyObject(report) && !isDM(report);

return isActionOwner || isAdmin;
}
Expand Down Expand Up @@ -1611,7 +1611,7 @@ function getLastVisibleMessage(reportID: string | undefined, actionsToMerge: Rep
const lastVisibleAction = ReportActionsUtils.getLastVisibleAction(reportID ?? '', actionsToMerge);

// For Chat Report with deleted parent actions, let us fetch the correct message
if (ReportActionsUtils.isDeletedParentAction(lastVisibleAction) && isNotEmptyObject(report) && isChatReport(report)) {
if (ReportActionsUtils.isDeletedParentAction(lastVisibleAction) && !isEmptyObject(report) && isChatReport(report)) {
const lastMessageText = getDeletedParentActionMessageForChatReport(lastVisibleAction);
return {
lastMessageText,
Expand Down Expand Up @@ -1832,7 +1832,7 @@ function getTransactionDetails(transaction: OnyxEntry<Transaction>, createdDateF
const report = getReport(transaction?.reportID);
return {
created: TransactionUtils.getCreated(transaction, createdDateFormat),
amount: TransactionUtils.getAmount(transaction, isNotEmptyObject(report) && isExpenseReport(report)),
amount: TransactionUtils.getAmount(transaction, !isEmptyObject(report) && isExpenseReport(report)),
currency: TransactionUtils.getCurrency(transaction),
comment: TransactionUtils.getDescription(transaction),
merchant: TransactionUtils.getMerchant(transaction),
Expand Down Expand Up @@ -2023,7 +2023,7 @@ function getTransactionReportName(reportAction: OnyxEntry<ReportAction>): string
}

const transaction = TransactionUtils.getLinkedTransaction(reportAction);
if (!isNotEmptyObject(transaction)) {
if (isEmptyObject(transaction)) {
// Transaction data might be empty on app's first load, if so we fallback to Request
return Localize.translateLocal('iou.request');
}
Expand Down Expand Up @@ -2064,14 +2064,14 @@ function getReportPreviewMessage(
return reportActionMessage;
}

if (isNotEmptyObject(reportAction) && !isIOUReport(report) && reportAction && ReportActionsUtils.isSplitBillAction(reportAction)) {
if (!isEmptyObject(reportAction) && !isIOUReport(report) && reportAction && ReportActionsUtils.isSplitBillAction(reportAction)) {
// This covers group chats where the last action is a split bill action
const linkedTransaction = TransactionUtils.getLinkedTransaction(reportAction);
if (isEmptyObject(linkedTransaction)) {
return reportActionMessage;
}

if (isNotEmptyObject(linkedTransaction)) {
if (!isEmptyObject(linkedTransaction)) {
if (TransactionUtils.isReceiptBeingScanned(linkedTransaction)) {
return Localize.translateLocal('iou.receiptScanning');
}
Expand Down Expand Up @@ -2099,10 +2099,10 @@ function getReportPreviewMessage(
});
}

if (isNotEmptyObject(reportAction) && shouldConsiderReceiptBeingScanned && reportAction && ReportActionsUtils.isMoneyRequestAction(reportAction)) {
if (!isEmptyObject(reportAction) && shouldConsiderReceiptBeingScanned && reportAction && ReportActionsUtils.isMoneyRequestAction(reportAction)) {
const linkedTransaction = TransactionUtils.getLinkedTransaction(reportAction);

if (isNotEmptyObject(linkedTransaction) && TransactionUtils.hasReceipt(linkedTransaction) && TransactionUtils.isReceiptBeingScanned(linkedTransaction)) {
if (!isEmptyObject(linkedTransaction) && TransactionUtils.hasReceipt(linkedTransaction) && TransactionUtils.isReceiptBeingScanned(linkedTransaction)) {
return Localize.translateLocal('iou.receiptScanning');
}
}
Expand Down Expand Up @@ -2208,11 +2208,11 @@ function getReportName(report: OnyxEntry<Report>, policy: OnyxEntry<Policy> = nu
let formattedName: string | undefined;
const parentReportAction = ReportActionsUtils.getParentReportAction(report);
if (isChatThread(report)) {
if (isNotEmptyObject(parentReportAction) && ReportActionsUtils.isTransactionThread(parentReportAction)) {
if (!isEmptyObject(parentReportAction) && ReportActionsUtils.isTransactionThread(parentReportAction)) {
return getTransactionReportName(parentReportAction);
}

const isAttachment = ReportActionsUtils.isReportActionAttachment(isNotEmptyObject(parentReportAction) ? parentReportAction : null);
const isAttachment = ReportActionsUtils.isReportActionAttachment(!isEmptyObject(parentReportAction) ? parentReportAction : null);
const parentReportActionMessage = (parentReportAction?.message?.[0]?.text ?? '').replace(/(\r\n|\n|\r)/gm, ' ');
if (isAttachment && parentReportActionMessage) {
return `[${Localize.translateLocal('common.attachment')}]`;
Expand Down Expand Up @@ -2477,11 +2477,11 @@ function updateOptimisticParentReportAction(parentReportAction: OnyxEntry<Report
*/
function getOptimisticDataForParentReportAction(reportID: string, lastVisibleActionCreated: string, type: string, parentReportID = '', parentReportActionID = ''): OnyxUpdate | EmptyObject {
const report = getReport(reportID);
if (!report || !isNotEmptyObject(report)) {
if (!report || isEmptyObject(report)) {
return {};
}
const parentReportAction = ReportActionsUtils.getParentReportAction(report);
if (!parentReportAction || !isNotEmptyObject(parentReportAction)) {
if (!parentReportAction || isEmptyObject(parentReportAction)) {
return {};
}

Expand Down Expand Up @@ -2625,7 +2625,7 @@ function getIOUReportActionMessage(iouReportID: string, type: string, total: num
const report = getReport(iouReportID);
const amount =
type === CONST.IOU.REPORT_ACTION_TYPE.PAY
? CurrencyUtils.convertToDisplayString(getMoneyRequestReimbursableTotal(isNotEmptyObject(report) ? report : null), currency)
? CurrencyUtils.convertToDisplayString(getMoneyRequestReimbursableTotal(!isEmptyObject(report) ? report : null), currency)
: CurrencyUtils.convertToDisplayString(total, currency);

let paymentMethodMessage;
Expand Down Expand Up @@ -2919,7 +2919,7 @@ function buildOptimisticReportPreview(
childReportID: childReportID ?? iouReport?.reportID,
childMoneyRequestCount: 1,
childLastMoneyRequestComment: comment,
childRecentReceiptTransactionIDs: hasReceipt && isNotEmptyObject(transaction) ? {[transaction?.transactionID ?? '']: created} : undefined,
childRecentReceiptTransactionIDs: hasReceipt && !isEmptyObject(transaction) ? {[transaction?.transactionID ?? '']: created} : undefined,
whisperedToAccountIDs: isReceiptBeingScanned ? [currentUserAccountID ?? -1] : [],
};
}
Expand Down Expand Up @@ -3418,7 +3418,7 @@ function canAccessReport(report: OnyxEntry<Report>, policies: OnyxCollection<Pol
*/
function shouldHideReport(report: OnyxEntry<Report>, currentReportId: string): boolean {
const currentReport = getReport(currentReportId);
const parentReport = getParentReport(isNotEmptyObject(currentReport) ? currentReport : null);
const parentReport = getParentReport(!isEmptyObject(currentReport) ? currentReport : null);
const reportActions = ReportActionsUtils.getAllReportActions(report?.reportID ?? '');
const isChildReportHasComment = Object.values(reportActions ?? {})?.some((reportAction) => (reportAction?.childVisibleActionCount ?? 0) > 0);
return parentReport?.reportID !== report?.reportID && !isChildReportHasComment;
Expand Down Expand Up @@ -3592,7 +3592,7 @@ function canFlagReportAction(reportAction: OnyxEntry<ReportAction>, reportID: st
reportAction?.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT &&
!ReportActionsUtils.isDeletedAction(reportAction) &&
!ReportActionsUtils.isCreatedTaskReportAction(reportAction) &&
isNotEmptyObject(report) &&
!isEmptyObject(report) &&
report &&
isAllowedToComment(report),
);
Expand Down Expand Up @@ -4215,7 +4215,7 @@ function getIOUReportActionDisplayMessage(reportAction: OnyxEntry<ReportAction>)
}

const transaction = TransactionUtils.getTransaction(originalMessage.IOUTransactionID ?? '');
const transactionDetails = getTransactionDetails(isNotEmptyObject(transaction) ? transaction : null);
const transactionDetails = getTransactionDetails(!isEmptyObject(transaction) ? transaction : null);
const formattedAmount = CurrencyUtils.convertToDisplayString(transactionDetails?.amount ?? 0, transactionDetails?.currency);
const isRequestSettled = isSettled(originalMessage.IOUReportID);
const isApproved = isReportApproved(iouReport);
Expand Down
14 changes: 7 additions & 7 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import type Report from '@src/types/onyx/Report';
import type {Message, ReportActionBase, ReportActions} from '@src/types/onyx/ReportAction';
import type ReportAction from '@src/types/onyx/ReportAction';
import type {EmptyObject} from '@src/types/utils/EmptyObject';
import {isEmptyObject, isNotEmptyObject} from '@src/types/utils/EmptyObject';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import * as Session from './Session';
import * as Welcome from './Welcome';

Expand Down Expand Up @@ -321,7 +321,7 @@ function addActions(reportID: string, text = '', file?: File) {

const report = ReportUtils.getReport(reportID);

if (isNotEmptyObject(report) && ReportUtils.getReportNotificationPreference(report) === CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN) {
if (!isEmptyObject(report) && ReportUtils.getReportNotificationPreference(report) === CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN) {
optimisticReport.notificationPreference = CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS;
}

Expand Down Expand Up @@ -424,7 +424,7 @@ function addActions(reportID: string, text = '', file?: File) {

// Update optimistic data for parent report action if the report is a child report
const optimisticParentReportData = ReportUtils.getOptimisticDataForParentReportAction(reportID, currentTime, CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD);
if (isNotEmptyObject(optimisticParentReportData)) {
if (!isEmptyObject(optimisticParentReportData)) {
optimisticData.push(optimisticParentReportData);
}

Expand Down Expand Up @@ -574,7 +574,7 @@ function openReport(
}

// If we are creating a new report, we need to add the optimistic report data and a report action
if (isNotEmptyObject(newReportObject)) {
if (!isEmptyObject(newReportObject)) {
// Change the method to set for new reports because it doesn't exist yet, is faster,
// and we need the data to be available when we navigate to the chat page
optimisticData[0].onyxMethod = Onyx.METHOD.SET;
Expand Down Expand Up @@ -1192,7 +1192,7 @@ function deleteReportComment(reportID: string, reportAction: ReportAction) {
optimisticReport?.lastVisibleActionCreated ?? '',
CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE,
);
if (isNotEmptyObject(optimisticParentReportData)) {
if (!isEmptyObject(optimisticParentReportData)) {
optimisticData.push(optimisticParentReportData);
}
}
Expand Down Expand Up @@ -1392,7 +1392,7 @@ function updateNotificationPreference(
report: OnyxEntry<Report> | EmptyObject = {},
) {
if (previousValue === newValue) {
if (navigate && isNotEmptyObject(report) && report.reportID) {
if (navigate && !isEmptyObject(report) && report.reportID) {
ReportUtils.goBackToDetailsPage(report);
}
return;
Expand Down Expand Up @@ -1435,7 +1435,7 @@ function updateNotificationPreference(
const parameters: UpdateReportNotificationPreferenceParameters = {reportID, notificationPreference: newValue};

API.write('UpdateReportNotificationPreference', parameters, {optimisticData, failureData});
if (navigate && isNotEmptyObject(report)) {
if (navigate && !isEmptyObject(report)) {
ReportUtils.goBackToDetailsPage(report);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/libs/actions/Task.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import * as UserUtils from '@libs/UserUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import {isNotEmptyObject} from '@src/types/utils/EmptyObject';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import * as Report from './Report';

let currentUserEmail;
Expand Down Expand Up @@ -183,7 +183,7 @@ function createTaskAndNavigate(parentReportID, title, description, assigneeEmail

// If needed, update optimistic data for parent report action of the parent report.
const optimisticParentReportData = ReportUtils.getOptimisticDataForParentReportAction(parentReportID, currentTime, CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD);
if (isNotEmptyObject(optimisticParentReportData)) {
if (!isEmptyObject(optimisticParentReportData)) {
optimisticData.push(optimisticParentReportData);
}

Expand Down Expand Up @@ -794,7 +794,7 @@ function deleteTask(taskReportID, taskTitle, originalStateNum, originalStatusNum
parentReport.lastVisibleActionCreated || '',
CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE,
);
if (isNotEmptyObject(optimisticParentReportData)) {
if (!isEmptyObject(optimisticParentReportData)) {
optimisticData.push(optimisticParentReportData);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/pages/home/report/withReportAndReportActionOrNotFound.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import NotFoundPage from '@pages/ErrorPage/NotFoundPage';
import * as Report from '@userActions/Report';
import ONYXKEYS from '@src/ONYXKEYS';
import type * as OnyxTypes from '@src/types/onyx';
import {isEmptyObject, isNotEmptyObject} from '@src/types/utils/EmptyObject';
import {isEmptyObject} from '@src/types/utils/EmptyObject';

type OnyxProps = {
/** The report currently being looked at */
Expand Down Expand Up @@ -60,7 +60,7 @@ export default function <TProps extends ComponentProps, TRef>(WrappedComponent:
// For small screen, we don't call openReport API when we go to a sub report page by deeplink
// So we need to call openReport here for small screen
useEffect(() => {
if (!props.isSmallScreenWidth || (isNotEmptyObject(props.report) && isNotEmptyObject(reportAction))) {
if (!props.isSmallScreenWidth || (!isEmptyObject(props.report) && !isEmptyObject(reportAction))) {
return;
}
Report.openReport(props.route.params.reportID);
Expand Down
9 changes: 1 addition & 8 deletions src/types/utils/EmptyObject.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import type Falsy from './Falsy';

type EmptyObject = Record<string, never>;

type EmptyValue = EmptyObject | null | undefined;

// eslint-disable-next-line rulesdir/no-negated-variables
function isNotEmptyObject<T extends Record<string, unknown> | Falsy>(arg: T | EmptyObject): arg is NonNullable<T> {
return Object.keys(arg ?? {}).length > 0;
}

function isEmptyObject<T>(obj: T | EmptyValue): obj is EmptyValue {
return Object.keys(obj ?? {}).length === 0;
}

export {isNotEmptyObject, isEmptyObject};
export {isEmptyObject};
export type {EmptyObject};

0 comments on commit 490236f

Please sign in to comment.