11import { PrivateChatMessage } from 'common/chat-message'
22import { millisToTs , tsToMillis } from 'common/supabase/utils'
33import { useEffect , useState } from 'react'
4- import { min , orderBy , uniq , uniqBy } from 'lodash'
4+ import { orderBy , uniq , uniqBy } from 'lodash'
55import { usePersistentLocalState } from 'web/hooks/use-persistent-local-state'
66import { getSortedChatMessageChannels , getTotalChatMessages , } from 'web/lib/supabase/private-messages'
77import { useIsPageVisible } from 'web/hooks/use-page-visible'
@@ -19,18 +19,21 @@ export function usePrivateMessages(
1919 userId : string
2020) {
2121 // console.debug('getWebsocketUrl', getWebsocketUrl())
22+ const key = `private-messages-${ channelId } -${ limit } -v1` ;
2223 const [ messages , setMessages ] = usePersistentLocalState <
2324 PrivateChatMessage [ ] | undefined
24- > ( undefined , `private-messages- ${ channelId } - ${ limit } -v1` )
25+ > ( undefined , key )
2526
2627 const fetchMessages = async ( id ?: number ) => {
2728 const data = {
2829 channelId,
2930 limit,
30- id : id ?? ( messages ? min ( messages . map ( ( m ) => m . id ) ) : undefined ) ,
31+ // id filter is useful to pull up new messages (later than the last message in messages),
32+ // but since messages can be deleted or edited, we can't rely on the id filter anymore (at least not in the same fashion)
33+ // id: id ?? (messages?.length ? max(messages.map((m) => m.id)) : undefined),
3134 }
3235 const newMessages = await api ( 'get-channel-messages' , data )
33- // console.debug('get-channel-messages', newMessages, messages, data)
36+ // console.debug(key, { newMessages, messages, data} )
3437 setMessages ( ( prevMessages ) =>
3538 orderBy (
3639 uniqBy ( [ ...newMessages , ...( prevMessages && id ? prevMessages : [ ] ) ] , ( m ) => m . id ) ,
0 commit comments