1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-18 09:35:20 +02:00
joplin/packages/app-mobile/components/screens/ConfigScreen/NoteExportSection/NoteExportButton.tsx

84 lines
2.6 KiB
TypeScript
Raw Normal View History

2023-07-18 15:58:06 +02:00
import * as React from 'react';
import { _ } from '@joplin/lib/locale';
import Logger from '@joplin/utils/Logger';
import { FunctionComponent } from 'react';
2023-07-18 15:58:06 +02:00
import shim from '@joplin/lib/shim';
import { join } from 'path';
import Share from 'react-native-share';
import exportAllFolders from './utils/exportAllFolders';
2023-07-18 15:58:06 +02:00
import { ExportProgressState } from '@joplin/lib/services/interop/types';
import { ConfigScreenStyles } from '../configScreenStyles';
import makeImportExportCacheDirectory from './utils/makeImportExportCacheDirectory';
import TaskButton, { OnProgressCallback, SetAfterCompleteListenerCallback, TaskStatus } from './TaskButton';
2023-07-18 15:58:06 +02:00
const logger = Logger.create('NoteExportButton');
interface Props {
styles: ConfigScreenStyles;
}
export const exportButtonDefaultTitle = () => _('Export all notes as JEX');
export const exportButtonDescription = () => _('Share a copy of all notes in a file format that can be imported by Joplin on a computer.');
const getTitle = (taskStatus: TaskStatus) => {
if (taskStatus === TaskStatus.InProgress) {
return _('Exporting...');
} else {
return exportButtonDefaultTitle();
}
};
2023-07-18 15:58:06 +02:00
const runExportTask = async (
onProgress: OnProgressCallback,
setAfterCompleteListener: SetAfterCompleteListenerCallback,
) => {
const exportTargetPath = join(await makeImportExportCacheDirectory(), 'jex-export.jex');
logger.info(`Exporting all folders to path ${exportTargetPath}`);
2023-07-18 15:58:06 +02:00
setAfterCompleteListener(async (success: boolean) => {
if (success) {
2023-07-18 15:58:06 +02:00
await Share.open({
type: 'application/jex',
filename: 'export.jex',
url: `file://${exportTargetPath}`,
failOnCancel: false,
});
}
await shim.fsDriver().remove(exportTargetPath);
});
// Initially, undetermined progress
onProgress(undefined);
const status = await exportAllFolders(exportTargetPath, (status, progress) => {
if (progress !== null) {
onProgress(progress);
} else if (status === ExportProgressState.Closing || status === ExportProgressState.QueuingItems) {
// We don't have a numeric progress value and the closing/queuing state may take a while.
// Set a special progress value:
onProgress(undefined);
}
});
2023-07-18 15:58:06 +02:00
onProgress(1);
2023-07-18 15:58:06 +02:00
logger.info('Export complete');
2023-07-18 15:58:06 +02:00
return { warnings: status.warnings, success: true };
};
2023-07-18 15:58:06 +02:00
const NoteExportButton: FunctionComponent<Props> = props => {
return (
<TaskButton
taskName={exportButtonDefaultTitle()}
buttonLabel={getTitle}
finishedLabel={_('Exported successfully!')}
description={exportButtonDescription()}
styles={props.styles}
onRunTask={runExportTask}
/>
);
2023-07-18 15:58:06 +02:00
};
export default NoteExportButton;