|
| 1 | +import { BAISessionAgentIdsFragment$key } from '../../__generated__/BAISessionAgentIdsFragment.graphql'; |
| 2 | +import BAIFlex from '../BAIFlex'; |
| 3 | +import { CopyOutlined } from '@ant-design/icons'; |
| 4 | +import { Popover, Typography, Button, theme } from 'antd'; |
| 5 | +import _ from 'lodash'; |
| 6 | +import React, { useMemo } from 'react'; |
| 7 | +import { CopyToClipboard } from 'react-copy-to-clipboard'; |
| 8 | +import { useTranslation } from 'react-i18next'; |
| 9 | +import { graphql, useFragment } from 'react-relay'; |
| 10 | + |
| 11 | +interface BAISessionAgentIdsProps { |
| 12 | + sessionFrgmt: BAISessionAgentIdsFragment$key; |
| 13 | + maxInline?: number; // New prop to control max inline display |
| 14 | + emptyText?: string; // New prop for empty state text |
| 15 | +} |
| 16 | +export const BAISessionAgentIds: React.FC<BAISessionAgentIdsProps> = ({ |
| 17 | + sessionFrgmt, |
| 18 | + maxInline = 3, |
| 19 | + emptyText = '-', |
| 20 | +}) => { |
| 21 | + const { t } = useTranslation(); |
| 22 | + const { token } = theme.useToken(); |
| 23 | + const session = useFragment( |
| 24 | + graphql` |
| 25 | + fragment BAISessionAgentIdsFragment on ComputeSessionNode { |
| 26 | + agent_ids |
| 27 | + } |
| 28 | + `, |
| 29 | + sessionFrgmt, |
| 30 | + ); |
| 31 | + |
| 32 | + const agents = useMemo( |
| 33 | + () => _.uniq(session.agent_ids ?? []), |
| 34 | + [session.agent_ids], |
| 35 | + ); |
| 36 | + |
| 37 | + const inline = agents.slice(0, maxInline).join(', '); |
| 38 | + const rest = agents.slice(maxInline); |
| 39 | + const restCount = _.max([agents.length - maxInline, 0]) || 0; |
| 40 | + |
| 41 | + return agents.length === 0 ? ( |
| 42 | + emptyText |
| 43 | + ) : ( |
| 44 | + <span> |
| 45 | + <Typography.Text>{inline}</Typography.Text> |
| 46 | + {restCount > 0 && ( |
| 47 | + <> |
| 48 | + |
| 49 | + <Popover |
| 50 | + trigger="click" |
| 51 | + title={ |
| 52 | + <BAIFlex justify="between"> |
| 53 | + <span> |
| 54 | + {t('comp:BAISessionAgentIds.Agent')} ({agents.length}) |
| 55 | + </span> |
| 56 | + <CopyToClipboard text={agents.join(', ')}> |
| 57 | + <Button size="small" type="text" icon={<CopyOutlined />}> |
| 58 | + {t('general.button.CopyAll')} |
| 59 | + </Button> |
| 60 | + </CopyToClipboard> |
| 61 | + </BAIFlex> |
| 62 | + } |
| 63 | + content={ |
| 64 | + <div style={{ maxHeight: 240, overflow: 'auto', minWidth: 260 }}> |
| 65 | + <ul style={{ paddingLeft: token.padding, margin: 0 }}> |
| 66 | + {rest.map((id) => ( |
| 67 | + <li key={id} style={{ listStyle: 'disc' }}> |
| 68 | + <Typography.Text>{id}</Typography.Text> |
| 69 | + </li> |
| 70 | + ))} |
| 71 | + </ul> |
| 72 | + </div> |
| 73 | + } |
| 74 | + > |
| 75 | + <Typography.Link>+{restCount}</Typography.Link> |
| 76 | + </Popover> |
| 77 | + </> |
| 78 | + )} |
| 79 | + </span> |
| 80 | + ); |
| 81 | +}; |
0 commit comments