-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
74d55cc
commit 884aeb7
Showing
10 changed files
with
409 additions
and
403 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
54 changes: 35 additions & 19 deletions
54
src/components/accessing-local-storage/access-local-storage.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,48 @@ | ||
import { createStorage, StorageType } from "@martinstark/storage-ts"; | ||
import { createStorage, StorageTS, StorageType } from "@martinstark/storage-ts"; | ||
import { useCallback, useEffect, useState } from "react"; | ||
|
||
type Schema = { | ||
username: string; | ||
audioinput?: string; | ||
audiooutput?: string; | ||
}; | ||
|
||
// Create a store of the desired type. If it is not available, | ||
// in-memory storage will be used as a fallback. | ||
const store = createStorage<Schema>({ | ||
type: StorageType.LOCAL, | ||
prefix: "id", | ||
silent: true, | ||
}); | ||
|
||
export function useStorage() { | ||
const [store, setStore] = useState<StorageTS<Schema>>(); | ||
|
||
useEffect(() => { | ||
// Create a store of the desired type. If it is not available, | ||
// in-memory storage will be used as a fallback. | ||
setStore( | ||
createStorage<Schema>({ | ||
type: StorageType.LOCAL, | ||
prefix: "id", | ||
silent: true, | ||
}) | ||
); | ||
}, []); | ||
|
||
type Key = keyof Schema; | ||
const readFromStorage = (key: keyof Schema): Schema[Key] | null => { | ||
return store.read(key); | ||
}; | ||
const readFromStorage = useCallback( | ||
(key: keyof Schema): Schema[Key] | null => { | ||
return store?.read(key); | ||
}, | ||
[store] | ||
); | ||
|
||
const writeToStorage = (key: keyof Schema, value: Schema[Key]): void => { | ||
store.write(key, value); | ||
}; | ||
const writeToStorage = useCallback( | ||
(key: keyof Schema, value: Schema[Key]): void => { | ||
store?.write(key, value); | ||
}, | ||
[store] | ||
); | ||
|
||
const clearStorage = (key: keyof Schema) => { | ||
store.delete(key); | ||
}; | ||
const removeFromStorage = useCallback( | ||
(key: keyof Schema) => { | ||
store?.delete(key); | ||
}, | ||
[store] | ||
); | ||
|
||
return { readFromStorage, writeToStorage, clearStorage }; | ||
return { readFromStorage, writeToStorage, removeFromStorage }; | ||
} |
Oops, something went wrong.