import { isUndefined } from 'lodash'; import React from 'react'; import { HelpTextExpandedContext } from './HelpTextExpandedContext'; import { HelpContentsRegistry } from './helpContents.registry'; import { HoverablePopover } from '../presentation/HoverablePopover'; import { Markdown } from '../presentation/Markdown'; import type { Placement } from '../presentation/Placement'; import { logger } from '../utils'; export interface IHelpFieldProps { id?: string; fallback?: string; content?: string; placement?: Placement; expand?: boolean; label?: string; } function HelpFieldContents(props: Pick): JSX.Element { const { id, fallback, content } = props; let contentString = content; if (id && !contentString) { contentString = HelpContentsRegistry.getHelpField(id) || fallback; } const config = { ADD_ATTR: ['target'] }; // allow: target="_blank" return ; } export function HelpField(props: IHelpFieldProps) { const { content, expand, fallback, id, label, placement } = props; const [popoverShownStart, setPopoverShownStart] = React.useState(); const onShow = (): void => setPopoverShownStart(Date.now()); const onHide = (): void => { if (Date.now() - popoverShownStart > 500) { logger.log({ action: 'Help contents viewed', category: 'Help', data: { label: props.id || props.content } }); } }; const icon = ; const shouldExpandFromContext = React.useContext(HelpTextExpandedContext); const expandHelpText = isUndefined(expand) ? shouldExpandFromContext : expand; if (!id && !content) { return null; } const contents = ; const popover = ( {label || icon} ); if (label) { return
{!expandHelpText && contents && popover}
; } else { const expanded =
{contents}
; return (
{!expandHelpText && contents && popover} {expandHelpText && contents && expanded}
); } }