-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathbindings.tsx
More file actions
157 lines (136 loc) · 4.12 KB
/
Copy pathbindings.tsx
File metadata and controls
157 lines (136 loc) · 4.12 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
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
import {
type GoConfig,
type RouterProvider,
ResourceContext,
matchResourceFromRoute,
type ParseResponse,
QS_PARSE_DEPTH,
} from "@refinedev/core";
import { useParams, useLocation, useNavigate, Link } from "@remix-run/react";
import qs from "qs";
import React, { type ComponentProps, useCallback, useContext } from "react";
import { paramsFromCurrentPath } from "./params-from-current-path";
import { convertToNumberIfPossible } from "./convert-to-number-if-possible";
import { safeDecodeURIComponent } from "./safe-decode-uri-component";
export const stringifyConfig = {
addQueryPrefix: true,
skipNulls: true,
arrayFormat: "indices" as const,
encode: false,
encodeValuesOnly: true,
};
export const routerProvider: RouterProvider = {
go: () => {
const { search: existingSearch, hash: existingHash } = useLocation();
const navigate = useNavigate();
const fn = useCallback(
({
to,
type,
query,
hash,
options: { keepQuery, keepHash } = {},
}: GoConfig) => {
/** Construct query params */
const urlQuery = {
...(keepQuery &&
existingSearch &&
qs.parse(existingSearch, {
ignoreQueryPrefix: true,
depth: QS_PARSE_DEPTH,
})),
...query,
};
if (urlQuery.to) {
urlQuery.to = encodeURIComponent(`${urlQuery.to}`);
}
const hasUrlQuery = Object.keys(urlQuery).length > 0;
/** Get hash */
const urlHash = `#${(hash || (keepHash && existingHash) || "").replace(
/^#/,
"",
)}`;
const hasUrlHash = urlHash.length > 1;
const urlTo = to || "";
const fullPath = `${urlTo}${
hasUrlQuery ? qs.stringify(urlQuery, stringifyConfig) : ""
}${hasUrlHash ? urlHash : ""}`;
if (type === "path") {
return fullPath;
}
/** Navigate to the url */
return navigate(fullPath, {
replace: type === "replace",
});
},
[existingHash, existingSearch, navigate],
);
return fn;
},
back: () => {
const navigate = useNavigate();
return () => {
navigate(-1);
};
},
parse: () => {
const params = useParams();
const { pathname, search } = useLocation();
const { resources } = useContext(ResourceContext);
const { resource, action, matchedRoute } = React.useMemo(() => {
return matchResourceFromRoute(pathname, resources);
}, [resources, pathname]);
const inferredParams =
matchedRoute && pathname
? paramsFromCurrentPath(pathname, matchedRoute)
: {};
const inferredId = inferredParams.id;
const fn = useCallback(() => {
const parsedSearch = qs.parse(search, {
ignoreQueryPrefix: true,
depth: QS_PARSE_DEPTH,
});
const combinedParams = {
...inferredParams,
...params,
...parsedSearch,
};
const response: ParseResponse = {
...(resource && { resource }),
...(action && { action }),
...(inferredId && { id: safeDecodeURIComponent(inferredId) }),
...(params?.id && { id: safeDecodeURIComponent(params.id) }),
// ...(params?.action && { action: params.action }), // lets see if there is a need for this
pathname,
params: {
...combinedParams,
currentPage: convertToNumberIfPossible(
combinedParams.currentPage as string,
) as number | undefined,
pageSize: convertToNumberIfPossible(
combinedParams.pageSize as string,
) as number | undefined,
to: combinedParams.to
? safeDecodeURIComponent(combinedParams.to as string)
: undefined,
},
};
return response;
}, [
pathname,
search,
params,
resource,
action,
inferredParams,
inferredId,
]);
return fn;
},
Link: React.forwardRef<
HTMLAnchorElement,
ComponentProps<NonNullable<RouterProvider["Link"]>>
>(function RefineLink(props, ref) {
return <Link to={props.to} {...props} ref={ref} />;
}),
};