-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[menu-bar] Migrate AsyncStorage records to react-native-mmkv (#82)
* [menu-bar] Migrate AsyncStorage records to react-native-mmkv * Add changelog entry
- Loading branch information
1 parent
6056712
commit b19df9d
Showing
9 changed files
with
123 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import AsyncStorage from '@react-native-async-storage/async-storage'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
import { storage } from './Storage'; | ||
|
||
// TODO: Remove after v1.0.0 | ||
const migratedStorageKey = 'hasMigratedFromAsyncStorage'; | ||
|
||
interface Props { | ||
children: React.ReactElement; | ||
} | ||
|
||
export function PersistGate({ children }: Props) { | ||
const [hasMigrated, setHasMigrated] = useState(storage.getBoolean(migratedStorageKey) || false); | ||
|
||
useEffect(() => { | ||
async function migrateFromAsyncStorage() { | ||
const keys = await AsyncStorage.getAllKeys(); | ||
|
||
for (const key of keys) { | ||
const value = await AsyncStorage.getItem(key); | ||
|
||
if (value != null) { | ||
if (['true', 'false'].includes(value)) { | ||
storage.set(key, value === 'true'); | ||
} else { | ||
storage.set(key, value); | ||
} | ||
|
||
AsyncStorage.removeItem(key); | ||
} | ||
} | ||
|
||
storage.set(migratedStorageKey, true); | ||
setHasMigrated(true); | ||
} | ||
|
||
if (!hasMigrated) { | ||
migrateFromAsyncStorage(); | ||
} | ||
}, [hasMigrated]); | ||
|
||
if (!hasMigrated) { | ||
return null; | ||
} | ||
|
||
return children; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.