import type { PropsWithChildren, ReactElement } from "react"; import React, { useState } from "react"; import type { GestureResponderEvent } from "react-native"; import { Pressable, Text as RNText, View } from "react-native"; import type { IconNames } from "@jobber/design"; import type { BannerProps, BannerTypes } from "./types"; import { useStyles } from "./Banner.style"; import { BannerIcon } from "./components/BannerIcon/BannerIcon"; import { Text } from "../Text"; import { TextList } from "../TextList"; import { ActionLabel } from "../ActionLabel"; import { Typography } from "../Typography"; import { useAtlantisI18n } from "../hooks/useAtlantisI18n"; import { Icon } from "../Icon"; export function Banner({ action, details, dismissAccessibilityLabel, dismissible = false, text, type, children, icon, onDismiss, }: BannerProps) { const bannerIcon = icon || getBannerIcon(type); const [isVisible, setIsVisible] = useState(true); const { t } = useAtlantisI18n(); const shouldFlow = Boolean(React.Children.count(children) === 1 && !text && !details) || Boolean(text && !details && !children); const styles = useStyles(); if (!isVisible) return null; const handleDismiss = (event?: GestureResponderEvent) => { event?.stopPropagation(); setIsVisible(false); onDismiss?.(); }; const bannerContent = ( <> {bannerIcon && } {children} {text && {text}} {details && } {action && ( {shouldFlow && | } {action.label} )} ); const renderBannerMainContent = () => { const bannerMainContentStyles = [ styles.bannerMainContent, !dismissible && styles.bannerMainContentWithoutDismiss, ]; if (!action) { return ( {bannerContent} ); } return ( {bannerContent} ); }; return ( {renderBannerMainContent()} {dismissible && ( )} ); } function BannerChildren({ children }: PropsWithChildren) { if (!children) return null; if (children && typeof children === "string") { return {children}; } return children; } function WrappingElement({ shouldFlow, children, styles, }: PropsWithChildren<{ readonly shouldFlow: boolean; readonly styles: ReturnType; }>): ReactElement { if (shouldFlow) { return {children}; } return ( {children} ); } function getBannerIcon(type: BannerTypes): IconNames | undefined { switch (type) { case "notice": return "info"; case "warning": return "warning"; case "error": return "alert"; case "success": return "checkmark"; } }