import cn from 'classnames'; import { ElementType, MouseEventHandler, ReactNode, useEffect, useMemo, useRef, useState } from 'react'; import { ButtonSimple, ButtonSimpleProps, ButtonTonal, ButtonTonalProps } from '@snack-uikit/button'; import { KebabSVG } from '@snack-uikit/icons'; import { Link, PickLinkProps } from '@snack-uikit/link'; import { StatusIndicator } from '@snack-uikit/status'; import { TruncateString } from '@snack-uikit/truncate-string'; import { Typography } from '@snack-uikit/typography'; import { extractSupportProps, WithSupportProps } from '@snack-uikit/utils'; import { type Action, ActionsButton } from '../../helperComponents/ActionsButton'; import { APPEARANCE, TEST_IDS } from './constants'; import { getIcon, stopPropagationClick } from './helpers'; import styles from './styles.module.scss'; import { Appearance } from './types'; export type NotificationCardProps = WithSupportProps<{ /** Идентификатор уведомления */ id: string; /** Тип уведомления */ appearance?: Appearance; /** Лейбл перед заголовком */ label?: string; /** Управление состоянием прочитано/не прочитано */ unread?: boolean; /** Заголовок уведомления */ title: string; /** Контент уведомления */ content: ReactNode; /** Ссылка */ link?: PickLinkProps; /** Дата уведомления */ date: string; /** Колбэк клика по карточке */ onClick?: MouseEventHandler; /** Колбэк при попадании карточки в область видимости на 80% */ onVisible?(cardId: string): void; /** Кнопка главного действия у карточки */ primaryButton?: Omit; /** Кнопка второстепенного действия у карточки */ secondaryButton?: Omit; /** Дополнительные действия у карточки */ actions?: Action[]; /** CSS-класс */ className?: string; }>; /** Компонент карточки уведомления */ export function NotificationCard({ id, appearance = APPEARANCE.Neutral, label, unread, title, content, link, date, onClick, primaryButton, secondaryButton, actions, onVisible, className, ...rest }: NotificationCardProps) { const icon = useMemo(() => getIcon(appearance), [appearance]); const [isDroplistOpen, setDroplistOpen] = useState(false); const cardRef = useRef(null); useEffect(() => { if (!unread || !onVisible) { return; } const handleIntersection = (entries: IntersectionObserverEntry[]) => { const [entry] = entries; if (entry.isIntersecting) { onVisible(id); } }; const observer = new IntersectionObserver(handleIntersection, { root: null, threshold: 0.8, rootMargin: '0px', }); observer.observe(cardRef.current as HTMLElement); return () => { observer.disconnect(); }; }, [id, onVisible, unread]); const showFooter = Boolean(link || date); const handleLinkClick: MouseEventHandler = e => { e.stopPropagation(); link?.onClick?.(e); }; return (
{actions?.length && ( } /> )} {label && ( )}
{icon}
{content && ( {content} )} {(primaryButton || secondaryButton) && ( // eslint-disable-next-line jsx-a11y/no-static-element-interactions
{secondaryButton && ( )} {primaryButton && ( )}
)} {showFooter && (
{link && ( )} {date && ( {date} )}
)} {unread && }
); }