Skip to content

Commit

Permalink
distinction between web and native way of storing Onyx data taken int…
Browse files Browse the repository at this point in the history
…o account
  • Loading branch information
burczu committed Jun 13, 2024
1 parent 95e118b commit fca2b12
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 30 deletions.
19 changes: 19 additions & 0 deletions src/libs/ExportOnyxState/index.native.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {open} from 'react-native-quick-sqlite';

const readFromIndexedDB = () => new Promise((resolve) => {
const db = open({name: 'OnyxDB'});
const query = 'SELECT * FROM keyvaluepairs';

db.executeAsync(query, []).then(({rows}) => {
// eslint-disable-next-line no-underscore-dangle
const result = rows?._array.map((row) => ({[row.record_key]: JSON.parse(row.valueJSON as string)}));

resolve(result);
});

db.close();
});

export default {
readFromIndexedDB,
}
31 changes: 31 additions & 0 deletions src/libs/ExportOnyxState/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const readFromIndexedDB = () => new Promise((resolve) => {
let db: IDBDatabase;
const openRequest = indexedDB.open('OnyxDB', 1);
openRequest.onsuccess = () => {
db = openRequest.result;
const transaction = db.transaction('keyvaluepairs');
const objectStore = transaction.objectStore('keyvaluepairs');
const cursor = objectStore.openCursor();

const queryResult: Record<string, unknown> = {};

cursor.onerror = () => {
console.error('Error reading cursor');
}

cursor.onsuccess = (event) => {
const { result } = event.target as IDBRequest<IDBCursorWithValue>;
if (result) {
queryResult[result.primaryKey as string] = result.value;
result.continue();
}
else {
resolve(queryResult);
}
}
};
});

export default {
readFromIndexedDB,
}
33 changes: 3 additions & 30 deletions src/pages/settings/Troubleshoot/TroubleshootPage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, {useMemo, useState} from 'react';
import {View} from 'react-native';
import Onyx, {useOnyx, withOnyx} from 'react-native-onyx';
import Onyx, {withOnyx} from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
import type {SvgProps} from 'react-native-svg';
import ClientSideLoggingToolMenu from '@components/ClientSideLoggingToolMenu';
Expand Down Expand Up @@ -31,6 +31,7 @@ import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import SCREENS from '@src/SCREENS';
import getLightbulbIllustrationStyle from './getLightbulbIllustrationStyle';
import ExportOnyxState from '@libs/ExportOnyxState';

Check failure on line 34 in src/pages/settings/Troubleshoot/TroubleshootPage.tsx

View workflow job for this annotation

GitHub Actions / Run ESLint

`@libs/ExportOnyxState` import should occur before import of `./getLightbulbIllustrationStyle`

type BaseMenuItem = {
translationKey: TranslationPaths;
Expand Down Expand Up @@ -95,36 +96,8 @@ function TroubleshootPage({shouldStoreLogs}: TroubleshootPageProps) {
return Promise.all(promises);
};

const readFromIndexedDB = () => new Promise((resolve) => {
let db: IDBDatabase;
const openRequest = indexedDB.open('OnyxDB', 1);
openRequest.onsuccess = () => {
db = openRequest.result;
const transaction = db.transaction('keyvaluepairs');
const objectStore = transaction.objectStore('keyvaluepairs');
const cursor = objectStore.openCursor();

const queryResult: Record<string, unknown> = {};

cursor.onerror = () => {
console.error('Error reading cursor');
}

cursor.onsuccess = (event) => {
const { result } = event.target as IDBRequest<IDBCursorWithValue>;
if (result) {
queryResult[result.primaryKey as string] = result.value;
result.continue();
}
else {
resolve(queryResult);
}
}
};
});

const exportOnyxState = () => {
readFromIndexedDB().then((value) => {
ExportOnyxState.readFromIndexedDB().then((value) => {
console.log('exported indexedDB state: ', value);

Check failure on line 101 in src/pages/settings/Troubleshoot/TroubleshootPage.tsx

View workflow job for this annotation

GitHub Actions / Run ESLint

Unexpected console statement
});

Expand Down

0 comments on commit fca2b12

Please sign in to comment.