-
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
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
-
72
-
73
-
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
-
103
-
104
-
105
-
106
-
107
-
108
-
109
-
110
-
111
-
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129
-
130
-
131
-
132
-
133
-
134
-
135
-
136
-
137
-
138
-
139
-
140
-
141
-
142
-
143
-
144
-
145
-
146
-
147
-
148
-
149
-
150
-
151
-
152
-
153
-
154
-
155
-
156
-
157
-
158
-
159
-
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170
-
171
// SPDX-FileCopyrightText: 2024 Shota FUJI <pockawoooh@gmail.com>
// SPDX-License-Identifier: AGPL-3.0-only
import {
createContext,
type FC,
type ReactNode,
useEffect,
useMemo,
useRef,
useState,
} from "react";
export interface Route {
url: Readonly<URL>;
}
export const RouteContext = createContext<Route>({
url: new URL(location.href),
});
export interface Navigation {
push(url: string | URL): void;
forward(): void;
back(): void;
}
const nativeNavigation: Navigation = {
push(url) {
location.href = url instanceof URL ? url.href : url;
},
forward() {
history.forward();
},
back() {
history.back();
},
};
export const NavigationContext = createContext<Navigation>(nativeNavigation);
export interface InmemoryRouterProviderProps {
initialURL?: string | URL | undefined;
baseURL?: string | URL | undefined;
children: ReactNode;
onRouteChange?(url: URL): void;
}
export const InmemoryRouterProvider: FC<InmemoryRouterProviderProps> = ({
initialURL,
baseURL,
children,
onRouteChange,
}) => {
const [history, setHistory] = useState<{
current: URL;
backwards: readonly URL[];
forwards: readonly URL[];
}>(() => {
return {
current: new URL(initialURL ?? location.href, location.href),
forwards: [],
backwards: [],
};
});
const routeChangeCallback = useRef(onRouteChange);
routeChangeCallback.current = onRouteChange;
const navigation = useMemo<Navigation>(() => {
return {
push(url) {
setHistory(({ current, backwards }) => {
const next = new URL(url, current);
routeChangeCallback.current?.(next);
return {
current: next,
backwards: [current, ...backwards],
forwards: [],
};
});
},
forward() {
setHistory((history) => {
if (!history.forwards[0]) {
return history;
}
routeChangeCallback.current?.(history.forwards[0]);
return {
current: history.forwards[0],
backwards: [history.current, ...history.backwards],
forwards: history.forwards.slice(1),
};
});
},
back() {
setHistory((history) => {
if (!history.backwards[0]) {
return history;
}
routeChangeCallback.current?.(history.backwards[0]);
return {
current: history.backwards[0],
backwards: history.backwards.slice(1),
forwards: [history.current, ...history.forwards],
};
});
},
};
}, []);
const root = useMemo(() => new URL(baseURL ?? location.href, location.href), [baseURL]);
useEffect(() => {
const listener = (event: MouseEvent) => {
if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) {
return;
}
if (!(event.target instanceof Element)) {
return;
}
const anchor =
event.target instanceof HTMLAnchorElement
? event.target
: event.target.closest("a");
if (
!anchor ||
!anchor.href ||
(anchor.hasAttribute("target") && anchor.getAttribute("target") !== "_self")
) {
return;
}
const href = new URL(anchor.href, history.current);
if (!href.toString().startsWith(root.toString())) {
return;
}
event.preventDefault();
event.stopPropagation();
navigation.push(href);
};
document.addEventListener("click", listener);
return () => void document.removeEventListener("click", listener);
}, [root, history.current, navigation]);
const route = useMemo<Route>(() => {
return { url: history.current };
}, [history.current]);
return (
<NavigationContext.Provider value={navigation}>
<RouteContext.Provider value={route}>{children}</RouteContext.Provider>
</NavigationContext.Provider>
);
};