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(channel): open channel to newest messages instead of parked scroll position (#4454)

gbirmanc37e50564678
diff --git a/js/app/packages/app/component/command/CommandMenu.tsx b/js/app/packages/app/component/command/CommandMenu.tsx
index 16eb23a51e..db7e84f266 100644
--- a/js/app/packages/app/component/command/CommandMenu.tsx
+++ b/js/app/packages/app/component/command/CommandMenu.tsx
@@ -264,6 +264,7 @@ export function CommandMenuInner(props: {
{
referredFrom: 'kommand-menu',
preferNewSplit: openInNewSplit,
+ reopen: blockName === 'channel' ? 'latest' : undefined,
}
);
}
diff --git a/js/app/packages/app/component/next-soup/utils.ts b/js/app/packages/app/component/next-soup/utils.ts
index 86a1b8067e..4a55ce1111 100644
--- a/js/app/packages/app/component/next-soup/utils.ts
+++ b/js/app/packages/app/component/next-soup/utils.ts
@@ -397,6 +397,7 @@ export const openEntityInSplitFromUnifiedList = async (
handle: splitHandle,
mergeHistory,
allowDuplicate,
+ reopen: entity.type === 'channel' && !location ? 'latest' : undefined,
}
);
diff --git a/js/app/packages/app/component/split-layout/layoutManager.ts b/js/app/packages/app/component/split-layout/layoutManager.ts
index d9ef6d06fa..affdbf9ac7 100644
--- a/js/app/packages/app/component/split-layout/layoutManager.ts
+++ b/js/app/packages/app/component/split-layout/layoutManager.ts
@@ -169,6 +169,14 @@ export type OpenWithSplitOptions = {
preferNewSplit?: boolean;
insertIndex?: number;
handle?: SplitHandle;
+ /**
+ * Ask the block to land on its latest content via the `goToLatest` block
+ * method. Covers content that is already mounted (e.g. a channel open in
+ * another split parked at an old scroll position), which would otherwise
+ * just be activated as-is. Omit when navigating to a specific location
+ * within the block.
+ */
+ reopen?: 'latest';
};
/**
@@ -1248,6 +1256,16 @@ export function createSplitLayout(
content: SplitContent,
options: OpenWithSplitOptions = {}
): SplitHandle | undefined {
+ if (options.reopen === 'latest') {
+ // Fire-and-forget so it covers every open path (fresh mount, duplicate
+ // activation, interceptor-consumed navigation). The block-handle proxy
+ // waits for the block and method to register before invoking.
+ void orchestrator
+ .getBlockHandle(content.id)
+ .then((handle) => handle?.goToLatest())
+ .catch((e) => console.error('openWithSplit: goToLatest failed', e));
+ }
+
if (navigationInterceptor) {
const result = navigationInterceptor(content, options);
if (result.handled) return undefined;
diff --git a/js/app/packages/block-channel/component/NewChannelBlockAdapter.tsx b/js/app/packages/block-channel/component/NewChannelBlockAdapter.tsx
index 964aea01be..4c38e7776d 100644
--- a/js/app/packages/block-channel/component/NewChannelBlockAdapter.tsx
+++ b/js/app/packages/block-channel/component/NewChannelBlockAdapter.tsx
@@ -301,6 +301,15 @@ export function NewChannelBlockAdapter(props: BlockChannelProps) {
};
};
+ // The Messages tab may not have mounted yet (e.g. right after the split
+ // opens). Wait for its handle via the orchestrator's availability primitive.
+ const awaitMessagesHandle = async () => {
+ await awaitCondition(() => messagesHandle() !== undefined, 10_000).catch(
+ () => {}
+ );
+ return messagesHandle();
+ };
+
// Register on the block always — `goToLocationFromParams` used to live only
// inside `onChannelReady` (Messages tab), so open-call from Attachments/etc. was a no-op.
createMethodRegistration(blockHandle, {
@@ -315,14 +324,8 @@ export function NewChannelBlockAdapter(props: BlockChannelProps) {
if (targetMessageId) {
setActiveTab(DEFAULT_CHANNEL_TAB);
- // The Messages tab may not have mounted yet (e.g. right after the split
- // opens). Wait for its handle via the orchestrator's availability
- // primitive, then navigate.
- await awaitCondition(
- () => messagesHandle() !== undefined,
- 10_000
- ).catch(() => {});
- messagesHandle()?.goToMessage(targetMessageId, targetMessageReplyId);
+ const handle = await awaitMessagesHandle();
+ handle?.goToMessage(targetMessageId, targetMessageReplyId);
}
if (isJoinCallRequested(params[CHANNEL_URL_PARAMS.joinCall])) {
@@ -330,6 +333,11 @@ export function NewChannelBlockAdapter(props: BlockChannelProps) {
setPendingJoinCall(true);
}
},
+ goToLatest: async () => {
+ setActiveTab(DEFAULT_CHANNEL_TAB);
+ const handle = await awaitMessagesHandle();
+ handle?.goToLatest();
+ },
});
const initialTargetMessageParams = (): ChannelTargetMessageParams => {
diff --git a/js/app/packages/channel/Channel/Channel.tsx b/js/app/packages/channel/Channel/Channel.tsx
index 6f1e297def..38e21550bf 100644
--- a/js/app/packages/channel/Channel/Channel.tsx
+++ b/js/app/packages/channel/Channel/Channel.tsx
@@ -120,6 +120,7 @@ export type ChannelMessagesStateSnapshot = {
export type ChannelHandle = {
goToMessage: TargetMessageController['goToMessage'];
+ goToLatest: () => void;
getMessagesStateSnapshot: () => ChannelMessagesStateSnapshot | undefined;
};
@@ -420,6 +421,31 @@ export function Channel(props: ChannelProps) {
}
};
+ const [pendingScrollToLatest, setPendingScrollToLatest] = createSignal(false);
+
+ const goToLatest: ChannelHandle['goToLatest'] = () => {
+ handleScrollToBottom();
+ setPendingScrollToLatest(true);
+ };
+
+ // When handleScrollToBottom resets a mid-history slice, the newest page
+ // arrives asynchronously and swaps the message set, so a single scroll can
+ // settle mid-list. Keep scrolling until the viewport rests at the bottom of
+ // fully loaded data.
+ createEffect(() => {
+ if (!pendingScrollToLatest()) return;
+ const navigation = threadListNavigation();
+ const scrollState = threadListScrollState();
+ if (!navigation || !scrollState?.didInitialScroll) return;
+ if (messageIndex.keys.length === 0) return;
+ if (messagesQuery.isFetching || messagesQuery.hasPreviousPage) return;
+ if (scrollState.isNearBottom) {
+ setPendingScrollToLatest(false);
+ return;
+ }
+ navigation.scrollToBottom('end');
+ });
+
const { messageListScopeId, attachMessageListRef, attachInputRef } =
createChannelHotkeys({
selection,
@@ -490,6 +516,7 @@ export function Channel(props: ChannelProps) {
if (props.onHandleReady)
props.onHandleReady({
goToMessage,
+ goToLatest,
getMessagesStateSnapshot,
});
})
diff --git a/js/app/packages/core/blockMethodRegistry.ts b/js/app/packages/core/blockMethodRegistry.ts
index 7d69ea8c7f..4877168720 100644
--- a/js/app/packages/core/blockMethodRegistry.ts
+++ b/js/app/packages/core/blockMethodRegistry.ts
@@ -8,6 +8,8 @@ type BlockMethodSpec = Record<string, (...args: any[]) => any | Promise<any>>;
export type SharedBlockSpec = {
goToLocationFromParams: (params: Record<string, any>) => Promise<void>;
+ /** Land the block on its latest content (e.g. newest channel messages). */
+ goToLatest: () => Promise<void>;
};
// Ensure all block specs extend BlockMethodSpec
diff --git a/js/app/packages/core/component/LexicalMarkdown/component/core/BlockLink.tsx b/js/app/packages/core/component/LexicalMarkdown/component/core/BlockLink.tsx
index 56740bf3d6..d28d9758c1 100644
--- a/js/app/packages/core/component/LexicalMarkdown/component/core/BlockLink.tsx
+++ b/js/app/packages/core/component/LexicalMarkdown/component/core/BlockLink.tsx
@@ -46,10 +46,11 @@ export function openDocument(
const targetBlock = fileTypeToBlockName(blockOrFileType);
if (!targetBlock) return;
+ const hasParams = !!params && Object.keys(params).length > 0;
+
if (
currentBlockId === id &&
- params &&
- Object.keys(params).length > 0 &&
+ hasParams &&
isBlockNameWithLocation(targetBlock) &&
!inNewSplit
) {
@@ -57,7 +58,13 @@ export function openDocument(
return;
}
- openWithSplit({ type: targetBlock, id }, { preferNewSplit: inNewSplit });
+ openWithSplit(
+ { type: targetBlock, id },
+ {
+ preferNewSplit: inNewSplit,
+ reopen: targetBlock === 'channel' && !hasParams ? 'latest' : undefined,
+ }
+ );
if (isBlockNameWithLocation(targetBlock)) {
openLocation(targetBlock, id, params);
diff --git a/js/app/packages/core/component/UserTooltip.tsx b/js/app/packages/core/component/UserTooltip.tsx
index eedfee4db1..e56b5b4378 100644
--- a/js/app/packages/core/component/UserTooltip.tsx
+++ b/js/app/packages/core/component/UserTooltip.tsx
@@ -53,7 +53,10 @@ export function UserTooltip(props: UserTooltipProps) {
const { channel_id } = await getOrCreateDmMutation.mutateAsync({
recipient_id: props.id,
});
- openWithSplit({ type: 'channel', id: channel_id }, { preferNewSplit });
+ openWithSplit(
+ { type: 'channel', id: channel_id },
+ { preferNewSplit, reopen: 'latest' }
+ );
} catch {
// The mutation's onError callback handles the toast.
} finally {