Skip to content
16 changes: 11 additions & 5 deletions src/pages/workspace/accounting/PolicyAccountingPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, {useMemo, useRef, useState} from 'react';
import {formatDistanceToNow} from 'date-fns';
import React, {useEffect, useMemo, useRef, useState} from 'react';
import {ActivityIndicator, View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
Expand Down Expand Up @@ -109,13 +110,16 @@ function PolicyAccountingPage({policy, connectionSyncProgress}: PolicyAccounting
const {isSmallScreenWidth, windowWidth} = useWindowDimensions();
const [threeDotsMenuPosition, setThreeDotsMenuPosition] = useState<AnchorPosition>({horizontal: 0, vertical: 0});
const [isDisconnectModalOpen, setIsDisconnectModalOpen] = useState(false);
const [datetimeToRelative, setDateTimeToRelative] = useState('');
const threeDotsMenuContainerRef = useRef<View>(null);

const isSyncInProgress = !!connectionSyncProgress?.stageInProgress && connectionSyncProgress.stageInProgress !== CONST.POLICY.CONNECTIONS.SYNC_STAGE_NAME.JOB_DONE;

const accountingIntegrations = Object.values(CONST.POLICY.CONNECTIONS.NAME).filter((name) => !(name === CONST.POLICY.CONNECTIONS.NAME.XERO && !canUseXeroIntegration));
const connectedIntegration = accountingIntegrations.find((integration) => !!policy?.connections?.[integration]) ?? connectionSyncProgress?.connectionName;
const policyID = policy?.id ?? '';
const successfulDate = policy?.connections?.quickbooksOnline?.lastSync?.successfulDate;
const formattedDate = useMemo(() => (successfulDate ? new Date(successfulDate) : new Date()), [successfulDate]);

const policyConnectedToXero = connectedIntegration === CONST.POLICY.CONNECTIONS.NAME.XERO;

Expand All @@ -140,6 +144,10 @@ function PolicyAccountingPage({policy, connectionSyncProgress}: PolicyAccounting
[translate, policyID, isOffline],
);

useEffect(() => {
setDateTimeToRelative(formatDistanceToNow(formattedDate, {addSuffix: true}));
}, [formattedDate]);

const connectionsMenuItems: MenuItemProps[] = useMemo(() => {
if (isEmptyObject(policy?.connections) && !isSyncInProgress) {
return accountingIntegrations.map((integration) => {
Expand Down Expand Up @@ -168,10 +176,7 @@ function PolicyAccountingPage({policy, connectionSyncProgress}: PolicyAccounting
wrapperStyle: [styles.sectionMenuItemTopDescription],
shouldShowRightComponent: true,
title: integrationData?.title,

description: isSyncInProgress
? translate('workspace.accounting.connections.syncStageName', connectionSyncProgress.stageInProgress)
: translate('workspace.accounting.lastSync'),
description: isSyncInProgress ? translate('workspace.accounting.connections.syncStageName', connectionSyncProgress.stageInProgress) : datetimeToRelative,
rightComponent: isSyncInProgress ? (
<ActivityIndicator
style={[styles.popoverMenuIcon]}
Expand Down Expand Up @@ -257,6 +262,7 @@ function PolicyAccountingPage({policy, connectionSyncProgress}: PolicyAccounting
theme.spinner,
threeDotsMenuPosition,
translate,
datetimeToRelative,
accountingIntegrations,
]);

Expand Down