Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DDW-814] Create LocalStorageFeatureProvider
- Loading branch information
1 parent
113fe12
commit 160452c
Showing
13 changed files
with
132 additions
and
55 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,20 +1,19 @@ | ||
// @flow | ||
import { localStorageBridge } from '../../../api/utils/localStorageBridge'; | ||
import { STORAGE_KEYS as storageKeys } from '../../../../../common/config/electron-store.config'; | ||
import type { LocalStorageApi } from '../../local-storage'; | ||
|
||
export class DiscreetModeApi { | ||
getDiscreetModeSettings = async (): Promise<boolean> => { | ||
const localStorage = await localStorageBridge(); | ||
return localStorage.get(storageKeys.DISCREET_MODE_ENABLED, false); | ||
}; | ||
localStorage: LocalStorageApi; | ||
constructor(localStorage: LocalStorageApi) { | ||
this.localStorage = localStorage; | ||
} | ||
|
||
setDiscreetModeSettings = async (enabled: boolean): Promise<void> => { | ||
const localStorage = await localStorageBridge(); | ||
localStorage.set(storageKeys.DISCREET_MODE_ENABLED, enabled); | ||
}; | ||
getDiscreetModeSettings = () => | ||
this.localStorage.get(storageKeys.DISCREET_MODE_ENABLED, false); | ||
|
||
unsetDiscreetModeSettings = async (): Promise<void> => { | ||
const localStorage = await localStorageBridge(); | ||
localStorage.unset(storageKeys.DISCREET_MODE_ENABLED); | ||
}; | ||
setDiscreetModeSettings = (enabled: boolean) => | ||
this.localStorage.set(storageKeys.DISCREET_MODE_ENABLED, enabled); | ||
|
||
unsetDiscreetModeSettings = async () => | ||
this.localStorage.unset(storageKeys.DISCREET_MODE_ENABLED); | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
// @flow | ||
|
||
export * from './discreet-mode'; | ||
export * from './local-storage'; |
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,42 @@ | ||
// @flow | ||
|
||
import React, { useState } from 'react'; | ||
import type { Node } from 'react'; | ||
import { merge } from 'lodash/fp'; | ||
import { getFeatureFromContext } from '../../utils/mobx-features/hooks'; | ||
|
||
import { LocalStorageApi } from './types'; | ||
|
||
export const localStorageContext = React.createContext<LocalStorageApi | null>( | ||
null | ||
); | ||
|
||
interface Props { | ||
children: Node; | ||
localStorage: LocalStorageApi; | ||
} | ||
|
||
export const LocalStorageFeatureProvider = ({ | ||
children, | ||
localStorage, | ||
}: Props) => { | ||
const [localStorageFeature] = useState<LocalStorageApi>(() => { | ||
window.daedalus = merge(window.daedalus, { | ||
features: { | ||
localStorage, | ||
}, | ||
}); | ||
|
||
return localStorage; | ||
}); | ||
|
||
return ( | ||
<localStorageContext.Provider value={localStorageFeature}> | ||
{children} | ||
</localStorageContext.Provider> | ||
); | ||
}; | ||
|
||
export function useLocalStorageFeature(): LocalStorageApi { | ||
return getFeatureFromContext(localStorageContext); | ||
} |
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,5 @@ | ||
// @flow | ||
|
||
export { useLocalStorageFeature, LocalStorageFeatureProvider } from './context'; | ||
export { BrowserLocalStorageBridge } from './ui'; | ||
export type { LocalStorageApi } from './types'; |
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,7 @@ | ||
// @flow | ||
|
||
export type LocalStorageApi = { | ||
get: (key: StorageKey, defaultValue: any) => boolean, | ||
set: (key: StorageKey, value: any) => Promise<void>, | ||
unset: (key: StorageKey) => Promise<void>, | ||
}; |
24 changes: 24 additions & 0 deletions
24
source/renderer/app/features/local-storage/ui/BrowserLocalStorageBridge.js
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,24 @@ | ||
// @flow | ||
|
||
import React from 'react'; | ||
import { LocalStorageFeatureProvider } from '../context'; | ||
|
||
type Props = { | ||
children: Node, | ||
}; | ||
|
||
export function BrowserLocalStorageBridge({ children }: Props) { | ||
return ( | ||
<LocalStorageFeatureProvider | ||
localStorage={{ | ||
get: (key: string, defaultValue: any) => | ||
Promise.resolve<any>(localStorage.getItem(key) || defaultValue), | ||
set: (key: string, value: any) => | ||
Promise.resolve(localStorage.setItem(key, value)), | ||
unset: (key: string) => Promise.resolve(localStorage.removeItem(key)), | ||
}} | ||
> | ||
{children} | ||
</LocalStorageFeatureProvider> | ||
); | ||
} |
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,3 @@ | ||
// @flow | ||
|
||
export { BrowserLocalStorageBridge } from './BrowserLocalStorageBridge'; |
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