import React, { useMemo, useState } from 'react'; import { Pressable, StyleSheet, Text, View } from 'react-native'; import { ThreadManagerState } from 'stream-chat'; import { useChatContext, useTheme, useTranslationContext } from '../../contexts'; import { useStateStore } from '../../hooks'; import { Loading, Reload } from '../../icons'; import { ExclamationCircle } from '../../icons/exclamation-circle-fill'; import { primitives } from '../../theme'; const selector = (nextValue: ThreadManagerState) => ({ unseenThreadIds: nextValue.unseenThreadIds }) as const; export const ThreadListUnreadBanner = () => { const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const { client } = useChatContext(); const { t } = useTranslationContext(); const { theme: { semantics }, } = useTheme(); const styles = useStyles(); const { unseenThreadIds } = useStateStore(client.threads.state, selector); if (!unseenThreadIds.length) { return null; } const handlePress = async () => { try { setLoading(true); await client.threads.reload(); } catch (err) { setError(err as Error); } finally { setLoading(false); } }; if (loading) { return ( {t('Loading...')} ); } if (error) { return ( {t("Couldn't load new threads. Tap to retry")} ); } return ( [ styles.container, { backgroundColor: pressed ? semantics.backgroundUtilityPressed : semantics.backgroundCoreSurfaceDefault, }, ]} > {t('{{count}} new threads', { count: unseenThreadIds.length })} ); }; const useStyles = () => { const { theme: { semantics, threadListUnreadBanner }, } = useTheme(); return useMemo(() => { return StyleSheet.create({ text: { color: semantics.textSecondary, fontSize: primitives.typographyFontSizeXs, fontWeight: primitives.typographyFontWeightSemiBold, lineHeight: primitives.typographyLineHeightTight, ...threadListUnreadBanner.text, }, container: { backgroundColor: semantics.backgroundCoreSurfaceDefault, flexDirection: 'row', alignItems: 'center', justifyContent: 'center', padding: primitives.spacingSm, gap: primitives.spacingXs, ...threadListUnreadBanner.container, }, }); }, [semantics, threadListUnreadBanner]); };