fixed react-helper hooks

slight performance hit but we can work on optimizing that later
(most noticable that the avatars flicker to loading when changing channels. although this may have already been happening...)
This commit is contained in:
Michael Peters 2022-02-07 01:15:32 -06:00
parent e002b7092c
commit f9ef4edf9f
3 changed files with 21 additions and 15 deletions

View File

@ -69,7 +69,7 @@ const GuildListElement: FC<GuildListElementProps> = (props: GuildListElementProp
const setSelfActiveGuild = useCallback(() => { const setSelfActiveGuild = useCallback(() => {
setCurrGuildId(guild.id); setCurrGuildId(guild.id);
}, [ guild ]); }, [ guild.id, setCurrGuildId ]);
const className = useMemo(() => currGuildId && guild.id === currGuildId ? 'guild active' : 'guild', [ guild, currGuildId ]); const className = useMemo(() => currGuildId && guild.id === currGuildId ? 'guild active' : 'guild', [ guild, currGuildId ]);

View File

@ -77,7 +77,7 @@ const PreviewImageElement: FC<PreviewImageElementProps> = (props: PreviewImageEl
const openImageOverlay = useCallback(() => { const openImageOverlay = useCallback(() => {
setOverlay(<ImageOverlay guild={guild} resourceId={resourceId} resourceName={resourceName} />); setOverlay(<ImageOverlay guild={guild} resourceId={resourceId} resourceName={resourceName} />);
}, [ guild, resourceId, resourceName ]); }, [ guild, resourceId, resourceName, setOverlay ]);
return ( return (
<div className="content image" style={{ width: previewWidth, height: previewHeight }}> <div className="content image" style={{ width: previewWidth, height: previewHeight }}>

View File

@ -38,7 +38,7 @@ function useShake(ms: number): [ shaking: boolean, doShake: () => void ] {
await Util.sleep(ms); await Util.sleep(ms);
if (!isMounted.current) return; if (!isMounted.current) return;
setShaking(false); setShaking(false);
}, [ ms ]); }, [ isMounted, ms, shaking ]);
return [ shaking, doShake ]; return [ shaking, doShake ];
} }
@ -71,7 +71,8 @@ export function useOneTimeAsyncAction<T, V>(
})(); })();
return () => { isMounted.current = false; }; return () => { isMounted.current = false; };
}, deps); // eslint-disable-next-line react-hooks/exhaustive-deps
}, [ actionFunc, ...deps ]);
return [ value, error ]; return [ value, error ];
} }
@ -108,7 +109,8 @@ export function useAsyncCallback<ResultType>(
if (errorMessage) doShake(); if (errorMessage) doShake();
setPending(false); setPending(false);
} }
}, [ ...deps, pending, actionFunc ]); // eslint-disable-next-line react-hooks/exhaustive-deps
}, [ isMounted, pending, actionFunc, doShake, errorMessage, ...deps ]);
return [ callable, result, errorMessage, shaking ]; return [ callable, result, errorMessage, shaking ];
} }
@ -248,7 +250,7 @@ export function useAsyncSubmitButton<ResultType>(
if (pending) return textMapping.pending; if (pending) return textMapping.pending;
if (complete) return textMapping.done; if (complete) return textMapping.done;
return textMapping.start; return textMapping.start;
}, [ pending, complete, errorMessage ]); }, [ errorMessage, textMapping.error, textMapping.pending, textMapping.done, textMapping.start, pending, complete ]);
return [ callable, text, shaking, result, errorMessage ]; return [ callable, text, shaking, result, errorMessage ];
} }
@ -278,7 +280,7 @@ export function useScrollableCallables<T>(scrollable: LoadableValueScrolling<T[]
setLoadingAbove(false); setLoadingAbove(false);
} }
} }
}, [ scrollable ]); }, [ isMounted, loadingAbove, scrollable ]);
const fetchBelowCallable = useCallback(async () => { const fetchBelowCallable = useCallback(async () => {
if (loadingBelow) return; if (loadingBelow) return;
if (!isLoaded(scrollable)) return; if (!isLoaded(scrollable)) return;
@ -293,7 +295,7 @@ export function useScrollableCallables<T>(scrollable: LoadableValueScrolling<T[]
setLoadingBelow(false); setLoadingBelow(false);
} }
} }
}, [ scrollable ]); }, [ isMounted, loadingBelow, scrollable ]);
const onScrollCallable = useCallback(async (event: UIEvent<HTMLElement>) => { const onScrollCallable = useCallback(async (event: UIEvent<HTMLElement>) => {
const scrollTop = event.currentTarget.scrollTop; const scrollTop = event.currentTarget.scrollTop;
@ -369,14 +371,14 @@ export function useActionWhenEscapeOrClickedOrContextOutsideEffect(ref: RefObjec
if (ref.current.contains(event.target as Node)) return; if (ref.current.contains(event.target as Node)) return;
// context menu is fired on mouse-down so no need to do special checks. // context menu is fired on mouse-down so no need to do special checks.
actionFunc(); actionFunc();
}, [ ref ]); }, [ actionFunc, ref ]);
const handleKeyDown = useCallback((event: KeyboardEvent) => { const handleKeyDown = useCallback((event: KeyboardEvent) => {
if (!ref.current) return; if (!ref.current) return;
if (event.key !== 'Escape') return; if (event.key !== 'Escape') return;
actionFunc(); actionFunc();
}, [ ref ]); }, [ actionFunc, ref ]);
useEffect(() => { useEffect(() => {
document.addEventListener('click', handleClickOutside); document.addEventListener('click', handleClickOutside);
@ -404,7 +406,7 @@ export function useActionWhenEscapeOrClickedOrContextOutsideEffect(ref: RefObjec
return () => { return () => {
document.removeEventListener('contextmenu', handleContextMenu); document.removeEventListener('contextmenu', handleContextMenu);
}; };
}, [ handleClickOutside ]); }, [ handleContextMenu ]);
useEffect(() => { useEffect(() => {
document.addEventListener('keydown', handleKeyDown); document.addEventListener('keydown', handleKeyDown);
@ -433,7 +435,8 @@ export function useAlignment(
if (!relativeTo) throw new ShouldNeverHappenError('invalid alignment props'); if (!relativeTo) throw new ShouldNeverHappenError('invalid alignment props');
ElementsUtil.alignContextElement(rootRef.current, relativeTo, alignment); ElementsUtil.alignContextElement(rootRef.current, relativeTo, alignment);
setAligned(true); setAligned(true);
}, [ rootRef, relativeToRef, relativeToPos, ...realignDeps ]); // eslint-disable-next-line react-hooks/exhaustive-deps
}, [ rootRef, relativeToRef, relativeToPos, alignment, ...realignDeps ]);
const className = useMemo(() => baseClassName + (aligned ? ' aligned' : ''), [ baseClassName, aligned ]); const className = useMemo(() => baseClassName + (aligned ? ' aligned' : ''), [ baseClassName, aligned ]);
@ -456,6 +459,8 @@ export function useContextMenu(
const close = useCallback(() => { const close = useCallback(() => {
setIsOpen(false); setIsOpen(false);
}, []); }, []);
// eslint-disable-next-line react-hooks/exhaustive-deps
const contextMenu = useMemo(() => createContextMenu(close), [ close, createContextMenu, ...createContextMenuDeps ]); const contextMenu = useMemo(() => createContextMenu(close), [ close, createContextMenu, ...createContextMenuDeps ]);
const toggle = useCallback(() => { const toggle = useCallback(() => {
@ -463,7 +468,7 @@ export function useContextMenu(
}, [ contextMenu ]); }, [ contextMenu ]);
const open = useCallback(() => { const open = useCallback(() => {
setIsOpen(true); setIsOpen(true);
}, [ contextMenu ]); }, []);
return [ isOpen ? contextMenu : null, toggle, close, open, isOpen ]; return [ isOpen ? contextMenu : null, toggle, close, open, isOpen ];
} }
@ -482,7 +487,7 @@ export function useContextClickContextMenu(
const createContextMenuWithRelativeToPos = useCallback( const createContextMenuWithRelativeToPos = useCallback(
(close: () => void) => createContextMenu(alignment, relativeToPos, close), (close: () => void) => createContextMenu(alignment, relativeToPos, close),
[ alignment, relativeToPos ] [ alignment, createContextMenu, relativeToPos ]
); );
const [ contextMenu, _toggle, _close, open ] = useContextMenu(createContextMenuWithRelativeToPos, createContextMenuDeps); const [ contextMenu, _toggle, _close, open ] = useContextMenu(createContextMenuWithRelativeToPos, createContextMenuDeps);
@ -508,6 +513,7 @@ export function useContextHover(
const contextHover = useMemo( const contextHover = useMemo(
() => createContextHover(), () => createContextHover(),
// eslint-disable-next-line react-hooks/exhaustive-deps
[ createContextHover, ...createContextHoverDeps ] [ createContextHover, ...createContextHoverDeps ]
); );
@ -555,7 +561,7 @@ export function useDocumentDropTarget(
setName={setName} setName={setName}
/> />
); );
}, [ dragEnabled, message, setBuff, setName ]); }, [ closeDragOverlay, dragEnabled, message, setBuff, setName ]);
return [ dropTarget ]; return [ dropTarget ];
} }