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
import { StaticMarkdownContext } from '@core/component/LexicalMarkdown/component/core/StaticMarkdown';
import { fireAndForget } from '@core/util/fireAndForget';
import { Key } from '@solid-primitives/keyed';
import { cn, Layer } from '@ui';
import { CollapseTransition } from '@ui/components/CollapseTransition';
import {
createEffect,
createMemo,
createSelector,
onCleanup,
Show,
} from 'solid-js';
import { EmailThreadTitle } from '../components/email-thread-title';
import { useEmailThreadState } from '../context/email-thread-state-context';
import { useEmailThreadViewContext } from '../context/email-thread-view-context';
import {
fetchOlderMessages,
isTruncatedMiddleMessage,
isUnreadMessage,
listNeedsOlderPage,
threadMessageIsExpanded,
truncatedMiddleCount,
} from '../primitives/scroll-to-message';
import { MessageContainer } from './message-container';
interface MessageListProps {
initialLoadComplete: boolean;
markdownDomRef?: (ref: HTMLDivElement) => void | HTMLDivElement;
title?: string;
/**
* Full-frame mobile: when nothing is in flow below the list (the collapsed
* reply buttons float in the accessory region), the list carries the bottom
* inset in-scroll so the last message rests above the floating chrome.
*/
underScrollsBottom?: boolean;
showMiddleMessages: boolean;
hiddenChipFocused: boolean;
allowRowHover: boolean;
onHiddenChipFocus: () => void;
onOpenMiddle: () => void;
}
export function MessageList(props: MessageListProps) {
const getIsScrollingToMessage = () => context.isScrollingToMessage();
const context = useEmailThreadState();
const viewContext = useEmailThreadViewContext();
// One selected message at a time, whether it was reached by click, tab, or
// arrow keys — they all write the same focused id.
const isSelectedSelector = createSelector(
context.messages.focusedId,
(a, b) => !!a && !!b && a === b
);
const hiddenCount = createMemo(() =>
truncatedMiddleCount(context.messages.list().length)
);
createEffect(() => {
const list = context.messagesListRef();
if (!list) return;
const applyHeight = () => {
list.style.setProperty('--thread-height', `${list.clientHeight}px`);
};
const maybeFetchOlder = () => {
if (
!listNeedsOlderPage({
initialLoadComplete: props.initialLoadComplete,
isScrollingToMessage: getIsScrollingToMessage(),
isFetching: context.query.isFetching(),
hasMore: context.query.hasMore(),
scrollHeight: list.scrollHeight,
clientHeight: list.clientHeight,
})
) {
return;
}
fireAndForget(
fetchOlderMessages(list, context.query.fetchNextPage),
'email-thread'
);
};
applyHeight();
maybeFetchOlder();
const observer = new ResizeObserver(() => {
applyHeight();
maybeFetchOlder();
});
observer.observe(list);
onCleanup(() => observer.disconnect());
});
return (
<div
class={cn(
'pt-4 pb-6 w-full flex flex-col items-center gap-2 overflow-y-scroll overflow-x-hidden [overflow-anchor:none] scrollbar-hidden text-sm scroll-pt-4 scroll-pb-4 scroll-smooth motion-reduce:scroll-auto',
'touch:pt-[calc(var(--mobile-content-inset-top,0)+0.5rem)] mobile:gap-0',
// The in-scroll insets only clear the floating chrome at the ends of
// the list. Every alignment in between — focusing a card, revealing one
// that just grew, keyboard paging — lands against the scrollport edges,
// so scroll-padding has to describe the same chrome or a card gets
// parked underneath it with its attachments out of reach.
'touch:scroll-pt-[calc(var(--mobile-content-inset-top,0px)+1rem)] touch:scroll-pb-[calc(var(--mobile-content-inset-bottom,0px)+1rem)]',
props.underScrollsBottom &&
'touch:pb-[calc(var(--mobile-content-inset-bottom,0)+1.5rem)]'
)}
ref={context.registerMessagesList}
onscroll={(e) => {
const list = e.currentTarget;
const scrollFromTop = list.scrollTop;
if (getIsScrollingToMessage() || !props.initialLoadComplete) return;
const isNearBeginning = scrollFromTop <= 300;
if (
isNearBeginning &&
!context.query.isFetching() &&
context.query.hasMore()
) {
fireAndForget(
fetchOlderMessages(list, context.query.fetchNextPage),
'email-thread'
);
}
}}
>
{/* Desktop pins the subject and participants above this list; touch
scrolls the subject with the messages. */}
<Show when={viewContext.thread.isTouch() && props.title}>
{(title) => (
<div class="shrink-0 w-full flex justify-center">
<div class="macro-message-width macro-message-padding w-full pt-6 pb-3">
<EmailThreadTitle
onCopy={viewContext.copySubject}
title={title()}
copyReveal="always"
class="text-xl pt-1 pb-0"
/>
</div>
</div>
)}
</Show>
<Layer depth={1}>
<StaticMarkdownContext>
{/* Key by db_id so prepends and refetches keep per-row state. */}
<Key each={context.messages.list()} by="db_id">
{(message) => {
const chronologicalIndex = createMemo(() =>
context.messages
.list()
.findIndex((item) => item.db_id === message().db_id)
);
const normalizedIndex = createMemo(() => {
// The element at the 0th index isn't actually the first message
// if there is more data to load so we return -1 so that `isFirstMessage`
// evaluates to false. This fixes an issue with the "first" message' full
// html to show in `EmailMessageBody`
if (chronologicalIndex() === 0 && context.query.hasMore()) {
return -1;
}
return chronologicalIndex();
});
const isLastMessage = createMemo(() => {
return (
chronologicalIndex() ===
(context.messages.list().length ?? 0) - 1
);
});
const hideMiddle = createMemo(() => {
return (
!props.showMiddleMessages &&
isTruncatedMiddleMessage(
chronologicalIndex(),
context.messages.list().length
)
);
});
// A message with an in-progress draft reply stays expanded so the
// draft (inline on desktop, a preview on touch) remains visible
// even after newer messages arrive.
const hasDraft = createMemo(() => {
const messageId = message().db_id;
if (!messageId) return false;
return !!context.drafts.getDraftForMessage(messageId);
});
const isExpanded = createMemo(() => {
const messageId = message().db_id;
if (!messageId) return false;
return threadMessageIsExpanded({
chronologicalIndex: chronologicalIndex(),
listLength: context.messages.list().length,
expansionOverride:
context.messages.expandedBodyIds[messageId],
isUnread: isUnreadMessage(message()),
hasDraft: hasDraft(),
});
});
return (
<>
<CollapseTransition open={!hideMiddle()}>
<div class="w-full shrink-0">
<MessageContainer
isFirstMessage={normalizedIndex() === 0}
isLastMessage={isLastMessage()}
isSelected={isSelectedSelector(
message().db_id ?? undefined
)}
allowHover={props.allowRowHover}
message={message()}
isExpanded={isExpanded()}
markdownDomRef={
isLastMessage() ? props.markdownDomRef : undefined
}
/>
</div>
</CollapseTransition>
<Show
when={
chronologicalIndex() === 0 &&
!props.showMiddleMessages &&
hiddenCount() > 0
}
>
<div class="shrink-0 w-full flex justify-center">
<div class="macro-message-width macro-message-padding w-full">
<div class="grid grid-cols-[minmax(0,1fr)_auto_minmax(0,1fr)] items-center gap-2">
<span
aria-hidden="true"
class="border-t border-edge-muted"
/>
{/* Plain text, no chip: the same color as the
collapsed previews around it, faded until
hovered or focused. */}
<button
type="button"
class={cn(
'shrink-0 px-2 py-1 text-sm text-ink-extra-muted/60 outline-none transition-colors not-touch:hover:text-ink-extra-muted focus-visible:text-ink-muted',
props.hiddenChipFocused && 'text-ink-muted'
)}
data-hidden-messages
onPointerEnter={() =>
context.messages.setHovered({
kind: 'hidden-chip',
})
}
onPointerLeave={() => {
if (
context.messages.hovered()?.kind ===
'hidden-chip'
) {
context.messages.setHovered(undefined);
}
}}
onFocus={() => props.onHiddenChipFocus()}
onClick={() => props.onOpenMiddle()}
>
Show {hiddenCount()} hidden{' '}
{hiddenCount() === 1 ? 'message' : 'messages'}
</button>
<span
aria-hidden="true"
class="border-t border-edge-muted"
/>
</div>
</div>
</div>
</Show>
</>
);
}}
</Key>
</StaticMarkdownContext>
</Layer>
</div>
);
}