Skip to content

Commit

Permalink
Merge pull request #53370 from callstack-internal/fix/import-onyx-sta…
Browse files Browse the repository at this point in the history
…te-on-ios

fix: Import Onyx state on iOS
  • Loading branch information
Gonals authored Dec 10, 2024
2 parents 9fe5511 + 337361c commit 7f2c689
Showing 1 changed file with 10 additions and 28 deletions.
38 changes: 10 additions & 28 deletions src/components/ImportOnyxState/index.native.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, {useState} from 'react';
import RNFS from 'react-native-fs';
import ReactNativeBlobUtil from 'react-native-blob-util';
import Onyx from 'react-native-onyx';
import type {FileObject} from '@components/AttachmentModal';
import {KEYS_TO_PRESERVE, setIsUsingImportedState} from '@libs/actions/App';
Expand All @@ -13,33 +13,15 @@ import {cleanAndTransformState} from './utils';

const CHUNK_SIZE = 100;

function readFileInChunks(fileUri: string, chunkSize = 1024 * 1024) {
function readOnyxFile(fileUri: string) {
const filePath = decodeURIComponent(fileUri.replace('file://', ''));

return RNFS.exists(filePath)
.then((exists) => {
if (!exists) {
throw new Error('File does not exist');
}
return RNFS.stat(filePath);
})
.then((fileStats) => {
const fileSize = fileStats.size;
let fileContent = '';
const promises = [];

// Chunk the file into smaller parts to avoid memory issues
for (let i = 0; i < fileSize; i += chunkSize) {
promises.push(RNFS.read(filePath, chunkSize, i, 'utf8').then((chunk) => chunk));
}

// After all chunks have been read, join them together
return Promise.all(promises).then((chunks) => {
fileContent = chunks.join('');

return fileContent;
});
});
return ReactNativeBlobUtil.fs.exists(filePath).then((exists) => {
if (!exists) {
throw new Error('File does not exist');
}
return ReactNativeBlobUtil.fs.readFile(filePath, 'utf8');
});
}

function chunkArray<T>(array: T[], size: number): T[][] {
Expand Down Expand Up @@ -72,8 +54,8 @@ export default function ImportOnyxState({setIsLoading, isLoading}: ImportOnyxSta
}

setIsLoading(true);
readFileInChunks(file.uri)
.then((fileContent) => {
readOnyxFile(file.uri)
.then((fileContent: string) => {
const transformedState = cleanAndTransformState<OnyxValues>(fileContent);
setShouldForceOffline(true);
Onyx.clear(KEYS_TO_PRESERVE).then(() => {
Expand Down

0 comments on commit 7f2c689

Please sign in to comment.