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 { getPreferredCalendarPeriodView } from '@app/features/calendar/calendar-preferences';
import {
CALENDAR_ROUTE_ID,
CALENDAR_SEARCH_NAMESPACE,
calendarPath,
} from '@app/features/calendar-view/calendar-url';
import { CALENDAR_VIEW_ID } from '@app/features/calendar-view/types';
import { channelsSearch } from '@app/features/channels-view/channels-route';
import {
crmPeopleSearch,
crmRecordSearch,
isCrmRetiredDatabasesRoute,
legacyCrmPipelineId,
retiredCrmDatabasesPath,
} from '@app/features/crm/crm-route';
import {
driveDocumentFromContent,
drivePath,
} from '@app/features/drive-view/primitives/drive-route';
import { driveDocumentBlockType } from '@app/features/drive-view/primitives/drive-route-schema';
import { driveSearch } from '@app/features/drive-view/primitives/drive-search';
import { composeDraftSearch } from '@app/features/email-compose/compose-route-draft';
import { URL_PARAMS as EMAIL_URL_PARAMS } from '@app/features/email-thread/core/location';
import { EMAIL_DETAIL_SEARCH_NAMESPACE } from '@app/features/email-view/email-route';
import {
replacePaneSearchParams,
routeParams,
type SerializedSearchParams,
type SplitRouterMiddleware,
type SplitRouterMiddlewareContext,
type SplitRouterMiddlewareResult,
} from '@app/lib/split-router';
import { recordNavigationBreadcrumb } from '@app/observability/navigation-breadcrumb';
import { paneRootMatch } from '@app/routes/app-route';
import { URL_PARAMS as CALL_URL_PARAMS } from '@block-call/constants';
import { URL_PARAMS as CHANNEL_URL_PARAMS } from '@block-channel/constants';
import { URL_PARAMS as MD_URL_PARAMS } from '@block-md/constants';
import { URL_PARAMS as PDF_URL_PARAMS } from '@block-pdf/constants';
import { openInNewTabMiddleware } from '@components/app/tab-layout/open-in-tab-middleware';
import { COMMENT_LINK_PARAM } from '@core/messages/comment-link';
import { match } from 'ts-pattern';
import { decodeLegacyPair } from './legacy-route';
type AppMiddlewareState = {
isTouchDevice: () => boolean;
};
function crmPipelinePath(pipelineId: string): string {
return `/companies/pipeline/${encodeURIComponent(pipelineId)}`;
}
/** Customers showed a pipeline as a list tab before it had its own route. */
function redirectLegacyCrmPipeline({
to,
cause,
externalSearch,
redirect,
}: SplitRouterMiddlewareContext): SplitRouterMiddlewareResult | undefined {
if (to.location.route.matches.at(-1)?.id !== 'view-companies') return;
const pipelineId = legacyCrmPipelineId(
to.location.search,
cause === 'initial' || cause === 'external' ? externalSearch : undefined
);
if (!pipelineId) return;
return redirect(crmPipelinePath(pipelineId));
}
/** Customers no longer lists databases; old links go to the record or database. */
function redirectRetiredCrmDatabases({
to,
redirect,
}: SplitRouterMiddlewareContext): SplitRouterMiddlewareResult | undefined {
const route = to.location.route;
const leaf = route.matches.at(-1)?.id;
if (!leaf || !isCrmRetiredDatabasesRoute(leaf)) return;
const destination = retiredCrmDatabasesPath(routeParams(route));
return destination ? redirect(destination) : undefined;
}
/** Upgrade legacy component and block URLs when supported; on touch, keep
* document details in their full-block routes instead of inline Drive views. */
function redirectLegacyRoutes(
{ routes, to, redirect }: SplitRouterMiddlewareContext,
options: AppMiddlewareState
): SplitRouterMiddlewareResult | undefined {
const route = to.location.route;
const rootId = paneRootMatch(route)?.id;
const isTouch = options.isTouchDevice();
if (rootId === 'drive' && isTouch) {
const { documentType, documentId } = routeParams(route);
if (typeof documentType === 'string' && typeof documentId === 'string') {
return redirect(
`/${driveDocumentBlockType(documentType)}/${encodeURIComponent(documentId)}`
);
}
}
// Touch shows records on their own pages, as the Customers lists open them.
if (rootId === 'view-companies' && isTouch) {
const { recordType, recordId, itemId } = routeParams(route);
if (
typeof recordType === 'string' &&
typeof recordId === 'string' &&
typeof itemId !== 'string'
) {
return redirect(`/${recordType}/${encodeURIComponent(recordId)}`);
}
}
if (rootId === 'call-detail') {
const { callId } = routeParams(route);
if (typeof callId === 'string') {
return redirect(`/calls/call/${encodeURIComponent(callId)}`);
}
}
if (rootId === 'pr-detail') {
const { foreignEntityId } = routeParams(route);
if (typeof foreignEntityId === 'string') {
return redirect(`/reviews/pr/${encodeURIComponent(foreignEntityId)}`);
}
}
if (rootId !== 'legacy-content') return;
const { type, id } = routeParams(route);
if (typeof type !== 'string' || typeof id !== 'string') return;
const content = decodeLegacyPair(type, id);
if (!content) return;
const path = match(content)
.with({ type: 'component', id: 'documents' }, () => '/drive')
.with({ type: 'component', id: 'settings' }, () => '/settings')
.with({ type: 'component', id: CALENDAR_VIEW_ID }, () =>
calendarPath(getPreferredCalendarPeriodView())
)
// Desktop writes a new email beside the Email list; touch keeps the
// full-screen composer.
.with({ type: 'component', id: 'email-compose' }, () =>
isTouch ? undefined : '/mail/new'
)
.with({ type: 'component' }, ({ id }) =>
routes.byId.has(`view-${id}`) ? `/${id}` : undefined
)
// A pipeline opens in Customers, on touch too.
.with({ type: 'pipeline' }, ({ id }) => crmPipelinePath(id))
.when(
() => isTouch,
() => undefined
)
.with({ type: 'email' }, ({ id }) => `/mail/${encodeURIComponent(id)}`)
.with(
{ type: 'channel' },
({ id }) => `/channels/${encodeURIComponent(id)}`
)
.with({ type: 'task' }, ({ id }) => `/tasks/${encodeURIComponent(id)}`)
.with(
{ type: 'company' },
({ id }) => `/companies/company/${encodeURIComponent(id)}`
)
// A contact opens from People, whose list its breadcrumb returns to.
.with({ type: 'contact' }, ({ id }) => {
const query = new URLSearchParams();
// An explicit query replaces the entry's search, so keep what it carries.
replacePaneSearchParams(query, [
{ ...to.location.search, ...crmPeopleSearch },
]);
return `/companies/contact/${encodeURIComponent(id)}?${query}`;
})
// Like a task, a project opens in Tasks, under its Projects tab.
.with({ type: 'initiative' }, ({ id }) => {
const query = new URLSearchParams();
replacePaneSearchParams(query, [{ tasks: { tab: ['projects'] } }]);
return `/tasks/projects/${encodeURIComponent(id)}/overview?${query}`;
})
.otherwise((content) => {
const document = driveDocumentFromContent(content);
return document
? drivePath({ kind: 'tab', tab: 'owned' }, document)
: undefined;
});
if (!path) return;
return redirect(path);
}
/** On external entry, copy legacy detail query keys into the destination pane.
* Keep repeated values and let explicit pane-local values take precedence. */
function migrateLegacySearch({
to,
path,
cause,
externalSearch,
redirect,
}: SplitRouterMiddlewareContext): SplitRouterMiddlewareResult | undefined {
if (cause !== 'initial' && cause !== 'external') return;
if (!externalSearch) return;
const leafId = to.location.route.matches.at(-1)?.id;
const homeChannel =
leafId === 'home-channel' ||
(leafId === 'home-preview' &&
routeParams(to.location.route).blockType === 'channel');
const homeDocumentType =
leafId === 'home-document'
? routeParams(to.location.route).documentType
: leafId === 'home-preview'
? routeParams(to.location.route).blockType
: undefined;
const commentKey = (() => {
switch (homeDocumentType) {
case 'md':
case 'task':
case 'skill':
case 'snippet':
case 'spreadsheet':
return MD_URL_PARAMS.commentId;
case 'pdf':
return PDF_URL_PARAMS.annotationId;
}
})();
const homeDocumentMapping = commentKey
? {
namespace: driveSearch.namespace,
fields: [[commentKey, 'commentId']] as const,
}
: undefined;
const mapping = match(leafId)
.with('mail-thread', () => ({
namespace: EMAIL_DETAIL_SEARCH_NAMESPACE,
fields: [[EMAIL_URL_PARAMS.messageId, 'messageId']] as const,
}))
// A `component/email-compose?to=` link names its recipients.
.with('mail-compose', () => ({
namespace: composeDraftSearch.namespace,
fields: [['to', 'to']] as const,
}))
.when(
(id) => id === 'channels-channel' || homeChannel,
() => ({
namespace: channelsSearch.namespace,
fields: [
[CHANNEL_URL_PARAMS.message, 'messageId'],
[CHANNEL_URL_PARAMS.thread, 'threadId'],
] as const,
})
)
.when(
(id) => id === 'home-document' || id === 'home-preview',
() => homeDocumentMapping
)
.with('companies-record', () => ({
namespace: crmRecordSearch.namespace,
fields: [[COMMENT_LINK_PARAM, 'commentId']] as const,
}))
.with(CALENDAR_ROUTE_ID, () => ({
namespace: CALENDAR_SEARCH_NAMESPACE,
fields: [['eventId', 'eventId']] as const,
}))
.with('call-detail', 'drive-call', 'calls-call', () => ({
namespace: 'call-detail',
fields: [
[CALL_URL_PARAMS.transcriptId, 'transcriptId'],
[CALL_URL_PARAMS.messageId, 'messageId'],
] as const,
}))
.otherwise(() => undefined);
if (!mapping) return;
const { namespace, fields } = mapping;
const raw = new URLSearchParams(externalSearch);
const current = to.location.search?.[namespace] ?? {};
const additions: SerializedSearchParams = {};
for (const [legacyKey, field] of fields) {
// Explicit canonical values, including empty ones, take precedence.
if (Object.hasOwn(current, field)) continue;
const values = raw.getAll(legacyKey);
if (values.length) additions[field] = values;
}
if (!Object.keys(additions).length) return;
const search = {
...to.location.search,
[namespace]: { ...current, ...additions },
};
const query = new URLSearchParams();
// Middleware redirects describe one entry; the router assigns its pane index.
replacePaneSearchParams(query, [search]);
return redirect(`${path}?${query}`);
}
export function createAppSplitRouterMiddleware(
state: AppMiddlewareState
): readonly SplitRouterMiddleware[] {
return [
// First: a cmd+click becomes a tab with the proposed entry as-is; legacy
// redirects still run when that tab is activated.
openInNewTabMiddleware,
redirectRetiredCrmDatabases,
(context) => redirectLegacyRoutes(context, state),
redirectLegacyCrmPipeline,
migrateLegacySearch,
// Last, so it records the entry that remains after every redirect.
recordNavigationBreadcrumb,
];
}