import React, { createContext, useContext, useMemo, useRef } from 'react'; import { Paper, Text, Stack, Box, Flex } from '@mantine/core'; import ExpandMoreIcon from '@mui/icons-material/ExpandMore'; import ChevronRightIcon from '@mui/icons-material/ChevronRight'; import { Tooltip } from '../../shared/Tooltip'; import { TooltipTip } from '../../shared/tooltip/TooltipContent'; interface ToolStepContextType { visibleStepCount: number; } const ToolStepContext = createContext(null); export interface ToolStepProps { title: string; isVisible?: boolean; isCollapsed?: boolean; isCompleted?: boolean; onCollapsedClick?: () => void; children?: React.ReactNode; completedMessage?: string; helpText?: string; showNumber?: boolean; _stepNumber?: number; // Internal prop set by ToolStepContainer tooltip?: { content?: React.ReactNode; tips?: TooltipTip[]; header?: { title: string; logo?: React.ReactNode; }; }; } const renderTooltipTitle = ( title: string, tooltip: ToolStepProps['tooltip'], isCollapsed: boolean ) => { if (tooltip && !isCollapsed) { return ( e.stopPropagation()}> {title} gpp_maybe ); } return ( {title} ); }; const ToolStep = ({ title, isVisible = true, isCollapsed = false, isCompleted = false, onCollapsedClick, children, completedMessage, helpText, showNumber, _stepNumber, tooltip }: ToolStepProps) => { if (!isVisible) return null; const parent = useContext(ToolStepContext); // Auto-detect if we should show numbers based on sibling count const shouldShowNumber = useMemo(() => { if (showNumber !== undefined) return showNumber; return parent ? parent.visibleStepCount >= 3 : false; }, [showNumber, parent]); const stepNumber = _stepNumber || 1; return ( {/* Chevron icon to collapse/expand the step */} {shouldShowNumber && ( {stepNumber} )} {renderTooltipTitle(title, tooltip, isCollapsed)} {isCollapsed ? ( ) : ( )} {isCollapsed ? ( {isCompleted && completedMessage && ( ✓ {completedMessage} {onCollapsedClick && ( (click to change) )} )} ) : ( {helpText && ( {helpText} )} {children} )} ); } export interface ToolStepContainerProps { children: React.ReactNode; } export const ToolStepContainer = ({ children }: ToolStepContainerProps) => { // Process children and inject step numbers for visible ToolSteps const processedChildren = useMemo(() => { let visibleStepNumber = 1; return React.Children.map(children, (child) => { if (React.isValidElement(child) && child.type === ToolStep) { const isVisible = (child.props as ToolStepProps).isVisible !== false; if (isVisible) { return React.cloneElement(child, { ...child.props, _stepNumber: visibleStepNumber++ } as ToolStepProps); } } return child; }); }, [children]); const visibleStepCount = useMemo(() => { let count = 0; React.Children.forEach(children, (child) => { if (React.isValidElement(child) && child.type === ToolStep) { const isVisible = (child.props as ToolStepProps).isVisible !== false; if (isVisible) count++; } }); return count; }, [children]); const contextValue = useMemo(() => ({ visibleStepCount }), [visibleStepCount]); return ( {processedChildren} ); } export default ToolStep;