Bin
2025-12-17 1d710f844b65d9bfdf986a71a3b924cd70598a41
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
import { createContext, useContext, useEffect, useMemo, useState } from "react";
import { generatePath, matchPath, useHistory, useLocation } from "react-router";
import { Pages } from "../pages";
import { setBreadcrumbs, useBreadcrumbControls } from "../services/breadrumbs";
import { pageSetToRoutes } from "../utils/routeHelpers";
import { useAppStore } from "./AppStoreProvider";
import { useConfig } from "./ConfigProvider";
 
export const RoutesContext = createContext();
 
const findMacthingComponents = (path, routesMap, parentPath = "") => {
  const result = [];
 
  const match =
    path === "/"
      ? routesMap.at(0)
      : routesMap.find((route) => {
          // if (route.path === "/") return false;
 
          const isRoot = route.path === "/";
          const matchingPath = `${parentPath}${route.path}`;
          const match = matchPath(path, { path: matchingPath, exact: isRoot });
 
          return match;
        });
 
  if (match) {
    const routePath = `${parentPath}${match.path}`;
 
    result.push({ ...match, path: routePath });
 
    if (match.routes) {
      result.push(...findMacthingComponents(path, match.routes, routePath));
    }
  }
 
  return result;
};
 
export const RoutesProvider = ({ children }) => {
  const history = useHistory();
  const location = useFixedLocation();
  const config = useConfig();
  const { store } = useAppStore();
  const breadcrumbs = useBreadcrumbControls();
  const [currentContext, setCurrentContext] = useState(null);
  const [currentContextProps, setCurrentContextProps] = useState(null);
 
  const routesMap = useMemo(() => {
    return pageSetToRoutes(Pages, { config, store });
  }, [location, config, store, history]);
 
  const routesChain = useMemo(() => {
    return findMacthingComponents(location.pathname, routesMap);
  }, [location, routesMap]);
 
  const lastRoute = useMemo(() => {
    return routesChain.filter((r) => !r.modal).slice(-1)[0];
  }, [routesChain]);
 
  const [currentPath, setCurrentPath] = useState(lastRoute?.path);
 
  const contextValue = useMemo(
    () => ({
      routesMap,
      breadcrumbs,
      currentContext,
      setContextProps: setCurrentContextProps,
      path: currentPath,
      findComponent: (path) => findMacthingComponents(path, routesMap),
    }),
    [breadcrumbs, routesMap, currentContext, currentPath, setCurrentContext],
  );
 
  useEffect(() => {
    const ContextComponent = lastRoute?.context;
 
    setCurrentContext({
      component: ContextComponent ?? null,
      props: currentContextProps,
    });
 
    setCurrentPath(lastRoute?.path);
 
    try {
      const crumbs = routesChain
        .map((route) => {
          const params = matchPath(location.pathname, { path: route.path });
          const path = generatePath(route.path, params.params);
          const title = route.title instanceof Function ? route.title() : route.title;
          const key = route.component?.displayName ?? route.key ?? path;
 
          return { path, title, key };
        })
        .filter((c) => !!c.title);
 
      setBreadcrumbs(crumbs);
    } catch (err) {
      console.log(err);
    }
  }, [location, routesMap, currentContextProps, routesChain, lastRoute]);
 
  return <RoutesContext.Provider value={contextValue}>{children}</RoutesContext.Provider>;
};
 
export const useRoutesMap = () => {
  return useContext(RoutesContext)?.routesMap ?? [];
};
 
export const useFindRouteComponent = () => {
  return useContext(RoutesContext)?.findComponent ?? (() => null);
};
 
export const useBreadcrumbs = () => {
  return useBreadcrumbControls();
};
 
export const useCurrentPath = () => {
  return useContext(RoutesContext)?.path;
};
 
export const useParams = () => {
  const location = useFixedLocation();
  const currentPath = useCurrentPath();
 
  const match = useMemo(() => {
    const parsedLocation = location.search
      .replace(/^\?/, "")
      .split("&")
      .map((pair) => {
        const [key, value] = pair.split("=").map((p) => decodeURIComponent(p));
        return [key, value];
      });
 
    const search = Object.fromEntries(parsedLocation);
 
    const urlParams = matchPath(location.pathname, currentPath ?? "");
 
    return { ...search, ...(urlParams?.params ?? {}) };
  }, [location, currentPath]);
 
  return match ?? {};
};
 
export const useContextComponent = () => {
  const ctx = useContext(RoutesContext);
  const { component: ContextComponent, props: contextProps } = ctx?.currentContext ?? {};
 
  return { ContextComponent, contextProps };
};
 
export const useFixedLocation = () => {
  const location = useLocation();
 
  location;
 
  const result = useMemo(() => {
    return location.location ?? location;
  }, [location]);
 
  return result;
};
 
export const useContextProps = () => {
  const setProps = useContext(RoutesContext).setContextProps;
  return useMemo(() => setProps, [setProps]);
};