import dayjs from 'dayjs'; import type { ForwardedRef } from 'react'; import React, { useCallback, useEffect, useRef } from 'react'; import { ActivityIndicator, Keyboard, KeyboardAvoidingView, Platform, StyleSheet, View, } from 'react-native'; import { SafeAreaView } from 'react-native-safe-area-context'; import { Footer } from './Footer'; import { Header } from './Header'; import { List } from './List'; import type { IChatty, ListRef } from './types/Chatty.types'; export const PropsContext = React.createContext({} as IChatty); export const Chatty = React.forwardRef( (props: IChatty, ref: ForwardedRef) => { const listRef = useRef(); const { messages } = props; /* This is a way to scroll to the end of the list when the keyboard is shown. */ useEffect(() => { const listener = Keyboard.addListener('keyboardDidShow', () => { if (listRef.current) { listRef.current?.scrollToEnd(true); } else if (ref) { //@ts-ignore ref.current?.scrollToEnd(true); } else { console.warn('No ref found'); } }); return () => { listener.remove(); }; }, [ref]); useEffect(() => { if (props?.setDateLocale) { dayjs.locale(props.setDateLocale); } }, [props.setDateLocale]); const renderLoading = useCallback(() => { return ( ); }, []); return ( {props?.renderHeader ? ( props.renderHeader(props.headerProps) ) : (
)} {props.messages.length < 1 ? ( renderLoading() ) : ( <> {props?.renderFooter ? ( props.renderFooter(props.footerProps) ) : (