Files
supersonic/webapp/packages/chat-sdk/src/components/MetricOptions/index.tsx

76 lines
2.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { CLS_PREFIX } from '../../common/constants';
import { FieldType } from '../../common/type';
import classNames from 'classnames';
import { isMobile } from '../../utils/utils';
type Props = {
metrics: FieldType[];
defaultMetric?: FieldType;
currentMetric?: FieldType;
isMetricCard?: boolean;
onSelectMetric: (metric?: FieldType) => void;
};
const MetricOptions: React.FC<Props> = ({
metrics,
defaultMetric,
currentMetric,
isMetricCard,
onSelectMetric,
}) => {
const DEFAULT_DIMENSION_COUNT = isMobile ? 2 : 5;
const prefixCls = `${CLS_PREFIX}-metric-options`;
const defaultMetrics = metrics
.filter(metric => metric.id !== defaultMetric?.id)
.slice(0, DEFAULT_DIMENSION_COUNT);
const sectionClass = classNames(`${prefixCls}-section`, {
[`${prefixCls}-metric-card`]: isMetricCard,
});
if (!defaultMetrics.length) {
return null;
}
return (
<div className={prefixCls}>
<div className={sectionClass}>
<div className={`${prefixCls}-title`}></div>
<div className={`${prefixCls}-content`}>
{defaultMetrics.map((metric, index) => {
const itemNameClass = classNames(`${prefixCls}-content-item-name`, {
[`${prefixCls}-content-item-active`]: currentMetric?.id === metric.id,
});
return (
<div>
<span
className={itemNameClass}
onClick={() => {
onSelectMetric(currentMetric?.id === metric.id ? defaultMetric : metric);
}}
>
{metric.name}
</span>
{index !== defaultMetrics.length - 1 && <span></span>}
</div>
);
})}
</div>
{currentMetric?.id !== defaultMetric?.id && (
<div
className={`${prefixCls}-cancel-select`}
onClick={() => {
onSelectMetric(defaultMetric);
}}
>
</div>
)}
</div>
</div>
);
};
export default MetricOptions;