import * as React from "react"; import { useMemo } from "react"; import { Entity, EntityCollection, ResolvedProperty } from "../types"; import { getEntityImagePreviewPropertyKey, getEntityPreviewKeys, getEntityTitlePropertyKey, getValueInPath, resolveCollection } from "../util"; import { cn, defaultBorderMixin, IconButton, KeyboardTabIcon, Skeleton, Tooltip, Typography } from "@firecms/ui"; import { PreviewSize, PropertyPreview, SkeletonPropertyComponent } from "../preview"; import { useCustomizationController, useNavigationController, useSideEntityController } from "../hooks"; import { useAnalyticsController } from "../hooks/useAnalyticsController"; export type EntityPreviewProps = { size: PreviewSize, actions?: React.ReactNode, collection?: EntityCollection, hover?: boolean; previewProperties?: string[], disabled: undefined | boolean, entity: Entity, includeEntityNavigation?: boolean, onClick?: (e: React.SyntheticEvent) => void; }; /** * This view is used to display a preview of an entity. * It is used by default in reference fields and whenever a reference is displayed. */ export function EntityPreview({ actions, disabled, hover, collection: collectionProp, previewProperties, onClick, size, includeEntityNavigation, entity }: EntityPreviewProps) { const analyticsController = useAnalyticsController(); const sideEntityController = useSideEntityController(); const customizationController = useCustomizationController(); const navigationController = useNavigationController(); const collection = collectionProp ?? navigationController.getCollection(entity.path); if (!collection) { throw Error(`Couldn't find the corresponding collection view for the path: ${entity.path}`); } const resolvedCollection = React.useMemo(() => resolveCollection({ collection, path: entity.path, values: entity.values, fields: customizationController.propertyConfigs }), [collection]); const listProperties = useMemo(() => getEntityPreviewKeys(resolvedCollection, customizationController.propertyConfigs, previewProperties, size === "small" || size === "medium" ? 3 : 1), [previewProperties, resolvedCollection, size]); const titleProperty = getEntityTitlePropertyKey(resolvedCollection, customizationController.propertyConfigs); const imagePropertyKey = getEntityImagePreviewPropertyKey(resolvedCollection); const imageProperty = imagePropertyKey ? resolvedCollection.properties[imagePropertyKey] : undefined; const restProperties = listProperties.filter(p => p !== titleProperty && p !== imagePropertyKey); return {imageProperty && (
)}
{size !== "tiny" && ( entity ?
{entity.id}
: )} {titleProperty && (
{ entity ? : }
)} {restProperties && restProperties.map((key) => { const childProperty = resolvedCollection.properties[key as string]; if (!childProperty) return null; return (
1 ? "my-0.5" : "my-0"}> { entity ? : }
); })}
{entity && includeEntityNavigation && { e.stopPropagation(); analyticsController.onAnalyticsEvent?.("entity_click_from_reference", { path: entity.path, entityId: entity.id }); sideEntityController.open({ entityId: entity.id, path: entity.path, collection, updateUrl: true }); }}> } {actions}
; } export type EntityPreviewContainerProps = { children: React.ReactNode; hover?: boolean; fullwidth?: boolean; size: PreviewSize; className?: string; style?: React.CSSProperties; onClick?: (e: React.SyntheticEvent) => void; }; const EntityPreviewContainerInner = React.forwardRef(({ children, hover, onClick, size, style, className, fullwidth = true, ...props }, ref) => { return
{ if (onClick) { event.preventDefault(); onClick(event); } }} {...props}> {children}
; }); EntityPreviewContainerInner.displayName = "EntityPreviewContainer"; export const EntityPreviewContainer = React.memo(EntityPreviewContainerInner) as React.FC;