|
| 1 | +import type {OnyxEntry} from 'react-native-onyx'; |
| 2 | +import Onyx from 'react-native-onyx'; |
| 3 | +import {write} from '@libs/API'; |
| 4 | +import {WRITE_COMMANDS} from '@libs/API/types'; |
| 5 | +import CONST from '@src/CONST'; |
| 6 | +import ONYXKEYS from '@src/ONYXKEYS'; |
| 7 | +import type ExportDownload from '@src/types/onyx/ExportDownload'; |
| 8 | +import type {AnyOnyxUpdate} from '@src/types/onyx/Request'; |
| 9 | + |
| 10 | +function sendExportFileFromConcierge(exportID: string, exportDownload: OnyxEntry<ExportDownload>) { |
| 11 | + const onyxKey = `${ONYXKEYS.COLLECTION.EXPORT_DOWNLOAD}${exportID}` as const; |
| 12 | + |
| 13 | + const optimisticData: AnyOnyxUpdate[] = [ |
| 14 | + { |
| 15 | + onyxMethod: Onyx.METHOD.MERGE, |
| 16 | + key: onyxKey, |
| 17 | + value: {shouldSendFromConcierge: true}, |
| 18 | + }, |
| 19 | + ]; |
| 20 | + |
| 21 | + const failureData: AnyOnyxUpdate[] = [ |
| 22 | + { |
| 23 | + onyxMethod: Onyx.METHOD.MERGE, |
| 24 | + key: onyxKey, |
| 25 | + value: {shouldSendFromConcierge: exportDownload?.shouldSendFromConcierge ?? null}, |
| 26 | + }, |
| 27 | + ]; |
| 28 | + |
| 29 | + write(WRITE_COMMANDS.SEND_EXPORT_FILE_FROM_CONCIERGE, {exportID}, {optimisticData, failureData}); |
| 30 | +} |
| 31 | + |
| 32 | +function clearExportDownload(exportID: string, exportDownload: OnyxEntry<ExportDownload>) { |
| 33 | + const onyxKey = `${ONYXKEYS.COLLECTION.EXPORT_DOWNLOAD}${exportID}` as const; |
| 34 | + |
| 35 | + const optimisticData: AnyOnyxUpdate[] = [ |
| 36 | + { |
| 37 | + onyxMethod: Onyx.METHOD.SET, |
| 38 | + key: onyxKey, |
| 39 | + value: null, |
| 40 | + }, |
| 41 | + ]; |
| 42 | + |
| 43 | + const failureData: AnyOnyxUpdate[] = [ |
| 44 | + { |
| 45 | + onyxMethod: Onyx.METHOD.SET, |
| 46 | + key: onyxKey, |
| 47 | + value: exportDownload ?? null, |
| 48 | + }, |
| 49 | + ]; |
| 50 | + |
| 51 | + write(WRITE_COMMANDS.CLEAR_EXPORT_DOWNLOAD, {exportID}, {optimisticData, failureData}); |
| 52 | +} |
| 53 | + |
| 54 | +function clearStaleExportDownloads() { |
| 55 | + // Uses connectWithoutView instead of useOnyx to avoid subscribing the caller component |
| 56 | + // to the entire collection, which would cause unnecessary re-renders on every change. |
| 57 | + const connectionID = Onyx.connectWithoutView({ |
| 58 | + key: ONYXKEYS.COLLECTION.EXPORT_DOWNLOAD, |
| 59 | + waitForCollectionCallback: true, |
| 60 | + callback: (exportDownloads) => { |
| 61 | + Onyx.disconnect(connectionID); |
| 62 | + if (!exportDownloads) { |
| 63 | + return; |
| 64 | + } |
| 65 | + for (const key of Object.keys(exportDownloads)) { |
| 66 | + const exportDownload = exportDownloads[key]; |
| 67 | + if (!exportDownload || exportDownload.state === CONST.EXPORT_DOWNLOAD.STATE.PREPARING) { |
| 68 | + continue; |
| 69 | + } |
| 70 | + const exportID = key.replace(ONYXKEYS.COLLECTION.EXPORT_DOWNLOAD, ''); |
| 71 | + clearExportDownload(exportID, exportDownload); |
| 72 | + } |
| 73 | + }, |
| 74 | + }); |
| 75 | +} |
| 76 | + |
| 77 | +export {sendExportFileFromConcierge, clearExportDownload, clearStaleExportDownloads}; |
0 commit comments