Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/pages/wallet/WalletStatementPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ import useNetwork from '@hooks/useNetwork';
import useOnyx from '@hooks/useOnyx';
import usePrevious from '@hooks/usePrevious';
import useThemePreference from '@hooks/useThemePreference';
import addEncryptedAuthTokenToURL from '@libs/addEncryptedAuthTokenToURL';
import * as Browser from '@libs/Browser';
import {getOldDotURLFromEnvironment} from '@libs/Environment/Environment';
import fileDownload from '@libs/fileDownload';
import Navigation from '@libs/Navigation/Navigation';
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
import addTrailingForwardSlash from '@libs/UrlUtils';
import type {WalletStatementNavigatorParamList} from '@navigation/types';
import {generateStatementPDF} from '@userActions/User';
import {emailSelector} from '@selectors/Session';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type SCREENS from '@src/SCREENS';
Expand All @@ -26,6 +29,7 @@ type WalletStatementPageProps = PlatformStackScreenProps<WalletStatementNavigato

function WalletStatementPage({route}: WalletStatementPageProps) {
const [walletStatement] = useOnyx(ONYXKEYS.WALLET_STATEMENT, {canBeMissing: true});
const [currentUserLogin] = useOnyx(ONYXKEYS.SESSION, {selector: emailSelector});
const themePreference = useThemePreference();
const yearMonth = route.params.yearMonth ?? null;
const isWalletStatementGenerating = walletStatement?.isGenerating ?? false;
Expand All @@ -51,17 +55,21 @@ function WalletStatementPage({route}: WalletStatementPageProps) {
}

setIsDownloading(true);
if (walletStatement?.[yearMonth]) {
// We already have a file URL for this statement, so we can download it immediately
if (walletStatement?.[yearMonth] && currentUserLogin) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Remove login gate before reusing cached statement URL

The new currentUserLogin check causes a retry loop when session.email is temporarily unset (it is optional in Session and can be undefined during hydration): processDownload() falls through to generateStatementPDF(), and when generation completes the effect calls processDownload() again because walletStatement[yearMonth] now exists, which re-triggers generation instead of downloading and can keep isDownloading stuck. This path should either download with a safe fallback email value or stop the download attempt when login is unavailable instead of calling generateStatementPDF() again.

Useful? React with 👍 / 👎.

// We already have a file URL for this statement, so we can download it immediately.
// Auth token and email are required so the hybrid app's native download manager can authenticate with the secure endpoint.
const downloadFileName = `Expensify_Statement_${yearMonth}.pdf`;
const fileName = walletStatement[yearMonth];
const pdfURL = `${baseURL}secure?secureType=pdfreport&filename=${fileName}&downloadName=${downloadFileName}`;
fileDownload(translate, pdfURL, downloadFileName).finally(() => setIsDownloading(false));
const pdfURL = `${baseURL}secure?secureType=pdfreport&filename=${encodeURIComponent(fileName)}&downloadName=${encodeURIComponent(downloadFileName)}&email=${encodeURIComponent(
currentUserLogin,
)}`;
const pdfURLWithAuth = addEncryptedAuthTokenToURL(pdfURL, true);
fileDownload(translate, pdfURLWithAuth, downloadFileName, '', Browser.isMobileSafari()).finally(() => setIsDownloading(false));
return;
}

generateStatementPDF(yearMonth);
}, [baseURL, isWalletStatementGenerating, translate, walletStatement, yearMonth]);
}, [baseURL, currentUserLogin, isWalletStatementGenerating, translate, walletStatement, yearMonth]);

// eslint-disable-next-line rulesdir/prefer-early-return
useEffect(() => {
Expand Down