import React, { ReactNode } from 'react'; import { AGG_TYPE_MAP, PREFIX_CLS } from '../../common/constants'; import { ChatContextType } from '../../common/type'; import Text from './Text'; import Typing from './Typing'; import classNames from 'classnames'; type Props = { parseLoading: boolean; parseInfoOptions: ChatContextType[]; parseTip: string; currentParseInfo?: ChatContextType; optionMode?: boolean; onSelectParseInfo: (parseInfo: ChatContextType) => void; }; const MAX_OPTION_VALUES_COUNT = 2; const ParseTip: React.FC = ({ parseLoading, parseInfoOptions, parseTip, currentParseInfo, optionMode, onSelectParseInfo, }) => { const prefixCls = `${PREFIX_CLS}-item`; if (parseLoading) { return ; } if (parseTip) { return ; } if (parseInfoOptions.length === 0) { return null; } const getTipNode = (parseInfo: ChatContextType, isOptions?: boolean, index?: number) => { const { modelName, dateInfo, dimensionFilters, dimensions, metrics, aggType, queryMode, properties, entity, elementMatches, } = parseInfo || {}; const { startDate, endDate } = dateInfo || {}; const dimensionItems = dimensions?.filter(item => item.type === 'DIMENSION'); const metric = metrics?.[0]; const tipContentClass = classNames(`${prefixCls}-tip-content`, { [`${prefixCls}-tip-content-option`]: isOptions, [`${prefixCls}-tip-content-option-active`]: isOptions && currentParseInfo && JSON.stringify(currentParseInfo) === JSON.stringify(parseInfo), [`${prefixCls}-tip-content-option-disabled`]: isOptions && currentParseInfo !== undefined && JSON.stringify(currentParseInfo) !== JSON.stringify(parseInfo), }); const itemValueClass = classNames({ [`${prefixCls}-tip-item-value`]: !isOptions, [`${prefixCls}-tip-item-option`]: isOptions, }); const entityId = dimensionFilters?.length > 0 ? dimensionFilters[0].value : undefined; const entityAlias = entity?.alias?.[0]?.split('.')?.[0]; const entityName = elementMatches?.find(item => item.element?.type === 'ID')?.element.name; const pluginName = properties?.CONTEXT?.plugin?.name; const modeName = pluginName ? '调插件' : queryMode.includes('METRIC') ? '算指标' : queryMode === 'ENTITY_DETAIL' ? '查明细' : queryMode === 'ENTITY_LIST_FILTER' ? '做圈选' : ''; const fields = queryMode === 'ENTITY_DETAIL' ? dimensionItems?.concat(metrics || []) : dimensionItems; return (
{ if (isOptions && currentParseInfo === undefined) { onSelectParseInfo(parseInfo); } }} > {index !== undefined &&
{index + 1}.
} {!pluginName && isOptions &&
{modeName}:
} {!!pluginName ? (
将由问答插件 {pluginName}来解答
) : ( <> {queryMode.includes('ENTITY') && typeof entityId === 'string' && !!entityAlias && !!entityName ? (
{entityAlias}:
{entityName}
) : (
主题域:
{modelName}
)} {modeName === '算指标' && metric && (
指标:
{metric.name}
)} {!isOptions && (
时间:
{startDate === endDate ? startDate : `${startDate} ~ ${endDate}`}
)} {['METRIC_GROUPBY', 'METRIC_ORDERBY', 'ENTITY_DETAIL'].includes(queryMode) && fields && fields.length > 0 && (
{queryMode === 'ENTITY_DETAIL' ? '查询字段' : '下钻维度'}:
{fields .slice(0, MAX_OPTION_VALUES_COUNT) .map(field => field.name) .join('、')} {fields.length > MAX_OPTION_VALUES_COUNT && '...'}
)} {['METRIC_FILTER', 'METRIC_ENTITY', 'ENTITY_DETAIL', 'ENTITY_LIST_FILTER'].includes( queryMode ) && dimensionFilters && dimensionFilters?.length > 0 && (
筛选条件:
{dimensionFilters.slice(0, MAX_OPTION_VALUES_COUNT).map((filter, index) => (
{filter.name}: {Array.isArray(filter.value) ? filter.value.join('、') : filter.value} {index !== dimensionFilters.length - 1 && }
))} {dimensionFilters.length > MAX_OPTION_VALUES_COUNT && '...'}
)} {queryMode === 'METRIC_ORDERBY' && aggType && aggType !== 'NONE' && (
聚合方式:
{AGG_TYPE_MAP[aggType]}
)} )}
); }; let tipNode: ReactNode; if (parseInfoOptions.length > 1 || optionMode) { tipNode = (
还有以下的相关问题,请您点击提交
{parseInfoOptions.map((item, index) => getTipNode(item, true, index))}
); } else { const pluginName = parseInfoOptions[0]?.properties?.CONTEXT?.plugin?.name; tipNode = (
{!!pluginName ? '您的问题' : '您的问题解析为:'}
{getTipNode(parseInfoOptions[0])}
); } return ; }; export default ParseTip;