import { FC } from 'react' import type { AnyEvent, DisplayMode, Display, JsonData, FastProps } from '../models' import { useCustomRender } from '../hooks/config' import { unreachable, asTitle } from '../tools' import { AnyComp } from '.' import { JsonComp } from './Json' import { LinkRender } from './link' import MarkdownComp from './MarkdownLazy' export const DisplayComp: FC = (props) => { const CustomRenderComp = useCustomRender(props) if (CustomRenderComp) { return } const { onClick } = props if (onClick) { return ( ) } else { return } } // todo: this list should probably be defined in the models file const nestableSubcomponents = [ 'Text', 'Paragraph', 'Div', 'Heading', 'Markdown', 'Code', 'Json', 'Button', 'Link', 'LinkList', 'ServerLoad', 'Image', 'Iframe', 'Video', 'Spinner', 'Custom', 'Table', 'Details', ] const DisplayRender: FC = (props) => { const mode = props.mode ?? 'auto' const value = props.value ?? null if (mode === 'json') { return } else if (Array.isArray(value)) { return } else if (typeof value === 'object' && value !== null) { if (value.type !== null && typeof value.type === 'string' && nestableSubcomponents.includes(value.type)) { return } else { return } } else { return } } interface DisplayArrayProps { value: JsonData[] mode?: DisplayMode } export const DisplayArray: FC = (props) => { const { mode, value } = props return ( <> {value.map((v, i) => ( ,{' '} ))} ) } interface DisplayObjectProps { value: { [key: string]: JsonData } mode?: DisplayMode } export const DisplayObject: FC = (props) => { const { mode, value } = props return ( <> {Object.entries(value).map(([key, v], i) => ( {key}: ,{' '} ))} ) } type JSONPrimitive = string | number | boolean | null export interface DisplayPrimitiveProps { value: JSONPrimitive mode: DisplayMode } export const DisplayPrimitive: FC = (props) => { const { mode, value } = props switch (mode) { case 'auto': return case 'plain': return case 'datetime': return case 'date': return case 'duration': return case 'as_title': return case 'markdown': return case 'json': case 'inline_code': return case 'currency': return default: unreachable('Unexpected display type', mode, props) } } const DisplayNull: FC = () => { return — } const DisplayAuto: FC<{ value: JSONPrimitive }> = ({ value }) => { if (value === null) { return } else if (typeof value === 'boolean') { if (value) { return <>✓ } else { return <>× } } else if (typeof value === 'number') { return <>{value.toLocaleString()} } else { return <>{value} } } const DisplayPlain: FC<{ value: JSONPrimitive }> = ({ value }) => { if (value === null) { return } else { return <>{value.toString()} } } const DisplayDateTime: FC<{ value: JSONPrimitive }> = ({ value }) => { if (value === null) { return } else { // TODO return <>{value.toString()} } } const DisplayDate: FC<{ value: JSONPrimitive }> = ({ value }) => { if (value === null) { return } else { // TODO return <>{value.toString()} } } const DisplayDuration: FC<{ value: JSONPrimitive }> = ({ value }) => { if (value === null) { return } else { // TODO return <>{value.toString()} } } const DisplayAsTitle: FC<{ value: JSONPrimitive }> = ({ value }) => { if (value === null) { return } else { return <>{asTitle(value.toString())} } } const DisplayMarkdown: FC<{ value: JSONPrimitive }> = ({ value }) => { if (value === null) { return } else { return } } const DisplayInlineCode: FC<{ value: JSONPrimitive }> = ({ value }) => { if (value === null) { return } else { return {value.toString()} } } const DisplayCurrency: FC<{ value: JSONPrimitive }> = ({ value }) => { if (typeof value === 'boolean') { return value.toString() } else if (value === null) { return } else { return Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format( typeof value === 'string' ? parseFloat(value) : value, ) } } export type DataModel = Record export interface DisplayLookupProps extends Omit { field: string tableWidthPercent?: number } export function renderEvent(event: AnyEvent | undefined, data: DataModel): AnyEvent | undefined { let newEvent: AnyEvent | undefined = event ? { ...event } : undefined if (newEvent) { if (newEvent.type === 'go-to' && newEvent.url) { // for go-to events with a URL, substitute the row values into the url let url: string | null = null try { url = subKeys(newEvent.url, data) } catch (e) { url = null } if (url === null) { newEvent = undefined } else { newEvent.url = url } } } return newEvent } const subKeys = (template: string, row: DataModel): string | null => { let returnNull = false const r = template.replace(/{(.+?)}/g, (_, key: string): string => { const v: JsonData | undefined = row[key] if (v === undefined) { throw new Error(`field "${key}" not found in ${JSON.stringify(row)}`) } else if (v === null) { returnNull = true return 'null' } else { return v.toString() } }) if (returnNull) { return null } else { return r } }