// Copyright 2021 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only import React, { useEffect, useState } from 'react'; import classNames from 'classnames'; import { strictAssert } from '../util/assert'; import type { LocalizerType } from '../types/Util'; import type { BadgeType } from '../badges/types'; import { BadgeCategory } from '../badges/BadgeCategory'; import { Modal } from './Modal'; import { Button, ButtonSize } from './Button'; import { BadgeDescription } from './BadgeDescription'; import { BadgeImage } from './BadgeImage'; import { BadgeCarouselIndex } from './BadgeCarouselIndex'; import { BadgeSustainerInstructionsDialog } from './BadgeSustainerInstructionsDialog'; type PropsType = Readonly<{ areWeASubscriber: boolean; badges: ReadonlyArray; firstName?: string; i18n: LocalizerType; onClose: () => unknown; title: string; }>; export function BadgeDialog(props: PropsType): null | JSX.Element { const { badges, i18n, onClose } = props; const [isShowingInstructions, setIsShowingInstructions] = useState(false); const hasBadges = badges.length > 0; useEffect(() => { if (!hasBadges && !isShowingInstructions) { onClose(); } }, [hasBadges, isShowingInstructions, onClose]); if (isShowingInstructions) { return ( setIsShowingInstructions(false)} /> ); } return hasBadges ? ( setIsShowingInstructions(true)} /> ) : null; } function BadgeDialogWithBadges({ areWeASubscriber, badges, firstName, i18n, onClose, onShowInstructions, title, }: PropsType & { onShowInstructions: () => unknown }): JSX.Element { const firstBadge = badges[0]; strictAssert( firstBadge, ' got an empty array of badges' ); const [currentBadgeId, setCurrentBadgeId] = useState(firstBadge.id); let currentBadge: BadgeType; let currentBadgeIndex: number = badges.findIndex( b => b.id === currentBadgeId ); if (currentBadgeIndex === -1) { currentBadgeIndex = 0; currentBadge = firstBadge; } else { currentBadge = badges[currentBadgeIndex]; } const setCurrentBadgeIndex = (index: number): void => { const newBadge = badges[index]; strictAssert(newBadge, ' tried to select a nonexistent badge'); setCurrentBadgeId(newBadge.id); }; const navigate = (change: number): void => { setCurrentBadgeIndex(currentBadgeIndex + change); }; return (
)}