import React from 'react'; import { XStack, YStack } from '@tamagui/stacks'; import { SizableText as Text } from '@tamagui/text'; import { buildLimitWarnings } from '../../lib/limitWarnings'; import type { EventAddonCatalogItem, EventLimitSummary } from '../../api'; import { scopeDefaults, selectAddonKeyForScope } from '../addons'; import { CTAButton, MobileCard } from './Primitives'; import { MobileSelect } from './FormControls'; type LimitTranslator = (key: string, options?: Record) => string; export function LimitWarnings({ limits, addons, onCheckout, busyScope, translate, textColor, borderColor, }: { limits: EventLimitSummary | null; addons: EventAddonCatalogItem[]; onCheckout: (scopeOrKey: 'photos' | 'gallery' | string) => void; busyScope: string | null; translate: LimitTranslator; textColor: string; borderColor: string; }) { const warnings = React.useMemo(() => buildLimitWarnings(limits, translate), [limits, translate]); if (!warnings.length) { return null; } return ( {warnings.map((warning) => ( {warning.message} {(warning.scope === 'photos' || warning.scope === 'gallery' || warning.scope === 'guests') && resolveAddonOptions(addons, warning.scope).length ? ( ) : ( onCheckout(warning.scope)} loading={busyScope === warning.scope} /> )} ))} ); } function resolveAddonOptions(addons: EventAddonCatalogItem[], scope: 'photos' | 'gallery' | 'guests'): EventAddonCatalogItem[] { const whitelist = scopeDefaults[scope]; const filtered = addons.filter((addon) => addon.price_id && whitelist.includes(addon.key)); return filtered.length ? filtered : addons.filter((addon) => addon.price_id); } function MobileAddonsPicker({ scope, addons, busy, onCheckout, translate, }: { scope: 'photos' | 'gallery' | 'guests'; addons: EventAddonCatalogItem[]; busy: boolean; onCheckout: (addonKey: string) => void; translate: LimitTranslator; }) { const options = React.useMemo(() => resolveAddonOptions(addons, scope), [addons, scope]); const [selected, setSelected] = React.useState(() => options[0]?.key ?? selectAddonKeyForScope(addons, scope)); React.useEffect(() => { if (options[0]?.key) { setSelected(options[0].key); } }, [options]); if (!options.length) { return null; } return ( setSelected(event.target.value)} containerStyle={{ flex: 1, minWidth: 0 }} compact > {options.map((addon) => ( ))} selected && onCheckout(selected)} loading={busy} fullWidth={false} /> ); }