1import { useCallback, useMemo, useState, useEffect } from 'react';
2import { last, includes as _includes } from 'lodash';
5 * @param primary whether or not this is the top-level router
6 * @param names map of string to view key, used for consumer references
7 * @param views map of view keys to react component views
8 * @param initialRoutes initial set of routes following the { key, data } format
10export default function useRouter({
16 const [routes, setRoutes] = useState(initialRoutes);
18 const push = useCallback(
22 `Invalid Route: ${key}. Possible Routes: ${Object.keys(views).join(
28 setRoutes(routes => [...routes, { key, data }]);
32 const popAndPush = useCallback(
36 `Invalid Route: ${key}. Possible Routes: ${Object.keys(views).join(
43 ...routes.slice(0, routes.length - 1),
49 const pop = useCallback(
52 routes.length > 1 ? [...routes.slice(0, routes.length - count)] : routes
54 // ^ can't pop root route
57 const peek = useCallback(() => last(routes), [routes]);
58 const includes = useCallback(key => _includes(routes.map(r => r.key), key), [
61 const size = routes.length;
62 const data = useMemo(() => last(routes).data, [routes]);
63 const Route = useMemo(() => views[peek().key], [views, peek]);
65 // Scroll to top of page with each route transition on primary navigation
71 window.scrollTo(0, 0);
72 }, [routes, primary]);
74 // capture browser pop in primary router
80 window.history.pushState(null, null, null);
82 window.onpopstate = e => {
83 window.history.pushState(null, null, null);
87 // TODO: disposer functon
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list