-
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.
Extract hook functions to individual files
- Loading branch information
1 parent
7a837c2
commit 4b119df
Showing
20 changed files
with
104 additions
and
76 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { useReducer } from "react"; | ||
import { useObserver } from "../../utils/Observer"; | ||
import { strings } from "../../utils/lang"; | ||
|
||
/** | ||
* To use in components. | ||
* Forces a refresh of the component when the language is loaded. | ||
*/ | ||
export function useLanguageRefresh() { | ||
const [_updateNum, forceUpdate] = useReducer(x => (x + 1) % 100, 0); | ||
useObserver(forceUpdate, strings, 'translation-name') | ||
} |
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,19 @@ | ||
import { useEffect } from "react" | ||
import { useNavigate } from "react-router-dom" | ||
import { observe, unobserve } from "../../utils/Observer" | ||
import { SCREEN, displayMode } from "../../utils/display" | ||
|
||
/** | ||
* Navigate to the correct screen when setting the `displayMode.screen` value. | ||
* Sets this attribute to the provided `currentScreen`. | ||
* @param currentScreen label of the current screen this hook is used on | ||
*/ | ||
export function useScreenAutoNavigate(currentScreen: SCREEN) { | ||
const navigate = useNavigate() | ||
useEffect(()=> { | ||
displayMode.screen = currentScreen | ||
observe(displayMode, 'screen', navigate, | ||
{ filter: (s)=> s != currentScreen }) | ||
return unobserve.bind(null, displayMode, 'screen', navigate) as VoidFunction | ||
}, []) | ||
} |
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,25 @@ | ||
import { useRef, useEffect } from "react"; | ||
|
||
/** | ||
* Prints the changes in the provided properties everytime the component updates. | ||
* The logs are outputed using the function `console.debug()`. | ||
* @param prefix prefix to print before the changes in the output | ||
* @param props Properties to watch | ||
*/ | ||
export function useTraceUpdate(prefix: string, props: Record<string, any>) { | ||
const prev = useRef(props); | ||
useEffect(() => { | ||
const changedProps = Object.entries(props).reduce((ps, [k, v]) => { | ||
if (prev.current[k] !== v) { | ||
ps[k] = [prev.current[k], v]; | ||
} | ||
return ps; | ||
}, {} as Record<string, any>); | ||
if (Object.keys(changedProps).length > 0) { | ||
console.debug(prefix, 'Changed props:', changedProps); | ||
} else { | ||
console.debug(prefix, 'No changed props'); | ||
} | ||
prev.current = props; | ||
}); | ||
} |
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
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