import React, { ReactNode } from 'react'; import { AGG_TYPE_MAP, PREFIX_CLS } from '../../common/constants'; import { ChatContextType } from '../../common/type'; import { CheckCircleFilled, InfoCircleOutlined } from '@ant-design/icons'; import classNames from 'classnames'; import SwicthEntity from './SwitchEntity'; import { Tooltip } from 'antd'; import Loading from './Loading'; type Props = { parseLoading: boolean; parseInfoOptions: ChatContextType[]; parseTip: string; currentParseInfo?: ChatContextType; optionMode?: boolean; onSelectParseInfo: (parseInfo: ChatContextType) => void; onSwitchEntity: (entityId: string) => void; }; const MAX_OPTION_VALUES_COUNT = 2; const ParseTip: React.FC = ({ parseLoading, parseInfoOptions, parseTip, currentParseInfo, optionMode, onSelectParseInfo, onSwitchEntity, }) => { const prefixCls = `${PREFIX_CLS}-item`; const getNode = (tipTitle: string, tipNode?: ReactNode, parseSucceed?: boolean) => { const contentContainerClass = classNames(`${prefixCls}-content-container`, { [`${prefixCls}-content-container-succeed`]: parseSucceed, }); return (
{tipTitle} {!tipNode && }
{tipNode &&
{tipNode}
}
); }; if (parseLoading) { return getNode('意图解析中'); } if (parseTip) { return getNode('意图解析失败', parseTip); } 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 { type: agentType, name: agentName } = properties || {}; const fields = queryMode === 'ENTITY_DETAIL' ? dimensionItems?.concat(metrics || []) : dimensionItems; const getFilterContent = (filters: any) => { return (
{filters.map((filter: any, index: number) => (
{filter.name} {filter.operator !== '=' ? ` ${filter.operator} ` : ':'} {Array.isArray(filter.value) ? filter.value.join('、') : filter.value} {index !== filters.length - 1 && }
))}
); }; const getFiltersNode = () => { return (
筛选条件:
MAX_OPTION_VALUES_COUNT ? getFilterContent(dimensionFilters) : '' } color="#fff" overlayStyle={{ maxWidth: 'none' }} >
{getFilterContent(dimensionFilters.slice(0, MAX_OPTION_VALUES_COUNT))} {dimensionFilters.length > MAX_OPTION_VALUES_COUNT && ' ...'}
); }; return (
{ if (isOptions && currentParseInfo === undefined) { onSelectParseInfo(parseInfo); } }} > {index !== undefined &&
{index + 1}.
} {!!agentType && queryMode !== 'DSL' ? (
将由{agentType === 'plugin' ? '插件' : '内置'}工具 {agentName}来解答
) : ( <> {(queryMode.includes('ENTITY') || queryMode === 'DSL') && typeof entityId === 'string' && !!entityAlias && !!entityName ? (
{entityAlias}:
{!isOptions && (entityAlias === '歌曲' || entityAlias === '艺人') ? ( ) : (
{entityName}
)}
) : (
主题域:
{modelName}
)} {queryMode !== 'ENTITY_ID' && 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', 'ENTITY_ID', 'DSL', ].includes(queryMode) && dimensionFilters && dimensionFilters?.length > 0 && getFiltersNode()} {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 { type } = parseInfoOptions[0]?.properties || {}; const entityAlias = parseInfoOptions[0]?.entity?.alias?.[0]?.split('.')?.[0]; const entityName = parseInfoOptions[0]?.elementMatches?.find( item => item.element?.type === 'ID' )?.element.name; const queryMode = parseInfoOptions[0]?.queryMode; tipNode = (
{getTipNode(parseInfoOptions[0])} {(!type || queryMode === 'DSL') && entityAlias && entityName && (
如果未匹配到您查询的{entityAlias},可点击上面的{entityAlias}名切换
)}
); } return getNode('意图解析结果', tipNode, true); }; export default ParseTip;