import React, { createElement } from "react"; import equal from "react-fast-compare" import { CMSType, EntityReference, ResolvedArrayProperty, ResolvedMapProperty, ResolvedNumberProperty, ResolvedStringProperty } from "../types"; import { resolveProperty } from "../util"; import { PropertyPreviewProps } from "./PropertyPreviewProps"; import { useCustomizationController } from "../hooks"; import { EmptyValue } from "./components/EmptyValue"; import { UrlComponentPreview } from "./components/UrlComponentPreview"; import { StorageThumbnail } from "./components/StorageThumbnail"; import { Markdown } from "@firecms/ui"; import { StringPropertyPreview } from "./property_previews/StringPropertyPreview"; import { ArrayPropertyPreview } from "./property_previews/ArrayPropertyPreview"; import { ArrayOfReferencesPreview } from "./property_previews/ArrayOfReferencesPreview"; import { ArrayOfStorageComponentsPreview } from "./property_previews/ArrayOfStorageComponentsPreview"; import { ArrayPropertyEnumPreview } from "./property_previews/ArrayPropertyEnumPreview"; import { ArrayOfStringsPreview } from "./property_previews/ArrayOfStringsPreview"; import { ArrayOneOfPreview } from "./property_previews/ArrayOneOfPreview"; import { MapPropertyPreview } from "./property_previews/MapPropertyPreview"; import { ReferencePreview } from "./components/ReferencePreview"; import { DatePreview } from "./components/DatePreview"; import { BooleanPreview } from "./components/BooleanPreview"; import { NumberPropertyPreview } from "./property_previews/NumberPropertyPreview"; import { ErrorView } from "../components"; /** * @group Preview components */ export const PropertyPreview = React.memo(function PropertyPreview(props: PropertyPreviewProps) { const customizationController = useCustomizationController(); let content: React.ReactNode | any; const { property: inputProperty, propertyKey, value, size, height, width, // entity } = props; const property = resolveProperty({ propertyKey, propertyOrBuilder: inputProperty, fields: customizationController.propertyConfigs }); if (value === undefined || property === null) { content = ; } else if (property.Preview) { content = createElement(property.Preview as React.ComponentType, { propertyKey, value, property, size, height, width, // entity, customProps: property.customProps }); } else if (value === null) { content = ; } else if (property.dataType === "string") { const stringProperty = property as ResolvedStringProperty; if (typeof value === "string") { if (stringProperty.url) { if (typeof stringProperty.url === "boolean") content = ; else if (typeof stringProperty.url === "string") content = ; } else if (stringProperty.storage) { content = ; } else if (stringProperty.markdown) { content = ; } else { content = ; } } else { content = buildWrongValueType(propertyKey, property.dataType, value); } } else if (property.dataType === "array") { if (value instanceof Array) { const arrayProperty = property as ResolvedArrayProperty; if (!arrayProperty.of && !arrayProperty.oneOf) { throw Error(`You need to specify an 'of' or 'oneOf' prop (or specify a custom field) in your array property ${propertyKey}`); } if (arrayProperty.of) { if (Array.isArray(arrayProperty.of)) { content = ; } else if (arrayProperty.of.dataType === "reference") { content = ; } else if (arrayProperty.of.dataType === "string") { if (arrayProperty.of.enumValues) { content = ; } else if (arrayProperty.of.storage) { content = ; } else { content = ; } } else if (arrayProperty.of.dataType === "number" && arrayProperty.of.enumValues) { content = ; } else { content = ; } } else if (arrayProperty.oneOf) { content = ; } } else { content = buildWrongValueType(propertyKey, property.dataType, value); } } else if (property.dataType === "map") { if (typeof value === "object") { content = ; } else { content = buildWrongValueType(propertyKey, property.dataType, value); } } else if (property.dataType === "date") { if (value instanceof Date) { content = ; } else { content = buildWrongValueType(propertyKey, property.dataType, value); } } else if (property.dataType === "reference") { if (typeof property.path === "string") { if (typeof value === "object" && "isEntityReference" in value && value.isEntityReference()) { content = ; } else { content = buildWrongValueType(propertyKey, property.dataType, value); } } else { content = ; } } else if (property.dataType === "boolean") { if (typeof value === "boolean") { content = ; } else { content = buildWrongValueType(propertyKey, property.dataType, value); } } else if (property.dataType === "number") { if (typeof value === "number") { content = ; } else { content = buildWrongValueType(propertyKey, property.dataType, value); } } else { content = JSON.stringify(value); } return content === undefined || content === null || (Array.isArray(content) && content.length === 0) ? : content; }, equal); function buildWrongValueType(name: string | undefined, dataType: string, value: any) { console.warn(`Unexpected value for property ${name}, of type ${dataType}`, value); return ( ); }