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(new-channels): blanking on new message (#2353)

dev-rb49902fa7a590
diff --git a/js/app/packages/channel/Channel/Channel.tsx b/js/app/packages/channel/Channel/Channel.tsx
index 344b2c54e8..72066e5292 100644
--- a/js/app/packages/channel/Channel/Channel.tsx
+++ b/js/app/packages/channel/Channel/Channel.tsx
@@ -1,7 +1,7 @@
import {
- makeMessageIndex,
type ChannelMessagesData,
useChannelMessagesQuery,
+ createMessageIndex,
} from '@queries/channel/channel-messages';
import {
createEffect,
@@ -93,12 +93,14 @@ export type ChannelHandle = {
export function Channel(props: ChannelProps) {
const userId = useUserId();
+
const sendMessageMutation = useSendMessageMutation();
const patchMessageMutation = usePatchMessageMutation();
const deleteMessageMutation = useDeleteMessageMutation();
const typingMutation = usePostTypingUpdateMutation();
const addReactionMutation = useAddReactionMutation();
const removeReactionMutation = useRemoveReactionMutation();
+
const [threadListNavigation, setThreadListNavigation] =
createSignal<ThreadListNavigation>();
const [threadListScrollState, setThreadListScrollState] =
@@ -109,7 +111,7 @@ export function Channel(props: ChannelProps) {
channelId: () => props.channelId,
initialTargetMessageId: props.targetMessageId,
initialTargetMessageReplyId: props.targetMessageReplyId,
- messageKeys: () => messageIndex().keys,
+ messageKeys: () => messageIndex.keys(),
navigation: threadListNavigation,
});
@@ -120,11 +122,13 @@ export function Channel(props: ChannelProps) {
() => props.channelId,
targetMessageController.loadAroundMessageId
);
- const messageIndex = createMemo(() =>
- makeMessageIndex(messagesQuery.data as ChannelMessagesData | undefined)
+
+ const messageIndex = createMessageIndex(
+ () => messagesQuery.data as ChannelMessagesData | undefined
);
- const messages = createMemo(() => messageIndex().items);
- const messageById = createMemo(() => messageIndex().byId);
+
+ const messages = createMemo(() => messageIndex.items());
+ const messageById = createMemo(() => messageIndex.byId());
const participants = useChannelParticipants(() => props.channelId);
@@ -209,7 +213,7 @@ export function Channel(props: ChannelProps) {
});
const selection = createMessageSelection({
- keys: () => messageIndex().keys,
+ keys: () => messageIndex.keys(),
});
const { messageListScopeId, attachMessageListRef, attachInputRef } =
@@ -293,7 +297,7 @@ export function Channel(props: ChannelProps) {
}}
>
<ThreadList
- keys={() => messageIndex().keys}
+ keys={() => messageIndex.keys()}
initialScrollTarget={threadListInitialScrollTarget()}
shift={shift}
prepend={threadPaginator.isPrepending}
@@ -306,7 +310,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);
if (isMobile()) {
createEffect(() => {
diff --git a/js/app/packages/queries/channel/channel-messages.ts b/js/app/packages/queries/channel/channel-messages.ts
index c89540eb3e..bf01e4c580 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, createMemo } from 'solid-js';
+import { type Accessor, createEffect, createSignal, 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';
@@ -19,6 +19,7 @@ import {
replaceReplyReactionsInThreadPreview,
restoreReplyToThreadPreview,
} from './thread-preview';
+import { createStore, reconcile } from 'solid-js/store';
export type ChannelMessagesData = InfiniteData<
ChannelMessagesPage,
@@ -101,18 +102,6 @@ export function useChannelMessagesQuery(
);
}
-export function useChannelMessagesWithIndex(channelId: Accessor<string>) {
- const query = useChannelMessagesQuery(channelId, () => undefined);
- const index = createMemo(() =>
- makeMessageIndex(query.data as ChannelMessagesData | undefined)
- );
- return {
- query,
- index,
- byId: createMemo(() => index().byId),
- };
-}
-
/** Returns the cache key for one channel message query variant. */
export function getChannelMessagesQueryKey(
channelId: string,
@@ -578,24 +567,51 @@ export function softInvalidateChannelMessages(channelId: string) {
* Pages arrive newest-first, items within each page are newest-first,
* so we reverse both layers in one pass.
*/
-export function makeMessageIndex(
- data: ChannelMessagesData | undefined
-): IndexedChannelMessages {
- const items: ApiChannelMessage[] = [];
- const keys: string[] = [];
+export function createMessageIndex(
+ data: Accessor<ChannelMessagesData | undefined>
+) {
const byId = new Map<string, ApiChannelMessage>();
+ let items: ApiChannelMessage[] = [];
- if (!data?.pages?.length) return { items, keys, byId };
+ const [listen, notify] = createSignal(undefined, { equals: false });
- for (let i = data.pages.length - 1; i >= 0; i--) {
- const pageItems = data.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<string[]>([]);
+
+ createEffect(
+ on(data, (data) => {
+ byId.clear();
+ items = [];
+
+ const pages = data?.pages;
+
+ 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);
+ }
+ }
+ }
+
+ notify();
+ setMessageIndex(reconcile(keys));
+ })
+ );
+
+ return {
+ keys: () => messageIndex,
+ byId: () => {
+ listen();
+ return byId;
+ },
+ items: () => {
+ listen();
+ return items;
+ },
+ };
}