-
-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #241 from apotdevin/transaction-filters
chore: transaction filters
- Loading branch information
Showing
10 changed files
with
249 additions
and
39 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
export const useLocalStorage = <T,>( | ||
key: string, | ||
initialValue: T | ||
): [T, (value: T) => void] => { | ||
const readValue = () => { | ||
if (typeof window === 'undefined') { | ||
return initialValue; | ||
} | ||
|
||
try { | ||
const item = window.localStorage.getItem(key); | ||
return item ? JSON.parse(item) : initialValue; | ||
} catch (error) { | ||
console.warn(`Error reading localStorage key “${key}”:`, error); | ||
return initialValue; | ||
} | ||
}; | ||
|
||
const [storedValue, setStoredValue] = useState<T>(readValue); | ||
|
||
const setValue = (value: T) => { | ||
if (typeof window == 'undefined') { | ||
console.warn( | ||
`Tried setting localStorage key “${key}” even though environment is not a client` | ||
); | ||
} | ||
|
||
try { | ||
const newValue = value instanceof Function ? value(storedValue) : value; | ||
window.localStorage.setItem(key, JSON.stringify(newValue)); | ||
|
||
setStoredValue(newValue); | ||
|
||
// We dispatch a custom event so every useLocalStorage hook are notified | ||
window.dispatchEvent(new Event('local-storage')); | ||
} catch (error) { | ||
console.warn(`Error setting localStorage key “${key}”:`, error); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
setStoredValue(readValue()); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
useEffect(() => { | ||
const handleStorageChange = () => { | ||
setStoredValue(readValue()); | ||
}; | ||
|
||
// this only works for other documents, not the current one | ||
window.addEventListener('storage', handleStorageChange); | ||
|
||
// this is a custom event, triggered in writeValueToLocalStorage | ||
window.addEventListener('local-storage', handleStorageChange); | ||
|
||
return () => { | ||
window.removeEventListener('storage', handleStorageChange); | ||
window.removeEventListener('local-storage', handleStorageChange); | ||
}; | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
return [storedValue, setValue]; | ||
}; |
Oops, something went wrong.