import React, { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { Button, Group, Text, ActionIcon, Menu, Box } from '@mantine/core'; import MoreVertIcon from '@mui/icons-material/MoreVert'; import EditIcon from '@mui/icons-material/Edit'; import DeleteIcon from '@mui/icons-material/Delete'; import ContentCopyIcon from '@mui/icons-material/ContentCopy'; import { Tooltip } from '../../shared/Tooltip'; import { ToolRegistryEntry } from '../../../data/toolsTaxonomy'; interface AutomationEntryProps { /** Optional title for the automation (usually for custom ones) */ title?: string; /** Optional description for tooltip */ description?: string; /** MUI Icon component for the badge */ badgeIcon?: React.ComponentType; /** Array of tool operation names in the workflow */ operations: string[]; /** Click handler */ onClick: () => void; /** Whether to keep the icon at normal color (for special cases like "Add New") */ keepIconColor?: boolean; /** Show menu for saved/suggested automations */ showMenu?: boolean; /** Edit handler */ onEdit?: () => void; /** Delete handler */ onDelete?: () => void; /** Copy handler (for suggested automations) */ onCopy?: () => void; /** Tool registry to resolve operation names */ toolRegistry?: Record; } export default function AutomationEntry({ title, description, badgeIcon: BadgeIcon, operations, onClick, keepIconColor = false, showMenu = false, onEdit, onDelete, onCopy, toolRegistry }: AutomationEntryProps) { const { t } = useTranslation(); const [isHovered, setIsHovered] = useState(false); const [isMenuOpen, setIsMenuOpen] = useState(false); // Keep item in hovered state if menu is open const shouldShowHovered = isHovered || isMenuOpen; // Helper function to resolve tool display names const getToolDisplayName = (operation: string): string => { if (toolRegistry?.[operation]?.name) { return toolRegistry[operation].name; } // Fallback to translation or operation key return t(`${operation}.title`, operation); }; // Create tooltip content with description and tool chain const createTooltipContent = () => { // Show tooltip if there's a description OR if there are operations to show in the chain if (!description && operations.length === 0) return null; const toolChain = operations.map((op, index) => ( {getToolDisplayName(op)} {index < operations.length - 1 && ( )} )); return (
{description && ( {description} )} {operations.length > 0 && (
{toolChain}
)}
); }; const renderContent = () => { if (title) { // Custom automation with title return ( {BadgeIcon && ( )} {title} ); } else { // Suggested automation showing tool chain return ( {BadgeIcon && ( )} {operations.map((op, index) => ( {getToolDisplayName(op)} {index < operations.length - 1 && ( )} ))} ); } }; const boxContent = ( setIsHovered(true)} onMouseLeave={() => setIsHovered(false)} >
{renderContent()}
{showMenu && ( setIsMenuOpen(true)} onClose={() => setIsMenuOpen(false)} > e.stopPropagation()} style={{ opacity: shouldShowHovered ? 1 : 0, transform: shouldShowHovered ? 'scale(1)' : 'scale(0.8)', transition: 'opacity 0.3s ease, transform 0.3s ease', pointerEvents: shouldShowHovered ? 'auto' : 'none' }} > {onCopy && ( } onClick={(e) => { e.stopPropagation(); onCopy(); }} > {t('automate.copyToSaved', 'Copy to Saved')} )} {onEdit && ( } onClick={(e) => { e.stopPropagation(); onEdit(); }} > {t('edit', 'Edit')} )} {onDelete && ( } onClick={(e) => { e.stopPropagation(); onDelete(); }} > {t('delete', 'Delete')} )} )}
); // Show tooltip if there's a description OR operations to display const shouldShowTooltip = description || operations.length > 0; return shouldShowTooltip ? ( {boxContent} ) : ( boxContent ); }