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 { fireAndForget } from '@core/util/fireAndForget';
import { Telemetry } from '@macro-inc/observability';
import { useUpsertToHistoryMutation } from '@queries/history/history';
import {
hasSoupEntity,
optimisticUpdateSoupItemViewedAt,
refetchSoupEntity,
type SoupEntityTag,
} from '@queries/soup/cache';
import {
blockNameToItemType,
type HistoryItemType,
type ItemType,
isCloudStorageItem,
} from '@service-storage/client';
import type { QueryClient } from '@tanstack/solid-query';
import type { Accessor } from 'solid-js';
import { match } from 'ts-pattern';
import type { BlockName } from '../block';
function isSoupEntityTag(
itemType: ItemType
): itemType is ItemType & SoupEntityTag {
return match(itemType)
.with(
'email',
'channel_message',
'channel_thread',
'routine',
'agent_session',
'database',
'form',
'calendar_event',
'foreign',
'crm_company',
'crm_contact',
'crm_pipeline',
'initiative',
() => false
)
.with('document', 'chat', 'project', 'channel', 'call', () => true)
.exhaustive();
}
// Item types we want recorded in UserHistory on open. CRM types aren't
// CloudStorageItemType (they're not stored in document_storage), but the
// /history endpoint accepts any item_type as text and the soup's
// `viewed_updated` sort joins UserHistory generically — so writing
// these rows is what surfaces recently-opened companies/contacts in
// Quick Access and the @ mention menu.
//
// Foreign entities (GitHub PRs) are intentionally omitted: they are not
// documents, and posting `/history/document/{id}` 401s because ACL looks
// up a document that does not exist.
function shouldTrackInUserHistory(
itemType: ItemType
): itemType is HistoryItemType {
return (
isCloudStorageItem(itemType) ||
itemType === 'agent_session' ||
itemType === 'crm_company' ||
itemType === 'crm_contact'
);
}
async function fetchOpenedSoupEntity(
itemId: string,
itemType: ItemType & SoupEntityTag
) {
try {
await refetchSoupEntity(itemId, itemType);
} catch (error) {
Telemetry.error(error, { surface: 'track-block-opened', itemType });
}
}
/**
* Tracks opening of a block and updates history accordingly.
* We have this in a separate file to prevent cyclic dependencies.
*/
export function track({
itemId,
blockName,
client,
}: {
itemId: string;
blockName: BlockName;
client: Accessor<QueryClient>;
}) {
const itemType = blockNameToItemType(blockName);
optimisticUpdateSoupItemViewedAt(itemId);
const inSoup = hasSoupEntity(itemId);
if (!inSoup && isSoupEntityTag(itemType)) {
fireAndForget(
fetchOpenedSoupEntity(itemId, itemType),
'track-block-opened'
);
}
if (!shouldTrackInUserHistory(itemType)) return;
const upsertToHistoryMutation = useUpsertToHistoryMutation(undefined, client);
upsertToHistoryMutation.mutate({ itemId, itemType });
}