···33import {KeyboardAvoidingView} from 'react-native-keyboard-controller'
4455import {useChat} from '#/state/messages'
66-import {ChatProvider} from '#/state/messages'
76import {ConvoItem, ConvoStatus} from '#/state/messages/convo'
87import {isWeb} from 'platform/detection'
98import {MessageInput} from '#/screens/Messages/Conversation/MessageInput'
···3736 return null
3837}
39383939+function keyExtractor(item: ConvoItem) {
4040+ return item.key
4141+}
4242+4043function onScrollToEndFailed() {
4144 // Placeholder function. You have to give FlatList something or else it will error.
4245}
43464444-export function MessagesList({convoId}: {convoId: string}) {
4545- return (
4646- <ChatProvider convoId={convoId}>
4747- <MessagesListInner />
4848- </ChatProvider>
4949- )
5050-}
5151-5252-export function MessagesListInner() {
4747+export function MessagesList() {
5348 const chat = useChat()
5449 const flatListRef = useRef<FlatList>(null)
5550 // We use this to know if we should scroll after a new clop is added to the list
5651 const isAtBottom = useRef(false)
5757-5858- // Because the viewableItemsChanged callback won't have access to the updated state, we use a ref to store the
5959- // total number of clops
6060- // TODO this needs to be set to whatever the initial number of messages is
6161- // const totalMessages = useRef(10)
6262-6363- // TODO later
64526553 const [onViewableItemsChanged, viewabilityConfig] = useMemo(() => {
6654 return [
···94829583 const onInputBlur = useCallback(() => {}, [])
96848585+ const onSendMessage = useCallback(
8686+ (text: string) => {
8787+ chat.service.sendMessage({
8888+ text,
8989+ })
9090+ },
9191+ [chat.service],
9292+ )
9393+9794 return (
9895 <KeyboardAvoidingView
9996 style={{flex: 1, marginBottom: isWeb ? 20 : 85}}
10097 behavior="padding"
10198 keyboardVerticalOffset={70}
10299 contentContainerStyle={{flex: 1}}>
103103- {chat.state.status === ConvoStatus.Ready && (
104104- <FlatList
105105- data={chat.state.items}
106106- keyExtractor={item => item.key}
107107- renderItem={renderItem}
108108- contentContainerStyle={{paddingHorizontal: 10}}
109109- // In the future, we might want to adjust this value. Not very concerning right now as long as we are only
110110- // dealing with text. But whenever we have images or other media and things are taller, we will want to lower
111111- // this...probably.
112112- initialNumToRender={20}
113113- // Same with the max to render per batch. Let's be safe for now though.
114114- maxToRenderPerBatch={25}
115115- inverted={true}
116116- onEndReached={onEndReached}
117117- onScrollToIndexFailed={onScrollToEndFailed}
118118- onContentSizeChange={onContentSizeChange}
119119- onViewableItemsChanged={onViewableItemsChanged}
120120- viewabilityConfig={viewabilityConfig}
121121- maintainVisibleContentPosition={{
122122- minIndexForVisible: 0,
123123- }}
124124- ListFooterComponent={
125125- <MaybeLoader isLoading={chat.state.isFetchingHistory} />
126126- }
127127- removeClippedSubviews={true}
128128- ref={flatListRef}
129129- keyboardDismissMode="none"
130130- />
131131- )}
100100+ <FlatList
101101+ data={
102102+ chat.state.status === ConvoStatus.Ready ? chat.state.items : undefined
103103+ }
104104+ keyExtractor={keyExtractor}
105105+ renderItem={renderItem}
106106+ contentContainerStyle={{paddingHorizontal: 10}}
107107+ // In the future, we might want to adjust this value. Not very concerning right now as long as we are only
108108+ // dealing with text. But whenever we have images or other media and things are taller, we will want to lower
109109+ // this...probably.
110110+ initialNumToRender={20}
111111+ // Same with the max to render per batch. Let's be safe for now though.
112112+ maxToRenderPerBatch={25}
113113+ inverted={true}
114114+ onEndReached={onEndReached}
115115+ onScrollToIndexFailed={onScrollToEndFailed}
116116+ onContentSizeChange={onContentSizeChange}
117117+ onViewableItemsChanged={onViewableItemsChanged}
118118+ viewabilityConfig={viewabilityConfig}
119119+ maintainVisibleContentPosition={{
120120+ minIndexForVisible: 1,
121121+ }}
122122+ ListFooterComponent={
123123+ <MaybeLoader
124124+ isLoading={
125125+ chat.state.status === ConvoStatus.Ready &&
126126+ chat.state.isFetchingHistory
127127+ }
128128+ />
129129+ }
130130+ removeClippedSubviews={true}
131131+ ref={flatListRef}
132132+ keyboardDismissMode="none"
133133+ />
132134133135 <View style={{paddingHorizontal: 10}}>
134136 <MessageInput
135135- onSendMessage={text => {
136136- chat.service.sendMessage({
137137- text,
138138- })
139139- }}
137137+ onSendMessage={onSendMessage}
140138 onFocus={onInputFocus}
141139 onBlur={onInputBlur}
142140 />