/* eslint-disable max-lines */ import React, { useState, useEffect } from 'react'; import { observer } from 'mobx-react-lite'; import { CircularProgress, SxProps } from '@mui/material'; import Box from '@mui/material/Box'; import Tooltip, { TooltipProps, tooltipClasses } from '@mui/material/Tooltip'; import { styled } from '@mui/material/styles'; import Paper from '@mui/material/Paper'; import InputBase from '@mui/material/InputBase'; import IconButton from '@mui/material/IconButton'; // import SendIcon from '@mui/icons-material/Send'; import _ from 'lodash'; // import { autorun } from 'mobx'; // import { // fakeAgentScoreMap, // fakeAgentSelections, // fakeCurrentAgentSelection, // } from './data/fakeAgentAssignment'; import CheckIcon from '@/icons/checkIcon'; import AgentIcon from '@/components/AgentIcon'; import { globalStorage } from '@/storage'; import SendIcon from '@/icons/SendIcon'; const HtmlTooltip = styled(({ className, ...props }: TooltipProps) => ( ))(({ theme }) => ({ [`& .${tooltipClasses.tooltip}`]: { backgroundColor: '#e7e7e7', color: 'rgba(0, 0, 0, 0.87)', width: 'fit-content', fontSize: theme.typography.pxToRem(12), border: '1px solid #dadde9', }, })); const getHeatColor = (value: number) => { return `rgba(74, 156, 158,${value / 5})`; }; const AgentScoreCell: React.FC<{ data: { score: number; reason: string }; }> = ({ data }) => { return ( Score Reason: {data.reason} } followCursor placement="right-start" > {data.score} ); }; const AspectCell: React.FC<{ key?: string; aspect?: string; style?: SxProps; isSelected?: boolean; handleSelected?: (aspect: string) => void; }> = ({ key, aspect, style, isSelected, handleSelected }) => { const mystyle: SxProps = { width: '150px', height: '35px', position: 'sticky', // 使得这个Box成为sticky元素 right: -1, // 0距离左侧,这将确保它卡在左侧 backgroundColor: '#ffffff', // 防止滚动时格子被内容覆盖 zIndex: 1, // 确保它在其他内容上方 display: 'flex', alignItems: 'center', paddingLeft: '8px', fontSize: '14px', lineHeight: '1', borderBottom: '2px solid #ffffff', ...style, }; if (!aspect) { return ; } return ( { if (handleSelected) { handleSelected(aspect); } }} > {aspect || ''} ); }; const EmotionInput: React.FC<{ inputCallback: (arg0: string) => void; }> = ({ inputCallback }) => { const [inputValue, setInputValue] = useState(''); const inputRef = React.useRef(); const handleInputChange = (event: any) => { setInputValue(event.target.value); }; const handleButtonClick = () => { inputCallback(inputValue); setInputValue(''); }; return ( ); }; const findSameSelectionId = ( a: Record< string, { id: string; agents: string[]; } >, b: Set, ) => { const sortedB = Array.from(b).slice().sort(); // 对 b 进行排序 const bString = sortedB.join(','); const akeys = Object.keys(a); for (const akey of akeys) { const sortedA = a[akey].agents.slice().sort(); // 对 a 中的每个数组进行排序 if (sortedA.join(',') === bString) { return akey; // 找到相同数组则返回索引 } } return undefined; // 未找到相同数组 }; interface IPlanModification { style?: SxProps; } export default observer(({ style = {} }: IPlanModification) => { // console.log(prop); const { agentMap, renderingAgentSelections, api: { agentsReady }, } = globalStorage; // autorun(() => { // console.log(renderingAgentSelections); // }); const [agentSelections, setAgentSelections] = React.useState< Record< string, { id: string; agents: string[]; } > >({}); const [currentAgentSelection, setCurrentSelection] = React.useState< string | undefined >(); const [heatdata, setHeatdata] = React.useState< Record< string, Record< string, { score: number; reason: string; } > > >({}); const [aspectSelectedSet, setAspectSelectedSet] = React.useState( new Set(), ); useEffect(() => { if (renderingAgentSelections.current) { setAgentSelections(renderingAgentSelections.selections); setHeatdata(renderingAgentSelections.heatdata); setCurrentSelection(renderingAgentSelections.current); setAgentSelectedSet( new Set( renderingAgentSelections.selections[ renderingAgentSelections.current ].agents, ), ); } }, [renderingAgentSelections]); const handleAspectSelected = (aspect: string) => { const newSet = new Set(aspectSelectedSet); if (newSet.has(aspect)) { newSet.delete(aspect); } else { newSet.add(aspect); } setAspectSelectedSet(newSet); }; const [agentSelectedSet, setAgentSelectedSet] = React.useState( new Set(), ); const handleAgentSelected = (agent: string) => { const newSet = new Set(agentSelectedSet); if (newSet.has(agent)) { newSet.delete(agent); } else { newSet.add(agent); } setAgentSelectedSet(newSet); }; const [agentKeyList, setAgentKeyList] = useState([]); useEffect(() => { // 计算平均分的函数 function calculateAverageScore(agent: string) { const aspects = aspectSelectedSet.size ? Array.from(aspectSelectedSet) : Object.keys(heatdata); const meanScore = _.mean( aspects.map(aspect => heatdata[aspect]?.[agent]?.score ?? 0), ); return meanScore; } // 对agentMap.keys()进行排序 const newAgentKeyList = Array.from(agentMap.keys()).sort((a, b) => { const isSelectedA = agentSelectedSet.has(a); const isSelectedB = agentSelectedSet.has(b); if (isSelectedA && !isSelectedB) { return -1; } else if (!isSelectedA && isSelectedB) { return 1; } else { const averageScoreA = calculateAverageScore(a); const averageScoreB = calculateAverageScore(b); // 降序排序(平均分高的在前) return averageScoreB - averageScoreA; } }); setAgentKeyList(newAgentKeyList); }, [agentMap, heatdata, aspectSelectedSet, agentSelectedSet]); if (!agentsReady) { return <>; } return ( {globalStorage.api.agentAspectScoresGenerating && ( )} {/* assignments */} Assignment {Object.keys(agentSelections).map(selectionId => ( { if (selectionId === currentAgentSelection) { return '2px solid #508a87'; } return '2px solid #afafaf'; })(), borderRadius: '10px', margin: '4px 0px', padding: '4px 0px 4px 0px', backgroundColor: '#f6f6f6', cursor: 'pointer', display: 'flex', justifyContent: 'center', // 添加这一行 alignItems: 'center', // 添加这一行 flexWrap: 'wrap', }} onClick={() => { globalStorage.setCurrentAgentSelection(selectionId); }} > {agentSelections[selectionId].agents.map(agentName => ( ))} ))} {/* comparison */} Comparison {agentSelectedSet.size > 0 && (
{ const findSelectionId = findSameSelectionId( agentSelections, agentSelectedSet, ); if (findSelectionId) { globalStorage.setCurrentAgentSelection(findSelectionId); } else { globalStorage.addAgentSelection( Array.from(agentSelectedSet), ); } }} >
)} {agentKeyList.map((agentKey, agentIndex) => ( { handleAgentSelected(agentKey); }} style={{ display: 'grid', placeItems: 'center', gridColumn: `${agentIndex + 1} / ${agentIndex + 2}`, gridRow: '1 / 2', height: '100%', width: '100%', padding: '0px 0px', cursor: 'pointer', }} > {Object.keys(heatdata).map(aspect => { return ( ); })} ))} {Object.keys(heatdata).map(aspect => ( ))}
{ globalStorage.addAgentSelectionAspects(arg0); }} />
); // return hhh; }); /* eslint-enable max-lines */