Browse Source

refactor(dashboard): Make entityName optional for useDetailPage hook

Michael Bromley 6 months ago
parent
commit
2b4d9d5754

+ 2 - 1
packages/dashboard/src/lib/framework/page/detail-page-route-loader.tsx

@@ -3,10 +3,11 @@ import { NEW_ENTITY_PATH } from '@/constants.js';
 import { PageBreadcrumb } from '@/components/layout/generated-breadcrumbs.js';
 import { TypedDocumentNode } from '@graphql-typed-document-node/core';
 import { FileBaseRouteOptions } from '@tanstack/react-router';
+import { addCustomFields } from '../document-introspection/add-custom-fields.js';
 import { getQueryName, getQueryTypeFieldInfo } from '../document-introspection/get-document-structure.js';
 import { DetailEntity } from './page-types.js';
 import { getDetailQueryOptions } from './use-detail-page.js';
-import { addCustomFields } from '../document-introspection/add-custom-fields.js';
+
 export interface DetailPageRouteLoaderConfig<T extends TypedDocumentNode<any, any>> {
     queryDocument: T;
     breadcrumb: (isNew: boolean, entity: DetailEntity<T>) => Array<PageBreadcrumb | undefined>;

+ 9 - 3
packages/dashboard/src/lib/framework/page/use-detail-page.ts

@@ -16,7 +16,11 @@ import { FormEvent } from 'react';
 import { UseFormReturn } from 'react-hook-form';
 
 import { addCustomFields } from '../document-introspection/add-custom-fields.js';
-import { getMutationName, getQueryName } from '../document-introspection/get-document-structure.js';
+import {
+    getEntityName,
+    getMutationName,
+    getQueryName,
+} from '../document-introspection/get-document-structure.js';
 import { useGeneratedForm } from '../form-engine/use-generated-form.js';
 
 import { DetailEntityPath } from './page-types.js';
@@ -65,8 +69,9 @@ export interface DetailPageOptions<
      * @description
      * The entity type name for custom field configuration lookup.
      * Required to filter out readonly custom fields before mutations.
+     * If not provided, the function will try to infer it from the query document.
      */
-    entityName: string;
+    entityName?: string;
     /**
      * @description
      * The document to create the entity.
@@ -246,7 +251,8 @@ export function useDetailPage<
     } = options;
     const isNew = params.id === NEW_ENTITY_PATH;
     const queryClient = useQueryClient();
-    const customFieldConfig = useCustomFieldConfig(entityName);
+    const returnEntityName = entityName ?? getEntityName(queryDocument);
+    const customFieldConfig = useCustomFieldConfig(returnEntityName);
     const detailQueryOptions = getDetailQueryOptions(addCustomFields(queryDocument), {
         id: isNew ? '__NEW__' : params.id,
     });