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 { useUserId } from '@core/context/user';
import { fireAndForget } from '@core/util/fireAndForget';
import {
entityMessagesClient,
type MessageCursor,
type MessageParent,
type MessageThread,
type PostMessage,
} from '@service-storage/messages';
import { queryOptions, useQuery } from '@tanstack/solid-query';
import { type Accessor, createEffect } from 'solid-js';
import {
newMessageId,
useDeleteMessageMutation,
useDeleteThreadMutation,
usePatchThreadMutation,
useSendMessageMutation,
} from './mutations';
import { fetchMessageThread } from './thread-replies';
import { useMessageTimelineQuery } from './timeline';
import { timelineMessages } from './timeline-entries';
/** Positioning annotations needs every root, but never fetches every root's replies. */
export function useMessageRootsQuery(parent: Accessor<MessageParent>) {
const query = useMessageTimelineQuery(parent, () => null);
createEffect(() => {
if (
query.isSuccess &&
query.hasNextPage &&
!query.isFetching &&
!query.isFetchNextPageError
)
fireAndForget(query.fetchNextPage(), 'document-messages');
});
return {
get data() {
return query.isSuccess ? query.data.pages.flatMap(timelineMessages) : [];
},
get isSuccess() {
return query.isSuccess;
},
get isPending() {
return query.isPending;
},
get isError() {
return query.isError;
},
refetch: query.refetch,
};
}
/** Every full thread for a document — copy/export needs whole discussions, not the timeline's bounded reply previews. */
export async function fetchDocumentThreads(
parent: MessageParent
): Promise<MessageThread[]> {
const threads: MessageThread[] = [];
let cursor: MessageCursor | null | undefined;
do {
const page = await entityMessagesClient.list(parent, {
anchored: false,
limit: 100,
cursor: cursor ?? undefined,
});
for (const root of page.items) {
threads.push(await fetchMessageThread(parent, root.id));
}
cursor = page.next_cursor;
} while (cursor);
return threads;
}
/** Bind the shared mutations to an annotation editor's parent. */
export function useMessageActions(parent: Accessor<MessageParent>) {
const userId = useUserId();
const send = useSendMessageMutation();
const remove = useDeleteMessageMutation();
const patchThread = usePatchThreadMutation();
const removeThread = useDeleteThreadMutation();
return {
post: (message: PostMessage) => {
const senderId = userId();
if (!senderId) throw new Error('Sign in to comment');
return send.mutateAsync({
parent: parent(),
message,
senderId,
optimisticId: newMessageId(),
});
},
delete: (id: string) =>
remove.mutateAsync({ parent: parent(), messageID: id }),
resolve: (rootId: string, resolved: boolean) =>
patchThread.mutateAsync({
parent: parent(),
rootId,
patch: { resolved },
}),
deleteThread: (rootId: string) =>
removeThread.mutateAsync({ parent: parent(), rootId }),
};
}
// Cached callbacks outlive the hook; only plain request values enter here.
function messageLinkQueryOptions(
parent: MessageParent,
target: string | null | undefined
) {
return queryOptions({
queryKey: ['historical-comment-link', parent.type, parent.id, target],
enabled: !!target,
queryFn: () =>
/^\d+$/.test(target!)
? entityMessagesClient.legacyLink(parent, target!)
: entityMessagesClient.get(parent, target!),
});
}
/** Resolve copied links, falling back to the root when the linked reply was deleted. */
export function useMessageLink(
parent: Accessor<MessageParent>,
target: Accessor<string | null | undefined>
) {
const legacy = useQuery(() => messageLinkQueryOptions(parent(), target()));
return {
error: () => legacy.error,
refetch: legacy.refetch,
messageId: () => {
const id = target();
if (!id) return null;
if (legacy.isSuccess)
return legacy.data.deleted_at
? (legacy.data.thread_id ?? legacy.data.id)
: legacy.data.id;
return /^\d+$/.test(id) ? null : id;
},
rootId: () =>
legacy.isSuccess ? (legacy.data.thread_id ?? legacy.data.id) : null,
/** True once the link is known to be a root, a reply, or nothing loadable. */
resolved: () => !target() || legacy.isSuccess || legacy.isError,
};
}