@@ -5,19 +5,26 @@ import { toast } from "sonner";
55import {
66 channelMessagesKey ,
77 channelWindowKey ,
8- dedupeMessagesById ,
9- normalizeTimelineMessages ,
10- sortMessages ,
118 threadRepliesKey ,
129} from "@/features/messages/lib/messageQueryKeys" ;
1310import {
1411 buildReplyTags ,
15- getChannelIdFromTags ,
1612 getThreadReference ,
1713 isBroadcastReply ,
1814 normalizeMentionPubkeys ,
1915 resolveReplyRootId ,
2016} from "@/features/messages/lib/threading" ;
17+ import {
18+ projectChannelWindowMessages ,
19+ refreshChannelWindowMessages ,
20+ } from "@/features/messages/lib/projectChannelWindow" ;
21+ import { reconcileChannelWindowMessages } from "@/features/messages/lib/channelWindowReconciliation" ;
22+ import {
23+ mergeMessages ,
24+ mergeTimelineCacheMessages ,
25+ } from "@/features/messages/lib/messageMerge" ;
26+
27+ export { mergeMessages , mergeTimelineCacheMessages } ;
2128import { splitOutgoingTags } from "@/features/messages/lib/imetaMediaMarkdown" ;
2229import {
2330 clearTimeoutState ,
@@ -42,7 +49,6 @@ import type { Channel, Identity, RelayEvent } from "@/shared/api/types";
4249import { applyEditTagOverlay } from "@/features/messages/lib/applyEditTagOverlay.mjs" ;
4350import {
4451 emptyChannelWindowStore ,
45- flattenChannelWindowEvents ,
4652 mapChannelWindowEvents ,
4753 mergeLiveChannelWindowEvent ,
4854 mergeLiveThreadSummary ,
@@ -72,74 +78,6 @@ type MessageQueryContext = {
7278const CHANNEL_TIMELINE_KINDS = new Set < number > ( CHANNEL_TIMELINE_CONTENT_KINDS ) ;
7379const CHANNEL_AUX_KINDS = new Set < number > ( CHANNEL_AUX_EVENT_KINDS ) ;
7480
75- function getLocalRenderKey ( message : RelayEvent ) {
76- return message . localKey ?? message . id ;
77- }
78-
79- function isMatchingPendingMessage ( pending : RelayEvent , incoming : RelayEvent ) {
80- if (
81- ! pending . pending ||
82- pending . content !== incoming . content ||
83- pending . kind !== incoming . kind ||
84- pending . pubkey . toLowerCase ( ) !== incoming . pubkey . toLowerCase ( ) ||
85- getChannelIdFromTags ( pending . tags ) !== getChannelIdFromTags ( incoming . tags )
86- ) {
87- return false ;
88- }
89-
90- const pendingThread = getThreadReference ( pending . tags ) ;
91- const incomingThread = getThreadReference ( incoming . tags ) ;
92-
93- return (
94- pendingThread . parentId === incomingThread . parentId &&
95- pendingThread . rootId === incomingThread . rootId
96- ) ;
97- }
98-
99- function mergeMessagesWithNormalizer (
100- current : RelayEvent [ ] ,
101- incoming : RelayEvent ,
102- normalize : ( messages : RelayEvent [ ] ) => RelayEvent [ ] ,
103- ) : RelayEvent [ ] {
104- const normalizedCurrent = dedupeMessagesById ( current ) ;
105- const replacedPending = normalizedCurrent . find ( ( message ) =>
106- isMatchingPendingMessage ( message , incoming ) ,
107- ) ;
108- const incomingWithLocalKey = replacedPending
109- ? {
110- ...incoming ,
111- localKey : replacedPending . localKey ?? replacedPending . id ,
112- }
113- : incoming ;
114- const incomingLocalKey = getLocalRenderKey ( incomingWithLocalKey ) ;
115- const deduped = normalizedCurrent . filter (
116- ( message ) =>
117- message . id !== incoming . id &&
118- getLocalRenderKey ( message ) !== incomingLocalKey &&
119- ! isMatchingPendingMessage ( message , incoming ) ,
120- ) ;
121-
122- return normalize ( [ ...deduped , incomingWithLocalKey ] ) ;
123- }
124-
125- export function mergeMessages (
126- current : RelayEvent [ ] ,
127- incoming : RelayEvent ,
128- ) : RelayEvent [ ] {
129- return mergeMessagesWithNormalizer ( current , incoming , sortMessages ) ;
130- }
131-
132- export function mergeTimelineCacheMessages (
133- current : RelayEvent [ ] ,
134- incoming : RelayEvent ,
135- ) : RelayEvent [ ] {
136- return mergeMessagesWithNormalizer (
137- current ,
138- incoming ,
139- normalizeTimelineMessages ,
140- ) ;
141- }
142-
14381export function createOptimisticMessage (
14482 channelId : string ,
14583 content : string ,
@@ -255,25 +193,6 @@ export function resolveThreadReplyTarget(
255193 } ;
256194}
257195
258- function retainRefetchReconciliationEvents ( events : RelayEvent [ ] ) {
259- return events . filter ( ( event ) => {
260- if ( ! CHANNEL_TIMELINE_KINDS . has ( event . kind ) ) return false ;
261- if ( event . pending ) return true ;
262- const thread = getThreadReference ( event . tags ) ;
263- return thread . parentId !== null && ! isBroadcastReply ( event . tags ) ;
264- } ) ;
265- }
266-
267- function mergeRefetchReconciliationEvents (
268- windowEvents : RelayEvent [ ] ,
269- previousMessages : RelayEvent [ ] ,
270- ) {
271- const authoritativeIds = new Set ( windowEvents . map ( ( event ) => event . id ) ) ;
272- return retainRefetchReconciliationEvents ( previousMessages )
273- . filter ( ( event ) => ! authoritativeIds . has ( event . id ) )
274- . reduce ( ( current , reply ) => mergeMessages ( current , reply ) , windowEvents ) ;
275- }
276-
277196export function useChannelWindowQuery ( channel : Channel | null ) {
278197 const queryClient = useQueryClient ( ) ;
279198 const queryKey = channelWindowKey ( channel ?. id ?? "none" ) ;
@@ -305,9 +224,8 @@ export function useChannelMessagesQuery(channel: Channel | null) {
305224 queryClient . getQueryData < ChannelWindowStore > ( windowKey ) ??
306225 emptyChannelWindowStore ( ) ;
307226 const next = replaceNewestChannelWindow ( current , page ) ;
308- const windowEvents = flattenChannelWindowEvents ( next ) ;
309227 queryClient . setQueryData ( windowKey , next ) ;
310- return mergeRefetchReconciliationEvents ( windowEvents , previousMessages ) ;
228+ return reconcileChannelWindowMessages ( next , previousMessages ) ;
311229 } ,
312230 staleTime : 5 * 60 * 1_000 ,
313231 gcTime : 60 * 60 * 1_000 ,
@@ -320,11 +238,7 @@ export function useChannelSubscription(channel: Channel | null) {
320238 const channelType = channel ?. channelType ?? null ;
321239 const refreshNewestWindow = useEffectEvent ( async ( ) => {
322240 if ( ! channelId ) return ;
323- await queryClient . invalidateQueries ( {
324- queryKey : channelMessagesKey ( channelId ) ,
325- exact : true ,
326- refetchType : "active" ,
327- } ) ;
241+ await refreshChannelWindowMessages ( queryClient , channelId ) ;
328242 } ) ;
329243
330244 const appendMessage = useEffectEvent ( ( event : RelayEvent ) => {
@@ -371,10 +285,7 @@ export function useChannelSubscription(channel: Channel | null) {
371285 const next = mergeLiveChannelWindowEvent ( current , event , isTimelineRow ) ;
372286 if ( next !== current ) {
373287 queryClient . setQueryData ( windowKey , next ) ;
374- queryClient . setQueryData < RelayEvent [ ] > (
375- channelMessagesKey ( channelId ) ,
376- flattenChannelWindowEvents ( next ) ,
377- ) ;
288+ projectChannelWindowMessages ( queryClient , channelId ) ;
378289 }
379290
380291 if ( event . kind === KIND_SYSTEM_MESSAGE ) {
@@ -629,10 +540,7 @@ export function useSendMessageMutation(
629540 optimisticMessage ,
630541 ) ;
631542 queryClient . setQueryData ( windowKey , nextWindow ) ;
632- queryClient . setQueryData < RelayEvent [ ] > (
633- queryKey ,
634- flattenChannelWindowEvents ( nextWindow ) ,
635- ) ;
543+ projectChannelWindowMessages ( queryClient , effectiveChannel . id ) ;
636544
637545 return {
638546 optimisticId : optimisticMessage . id ,
@@ -680,10 +588,7 @@ export function useSendMessageMutation(
680588 localKey : context . optimisticId ,
681589 } ) ;
682590 queryClient . setQueryData ( windowKey , next ) ;
683- queryClient . setQueryData < RelayEvent [ ] > (
684- context . queryKey ,
685- flattenChannelWindowEvents ( next ) ,
686- ) ;
591+ projectChannelWindowMessages ( queryClient , context . channelId ) ;
687592 } ,
688593 } ) ;
689594}
0 commit comments