|
1 |
| -// import { useContext } from "react"; |
2 |
| - |
3 |
| -// import { getXRay } from "@refinedev/devtools-internal"; |
4 |
| -// import { |
5 |
| -// type UseQueryOptions, |
6 |
| -// type UseQueryResult, |
7 |
| -// useQuery, |
8 |
| -// } from "@tanstack/react-query"; |
9 |
| - |
10 |
| -// import { AccessControlContext } from "@contexts/accessControl"; |
11 |
| -// import { sanitizeResource } from "@definitions/helpers/sanitize-resource"; |
12 |
| -// import { useKeys } from "@hooks/useKeys"; |
13 |
| -// import type { |
14 |
| -// CanParams, |
15 |
| -// CanReturnType, |
16 |
| -// } from "../../../contexts/accessControl/types"; |
17 |
| - |
18 |
| -// export type UseCanProps = CanParams & { |
19 |
| -// /** |
20 |
| -// * react-query's [useQuery](https://tanstack.com/query/v4/docs/reference/useQuery) options |
21 |
| -// */ |
22 |
| -// queryOptions?: UseQueryOptions<CanReturnType>; |
23 |
| -// }; |
24 |
| - |
25 |
| -// /** |
26 |
| -// * `useCan` uses the `can` as the query function for `react-query`'s {@link https://react-query.tanstack.com/guides/queries `useQuery`}. It takes the parameters that `can` takes. It can also be configured with `queryOptions` for `useQuery`. Returns the result of `useQuery`. |
27 |
| -// * @see {@link https://refine.dev/docs/api-reference/core/hooks/accessControl/useCan} for more details. |
28 |
| -// * |
29 |
| -// * @typeParam CanParams {@link https://refine.dev/docs/core/interfaceReferences#canparams} |
30 |
| -// * @typeParam CanReturnType {@link https://refine.dev/docs/core/interfaceReferences#canreturntype} |
31 |
| -// * |
32 |
| -// */ |
33 |
| -// export const useCan = ({ |
34 |
| -// action, |
35 |
| -// resource, |
36 |
| -// params, |
37 |
| -// queryOptions: hookQueryOptions, |
38 |
| -// }: UseCanProps): UseQueryResult<CanReturnType> => { |
39 |
| -// const { can, options: globalOptions } = useContext(AccessControlContext); |
40 |
| -// const { keys, preferLegacyKeys } = useKeys(); |
41 |
| - |
42 |
| -// const { queryOptions: globalQueryOptions } = globalOptions || {}; |
43 |
| - |
44 |
| -// const mergedQueryOptions = { |
45 |
| -// ...globalQueryOptions, |
46 |
| -// ...hookQueryOptions, |
47 |
| -// }; |
48 |
| - |
49 |
| -// /** |
50 |
| -// * Since `react-query` stringifies the query keys, it will throw an error for a circular dependency if we include `React.ReactNode` elements inside the keys. |
51 |
| -// * The feature in #2220(https://github.com/refinedev/refine/issues/2220) includes such change and to fix this, we need to remove `icon` property in the `resource` |
52 |
| -// */ |
53 |
| -// const { resource: _resource, ...paramsRest } = params ?? {}; |
54 |
| - |
55 |
| -// const sanitizedResource = sanitizeResource(_resource); |
56 |
| - |
57 |
| -// const queryResponse = useQuery<CanReturnType>({ |
58 |
| -// queryKey: keys() |
59 |
| -// .access() |
60 |
| -// .resource(resource) |
61 |
| -// .action(action) |
62 |
| -// .params({ |
63 |
| -// params: { ...paramsRest, resource: sanitizedResource }, |
64 |
| -// enabled: mergedQueryOptions?.enabled, |
65 |
| -// }) |
66 |
| -// .get(preferLegacyKeys), |
67 |
| -// // Enabled check for `can` is enough to be sure that it's defined in the query function but TS is not smart enough to know that. |
68 |
| -// queryFn: () => |
69 |
| -// can?.({ |
70 |
| -// action, |
71 |
| -// resource, |
72 |
| -// params: { ...paramsRest, resource: sanitizedResource }, |
73 |
| -// }) ?? Promise.resolve({ can: true }), |
74 |
| -// enabled: typeof can !== "undefined", |
75 |
| -// ...mergedQueryOptions, |
76 |
| -// meta: { |
77 |
| -// ...mergedQueryOptions?.meta, |
78 |
| -// ...getXRay("useCan", preferLegacyKeys, resource, [ |
79 |
| -// "useButtonCanAccess", |
80 |
| -// "useNavigationButton", |
81 |
| -// ]), |
82 |
| -// }, |
83 |
| -// retry: false, |
84 |
| -// }); |
85 |
| - |
86 |
| -// return typeof can === "undefined" |
87 |
| -// ? ({ data: { can: true } } as typeof queryResponse) |
88 |
| -// : queryResponse; |
89 |
| -// }; |
90 |
| - |
91 | 1 | import { useContext, useRef } from "react";
|
92 | 2 | import { getXRay } from "@refinedev/devtools-internal";
|
93 | 3 | import {
|
|
0 commit comments