2025-07-16 17:53:50 +01:00
|
|
|
import React, { createContext, useContext, useMemo, useRef } from 'react';
|
2025-08-01 14:22:19 +01:00
|
|
|
import { Paper, Text, Stack, Box, Flex } from '@mantine/core';
|
|
|
|
import ExpandMoreIcon from '@mui/icons-material/ExpandMore';
|
|
|
|
import ChevronRightIcon from '@mui/icons-material/ChevronRight';
|
2025-07-16 17:53:50 +01:00
|
|
|
|
|
|
|
interface ToolStepContextType {
|
|
|
|
visibleStepCount: number;
|
|
|
|
getStepNumber: () => number;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ToolStepContext = createContext<ToolStepContextType | null>(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;
|
|
|
|
|
2025-08-01 14:22:19 +01:00
|
|
|
const parent = useContext(ToolStepContext);
|
|
|
|
|
2025-07-16 17:53:50 +01:00
|
|
|
// 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;
|
2025-08-01 14:22:19 +01:00
|
|
|
}, [showNumber, parent]);
|
2025-07-16 17:53:50 +01:00
|
|
|
|
|
|
|
const stepNumber = useContext(ToolStepContext)?.getStepNumber?.() || 1;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Paper
|
|
|
|
p="md"
|
|
|
|
withBorder
|
|
|
|
style={{
|
|
|
|
opacity: isCollapsed ? 0.8 : 1,
|
|
|
|
transition: 'opacity 0.2s ease'
|
|
|
|
}}
|
|
|
|
>
|
2025-08-01 14:22:19 +01:00
|
|
|
{/* Chevron icon to collapse/expand the step */}
|
|
|
|
<Flex
|
|
|
|
align="center"
|
|
|
|
justify="space-between"
|
|
|
|
mb="sm"
|
|
|
|
style={{
|
|
|
|
cursor: onCollapsedClick ? 'pointer' : 'default'
|
|
|
|
}}
|
|
|
|
onClick={onCollapsedClick}
|
|
|
|
>
|
|
|
|
<Flex align="center" gap="sm">
|
|
|
|
{shouldShowNumber && (
|
|
|
|
<Text fw={500} size="lg" c="dimmed">
|
|
|
|
{stepNumber}
|
|
|
|
</Text>
|
|
|
|
)}
|
|
|
|
<Text fw={500} size="lg">
|
|
|
|
{title}
|
|
|
|
</Text>
|
|
|
|
</Flex>
|
|
|
|
|
|
|
|
{isCollapsed ? (
|
|
|
|
<ChevronRightIcon style={{
|
|
|
|
fontSize: '1.2rem',
|
|
|
|
color: 'var(--mantine-color-dimmed)',
|
|
|
|
opacity: onCollapsedClick ? 1 : 0.5
|
|
|
|
}} />
|
|
|
|
) : (
|
|
|
|
<ExpandMoreIcon style={{
|
|
|
|
fontSize: '1.2rem',
|
|
|
|
color: 'var(--mantine-color-dimmed)',
|
|
|
|
opacity: onCollapsedClick ? 1 : 0.5
|
|
|
|
}} />
|
|
|
|
)}
|
|
|
|
</Flex>
|
2025-07-16 17:53:50 +01:00
|
|
|
|
|
|
|
{isCollapsed ? (
|
|
|
|
<Box>
|
|
|
|
{isCompleted && completedMessage && (
|
|
|
|
<Text size="sm" c="green">
|
|
|
|
✓ {completedMessage}
|
|
|
|
{onCollapsedClick && (
|
|
|
|
<Text span c="dimmed" size="xs" ml="sm">
|
|
|
|
(click to change)
|
|
|
|
</Text>
|
|
|
|
)}
|
|
|
|
</Text>
|
|
|
|
)}
|
|
|
|
</Box>
|
|
|
|
) : (
|
|
|
|
<Stack gap="md">
|
|
|
|
{helpText && (
|
|
|
|
<Text size="sm" c="dimmed">
|
|
|
|
{helpText}
|
|
|
|
</Text>
|
|
|
|
)}
|
|
|
|
{children}
|
|
|
|
</Stack>
|
|
|
|
)}
|
|
|
|
</Paper>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2025-08-01 14:22:19 +01:00
|
|
|
const isVisible = (child.props as ToolStepProps).isVisible !== false;
|
2025-07-16 17:53:50 +01:00
|
|
|
if (isVisible) count++;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return count;
|
|
|
|
}, [children]);
|
|
|
|
|
|
|
|
const contextValue = useMemo(() => ({
|
|
|
|
visibleStepCount,
|
|
|
|
getStepNumber: () => ++stepCounterRef.current
|
|
|
|
}), [visibleStepCount]);
|
|
|
|
|
|
|
|
stepCounterRef.current = 0;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ToolStepContext.Provider value={contextValue}>
|
|
|
|
{children}
|
|
|
|
</ToolStepContext.Provider>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ToolStep;
|