Skip to content

Commit

Permalink
Merge pull request #40777 from ShridharGoel/save-downloads
Browse files Browse the repository at this point in the history
Save logs data in downloads and show meaningful path
  • Loading branch information
techievivek authored May 3, 2024
2 parents cb39a54 + 2cbb037 commit 0f39e43
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 12 deletions.
8 changes: 8 additions & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4699,6 +4699,14 @@ const CONST = {
MAX_TAX_RATE_INTEGER_PLACES: 4,
MAX_TAX_RATE_DECIMAL_PLACES: 4,

DOWNLOADS_PATH: '/Downloads',
NEW_EXPENSIFY_PATH: '/New Expensify',

ENVIRONMENT_SUFFIX: {
DEV: ' Dev',
ADHOC: ' AdHoc',
},

SEARCH_TRANSACTION_TYPE: {
CASH: 'cash',
CARD: 'card',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ type BaseClientSideLoggingToolProps = {
onDisableLogging: (logs: Log[]) => void;
/** Action to run when enabling logging */
onEnableLogging?: () => void;
/** Path used to display location of saved file */
displayPath?: string;
} & BaseClientSideLoggingToolMenuOnyxProps;

function BaseClientSideLoggingToolMenu({shouldStoreLogs, capturedLogs, file, onShareLogs, onDisableLogging, onEnableLogging}: BaseClientSideLoggingToolProps) {
function BaseClientSideLoggingToolMenu({shouldStoreLogs, capturedLogs, file, onShareLogs, onDisableLogging, onEnableLogging, displayPath}: BaseClientSideLoggingToolProps) {
const {translate} = useLocalize();

const onToggle = () => {
Expand Down Expand Up @@ -70,7 +72,7 @@ function BaseClientSideLoggingToolMenu({shouldStoreLogs, capturedLogs, file, onS
</TestToolRow>
{!!file && (
<>
<Text style={[styles.textLabelSupporting, styles.mb4]}>{`path: ${file.path}`}</Text>
<Text style={[styles.textLabelSupporting, styles.mb4]}>{`path: ${displayPath}`}</Text>
<TestToolRow title={translate('initialSettingsPage.debugConsole.logs')}>
<Button
small
Expand Down
2 changes: 2 additions & 0 deletions src/components/ClientSideLoggingToolMenu/index.android.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import RNFetchBlob from 'react-native-blob-util';
import Share from 'react-native-share';
import type {Log} from '@libs/Console';
import localFileCreate from '@libs/localFileCreate';
import CONST from '@src/CONST';
import BaseClientSideLoggingToolMenu from './BaseClientSideLoggingToolMenu';

function ClientSideLoggingToolMenu() {
Expand Down Expand Up @@ -38,6 +39,7 @@ function ClientSideLoggingToolMenu() {
onEnableLogging={() => setFile(undefined)}
onDisableLogging={createAndSaveFile}
onShareLogs={shareLogs}
displayPath={`${CONST.DOWNLOADS_PATH}/${file?.newFileName ?? ''}`}
/>
);
}
Expand Down
5 changes: 5 additions & 0 deletions src/components/ClientSideLoggingToolMenu/index.ios.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import React, {useState} from 'react';
import Share from 'react-native-share';
import useEnvironment from '@hooks/useEnvironment';
import type {Log} from '@libs/Console';
import getDownloadFolderPathSuffixForIOS from '@libs/getDownloadFolderPathSuffixForIOS';
import localFileCreate from '@libs/localFileCreate';
import CONST from '@src/CONST';
import BaseClientSideLoggingToolMenu from './BaseClientSideLoggingToolMenu';

function ClientSideLoggingToolMenu() {
const [file, setFile] = useState<{path: string; newFileName: string; size: number}>();
const {environment} = useEnvironment();

const createFile = (logs: Log[]) => {
localFileCreate('logs', JSON.stringify(logs, null, 2)).then((localFile) => {
Expand All @@ -28,6 +32,7 @@ function ClientSideLoggingToolMenu() {
onEnableLogging={() => setFile(undefined)}
onDisableLogging={createFile}
onShareLogs={shareLogs}
displayPath={`${CONST.NEW_EXPENSIFY_PATH}${getDownloadFolderPathSuffixForIOS(environment)}/${file?.newFileName ?? ''}`}
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@ import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import pkg from '../../../package.json';

type ProfilingToolMenuOnyxProps = {
type BaseProfilingToolMenuOnyxProps = {
isProfilingInProgress: OnyxEntry<boolean>;
};

type ProfilingToolMenuProps = ProfilingToolMenuOnyxProps;
type BaseProfilingToolMenuProps = {
/** Path used to save the file */
pathToBeUsed: string;
/** Path used to display location of saved file */
displayPath: string;
} & BaseProfilingToolMenuOnyxProps;

function formatBytes(bytes: number, decimals = 2) {
if (!+bytes) {
Expand All @@ -39,7 +44,9 @@ function formatBytes(bytes: number, decimals = 2) {
return `${parseFloat((bytes / k ** i).toFixed(dm))} ${sizes[i]}`;
}

function ProfilingToolMenu({isProfilingInProgress = false}: ProfilingToolMenuProps) {
const newFileName = `Profile_trace_for_${pkg.version}.cpuprofile`;

function BaseProfilingToolMenu({isProfilingInProgress = false, pathToBeUsed, displayPath}: BaseProfilingToolMenuProps) {
const styles = useThemeStyles();
const [pathIOS, setPathIOS] = useState('');
const [sharePath, setSharePath] = useState('');
Expand Down Expand Up @@ -90,8 +97,7 @@ function ProfilingToolMenu({isProfilingInProgress = false}: ProfilingToolMenuPro

// eslint-disable-next-line @lwc/lwc/no-async-await
const rename = async () => {
const newFileName = `Profile_trace_for_${pkg.version}.cpuprofile`;
const newFilePath = `${RNFS.DocumentDirectoryPath}/${newFileName}`;
const newFilePath = `${pathToBeUsed}/${newFileName}`;

try {
const fileExists = await RNFS.exists(newFilePath);
Expand All @@ -117,7 +123,7 @@ function ProfilingToolMenu({isProfilingInProgress = false}: ProfilingToolMenuPro
};

rename();
}, [pathIOS]);
}, [pathIOS, pathToBeUsed]);

const onDownloadProfiling = useCallback(() => {
// eslint-disable-next-line @lwc/lwc/no-async-await
Expand Down Expand Up @@ -153,7 +159,7 @@ function ProfilingToolMenu({isProfilingInProgress = false}: ProfilingToolMenuPro
</TestToolRow>
{!!pathIOS && (
<>
<Text style={[styles.textLabelSupporting, styles.mb4]}>{`path: ${pathIOS}`}</Text>
<Text style={[styles.textLabelSupporting, styles.mb4]}>{`path: ${displayPath}/${newFileName}`}</Text>
<TestToolRow title={translate('initialSettingsPage.troubleshoot.profileTrace')}>
<Button
small
Expand All @@ -167,10 +173,10 @@ function ProfilingToolMenu({isProfilingInProgress = false}: ProfilingToolMenuPro
);
}

ProfilingToolMenu.displayName = 'ProfilingToolMenu';
BaseProfilingToolMenu.displayName = 'BaseProfilingToolMenu';

export default withOnyx<ProfilingToolMenuProps, ProfilingToolMenuOnyxProps>({
export default withOnyx<BaseProfilingToolMenuProps, BaseProfilingToolMenuOnyxProps>({
isProfilingInProgress: {
key: ONYXKEYS.APP_PROFILING_IN_PROGRESS,
},
})(ProfilingToolMenu);
})(BaseProfilingToolMenu);
17 changes: 17 additions & 0 deletions src/components/ProfilingToolMenu/index.android.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react';
import RNFS from 'react-native-fs';
import CONST from '@src/CONST';
import BaseProfilingToolMenu from './BaseProfilingToolMenu';

function ProfilingToolMenu() {
return (
<BaseProfilingToolMenu
pathToBeUsed={RNFS.DownloadDirectoryPath}
displayPath={`${CONST.DOWNLOADS_PATH}`}
/>
);
}

ProfilingToolMenu.displayName = 'ProfilingToolMenu';

export default ProfilingToolMenu;
21 changes: 21 additions & 0 deletions src/components/ProfilingToolMenu/index.ios.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React from 'react';
import RNFS from 'react-native-fs';
import useEnvironment from '@hooks/useEnvironment';
import getDownloadFolderPathSuffixForIOS from '@libs/getDownloadFolderPathSuffixForIOS';
import CONST from '@src/CONST';
import BaseProfilingToolMenu from './BaseProfilingToolMenu';

function ProfilingToolMenu() {
const {environment} = useEnvironment();

return (
<BaseProfilingToolMenu
pathToBeUsed={RNFS.DocumentDirectoryPath}
displayPath={`${CONST.NEW_EXPENSIFY_PATH}${getDownloadFolderPathSuffixForIOS(environment)}`}
/>
);
}

ProfilingToolMenu.displayName = 'ProfilingToolMenu';

export default ProfilingToolMenu;
24 changes: 24 additions & 0 deletions src/libs/getDownloadFolderPathSuffixForIOS.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import CONST from '@src/CONST';

function getDownloadFolderPathSuffixForIOS(environment: string) {
let folderSuffix = '';

switch (environment) {
case CONST.ENVIRONMENT.PRODUCTION:
folderSuffix = '';
break;
case CONST.ENVIRONMENT.ADHOC:
folderSuffix = CONST.ENVIRONMENT_SUFFIX.ADHOC;
break;
case CONST.ENVIRONMENT.DEV:
folderSuffix = CONST.ENVIRONMENT_SUFFIX.DEV;
break;
default:
folderSuffix = '';
break;
}

return folderSuffix;
}

export default getDownloadFolderPathSuffixForIOS;

0 comments on commit 0f39e43

Please sign in to comment.