import React, { useCallback, useState } from "react"; import equal from "react-fast-compare" import { ReferencePreview } from "../../../preview"; import { CollectionSize, Entity, EntityCollection, EntityReference, FilterValues } from "../../../types"; import { getPreviewSizeFrom } from "../../../preview/util"; import { getReferenceFrom } from "../../../util"; import { useCustomizationController, useNavigationController, useReferenceDialog } from "../../../hooks"; import { ErrorView } from "../../ErrorView"; import { Button } from "@firecms/ui"; import { EntityPreviewContainer } from "../../EntityPreview"; type TableReferenceFieldProps = { name: string; disabled: boolean; internalValue: EntityReference | EntityReference[] | undefined | null; updateValue: (newValue: (EntityReference | EntityReference [] | null)) => void; size: CollectionSize; multiselect: boolean; previewProperties?: string[]; title?: string; path: string; forceFilter?: FilterValues; }; export function TableReferenceField(props: TableReferenceFieldProps) { const customizationController = useCustomizationController(); const navigationController = useNavigationController(); const { path } = props; const collection = navigationController.getCollection(path); if (!collection) { if (customizationController.components?.missingReference) { return ; } else { throw Error(`Couldn't find the corresponding collection view for the path: ${path}`); } } return ; } export const TableReferenceFieldSuccess = React.memo( function TableReferenceFieldSuccess(props: TableReferenceFieldProps & { collection: EntityCollection; }) { const { name, internalValue, updateValue, multiselect, path, size, previewProperties, title, disabled, forceFilter, collection } = props; const onSingleEntitySelected = useCallback((entity: Entity) => { updateValue(entity ? getReferenceFrom(entity) : null); }, [updateValue]); const onMultipleEntitiesSelected = useCallback((entities: Entity[]) => { updateValue(entities.map((e) => getReferenceFrom(e))); }, [updateValue]); const selectedEntityIds = internalValue ? (Array.isArray(internalValue) ? internalValue.map((ref) => ref.id) : internalValue.id ? [internalValue.id] : []) : []; const referenceDialogController = useReferenceDialog({ multiselect, path, collection, onMultipleEntitiesSelected, onSingleEntitySelected, selectedEntityIds, forceFilter } ); const handleOpen = useCallback(() => { if (disabled) return; referenceDialogController.open(); }, [disabled, referenceDialogController]); const valueNotSet = !internalValue || (Array.isArray(internalValue) && internalValue.length === 0); const buildSingleReferenceField = () => { if (internalValue && !Array.isArray(internalValue) && internalValue.isEntityReference && internalValue.isEntityReference()) return ; else return ; }; const buildMultipleReferenceField = () => { if (Array.isArray(internalValue)) return <> {internalValue.map((reference, index) =>
) } ; else return ; }; if (!collection) return ; return (
{internalValue && !multiselect && buildSingleReferenceField()} {internalValue && multiselect && buildMultipleReferenceField()} {valueNotSet && }
); }, equal);