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'; interface ToolStepContextType { visibleStepCount: number; getStepNumber: () => 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; } const ToolStep = ({ title, isVisible = true, isCollapsed = false, isCompleted = false, onCollapsedClick, children, completedMessage, helpText, showNumber }: 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 = useContext(ToolStepContext)?.getStepNumber?.() || 1; return ( {/* Chevron icon to collapse/expand the step */} {shouldShowNumber && ( {stepNumber} )} {title} {isCollapsed ? ( ) : ( )} {isCollapsed ? ( {isCompleted && completedMessage && ( ✓ {completedMessage} {onCollapsedClick && ( (click to change) )} )} ) : ( {helpText && ( {helpText} )} {children} )} ); } export interface ToolStepContainerProps { children: React.ReactNode; } export const ToolStepContainer = ({ children }: ToolStepContainerProps) => { const stepCounterRef = useRef(0); // Count visible ToolStep 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, getStepNumber: () => ++stepCounterRef.current }), [visibleStepCount]); stepCounterRef.current = 0; return ( {children} ); } export default ToolStep;