1 import { CORE_LAYOUT_IDS } from "@medusajs/admin-shared"
2 import { useLoaderData, useParams } from "react-router-dom"
3
4 import { SingleColumnPageSkeleton } from "../../../components/common/skeleton"
5 import { LayoutComposer, detailPageDefaultEntries } from "../../../components/layout-composer"
6 import { useRbacPolicy } from "../../../hooks/api/rbac-policies"
7 import { useRequireRbacFeature } from "../../../hooks/use-require-rbac-feature"
8 import { usePermissions } from "../../../providers/permissions-provider"
9 import { PolicyGeneralSection } from "./components/policy-general-section"
10 import { PolicyRolesSection } from "./components/policy-roles-section"
11 import { POLICY_DETAIL_FIELDS } from "./constants"
12 import { policyLoader } from "./loader"
13
14 export const PolicyDetail = () => {
15 const initialData = useLoaderData() as Awaited<
16 ReturnType<typeof policyLoader>
17 >
18 const { id } = useParams()
19 const isRbacEnabled = useRequireRbacFeature()
20 const { hasPermission } = usePermissions()
21
22 const {
23 policy,
24 isPending: isLoading,
25 isError,
26 error,
27 } = useRbacPolicy(
28 id!,
29 { fields: POLICY_DETAIL_FIELDS },
30 {
31 initialData,
32 enabled: !!id && isRbacEnabled,
33 }
34 )
35
36 if (isError) {
37 throw error
38 }
39
40 if (isLoading || !policy) {
41 return <SingleColumnPageSkeleton sections={2} showJSON showMetadata />
42 }
43
44 return (
45 <LayoutComposer
46 widgetsZonePrefix="policy.details"
47 preferredLayoutId={CORE_LAYOUT_IDS.SINGLE_COLUMN}
48 data={policy}
49 sections={{
50 main: (
51 <>
52 <LayoutComposer.Entry id="PolicyGeneralSection">
53 <PolicyGeneralSection policy={policy} />
54 </LayoutComposer.Entry>
55 {hasPermission("rbac_role:read") && (
56 <LayoutComposer.Entry id="PolicyRolesSection">
57 <PolicyRolesSection policy={policy} />
58 </LayoutComposer.Entry>
59 )}
60 {detailPageDefaultEntries(policy, { permissions: false })}
61 </>
62 ),
63 }}
64 />
65 )
66 }