@refinedev/core 4.42.4 → 4.44.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@refinedev/core",
3
- "version": "4.42.4",
3
+ "version": "4.44.0",
4
4
  "description": "refine is a React-based framework for building internal tools, rapidly. It ships with Ant Design System, an enterprise-level UI toolkit.",
5
5
  "private": false,
6
6
  "sideEffects": false,
@@ -1,15 +1,98 @@
1
1
  import { RouterBindingsContext } from "@contexts/router";
2
- import React, { useContext } from "react";
2
+ import { useResource } from "@hooks/resource";
3
+ import React, { useCallback, useContext } from "react";
4
+ import type {
5
+ Action,
6
+ BaseKey,
7
+ GoConfig as GoConfigBase,
8
+ IResourceItem,
9
+ } from "../../../interfaces";
10
+ import { useGetToPath } from "../use-get-to-path";
11
+
12
+ type ResourceWithoutId = {
13
+ /**
14
+ * The name or identifier of the resource.
15
+ */
16
+ resource: string;
17
+ action: Extract<Action, "create" | "list">;
18
+ id?: never;
19
+ };
20
+
21
+ type ResourceWithId = {
22
+ /**
23
+ * The name or identifier of the resource.
24
+ */
25
+ resource: string;
26
+ action: Extract<Action, "edit" | "show" | "clone">;
27
+ id: BaseKey;
28
+ };
29
+
30
+ export type Resource = ResourceWithoutId | ResourceWithId;
31
+
32
+ export type GoConfigWithResource = Omit<GoConfigBase, "to"> & {
33
+ to?: GoConfigBase["to"] | Resource;
34
+ };
3
35
 
4
36
  export const useGo = () => {
5
37
  const bindings = useContext(RouterBindingsContext);
38
+ const { select: resourceSelect } = useResource();
39
+ const getToPath = useGetToPath();
6
40
 
7
41
  const useGo = React.useMemo(
8
42
  () => bindings?.go ?? (() => () => undefined),
9
43
  [bindings?.go],
10
44
  );
11
45
 
12
- const go = useGo();
46
+ const goFromRouter = useGo();
47
+
48
+ const go = useCallback(
49
+ (config: GoConfigWithResource | GoConfigBase) => {
50
+ if (typeof config.to !== "object") {
51
+ return goFromRouter({ ...config, to: config.to });
52
+ }
53
+
54
+ // when config.to is an object, it means that we want to go to a resource.
55
+ // so we need to find the path for the resource.
56
+ const { resource } = resourceSelect(config.to.resource);
57
+ handleResourceErrors(config.to, resource);
58
+ const newTo = getToPath({
59
+ resource,
60
+ action: config.to.action,
61
+ meta: {
62
+ id: config.to.id,
63
+ },
64
+ });
65
+
66
+ return goFromRouter({
67
+ ...config,
68
+ to: newTo,
69
+ });
70
+ },
71
+ [resourceSelect, goFromRouter],
72
+ );
13
73
 
14
74
  return go;
15
75
  };
76
+
77
+ /**
78
+ * handle errors for resource
79
+ * @internal
80
+ */
81
+ export const handleResourceErrors = (to: Resource, resource: IResourceItem) => {
82
+ if (!to?.action || !to?.resource) {
83
+ throw new Error(`[useGo]: "action" or "resource" is required.`);
84
+ }
85
+
86
+ if (["edit", "show", "clone"].includes(to?.action) && !to.id) {
87
+ throw new Error(
88
+ `[useGo]: [action: ${to.action}] requires an "id" for resource [resource: ${to.resource}]`,
89
+ );
90
+ }
91
+
92
+ const actionUrl = resource[to.action];
93
+ if (!actionUrl) {
94
+ throw new Error(
95
+ `[useGo]: [action: ${to.action}] is not defined for [resource: ${to.resource}]`,
96
+ );
97
+ }
98
+ };