-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(global-message): dismissable messages
- Loading branch information
Showing
7 changed files
with
176 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import {useRef, useEffect} from 'react'; | ||
|
||
export default function useInterval( | ||
callback: Function, | ||
delay: number, | ||
deps: React.DependencyList = [], | ||
disabled: boolean = false, | ||
) { | ||
const savedCallback = useRef<Function>(() => {}); | ||
|
||
// Remember the latest callback. | ||
useEffect(() => { | ||
savedCallback.current = callback; | ||
}, [callback]); | ||
|
||
// Set up the interval. | ||
useEffect(() => { | ||
function tick() { | ||
savedCallback.current(); | ||
} | ||
if (delay !== null && !disabled) { | ||
let id = setInterval(tick, delay); | ||
return () => clearInterval(id); | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [delay, disabled, ...deps]); | ||
} |
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,43 @@ | ||
import {useCallback, useState} from 'react'; | ||
|
||
export default function useLocalStorage<T>( | ||
key: string, | ||
initialValue: T, | ||
): [T, (value: T) => void] { | ||
const [storedValue, setStoredValue] = useState<T>(() => { | ||
if (typeof window === 'undefined') { | ||
return initialValue; | ||
} | ||
try { | ||
// Get from local storage by key | ||
const item = window.localStorage.getItem(key); | ||
// Parse stored json or if none return initialValue | ||
return item ? JSON.parse(item) : initialValue; | ||
} catch (error) { | ||
// If error also return initialValue | ||
console.log(error); | ||
return initialValue; | ||
} | ||
}); | ||
// Return a wrapped version of useState's setter function that ... | ||
// ... persists the new value to localStorage. | ||
const setValue = useCallback( | ||
(value: T) => { | ||
try { | ||
// Allow value to be a function so we have same API as useState | ||
const valueToStore = | ||
value instanceof Function ? value(storedValue) : value; | ||
// Save state | ||
setStoredValue(valueToStore); | ||
// Save to local storage | ||
if (typeof window !== 'undefined') { | ||
window.localStorage.setItem(key, JSON.stringify(valueToStore)); | ||
} | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
}, | ||
[key, storedValue], | ||
); | ||
return [storedValue, setValue]; | ||
} |
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,8 @@ | ||
import {useState} from 'react'; | ||
import useInterval from '@atb/utils/use-interval'; | ||
|
||
export function useNow(milliseconds: number = 2500): number { | ||
const [now, setNow] = useState<number>(Date.now()); | ||
useInterval(() => setNow(Date.now()), milliseconds); | ||
return now; | ||
} |