Skip to content

Commit ec0e726

Browse files
committed
fix: replace @react-native-async-storage/async-storage with expo-file-system to fix build error
1 parent 3113c09 commit ec0e726

File tree

7 files changed

+51
-41
lines changed

7 files changed

+51
-41
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
"init-submodules": "git submodule update --init --recursive"
1919
},
2020
"dependencies": {
21-
"@react-native-async-storage/async-storage": "1.23.1",
2221
"@react-native-community/datetimepicker": "8.2.0",
2322
"@react-native-picker/picker": "2.9.0",
2423
"@react-navigation/bottom-tabs": "^7.2.0",

pnpm-lock.yaml

Lines changed: 0 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/constants/paths.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export const WIKI_SMALL_TEXT_STORE_CACHE_NAME = 'text-tiddlerStore.json';
2323
export const WIKI_SKINNY_TIDDLER_STORE_CACHE_NAME = 'skinny-tiddlerStore.json';
2424
export const WIKI_BINARY_TIDDLERS_LIST_CACHE_NAME = 'binaryTiddlersList.json';
2525
export const getWikiCacheFolderPath = (workspace: IWikiWorkspace) => `${fs.cacheDirectory ?? `${workspace.wikiFolderLocation}/cache/`}`;
26+
export const PERSIST_STORAGE_PATH = fs.documentDirectory === null ? undefined : `${fs.documentDirectory}persistStorage/`;
2627
/**
2728
* We download json to the cache folder (batch download as a single json is faster), then move it to the sqlite later.
2829
*/

src/store/config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import AsyncStorage from '@react-native-async-storage/async-storage';
21
import { ColorSchemeName } from 'react-native';
32
import { create } from 'zustand';
4-
import { createJSONStorage, devtools, persist } from 'zustand/middleware';
3+
import { devtools, persist } from 'zustand/middleware';
54
import { immer } from 'zustand/middleware/immer';
5+
import { expoFileSystemStorage } from '../utils/expoFileSystemStorage';
66

77
export interface ConfigState {
88
androidHardwareAcceleration?: boolean;
@@ -60,7 +60,7 @@ export const useConfigStore = create<ConfigState & ConfigActions>()(
6060
}),
6161
{
6262
name: 'config-storage',
63-
storage: createJSONStorage(() => AsyncStorage),
63+
storage: expoFileSystemStorage,
6464
},
6565
),
6666
)),

src/store/server.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/* eslint-disable @typescript-eslint/strict-boolean-expressions */
22
/* eslint-disable @typescript-eslint/prefer-nullish-coalescing */
3-
import AsyncStorage from '@react-native-async-storage/async-storage';
43
import { cloneDeep } from 'lodash';
54
import { create } from 'zustand';
6-
import { createJSONStorage, devtools, persist } from 'zustand/middleware';
5+
import { devtools, persist } from 'zustand/middleware';
76
import { immer } from 'zustand/middleware/immer';
7+
import { expoFileSystemStorage } from '../utils/expoFileSystemStorage';
88

99
export enum ServerStatus {
1010
disconnected = 'disconnected',
@@ -88,7 +88,7 @@ export const useServerStore = create<ServerState & ServerActions>()(
8888
}),
8989
{
9090
name: 'server-storage',
91-
storage: createJSONStorage(() => AsyncStorage),
91+
storage: expoFileSystemStorage,
9292
},
9393
),
9494
)),

src/store/workspace.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
/* eslint-disable @typescript-eslint/consistent-type-assertions */
22
/* eslint-disable @typescript-eslint/strict-boolean-expressions */
3-
import AsyncStorage from '@react-native-async-storage/async-storage';
43
import { cloneDeep, uniqBy } from 'lodash';
54
import { create } from 'zustand';
6-
import { createJSONStorage, devtools, persist } from 'zustand/middleware';
5+
import { devtools, persist } from 'zustand/middleware';
76
import { immer } from 'zustand/middleware/immer';
87
import { defaultTextBasedTiddlerFilter } from '../constants/filters';
98
import { WIKI_FOLDER_PATH } from '../constants/paths';
9+
import { expoFileSystemStorage } from '../utils/expoFileSystemStorage';
1010

1111
export interface IWikiWorkspace {
12+
/**
13+
* Allow reading file attachments in the workspace.
14+
*/
15+
allowReadFileAttachment?: boolean;
1216
/**
1317
* Enable quick load button on workspace list.
1418
* When click on button, will only load recent tiddlers, speed up loading time for huge wiki.
@@ -32,10 +36,6 @@ export interface IWikiWorkspace {
3236
* folder path for this wiki workspace
3337
*/
3438
wikiFolderLocation: string;
35-
/**
36-
* Allow reading file attachments in the workspace.
37-
*/
38-
allowReadFileAttachment?: boolean;
3939
}
4040
export interface IPageWorkspace {
4141
id: string;
@@ -184,7 +184,7 @@ export const useWorkspaceStore = create<WikiState & WikiActions>()(
184184
}),
185185
{
186186
name: 'wiki-storage',
187-
storage: createJSONStorage(() => AsyncStorage),
187+
storage: expoFileSystemStorage,
188188
},
189189
),
190190
)),

src/utils/expoFileSystemStorage.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import * as fs from 'expo-file-system';
2+
import { PersistStorage } from 'zustand/middleware/persist';
3+
4+
export type StorageValue<S> = S | null;
5+
6+
const storageDirectory = `${fs.documentDirectory}persistStorage/`;
7+
8+
const ensureDirectoryExists = async () => {
9+
const directoryInfo = await fs.getInfoAsync(storageDirectory);
10+
if (!directoryInfo.exists) {
11+
await fs.makeDirectoryAsync(storageDirectory, { intermediates: true });
12+
}
13+
};
14+
15+
export const expoFileSystemStorage: PersistStorage<unknown> = {
16+
getItem: async (name) => {
17+
await ensureDirectoryExists();
18+
const filePath = `${storageDirectory}${name}`;
19+
const fileInfo = await fs.getInfoAsync(filePath);
20+
if (!fileInfo.exists) {
21+
return undefined;
22+
}
23+
const content = await fs.readAsStringAsync(filePath);
24+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
25+
return JSON.parse(content);
26+
},
27+
setItem: async (name, value) => {
28+
await ensureDirectoryExists();
29+
const filePath = `${storageDirectory}${name}`;
30+
await fs.writeAsStringAsync(filePath, JSON.stringify(value));
31+
},
32+
removeItem: async (name) => {
33+
await ensureDirectoryExists();
34+
const filePath = `${storageDirectory}${name}`;
35+
await fs.deleteAsync(filePath);
36+
},
37+
};

0 commit comments

Comments
 (0)