import { useWorkflowDefinitionByIdQuery } from '@/domains/workflow-definitions/hooks/queries/useWorkflowDefinitionByQuery/useWorkflowDefinitionByIdQuery'; import { useCurrentCaseQuery } from '@/pages/Entity/hooks/useCurrentCaseQuery/useCurrentCaseQuery'; import { ubosFormJsonDefinition } from './ubos-form-json-definition'; import { useCallback } from 'react'; import { useCaseState } from '@/pages/Entity/components/Case/hooks/useCaseState/useCaseState'; import { useToggle } from '@/common/hooks/useToggle/useToggle'; import { baseLayouts, DynamicForm, FieldLayout, ScrollArea, TextWithNAFallback, } from '@ballerine/ui'; import { Button } from '@/common/components/atoms/Button/Button'; import { createColumnHelper } from '@tanstack/react-table'; import { createFormSchemaFromUIElements } from './utils/create-form-schema-from-ui-elements'; import { useAuthenticatedUserQuery } from '@/domains/auth/hooks/queries/useAuthenticatedUserQuery/useAuthenticatedUserQuery'; import { useMemo } from 'react'; import { ArrowLeft, Trash2Icon } from 'lucide-react'; import { set } from 'lodash-es'; import { Dialog } from '@/common/components/molecules/Dialog/Dialog'; import { createBlocksTyped } from '@/lib/blocks/create-blocks-typed/create-blocks-typed'; import { UrlDataTable } from '@/common/components/organisms/UrlDataTable/UrlDataTable'; import { transformErrors } from '@/pages/Entities/components/CaseCreation/components/CaseCreationForm/utils/transform-errors'; import { useTranslateUiDefinitionQuery } from '@/domains/ui-definition/hooks/queries/useTranslateUiDefinitionQuery/useTranslateUiDefinitionQuery'; import { useDeleteUbosByIdsMutation } from '@/domains/workflows/hooks/mutations/useDeleteUbosByIdsMutation/useDeleteUbosByIdsMutation'; import { useCreateUboMutation } from '@/domains/workflows/hooks/mutations/useCreateUboMutation/useCreateUboMutation'; import { useLocale } from '@/common/hooks/useLocale/useLocale'; export const useManageUbosBlock = ({ create, }: { create: { enabled: boolean; }; }) => { const { data: workflow } = useCurrentCaseQuery(); const { data: workflowDefinition } = useWorkflowDefinitionByIdQuery({ workflowDefinitionId: workflow?.workflowDefinition?.id ?? '', }); const uiDefinition = workflowDefinition?.uiDefinitions?.find( uiDefinition => uiDefinition.uiContext === 'collection_flow', ); const locale = useLocale(); const { data: translatedUbos } = useTranslateUiDefinitionQuery({ id: uiDefinition?.id ?? '', partialUiDefinition: ubosFormJsonDefinition, locale, }); const { formSchema, uiSchema } = createFormSchemaFromUIElements(translatedUbos ?? {}); const [isAddingUbo, _toggleIsAddingUbo, toggleOnIsAddingUbo, toggleOffIsAddingUbo] = useToggle(); const [isManageUbosOpen, toggleIsManageUbosOpen] = useToggle(); const { mutate: mutateCreateUbo } = useCreateUboMutation({ workflowId: workflow?.id, onSuccess: () => { if (!isAddingUbo) { return; } toggleOffIsAddingUbo(); }, }); const { mutate: mutateDeleteUbosByIds } = useDeleteUbosByIdsMutation({ workflowId: workflow?.id, }); const onRemoveUboFromContext = useCallback( (ids: string[]) => { return () => mutateDeleteUbosByIds(ids); }, [mutateDeleteUbosByIds], ); const { data: session } = useAuthenticatedUserQuery(); const caseState = useCaseState(session?.user, workflow); const layouts = useMemo( () => ({ ...baseLayouts, FieldTemplate: FieldLayout, ButtonTemplates: { ...baseLayouts.ButtonTemplates, SubmitButton: () => (
), }, }), [caseState.writeEnabled], ); const columnHelper = createColumnHelper<{ id: string; firstName: string; lastName: string; ownershipPercentage: number; }>(); const columns = useMemo( () => [ columnHelper.accessor('firstName', { header: 'First Name', }), columnHelper.accessor('lastName', { header: 'Last Name', }), columnHelper.accessor('ownershipPercentage', { header: '% of Ownership', cell: ({ getValue }) => { const value = getValue(); return ( {value || value === 0 ? `${value}%` : value} ); }, }), columnHelper.display({ id: 'remove', header: '', cell: ({ row }) => { return ( } title={'UBO removal confirmation'} description={

Are you sure you want to remove this UBO? This action will be logged, and the UBO's data will be removed from the case.

} content={null} close={
} props={{ content: { className: 'mb-96', }, title: { className: `text-2xl`, }, }} /> ); }, }), ], [caseState.writeEnabled, columnHelper, onRemoveUboFromContext], ); const onSubmit = useCallback( (data: Record) => { const ubo = Object.entries(data).reduce((acc, [key, value]) => { const element = ubosFormJsonDefinition.elements.find(element => element.name === key); if (!element?.valueDestination) { return acc; } set(acc, element.valueDestination, value); return acc; }, {} as Record); mutateCreateUbo(ubo); }, [mutateCreateUbo], ); const ubos = useMemo(() => { return ( workflow?.childWorkflows?.map(childWorkflow => ({ id: childWorkflow.context.entity.ballerineEntityId, firstName: childWorkflow.context.entity.data.firstName, lastName: childWorkflow.context.entity.data.lastName, ownershipPercentage: childWorkflow.context.entity.data.percentageOfOwnership ?? childWorkflow.context.entity.data.ownershipPercentage ?? childWorkflow.context.entity.data.additionalInfo.percentageOfOwnership ?? childWorkflow.context.entity.data.additionalInfo.ownershipPercentage, })) ?? [] ); }, [workflow?.childWorkflows]); return createBlocksTyped() .addBlock() .addCell({ type: 'node', value: ( Manage UBOs } content={
{(!create.enabled || !isAddingUbo) && (

Manage UBOs

row.id, }} props={{ scroll: { className: '[&>div]:max-h-[73vh]', }, }} /> {create.enabled && ( )}
)} {create.enabled && isAddingUbo && ( <>

Add UBO

div>fieldset>div:first-of-type]:py-0 [&>div]:py-0'} />
)}
} modal /> ), }) .build(); };