Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix: channel open jitters due to unstable location (#669)
gbirman49bf3f1cc06f
diff --git a/js/app/packages/app/component/UnifiedListView.tsx b/js/app/packages/app/component/UnifiedListView.tsxindex a59e7f6925..0c7baf8b87 100644--- a/js/app/packages/app/component/UnifiedListView.tsx+++ b/js/app/packages/app/component/UnifiedListView.tsx@@ -2,6 +2,7 @@ import {useGlobalBlockOrchestrator,useGlobalNotificationSource,} from '@app/component/GlobalAppState';+import type { BlockChannelProps } from '@block-channel/component/Block';import { URL_PARAMS as CHANNEL_PARAMS } from '@block-channel/constants';import { URL_PARAMS as EMAIL_PARAMS } from '@block-email/constants';import { URL_PARAMS as MD_PARAMS } from '@block-md/constants';@@ -1101,9 +1102,19 @@ export function UnifiedListView(props: UnifiedListViewProps) {if (entity.type === 'document')return documentEntityClickHandler(entity, event, location);+ const params =+ entity.type === 'channel' && location?.type === 'channel'+ ? ({+ target: {+ threadId: location.threadId,+ messageId: location.messageId,+ },+ } as BlockChannelProps)+ : undefined;+const handle = event.altKey- ? insertSplit({ type: entity.type, id: entity.id })- : replaceOrInsertSplit({ type: entity.type, id: entity.id });+ ? insertSplit({ type: entity.type, id: entity.id, params })+ : replaceOrInsertSplit({ type: entity.type, id: entity.id, params });handle?.activate();@@ -1111,10 +1122,12 @@ export function UnifiedListView(props: UnifiedListViewProps) {switch (location.type) {case 'channel': {- const blockHandle = await blockOrchestrator.getBlockHandle(entity.id);- await blockHandle?.goToLocationFromParams({- [CHANNEL_PARAMS.message]: location.messageId,- });+ // NOTE: this is handled by the channel block params+ // const blockHandle = await blockOrchestrator.getBlockHandle(entity.id);+ // await blockHandle?.goToLocationFromParams({+ // [CHANNEL_PARAMS.thread]: location.threadId,+ // [CHANNEL_PARAMS.message]: location.messageId,+ // });break;}case 'email': {diff --git a/js/app/packages/block-channel/component/Block.tsx b/js/app/packages/block-channel/component/Block.tsxindex c30e0d95fb..2ad770d36c 100644--- a/js/app/packages/block-channel/component/Block.tsx+++ b/js/app/packages/block-channel/component/Block.tsx@@ -12,6 +12,7 @@ import { useUserId } from '@service-gql/client';import { createSignal, type JSXElement, Match, Switch } from 'solid-js';import { Channel } from './Channel';import { JoinChannelDialog } from './JoinChannelDialog';+import type { TargetMessageInfo } from './MessageList/MessageList';export function WithTopBar(props: { children: JSXElement }) {return <div>{props.children}</div>;@@ -19,7 +20,11 @@ export function WithTopBar(props: { children: JSXElement }) {export type JoinState = 'REQUIRED' | 'NOT_REQUIRED';-export default function BlockChannel() {+export type BlockChannelProps = {+ target?: TargetMessageInfo;+};++export default function BlockChannel(props: BlockChannelProps) {const channelId = useBlockId();const channel = useChannelQuery(() => channelId);@@ -106,7 +111,9 @@ export default function BlockChannel() {)}</Match><Match when={validChannelData()}>- {(channelData) => <Channel data={channelData()} />}+ {(channelData) => (+ <Channel data={channelData()} target={props.target} />+ )}</Match></Switch></DocumentBlockContainer>diff --git a/js/app/packages/block-channel/component/Channel.tsx b/js/app/packages/block-channel/component/Channel.tsxindex d22126b2a8..a72d5bf2cf 100644--- a/js/app/packages/block-channel/component/Channel.tsx+++ b/js/app/packages/block-channel/component/Channel.tsx@@ -52,7 +52,7 @@ import {import { createStore } from 'solid-js/store';import { type FocusableElement, tabbable } from 'tabbable';import { ChannelInput } from './ChannelInput';-import { MessageList } from './MessageList/MessageList';+import { MessageList, type TargetMessageInfo } from './MessageList/MessageList';import { Top } from './Top';false && fileFolderDrop;@@ -78,7 +78,10 @@ export function createChannelRefetchEffect(channelId: string) {});}-export function Channel(props: { data: Required<ChannelData> }) {+export function Channel(props: {+ data: Required<ChannelData>;+ target?: TargetMessageInfo;+}) {const channel = useChannelQuery(() => props.data.channel.id);const [_activeThreadId, setActiveThreadId] = activeThreadIdSignal;@@ -101,24 +104,24 @@ export function Channel(props: { data: Required<ChannelData> }) {const blockHandle = blockHandleSignal.get;- const initialTargetMessage = () => {- const messageID = searchParams[URL_PARAMS.message];- const threadID = searchParams[URL_PARAMS.thread];+ // use props if available, fallback to search params+ const initialTargetMessage = (): TargetMessageInfo | undefined => {+ const target = props.target;+ if (target) return target;- if (!messageID) return;+ const messageId = searchParams[URL_PARAMS.message];+ const threadId = searchParams[URL_PARAMS.thread];++ if (!messageId) return;return {- messageId: Array.isArray(messageID) ? messageID[0] : messageID,- threadId: Array.isArray(threadID) ? threadID[0] : threadID,+ messageId: Array.isArray(messageId) ? messageId[0] : messageId,+ threadId: Array.isArray(threadId) ? threadId[0] : threadId,};};const [targetMessage, setTargetMessage] = createSignal<- | {- messageId: string;- threadId?: string;- }- | undefined+ TargetMessageInfo | undefined>(initialTargetMessage());createMethodRegistration(blockHandle, {diff --git a/js/app/packages/block-channel/component/MessageList/MessageList.tsx b/js/app/packages/block-channel/component/MessageList/MessageList.tsxindex f08c8b6262..3e74700247 100644--- a/js/app/packages/block-channel/component/MessageList/MessageList.tsx+++ b/js/app/packages/block-channel/component/MessageList/MessageList.tsx@@ -32,6 +32,7 @@ import {type Accessor,createEffect,createMemo,+ createRenderEffect,createSelector,createSignal,Match,@@ -68,7 +69,7 @@ const BASE_ITEM_SIZE = 50;const clampIndex = (value: number, minValue: number, maxValue: number) =>Math.min(Math.max(value, minValue), maxValue);-type TargetMessageInfo = { messageId: string; threadId?: string };+export type TargetMessageInfo = { messageId: string; threadId?: string };export type MessageListProps = {channelId: string;@@ -98,7 +99,9 @@ function EmptyMessageList() {export function MessageList(props: MessageListProps) {const [containerRef, setContainerRef] = createSignal<HTMLDivElement>();const [virtualHandle, setVirtualHandle] = createSignal<VirtualizerHandle>();- let listContainerRef!: HTMLDivElement;+ const [listContainerRef, setListContainerRef] = createSignal<+ HTMLDivElement | undefined+ >();const [scrollContainerRef, setScrollContainerRef] = createSignal<HTMLDivElement | undefined>();@@ -196,60 +199,59 @@ export function MessageList(props: MessageListProps) {* @param params.forceBottom - force the scroll to bottom ignoring target message* @returns*/- const scrollToBottomOrTarget = debounce(- (params: { forceBottom?: boolean } = {}) => {- const { forceBottom } = params;- const timeStamp = Date.now();- const delta = timeStamp - lastTargetMessageTimestamp();- const target = props.targetMessage();+ const scrollToBottomOrTarget = (params: { forceBottom?: boolean } = {}) => {+ const { forceBottom } = params;+ const timeStamp = Date.now();+ const delta = timeStamp - lastTargetMessageTimestamp();+ const target = props.targetMessage();- if (- forceBottom ||- ((!target || delta > TARGET_MESSAGE_ACTIVE_TIME) && isNearBottom())- ) {- if (scrollTimeoutId) clearTimeout(scrollTimeoutId);- const lastIndex = props.orderedMessages().length - 1;- virtualHandle()?.scrollToIndex(lastIndex, {- align: 'end',- });- return;- }+ if (+ forceBottom ||+ ((!target || delta > TARGET_MESSAGE_ACTIVE_TIME) && isNearBottom())+ ) {+ if (scrollTimeoutId) clearTimeout(scrollTimeoutId);+ const lastIndex = props.orderedMessages().length - 1;+ virtualHandle()?.scrollToIndex(lastIndex, {+ align: 'end',+ });+ return;+ }- const { messageId: targetMessageId, threadId } = target || {};+ const { messageId: targetMessageId, threadId } = target || {};- if (!targetMessageId) return;+ if (!targetMessageId) return;- // If we have a target message, scroll to it and focus it- const index = props- .orderedMessages()- ?.findIndex((m) => m.id === targetMessageId);+ // If we have a target message, scroll to it and focus it+ const index = props+ .orderedMessages()+ ?.findIndex((m) => m.id === targetMessageId);- if (index === -1) {- console.warn('Target message not found');- toast.failure('Message not found.');- scrollToBottomOrTarget({ forceBottom: true });- return;- }+ if (index === -1) {+ console.warn('Target message not found');+ toast.failure('Message not found.');+ scrollToBottomOrTarget({ forceBottom: true });+ return;+ }- if (threadId) {- setThreadViewStore(threadId, (prev) => ({- ...prev,- threadExpanded: true,- }));- }+ if (threadId) {+ setThreadViewStore(threadId, (prev) => ({+ ...prev,+ threadExpanded: true,+ }));+ }- if (scrollTimeoutId) clearTimeout(scrollTimeoutId);+ if (scrollTimeoutId) clearTimeout(scrollTimeoutId);- setTargetMessageActive(true);- virtualHandle()?.scrollToIndex(index, {- align: 'center',- });- setTimeout(() => {- setTargetMessageActive(false);- }, TARGET_MESSAGE_ACTIVE_TIME);- },- 5- );+ setTargetMessageActive(true);+ virtualHandle()?.scrollToIndex(index, {+ align: 'center',+ });+ setTimeout(() => {+ setTargetMessageActive(false);+ }, TARGET_MESSAGE_ACTIVE_TIME);+ };++ const debouncedScrollToBottomOrTarget = debounce(scrollToBottomOrTarget, 5);/*** Track context for messages as they are rendered in the list@@ -442,10 +444,14 @@ export function MessageList(props: MessageListProps) {// scroll to bottom on change to last message state (including new messages)createEffect(- on([lastMessageReaction, lastMessageThread, lastMessageThreadCount], () => {- if (!isNearBottom()) return;- scrollToBottomOrTarget({ forceBottom: true });- })+ on(+ [lastMessageReaction, lastMessageThread, lastMessageThreadCount],+ () => {+ if (!isNearBottom()) return;+ debouncedScrollToBottomOrTarget({ forceBottom: true });+ },+ { defer: true }+ ));const [unviewedMessages, setUnviewedMessages] = createSignal<Message[]>();@@ -515,30 +521,47 @@ export function MessageList(props: MessageListProps) {const [size, setSize] = createSignal<DOMRect>();- // scroll to bottom on size change, if the user is near the bottom+ // set scroll container ref on virtualizer updatescreateEffect(- on(virtualHandle, (handle) => {- if (!handle) return;+ on([virtualHandle, listContainerRef], ([handle, listContainerRef]) => {+ if (!handle || !listContainerRef) return;const scrollContainer = listContainerRef.querySelector('[data-channel-message-list]') as HTMLDivElement | null;setScrollContainerRef(scrollContainer ?? undefined);-- scrollToBottomOrTarget({- forceBottom: true,- });}));- createEffect(+ createRenderEffect(on(props.targetMessage, (target) => {if (!target) return;setLastTargetMessageTimestamp(Date.now());- scrollToBottomOrTarget();}));+ // initial scroll+ createRenderEffect(+ on(+ [+ virtualHandle,+ props.targetMessage,+ listContainerRef,+ props.orderedMessages,+ ],+ // NOTE: ordered messages gets updated on initial load due to thread layout calculations+ // so we need to keep scrolling down until it settles+ ([handle, target, listContainerRef, orderedMessages]) => {+ if (!handle || !listContainerRef || orderedMessages.length === 0)+ return;++ const forceBottom = target === undefined;++ scrollToBottomOrTarget({ forceBottom });+ }+ )+ );+const splitLayoutContext = useContext(SplitLayoutContext);const manager = splitLayoutContext?.manager;const splitCount = createMemo(() => manager?.splits().length ?? 0);@@ -628,12 +651,12 @@ export function MessageList(props: MessageListProps) {><divclass="flex flex-col h-full relative"- ref={listContainerRef}+ ref={setListContainerRef}onWheel={markUserScrolled}onTouchMove={markUserScrolled}onPointerDown={markUserScrolled}use:observedSize={{- setSize: setSize,+ setSize,}}><Switch fallback={<EmptyMessageList />}>@@ -740,7 +763,9 @@ export function MessageList(props: MessageListProps) {icon={ArrowDownIcon}theme="base"text="Jump to latest"- onMouseDown={() => scrollToBottomOrTarget({ forceBottom: true })}+ onMouseDown={() =>+ debouncedScrollToBottomOrTarget({ forceBottom: true })+ }secondaryIcon={XIcon}onOptionClick={() => setDismissJumpToLatest(true)}showSeparatordiff --git a/js/app/packages/core/block.ts b/js/app/packages/core/block.tsindex 391b35ad68..f65949c1a3 100644--- a/js/app/packages/core/block.ts+++ b/js/app/packages/core/block.ts@@ -1,5 +1,5 @@import type { BlockCanvasProps } from '@block-canvas/component/Block';-+import type { BlockChannelProps } from '@block-channel/component/Block';import type { IDocumentStorageServiceFile } from '@filesystem/file';import type { AccessLevel } from '@service-storage/generated/schemas/accessLevel';import type { DocumentMetadata } from '@service-storage/generated/schemas/documentMetadata';@@ -274,6 +274,7 @@ export type ExtractLoadType<T extends LoadFunction<any, any>> =export interface BlockComponentProps extends Record<BlockName, ObjectLike> {canvas: BlockCanvasProps;+ channel: BlockChannelProps;}interface BlockComponentLoadData extends Record<BlockName, ObjectLike> {diff --git a/js/app/packages/macro-entity/src/queries/search.ts b/js/app/packages/macro-entity/src/queries/search.tsindex 7392c01239..1feaa5efb4 100644--- a/js/app/packages/macro-entity/src/queries/search.ts+++ b/js/app/packages/macro-entity/src/queries/search.ts@@ -61,6 +61,7 @@ const getSearchData = (data: TypedInnerSearchResult): SearchData => {sentAt: r.created_at!,location: {type: 'channel' as const,+ threadId: r.thread_id ?? undefined,messageId: r.message_id!,},}));diff --git a/js/app/packages/macro-entity/src/types/search.ts b/js/app/packages/macro-entity/src/types/search.tsindex 353e42479b..35835d739f 100644--- a/js/app/packages/macro-entity/src/types/search.ts+++ b/js/app/packages/macro-entity/src/types/search.ts@@ -15,6 +15,7 @@ type PdfHighlightLocation = {type ChannelMessageHighlightLocation = {type: 'channel';+ threadId?: string;messageId: string;};