chatscope / chat-ui-kit-react

Build your own chat UI with React components in few minutes. Chat UI Kit from chatscope is an open source UI toolkit for developing web chat applications.
https://chatscope.io
MIT License
1.32k stars 110 forks source link

scrolling the bar to the top once, but onYReachStart() is triggered multiple times? #78

Open jazwu opened 1 year ago

jazwu commented 1 year ago

When I scroll the bar just once to the top to load more messages, onYReachStart() is triggered multiple times. Meanwhile, when I first click the chat user (if I don't click, I won't see the chat page), the chat page shows and onYReachStart() is triggered once automatically. My onYReachStart() code is as below ( this.state.noMoreMessages === true if there is no more messages to load and vice versa ):

 async onYReachStart(e) {
        console.log('YReachStart!!!!!!', this.state.loadingMore, this.state.noMoreMessages); // if onYReachStart is triggered, you'll see this line
        if (this.state.loadingMore) {
            return;
        }
        // this.setState({loadingMore: true});
        if (this.state.noMoreMessages === false) {

            await fetch('http://192.168.2.94:8585/httpserver/testing', {
                method: 'POST',
                headers: new Headers({'content-type' : 'application/json'}),
                body: JSON.stringify({
                    mode: 'history1',
                    id: this.state.portId,
                    num: this.getCount(this.state.msgDataList, this.state.loadedMsgList)
                })
            })
            .then (res => res.json())
            .then (data => {
                console.log(data);
                const received_loaded_messages = data.information;
                this.setState({noMoreMessages: data.no_more_message});

                var messages = [];
                let list = received_loaded_messages;
                list.reverse().forEach((val, i) => {
                  messages.push(
                      <Message model={{ direction: position }}>
                          <Message.TextContent text={val.information} />
                      </Message>
                  )

                this.setState({
                    loadedMsgList: messages.reverse().concat(this.state.loadedMsgList),
                    loadingMore: false
                });
            })
            .catch(function (error) {
            console.log(error);
        });
        } else {return;}
    }
KDederichs commented 1 year ago

I'm facing the same issue. I think what's happening is that the it's only fetching new elements on hard top position, not a margin. So when you reach the end, and it loads more the new elements are added but since you're at the top it'll just append them and you're still stuck at the top so it'll fetch more.

IMO it would be better if there was margin to configure like if you're 10% away from the end load more or something