import { Illustration, Assets } from '@wise/art'; import { clsx } from 'clsx'; import { ReactNode, useEffect, useState, MouseEvent, useCallback } from 'react'; import Body from '../Body'; import { Typography } from '../common'; import Link from '../Link'; import type { AlertAction } from '../_deprecated/Alert'; import IconButton from '../IconButton'; import { Cross } from '@transferwise/icons'; import { useIntl } from 'react-intl'; import closeBtnMessages from '../common/CloseButton/CloseButton.messages'; // WARNING: Changing this will cause nudges to reappear wherever persist nudge is used and privacy team will need to be updated too export const STORAGE_NAME = 'dismissedNudges'; const getLocalStorage = (): string[] => { try { const storageItem = localStorage.getItem(STORAGE_NAME); if (storageItem) { const storage: unknown = JSON.parse(storageItem); if (Array.isArray(storage)) { return storage.map((item) => String(item)); } } } catch {} return []; }; type MediaNameType = | `${Assets.GLOBE}` | `${Assets.LOCK}` | `${Assets.WALLET}` | `${Assets.GEAR}` | `${Assets.MAP}` | `${Assets.INVITE_LETTER}` | `${Assets.PERSONAL_CARD}` | `${Assets.BUSINESS_CARD}` | `${Assets.HEART}` | `${Assets.MULTI_CURRENCY}` | `${Assets.SHOPPING_BAG}` | `${Assets.FLOWER}` | `${Assets.GIFT_BOX}` | `${Assets.BACKPACK}` | `${Assets.DOCUMENTS}`; type BaseProps = { /** * @deprecated Use `mediaName` instead. * Legacy custom media content is no longer used by `Nudge`. */ media?: ReactNode; /** * Illustration asset name shown at the start of the nudge. */ mediaName?: MediaNameType; /** Main nudge message content. */ title: ReactNode; /** * Legacy CTA label content. * Rendered only when `action` is not provided. */ link?: ReactNode; /** * Legacy CTA destination for `link`. * Ignored when `action` is provided. */ href?: string; /** * Legacy click handler for `link`. * Ignored when `action` is provided. */ onClick?: (event?: MouseEvent) => void; /** Fired when the user clicks on close button */ onDismiss?: () => void; /** * Preferred CTA configuration rendered under the nudge body. * Single CTA rule: when `action` is provided, legacy `link`/`href` is not rendered. * If your label is short, use `aria-label` to provide more context. */ action?: AlertAction; /** Optional class name applied to the root nudge container. */ className?: string; }; export interface OptionalId extends BaseProps { id?: string; persistDismissal?: false; isPreviouslyDismissed?: undefined; } export interface RequiredPersistProps extends BaseProps { /** This ID should be completely unique to the page and feature as it uses a shared array which could conflict with other nudges in Wise */ id: string; /** Use persist dismissal to keep the nudge dismissed using the browser's localStorage */ persistDismissal: true; /** * Fired on mount for determining if nudge has been dismissed before * * @param {boolean} value - set to true if dismissed previously */ isPreviouslyDismissed?: (value: boolean) => void; } export type NudgeProps = OptionalId | RequiredPersistProps; const Nudge = ({ mediaName, title, link, href, onClick, onDismiss, persistDismissal, isPreviouslyDismissed, id, className, action, }: NudgeProps) => { const intl = useIntl(); const hasLegacyLink = Boolean(link); const hasAction = action != null; const shouldRenderLegacyLink = hasLegacyLink && !hasAction; useEffect(() => { if (process.env.NODE_ENV !== 'production' && hasAction && (hasLegacyLink || href != null)) { // eslint-disable-next-line no-console -- Intentional developer warning for conflicting API usage. console.warn( 'Nudge received both action and link/href props. Rendering action only. Remove link/href to silence this warning.', ); } }, [hasAction, hasLegacyLink, href]); const getIsDismissed = useCallback( () => (persistDismissal && id ? !!getLocalStorage()?.find((item) => item === id) : false), [persistDismissal, id], ); const [nudgeState, setNudgeState] = useState(() => ({ isDismissed: getIsDismissed(), isMounted: false, })); useEffect(() => { // eslint-disable-next-line react-hooks/set-state-in-effect -- Setting mount state in mount effect setNudgeState((prev) => ({ ...prev, isMounted: true })); }, []); useEffect(() => { // eslint-disable-next-line react-hooks/set-state-in-effect -- Syncing dismissed state from localStorage on prop change setNudgeState((prev) => ({ ...prev, isDismissed: getIsDismissed() })); }, [getIsDismissed, id, persistDismissal]); const { isDismissed } = nudgeState; const handleOnDismiss = () => { const dismissedNudgesStorage = getLocalStorage(); if (persistDismissal && id) { try { localStorage.setItem(STORAGE_NAME, JSON.stringify([...dismissedNudgesStorage, id])); } catch {} setNudgeState((prev) => ({ ...prev, isDismissed: true })); } if (onDismiss) { onDismiss(); } }; useEffect(() => { if (persistDismissal && id && isPreviouslyDismissed) { const dismissedNudgesStorage = getLocalStorage(); const wasDismissed = !!dismissedNudgesStorage?.find((item) => item === id); isPreviouslyDismissed(wasDismissed); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [id, persistDismissal]); if (persistDismissal && (isDismissed || !nudgeState.isMounted)) { return null; } return (
{!!mediaName && (
)}
{title} {shouldRenderLegacyLink && ( {link} )} {action && ( {action.text} )}
{onDismiss || persistDismissal ? ( ) : null}
); }; export default Nudge;