import { Alert, Box, Code, Collapsible, HStack, Span, Stack, Text, useCollapsible, type UseCollapsibleReturn } from '@chakra-ui/react'; import { Fragment, useEffect, useState, type ComponentProps } from 'react'; import { isErrorIsh, type ErrorIsh } from '../../core/ErrorUtils'; import { ChakraClip } from './ChakraClipboard'; import { ChakraCodeBlock } from './CodeBlock'; import { EllipsisButton, FatArrowRight } from './icons/ChakraIcons'; export interface ErrorAlertProps { error: ErrorIsh status?: "error" | "info" | "warning" | "success" | "neutral" } const ErrorBlock = (props: {data: ErrorData, cause?: boolean, messageProps?: ComponentProps, collapsible: UseCollapsibleReturn}) => { const { data, cause, collapsible, messageProps = {} } = props; const [open, setOpen] = useState(collapsible.open); useEffect(() => { setOpen(collapsible.open); }, [setOpen, collapsible]); const textProps: ComponentProps = {...(cause ? {color: "fg.muted"} : {}), ...messageProps}; const containerProps: ComponentProps = {}; if(cause) { containerProps.paddingLeft = "2"; } const errorIdentifier = `${data.name ?? ''}${data.code !== undefined ? ` (${data.code}) ` : ''}`; const errorElm = errorIdentifier === '' ? null : {errorIdentifier} const messageElm: React.JSX.Element = ( {cause ? : null} {cause ? Caused By: : ''}{errorElm}{data.message} ); if(data.stack === undefined) { return messageElm; } return ( <> {messageElm} setOpen(val.open)}> {messageElm} ) } export const ErrorAlert = (props: ErrorAlertProps) => { if(!isErrorIsh(props.error)) { return null; } let causes: ErrorData[] = []; if(isErrorIsh(props.error.cause)) { causes = walkError(props.error.cause); } const collapsible = useCollapsible() return ( {props.error.name ?? 'Error'} collapsible.setOpen(!collapsible.open)}/> {causes.map((x, index) => ( ))} ) } interface ErrorData { name?: string code?: string message?: string stack?: string } const walkError = (err: ErrorIsh, errors: ErrorData[] = []): ErrorData[] => { const thisErr: ErrorData = { name: err.name, code: 'code' in err ? err.code : undefined, message: err.message, stack: err.stack }; errors.push(thisErr); if(isErrorIsh(err.cause)) { return walkError(err.cause, errors); } return errors; }