/* Copyright 2026 Adobe. All rights reserved. This file is licensed to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import React, { cloneElement, ReactElement, useCallback, useEffect, useMemo, useState } from "react"; import { ActivityIndicator, FlatList, FlatListProps, ListRenderItem, ScrollView, StyleSheet, Text, useWindowDimensions, View, } from "react-native"; import { useContentCardUI } from "../../hooks"; import InboxProvider, { InboxSettings } from "../../providers/InboxProvider"; import { useTheme } from "../../theme"; import { ContentViewEvent } from "../../types/ContentViewEvent"; import { ContentTemplate } from "../../types/Templates"; import { generateCardHash } from "../../utils/generateCardHash"; import { loadInboxState, saveInboxState } from "../../utils/inboxStorage"; import { ContentCardView, ContentViewProps } from "../ContentCardView/ContentCardView"; import EmptyState from "./EmptyState"; // TODO: consider localizing in the future const DEFAULT_EMPTY_MESSAGE = 'No Content Available'; const cardStatusStore = new Map; interacted: Set }>(); export interface InboxProps extends Partial> { LoadingComponent?: ReactElement | null; ErrorComponent?: ReactElement | null; FallbackComponent?: ReactElement | null; EmptyComponent?: ReactElement | null; surface: string; settings: InboxSettings | null; isLoading?: boolean; error?: boolean; CardProps?: Partial; } function InboxInner({ contentContainerStyle, LoadingComponent = , ErrorComponent = null, FallbackComponent = null, EmptyComponent, settings, surface, style, CardProps, ListHeaderComponent, ...props }: InboxProps & { settings: InboxSettings; }) { const { colors, isDark } = useTheme(); const { width: windowWidth, height: windowHeight } = useWindowDimensions(); const isLandscape = windowWidth > windowHeight; const { content, error, isLoading } = useContentCardUI(surface); const { content: contentSettings } = settings; const { capacity, heading, layout, emptyStateSettings, isUnreadEnabled } = contentSettings; const getStore = useCallback(() => { if (!cardStatusStore.has(surface)) { cardStatusStore.set(surface, { dismissed: new Set(), interacted: new Set() }); } return cardStatusStore.get(surface)!; }, [surface]); const [dismissedIds, setDismissedIds] = useState>(() => new Set(getStore().dismissed)); const [interactedIds, setInteractedIds] = useState>(() => isUnreadEnabled ? new Set(getStore().interacted) : new Set()); const [storageLoaded, setStorageLoaded] = useState(false); useEffect(() => { const activityId = settings.activityId; if (!activityId) { setStorageLoaded(true); return; } let cancelled = false; loadInboxState(activityId).then((persisted) => { if (cancelled) return; const store = getStore(); persisted.dismissed.forEach((id) => store.dismissed.add(id)); persisted.interacted.forEach((id) => store.interacted.add(id)); setDismissedIds(new Set(store.dismissed)); if (isUnreadEnabled) setInteractedIds(new Set(store.interacted)); setStorageLoaded(true); }); return () => { cancelled = true; }; }, [settings.activityId, isUnreadEnabled, getStore]); useEffect(() => { if (!storageLoaded) return; const store = getStore(); setDismissedIds(new Set(store.dismissed)); if (isUnreadEnabled) setInteractedIds(new Set(store.interacted)); }, [content, isUnreadEnabled, getStore, storageLoaded]); const isHorizontal = layout?.orientation === 'horizontal'; const displayCards = useMemo(() => { if (!content) return [] as T[]; return content .map((it) => ({ item: it, hash: generateCardHash(it) })) .filter(({ hash }) => !dismissedIds.has(hash)) .map(({ item, hash }) => { const shouldBeRead = isUnreadEnabled && (interactedIds.has(hash) || item.isRead === true); return shouldBeRead ? { ...item, isRead: true } : item; }) .slice(0, capacity) as T[]; }, [content, capacity, isUnreadEnabled, dismissedIds, interactedIds]); const handleCardEvent = useCallback((event?: ContentViewEvent, data?: ContentTemplate, nativeEvent?: any) => { const cardHash = generateCardHash(data); const store = getStore(); if (event === 'onDismiss' && !store.dismissed.has(cardHash)) { store.dismissed.add(cardHash); setDismissedIds((prev) => new Set(prev).add(cardHash)); if (settings.activityId) { saveInboxState(settings.activityId, { dismissed: [...store.dismissed], interacted: [...store.interacted], }); } } else if (event === 'onInteract' && isUnreadEnabled && !store.interacted.has(cardHash)) { store.interacted.add(cardHash); setInteractedIds((prev) => new Set(prev).add(cardHash)); if (settings.activityId) { saveInboxState(settings.activityId, { dismissed: [...store.dismissed], interacted: [...store.interacted], }); } } CardProps?.listener?.(event, data, nativeEvent); }, [CardProps, isUnreadEnabled, getStore, settings.activityId]); const renderItem: ListRenderItem = useCallback(({ item }) => ( ), [isHorizontal, CardProps, windowWidth, handleCardEvent]); const EmptyList = useCallback(() => EmptyComponent ? cloneElement(EmptyComponent, { ...emptyStateSettings }) as React.ReactElement : , [isDark, emptyStateSettings, EmptyComponent]); if (isLoading) return LoadingComponent; if (error) return ErrorComponent; if (!content) return FallbackComponent; const topOfInbox = {ListHeaderComponent && ListHeaderComponent as React.ReactElement} {heading?.content ? ( {heading.content} ) : null} const listBody = ( item.id} contentContainerStyle={[ contentContainerStyle, isHorizontal && styles.horizontalListContent, styles.inbox ]} horizontal={isHorizontal} renderItem={renderItem} ListEmptyComponent={} ListHeaderComponent={!isHorizontal ? topOfInbox : undefined} /> ); const horizontalChrome = isHorizontal ? ( isLandscape ? ( {topOfInbox} {listBody} ) : ( {topOfInbox} {listBody} ) ) : listBody; return {horizontalChrome}; } /** * @experimental First React Native inbox UI */ export function Inbox({ LoadingComponent = , ErrorComponent = null, FallbackComponent = null, surface, settings, isLoading, error, ...props }: InboxProps): React.ReactElement { if (isLoading) return LoadingComponent as React.ReactElement; if (error) return ErrorComponent as React.ReactElement; if (!settings) return FallbackComponent as React.ReactElement; return ( ); } const styles = StyleSheet.create({ inbox: { flexGrow: 1 }, heading: { fontWeight: '600', fontSize: 18, lineHeight: 28, textAlign: 'center', marginBottom: 16 }, horizontalCardStyles: { flex: 0 }, horizontalListContent: { alignItems: 'center' } });