88} from '@assembly/types'
99import type { IconType } from '@assembly-js/design-system'
1010import { useQuery } from '@tanstack/react-query'
11+ import { useMemo } from 'react'
1112import { api } from '@/lib/core/axios.instance'
1213
1314const CUSTOM_FIELDS_QUERY_KEY = 'custom-fields'
@@ -35,43 +36,40 @@ export type CustomFieldItem = {
3536/** Nested map: { [entityType]: { [fieldKey]: { [optionKey]: optionLabel } } } */
3637export type CustomFieldOptionsMap = Record < string , Record < string , Record < string , string > > >
3738
38- export function useCustomFields ( ) {
39- const { data : clientCustomFields , isLoading : clientIsLoading } = useQuery ( {
40- queryKey : [ CUSTOM_FIELDS_QUERY_KEY , CustomFieldEntityType . CLIENT ] ,
41- queryFn : async ( ) : Promise < CustomFieldItem [ ] > => {
42- const res = await api . get ( `/api/custom-fields/${ CustomFieldEntityType . CLIENT } ` )
43- const parsed = ListCustomFieldResponseSchema . parse ( res . data )
44- return parsed . data
45- . sort ( ( a , b ) => a . order - b . order )
46- . map ( ( { id, key, name, type, options } ) => ( {
47- id,
48- key,
49- name,
50- type,
51- options,
52- icon : CUSTOM_FIELD_TYPE_ICON [ type ] ,
53- } ) )
54- } ,
55- } )
39+ type CustomFieldItemWithEntity = CustomFieldItem & { entityType : CustomFieldEntityType }
5640
57- const { data : companyCustomFields , isLoading : companyIsLoading } = useQuery ( {
58- queryKey : [ CUSTOM_FIELDS_QUERY_KEY , CustomFieldEntityType . COMPANY ] ,
59- queryFn : async ( ) : Promise < CustomFieldItem [ ] > => {
60- const res = await api . get ( `/api/custom-fields/${ CustomFieldEntityType . COMPANY } ` )
41+ export const useCustomFields = ( ) => {
42+ const { data : customFields , isLoading : fieldsIsLoading } = useQuery ( {
43+ queryKey : [ CUSTOM_FIELDS_QUERY_KEY ] ,
44+ queryFn : async ( ) : Promise < CustomFieldItemWithEntity [ ] > => {
45+ const res = await api . get ( '/api/custom-fields' )
6146 const parsed = ListCustomFieldResponseSchema . parse ( res . data )
6247 return parsed . data
6348 . sort ( ( a , b ) => a . order - b . order )
64- . map ( ( { id, key, name, type, options } ) => ( {
49+ . map ( ( { id, key, name, type, options, entityType } ) => ( {
6550 id,
6651 key,
6752 name,
6853 type,
6954 options,
55+ entityType,
7056 icon : CUSTOM_FIELD_TYPE_ICON [ type ] ,
7157 } ) )
7258 } ,
7359 } )
7460
61+ // Split the single response by entity type, since field keys are only
62+ // unique within an entity type (client vs company).
63+ const { clientCustomFields, companyCustomFields } = useMemo ( ( ) => {
64+ const client : CustomFieldItem [ ] = [ ]
65+ const company : CustomFieldItem [ ] = [ ]
66+ for ( const { entityType, ...field } of customFields ?? [ ] ) {
67+ if ( entityType === CustomFieldEntityType . CLIENT ) client . push ( field )
68+ else if ( entityType === CustomFieldEntityType . COMPANY ) company . push ( field )
69+ }
70+ return { clientCustomFields : client , companyCustomFields : company }
71+ } , [ customFields ] )
72+
7573 const { data : optionsMap , isLoading : optionsMapIsLoading } = useQuery ( {
7674 queryKey : [ CUSTOM_FIELD_OPTIONS_MAP_QUERY_KEY ] ,
7775 queryFn : async ( ) : Promise < CustomFieldOptionsMap > => {
@@ -81,9 +79,9 @@ export function useCustomFields() {
8179 } )
8280
8381 return {
84- clientCustomFields : clientCustomFields ?? [ ] ,
85- companyCustomFields : companyCustomFields ?? [ ] ,
82+ clientCustomFields,
83+ companyCustomFields,
8684 optionsMap : optionsMap ?? { } ,
87- isLoading : clientIsLoading || companyIsLoading || optionsMapIsLoading ,
85+ isLoading : fieldsIsLoading || optionsMapIsLoading ,
8886 }
8987}
0 commit comments