Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

4.1 KB
Raw Blame
import { createStaticStyles, Tooltip } from '@lobehub/ui';
import { BanIcon, CheckIcon, HandIcon } from 'lucide-react';
import { memo } from 'react';
import { useTranslation } from 'react-i18next';

import { ConnectorToolPermission } from '@/database/schemas';
import type { ConnectorTool } from '@/store/tool/slices/connector';
import { densityVars } from '@/styles/densityVars';

const styles = createStaticStyles(({ css, cssVar }) => ({
  btn: css`
    cursor: pointer;

    display: flex;
    align-items: center;
    justify-content: center;

    width: 28px;
    height: 28px;
    border-radius: 6px;

    color: ${cssVar.colorTextQuaternary};

    transition:
      color 0.15s,
      background 0.15s;

    &:hover {
      color: ${cssVar.colorText};
      background: ${cssVar.colorFillSecondary};
    }
  `,
  btnActive: css`
    color: ${cssVar.colorPrimary};
    background: ${cssVar.colorPrimaryBg};

    &:hover {
      color: ${cssVar.colorPrimary};
      background: ${cssVar.colorPrimaryBgHover};
    }
  `,
  description: css`
    overflow: hidden;

    font-size: 11px;
    line-height: 1.4;
    color: ${cssVar.colorTextTertiary};
    text-overflow: ellipsis;
    white-space: nowrap;
  `,
  nameCell: css`
    overflow: hidden;
    flex: 1;
    min-width: 0;
  `,
  row: css`
    display: flex;
    gap: 8px;
    align-items: center;

    padding-block: 8px;
    padding-inline: ${densityVars.contentGap};
    border-block-end: 1px solid ${cssVar.colorBorderSecondary};

    &:last-child {
      border-block-end: none;
    }

    &:hover {
      background: ${cssVar.colorFillQuaternary};
    }
  `,
  toolName: css`
    overflow: hidden;

    font-family: ${cssVar.fontFamilyCode};
    font-size: 13px;
    color: ${cssVar.colorText};
    text-overflow: ellipsis;
    white-space: nowrap;
  `,
}));

interface ToolPermissionRowProps {
  /** Read-only mode — the caller lacks the manage permission for this connector. */
  disabled?: boolean;
  onPermissionChange: (toolId: string, permission: ConnectorToolPermission) => void;
  tool: ConnectorTool;
}

const ToolPermissionRow = memo<ToolPermissionRowProps>(({ disabled, tool, onPermissionChange }) => {
  const { t } = useTranslation('tool');
  const btnClass = (permission: ConnectorToolPermission) =>
    tool.permission === permission ? `${styles.btn} ${styles.btnActive}` : styles.btn;

  const handleChange = (permission: ConnectorToolPermission) => {
    if (disabled) return;
    onPermissionChange(tool.id, permission);
  };

  return (
    <div className={styles.row}>
      <div className={styles.nameCell}>
        <div className={styles.toolName}>{tool.toolName}</div>
        {tool.description && (
          <Tooltip mouseEnterDelay={0.5} title={tool.description}>
            <div className={styles.description}>{tool.description}</div>
          </Tooltip>
        )}
      </div>
      <div
        style={{
          display: 'flex',
          flexShrink: 0,
          gap: 2,
          ...(disabled && { cursor: 'not-allowed', opacity: 0.45 }),
        }}
      >
        <div
          className={btnClass(ConnectorToolPermission.auto)}
          style={disabled ? { pointerEvents: 'none' } : undefined}
          title={t('connector.permission.auto')}
          onClick={() => handleChange(ConnectorToolPermission.auto)}
        >
          <CheckIcon size={15} />
        </div>
        <div
          className={btnClass(ConnectorToolPermission.needs_approval)}
          style={disabled ? { pointerEvents: 'none' } : undefined}
          title={t('connector.permission.needsApproval')}
          onClick={() => handleChange(ConnectorToolPermission.needs_approval)}
        >
          <HandIcon size={15} />
        </div>
        <div
          className={btnClass(ConnectorToolPermission.disabled)}
          style={disabled ? { pointerEvents: 'none' } : undefined}
          title={t('connector.permission.disabled')}
          onClick={() => handleChange(ConnectorToolPermission.disabled)}
        >
          <BanIcon size={15} />
        </div>
      </div>
    </div>
  );
});

ToolPermissionRow.displayName = 'ToolPermissionRow';

export default ToolPermissionRow;