import type { ReactElement } from 'react'; import React, { isValidElement, useCallback, useEffect, useState } from 'react'; import type { ImageSourcePropType, ViewProps, ImageProps as RNImageProps, } from 'react-native'; import type { ErrorVariant } from './StyledError'; import { StyledErrorButtonContainer, StyledErrorButtonSecondary, StyledErrorContainer, StyledErrorContent, StyledErrorDescription, StyledErrorIconContainer, StyledErrorImage, StyledErrorImageContainer, StyledErrorModal, StyledErrorTitle, } from './StyledError'; import { useDeprecation } from '../../../utils/hooks'; import type { ImageProps } from '../../Image/index'; import Illustration from '../../Illustration'; import type { IllustrationName } from '../../Illustration'; import Button from '../../Button'; interface ErrorProps extends ViewProps { /** * @deprecated use conditional rendering instead * * Visibility of the error */ visible?: boolean; /** * Variant of the error */ variant?: ErrorVariant; /** * Title of the error */ title: string | ReactElement; /** * Description of the error */ description?: string; /** * Image of the error * @deprecated The `image` prop is deprecated and will be removed in the next major version. Use the `icon` prop instead. */ image?: | ReactElement | ImageSourcePropType | string; /** * Testing id of the component. */ testID?: string; /** * Action button text */ ctaText?: string; /** * Callback when the action button is pressed. */ onCtaPress?: () => void; /** * Secondary button text */ secondaryCtaText?: string; /** * Callback when the secondary button is pressed. */ onSecondaryCtaPress?: () => void; /** * Status icon to be displayed, this will replace the deprecated image prop. */ icon?: IllustrationName; } type onCloseCallbackType = 'cta' | 'secondaryCta' | null; const renderImage = ( image: ReactElement | ImageSourcePropType | string ) => { if (isValidElement(image)) { return React.cloneElement(image, { testID: 'error-image', }); } return ( ); }; const renderImageOrIcon = ({ image, icon, }: { image?: | ReactElement | ImageSourcePropType | string; icon?: IllustrationName; }) => { if (icon) { return ( ); } if (image) { return ( {renderImage(image)} ); } return null; }; const ErrorPage = ({ variant = 'in-page', title, description, image, testID, ctaText, onCtaPress, secondaryCtaText, onSecondaryCtaPress, icon, ...nativeProps }: ErrorProps): ReactElement => { const showCta = ctaText && onCtaPress !== undefined; const showSecondaryCta = secondaryCtaText && onSecondaryCtaPress !== undefined; const showButtonContainer = showCta || showSecondaryCta; return ( {renderImageOrIcon({ image, icon })} {title} {description && ( {description} )} {showButtonContainer && ( {showCta && (