-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink.tsx
36 lines (34 loc) · 1.01 KB
/
link.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"use client";
import { useCallback, useEffect } from "react";
import { Link as WakuLink } from "waku";
import {
type LinkProps,
useRouter_UNSTABLE as useRouter,
} from "waku/router/client";
import { useAtom } from "jotai";
import { navigatingAtom } from "../atoms";
export const Link: React.FC<LinkProps> = (props) => {
const { hash, path, query } = useRouter();
const [_navigating, setNavigating] = useAtom(navigatingAtom);
const onClick = props.onClick;
const to = props.to;
const internalOnClick = useCallback(
(evt: React.MouseEvent<HTMLAnchorElement, MouseEvent>) => {
if (onClick) onClick(evt);
const from = `${path}${query ? `?${query}` : ""}${hash ? `#${hash}` : ""}`;
if (from.split("#")[0] === to.split("#")[0]) {
return;
}
setNavigating({
from,
to,
started: new Date(),
});
},
[path, to, onClick],
);
useEffect(() => {
setNavigating(undefined);
}, [path]);
return <WakuLink {...props} onClick={internalOnClick} />;
};