-
-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: store web panel open state in storage (#641)
* feat: store panel open state in storage * fix: use storage provided by user * fix: respect default value --------- Co-authored-by: Daniel Williams <[email protected]>
- Loading branch information
Showing
9 changed files
with
72 additions
and
13 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,21 @@ | ||
import type { FC, PropsWithChildren } from 'react'; | ||
import { createContext, useContext } from 'react'; | ||
|
||
interface Storage { | ||
getItem: (key: string) => Promise<string | null>; | ||
setItem: (key: string, value: string) => Promise<void>; | ||
} | ||
|
||
const StorageContext = createContext<Storage>({ | ||
getItem: async () => null, | ||
setItem: async () => {}, | ||
}); | ||
|
||
export const StorageProvider: FC<PropsWithChildren<{ storage: Storage }>> = ({ | ||
storage, | ||
children, | ||
}) => { | ||
return <StorageContext.Provider value={storage}>{children}</StorageContext.Provider>; | ||
}; | ||
|
||
export const useStorage = () => useContext(StorageContext); |
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
2 changes: 1 addition & 1 deletion
2
packages/react-native-ui/src/useExpanded.ts → .../react-native-ui/src/hooks/useExpanded.ts
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,27 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { useStorage } from '../StorageProvider'; | ||
|
||
export const useStoreBooleanState = ( | ||
key: string, | ||
defaultValue: boolean | ||
): ReturnType<typeof useState<boolean>> => { | ||
const storage = useStorage(); | ||
|
||
const [val, setVal] = useState<boolean>(defaultValue); | ||
|
||
useEffect(() => { | ||
storage.getItem(key).then((newVal) => { | ||
if (newVal === null || newVal === undefined) { | ||
setVal(defaultValue); | ||
} else { | ||
setVal(newVal === 'true'); | ||
} | ||
}); | ||
}, [key, storage, defaultValue]); | ||
|
||
useEffect(() => { | ||
storage.setItem(key, val.toString()); | ||
}, [key, storage, val]); | ||
|
||
return [val, setVal]; | ||
}; |
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