Improve data prefetching, less false positives
This commit is contained in:
@@ -118,7 +118,7 @@ const CollectionLink: React.FC<Props> = ({
|
||||
setIsEditing(isEditing);
|
||||
}, []);
|
||||
|
||||
const handleMouseEnter = React.useCallback(() => {
|
||||
const handlePrefetch = React.useCallback(() => {
|
||||
void collection.fetchDocuments();
|
||||
}, [collection]);
|
||||
|
||||
@@ -138,7 +138,7 @@ const CollectionLink: React.FC<Props> = ({
|
||||
}}
|
||||
expanded={expanded}
|
||||
onDisclosureClick={onDisclosureClick}
|
||||
onMouseEnter={handleMouseEnter}
|
||||
onClickIntent={handlePrefetch}
|
||||
icon={
|
||||
<CollectionIcon collection={collection} expanded={expanded} />
|
||||
}
|
||||
|
||||
@@ -114,7 +114,7 @@ function InnerDocumentLink(
|
||||
[expanded]
|
||||
);
|
||||
|
||||
const handleMouseEnter = React.useCallback(() => {
|
||||
const handlePrefetch = React.useCallback(() => {
|
||||
prefetchDocument?.(node.id);
|
||||
}, [prefetchDocument, node]);
|
||||
|
||||
@@ -317,7 +317,7 @@ function InnerDocumentLink(
|
||||
<SidebarLink
|
||||
expanded={hasChildren ? isExpanded : undefined}
|
||||
onDisclosureClick={handleDisclosureClick}
|
||||
onMouseEnter={handleMouseEnter}
|
||||
onClickIntent={handlePrefetch}
|
||||
to={{
|
||||
pathname: node.url,
|
||||
state: {
|
||||
|
||||
@@ -6,6 +6,7 @@ import { s } from "@shared/styles";
|
||||
import { NavigationNode } from "@shared/types";
|
||||
import EventBoundary from "~/components/EventBoundary";
|
||||
import NudeButton from "~/components/NudeButton";
|
||||
import useUnmount from "~/hooks/useUnmount";
|
||||
import { undraggableOnDesktop } from "~/styles";
|
||||
import Disclosure from "./Disclosure";
|
||||
import NavLink, { Props as NavLinkProps } from "./NavLink";
|
||||
@@ -20,7 +21,8 @@ type Props = Omit<NavLinkProps, "to"> & {
|
||||
to?: LocationDescriptor;
|
||||
innerRef?: (ref: HTMLElement | null | undefined) => void;
|
||||
onClick?: React.MouseEventHandler<HTMLAnchorElement>;
|
||||
onMouseEnter?: React.MouseEventHandler<HTMLAnchorElement>;
|
||||
/* Callback when we expect the user to click on the link. Used for prefetching data. */
|
||||
onClickIntent?: () => void;
|
||||
onDisclosureClick?: React.MouseEventHandler<HTMLButtonElement>;
|
||||
icon?: React.ReactNode;
|
||||
label?: React.ReactNode;
|
||||
@@ -44,7 +46,7 @@ function SidebarLink(
|
||||
{
|
||||
icon,
|
||||
onClick,
|
||||
onMouseEnter,
|
||||
onClickIntent,
|
||||
to,
|
||||
label,
|
||||
active,
|
||||
@@ -63,6 +65,7 @@ function SidebarLink(
|
||||
}: Props,
|
||||
ref: React.RefObject<HTMLAnchorElement>
|
||||
) {
|
||||
const timer = React.useRef<number>();
|
||||
const theme = useTheme();
|
||||
const style = React.useMemo(
|
||||
() => ({
|
||||
@@ -81,6 +84,28 @@ function SidebarLink(
|
||||
[theme.text, theme.sidebarActiveBackground, style]
|
||||
);
|
||||
|
||||
const handleMouseEnter = React.useCallback(() => {
|
||||
if (timer.current) {
|
||||
clearTimeout(timer.current);
|
||||
}
|
||||
|
||||
if (onClickIntent) {
|
||||
timer.current = window.setTimeout(onClickIntent, 100);
|
||||
}
|
||||
}, [onClickIntent]);
|
||||
|
||||
const handleMouseLeave = React.useCallback(() => {
|
||||
if (timer.current) {
|
||||
clearTimeout(timer.current);
|
||||
}
|
||||
}, []);
|
||||
|
||||
useUnmount(() => {
|
||||
if (timer.current) {
|
||||
clearTimeout(timer.current);
|
||||
}
|
||||
});
|
||||
|
||||
return (
|
||||
<>
|
||||
<Link
|
||||
@@ -90,7 +115,8 @@ function SidebarLink(
|
||||
activeStyle={isActiveDrop ? activeDropStyle : activeStyle}
|
||||
style={active ? activeStyle : style}
|
||||
onClick={onClick}
|
||||
onMouseEnter={onMouseEnter}
|
||||
onMouseEnter={handleMouseEnter}
|
||||
onMouseLeave={handleMouseLeave}
|
||||
// @ts-expect-error exact does not exist on div
|
||||
exact={exact !== false}
|
||||
to={to}
|
||||
|
||||
Reference in New Issue
Block a user