-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(local-storage): disconnect wallet properly (#156)
* fix(local-storage): fix wallet auto reconnecting on disconnect * fix(select-wallet): remove loading state * fix(eslint): unused rule * fix(ord-context): openModal, closeModal * refactor(select-wallet): move to useCallback * style(ord-context): dot * fix(ord-context): don't override initial value
- Loading branch information
Showing
5 changed files
with
115 additions
and
79 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
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,51 @@ | ||
import { Dispatch, SetStateAction, useCallback, useState } from "react"; | ||
|
||
const KEY_PREFIX = "ord-connect"; | ||
|
||
// Helper function to get item from localStorage | ||
function getItemFromLocalStorage<T>(_key: string): T | null { | ||
const key = `${KEY_PREFIX}_${_key}`; | ||
try { | ||
return JSON.parse(localStorage.getItem(key)) as T | null; | ||
} catch (error) { | ||
console.error(`Error retrieving ${key} from localStorage`, error); | ||
return null; | ||
} | ||
} | ||
|
||
// Helper function to set item to localStorage | ||
function setItemToLocalStorage<T>(_key: string, value: T) { | ||
const key = `${KEY_PREFIX}_${_key}`; | ||
try { | ||
if (value) { | ||
localStorage.setItem(key, JSON.stringify(value)); | ||
} else { | ||
localStorage.removeItem(key); | ||
} | ||
} catch (error) { | ||
console.error(`Error saving ${key} to localStorage`, error); | ||
} | ||
} | ||
|
||
export function useLocalStorage<T>( | ||
key: string, | ||
initialValue: T, | ||
): [T, Dispatch<SetStateAction<T>>] { | ||
const [state, setInnerState] = useState<T>(() => { | ||
const value = getItemFromLocalStorage<T>(key); | ||
if (!value) { | ||
setItemToLocalStorage(key, initialValue); | ||
} | ||
return value; | ||
}); | ||
|
||
const setState = useCallback( | ||
(newValue: T) => { | ||
setItemToLocalStorage(key, newValue); | ||
setInnerState(newValue); | ||
}, | ||
[key], | ||
); | ||
|
||
return [state, setState]; | ||
} |
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