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): messages sometimes duplicating (#2563)

dev-rb56482c6f3352
diff --git a/js/app/packages/channel/Channel/Channel.tsx b/js/app/packages/channel/Channel/Channel.tsx
index 25d8fbb6b4..bd35823273 100644
--- a/js/app/packages/channel/Channel/Channel.tsx
+++ b/js/app/packages/channel/Channel/Channel.tsx
@@ -120,7 +120,7 @@ export function Channel(props: ChannelProps) {
channelId: () => props.channelId,
initialTargetMessageId: props.targetMessageId,
initialTargetMessageReplyId: props.targetMessageReplyId,
- messageKeys: () => messageIndex.keys(),
+ messageKeys: () => messageIndex.keys,
navigation: threadListNavigation,
didInitialScroll: () => threadListScrollState()?.didInitialScroll ?? false,
});
@@ -137,8 +137,8 @@ export function Channel(props: ChannelProps) {
() => messagesQuery.data as ChannelMessagesData | undefined
);
- const messages = createMemo(() => messageIndex.items());
- const messageById = () => messageIndex.byId();
+ const messages = createMemo(() => [...messageIndex.items]);
+ const messageById = () => messageIndex.byId;
const participants = useChannelParticipants(() => props.channelId);
@@ -240,7 +240,7 @@ export function Channel(props: ChannelProps) {
});
const selection = createMessageSelection({
- keys: () => messageIndex.keys(),
+ keys: () => messageIndex.keys,
});
const { messageListScopeId, attachMessageListRef, attachInputRef } =
@@ -332,7 +332,7 @@ export function Channel(props: ChannelProps) {
>
<Show when={messages().length > 0}>
<ThreadList
- keys={() => messageIndex.keys()}
+ keys={() => messageIndex.keys}
initialScrollTarget={threadListInitialScrollTarget()}
shift={shift}
prepend={threadPaginator.isPrepending}
@@ -345,7 +345,7 @@ export function Channel(props: ChannelProps) {
const message = () => messageById().get(item.id);
const state = threadManager.getOrCreateThreadState(item.id);
const isNewestThread = () =>
- item.id === messageIndex.keys().at(-1);
+ item.id === messageIndex.keys.at(-1);
return (
<Show when={message()}>
diff --git a/js/app/packages/queries/channel/channel-messages.ts b/js/app/packages/queries/channel/channel-messages.ts
index 87f94c60cb..746f1fc140 100644
--- a/js/app/packages/queries/channel/channel-messages.ts
+++ b/js/app/packages/queries/channel/channel-messages.ts
@@ -6,7 +6,7 @@ import {
type ChannelMessagesPage,
} from '@service-comms/client';
import { type InfiniteData, useInfiniteQuery } from '@tanstack/solid-query';
-import { type Accessor, createRenderEffect, createSignal, on } from 'solid-js';
+import { type Accessor, createEffect, on } from 'solid-js';
import type { ApiCountedReaction } from '@service-storage/generated/schemas';
import type { ApiMessageAttachment } from '@service-storage/generated/schemas/apiMessageAttachment';
import { queryClient } from '../client';
@@ -570,48 +570,33 @@ export function softInvalidateChannelMessages(channelId: string) {
export function createMessageIndex(
data: Accessor<ChannelMessagesData | undefined>
) {
- const byId = new Map<string, ApiChannelMessage>();
- let items: ApiChannelMessage[] = [];
+ const buildIndex = () => {
+ const data_ = data();
- const [listen, notify] = createSignal(undefined, { equals: false });
+ const pages = data_?.pages;
- const [messageIndex, setMessageIndex] = createStore<string[]>([]);
+ const items: ApiChannelMessage[] = [];
+ const keys: string[] = [];
+ const byId = new Map<string, ApiChannelMessage>();
- createRenderEffect(
- on(data, (data) => {
- byId.clear();
- items = [];
+ if (!pages?.length) return { items, keys, byId };
- const pages = data?.pages;
+ 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];
+ items.push(message);
+ keys.push(message.id);
+ byId.set(message.id, message);
+ }
+ }
- const keys: string[] = [];
+ return { items, keys, byId };
+ };
- if (pages?.length) {
- 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];
- items.push(message);
- keys.push(message.id);
- byId.set(message.id, message);
- }
- }
- }
+ const [messageIndex, setMessageIndex] = createStore(buildIndex());
- notify();
- setMessageIndex(reconcile(keys));
- })
- );
+ createEffect(on(data, () => setMessageIndex(reconcile(buildIndex()))));
- return {
- keys: () => messageIndex,
- byId: () => {
- listen();
- return byId;
- },
- items: () => {
- listen();
- return items;
- },
- };
+ return messageIndex;
}