Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(channels): prevent optimistic inserts mid page (#2814)

teoaadb4de8badd
diff --git a/js/app/packages/queries/channel/channel-messages.ts b/js/app/packages/queries/channel/channel-messages.ts
index 18b9c99d57..e1c7a0775c 100644
--- a/js/app/packages/queries/channel/channel-messages.ts
+++ b/js/app/packages/queries/channel/channel-messages.ts
@@ -219,6 +219,15 @@ export function insertTopLevelMessageIntoChannelMessages(
const [newestPage, ...olderPages] = data.pages;
+ // Only insert into cache entries that represent the bottom of the
+ // conversation. If the newest page has a previous_cursor, we're viewing
+ // a mid-conversation slice (e.g. load-around) and prepending here would
+ // place the message in the wrong position — and cause duplicates when
+ // fetchPreviousPage later fetches the same message from the server.
+ if (newestPage.previous_cursor) {
+ return data;
+ }
+
return {
...data,
pages: [
@@ -697,10 +706,13 @@ export function createMessageIndex(
if (!pages?.length) return { items, keys, byId };
+ const seen = new Set<string>();
for (let i = pages.length - 1; i >= 0; i--) {
const pageItems = pages[i].items;
for (let j = pageItems.length - 1; j >= 0; j--) {
const message = pageItems[j];
+ if (seen.has(message.id)) continue;
+ seen.add(message.id);
items.push(message);
keys.push(message.id);
byId.set(message.id, message);
diff --git a/js/app/packages/queries/channel/message.ts b/js/app/packages/queries/channel/message.ts
index 2251f6eb41..790931ba53 100644
--- a/js/app/packages/queries/channel/message.ts
+++ b/js/app/packages/queries/channel/message.ts
@@ -18,7 +18,8 @@ import type { Attachment, Message } from '@service-comms/generated/models';
import type { NewAttachment } from '@service-comms/generated/models/newAttachment';
import { useMutation } from '@tanstack/solid-query';
import { queryClient } from '../client';
-import { channelKeys, ChannelNonceKeys } from './keys';
+import { ChannelNonceKeys } from './keys';
+import { getChannelMessagesQueryKeyPrefix } from './channel-messages';
import { createMutationNonce, registerNonce } from '../nonce';
import {
captureDeleteSnapshotForTarget,
@@ -157,9 +158,6 @@ function makeOptimisticThreadReply(
export function optimisticInsertChannelMessage(
vars: WithChannelId<WithOptimisticId<WithSenderId<PostMessageRequest>>>
): InsertMessageContext | undefined {
- const queryKey = channelKeys.withID(vars.channelId).queryKey;
- queryClient.cancelQueries({ queryKey });
-
const now = new Date().toISOString();
const newAttachments = makeOptimisticAttachments(
vars.channelId,
@@ -390,14 +388,13 @@ export function useSendMessageMutation(
...withCallbacks<IdResponse, Error, SendMessageParams, SendMessageContext>(
{
onMutate: async (vars) => {
- await queryClient.cancelQueries({
- queryKey: channelKeys.withID(vars.channelID).queryKey,
- });
- // Register nonces for deduplication when WebSocket events arrive
registerMessageNonces(
vars.optimisticId,
vars.message.attachments.length > 0
);
+ await queryClient.cancelQueries({
+ queryKey: getChannelMessagesQueryKeyPrefix(vars.channelID),
+ });
return optimisticInsertChannelMessage({
channelId: vars.channelID,
optimisticId: vars.optimisticId,
@@ -480,10 +477,10 @@ export function useDeleteMessageMutation(
...withCallbacks<void, Error, DeleteMessageParams, DeleteMutationContext>(
{
onMutate: async (vars) => {
+ deleteNonce.prepare(vars);
await queryClient.cancelQueries({
- queryKey: channelKeys.withID(vars.channelID).queryKey,
+ queryKey: getChannelMessagesQueryKeyPrefix(vars.channelID),
});
- deleteNonce.prepare(vars);
return optimisticDeleteChannelMessage({
channelId: vars.channelID,
message_id: vars.messageID,
@@ -563,10 +560,10 @@ export function usePatchMessageMutation(
>(
{
onMutate: async (vars) => {
+ patchNonce.prepare(vars);
await queryClient.cancelQueries({
- queryKey: channelKeys.withID(vars.channelID).queryKey,
+ queryKey: getChannelMessagesQueryKeyPrefix(vars.channelID),
});
- patchNonce.prepare(vars);
return optimisticUpdateChannelMessage({
channelId: vars.channelID,
message_id: vars.messageID,
diff --git a/js/app/packages/queries/channel/reaction.ts b/js/app/packages/queries/channel/reaction.ts
index cdf29c615d..df250bf1d1 100644
--- a/js/app/packages/queries/channel/reaction.ts
+++ b/js/app/packages/queries/channel/reaction.ts
@@ -11,7 +11,8 @@ import type {
} from '@service-comms/generated/models';
import { useMutation } from '@tanstack/solid-query';
import { queryClient } from '../client';
-import { channelKeys, ChannelNonceKeys } from './keys';
+import { ChannelNonceKeys } from './keys';
+import { getChannelMessagesQueryKeyPrefix } from './channel-messages';
import { createMutationNonce } from '../nonce';
import {
replaceTargetReactions,
@@ -255,10 +256,10 @@ export function useAddReactionMutation(
>(
{
onMutate: async (vars) => {
+ addReactionNonce.prepare(vars);
await queryClient.cancelQueries({
- queryKey: channelKeys.withID(vars.channelId).queryKey,
+ queryKey: getChannelMessagesQueryKeyPrefix(vars.channelId),
});
- addReactionNonce.prepare(vars);
return optimisticAddReaction({
channelId: vars.channelId,
message_id: vars.messageId,
@@ -325,10 +326,10 @@ export function useRemoveReactionMutation(
>(
{
onMutate: async (vars) => {
+ removeReactionNonce.prepare(vars);
await queryClient.cancelQueries({
- queryKey: channelKeys.withID(vars.channelId).queryKey,
+ queryKey: getChannelMessagesQueryKeyPrefix(vars.channelId),
});
- removeReactionNonce.prepare(vars);
return optimisticRemoveReaction({
channelId: vars.channelId,
message_id: vars.messageId,