-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRouteResolver.tsx
More file actions
66 lines (58 loc) · 1.9 KB
/
RouteResolver.tsx
File metadata and controls
66 lines (58 loc) · 1.9 KB
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
import React, { useEffect, useState } from 'react';
import * as R from 'remeda';
import { usePrevious } from 'src/hooks/usePrevious';
import { RouteConfig } from 'src/types';
import { useActions, useMappedState } from 'typeless';
import { getRouterState, RouterActions, RouterLocation } from 'typeless-router';
import { getGlobalState } from 'src/features/global/interface';
// load dynamically all routes from all interfaces
const req = require.context('../features', true, /interface.tsx?$/);
const routes = R.flatMap(req.keys(), key => {
const module = req(key);
const items = Object.values(module);
return items.filter((item: any) => item.type === 'route') as RouteConfig[];
});
function getMatch(loc: RouterLocation | null, isLogged: boolean) {
if (!loc) {
return null;
}
return routes.find(route => {
if ((route.auth && !isLogged) || (!route.auth && isLogged)) {
return false;
}
return route.path === loc.pathname;
});
}
export const RouteResolver = () => {
const { user, location } = useMappedState(
[getGlobalState, getRouterState],
(global, router) => ({
...R.pick(global, ['isLoaded', 'user']),
...R.pick(router, ['location']),
})
);
const { push } = useActions(RouterActions);
const [component, setComponent] = useState(<div />);
const prevUser = usePrevious(user);
useEffect(() => {
let match = getMatch(location, !!user);
if (match) {
setComponent(match.component);
return;
}
if (!prevUser && user) {
// user is logging in
// keep rendering current route if not found
match = getMatch(location, !user);
if (match) {
setComponent(match.component);
}
return;
}
// not found route
// you can display 404 or redirect to default routes
push(user ? '/' : '/login');
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [location, user]);
return component;
};