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 { listOwnedSlotName } from '@app/components/list';
import { openEntityInSplitFromUnifiedList } from '@app/features/next-soup/utils';
import {
type FacetSelection,
useSoupListNavigationHotkeys,
} from '@app/features/soup';
import { ViewTour } from '@app/features/tours/ViewTour';
import { useFeatureFlag } from '@app/lib/analytics/posthog';
import { usePending } from '@app/observability/flow';
import { useGlobalNotificationSource } from '@components/app/GlobalAppState';
import {
useSplitPanelOrThrow,
withSplitPanelOwner,
} from '@components/app/split-layout/layoutUtils';
import { toast } from '@core/component/Toast/Toast';
import { enableDatabases, enableForms } from '@core/constant/featureFlags';
import { useUserId } from '@core/context/user';
import { fireAndForget } from '@core/util/fireAndForget';
import { ListEntityMetadataQueryProvider } from '@entity';
import { markRowNotificationsSeenOnOpen } from '@notifications/mark-row-seen';
import { useTagSets, useTagSetsReady } from '@property/tags/tag-sets-context';
import {
createEffect,
createMemo,
on,
onCleanup,
onMount,
Suspense,
} from 'solid-js';
import { DriveProvider } from './context/drive-context';
import { driveLocationLabel } from './core/location-label';
import {
DriveDetailNavigationProvider,
useDriveDetailNavigation,
} from './drive-detail-navigation';
import { createDriveHostActions } from './drive-host-actions';
import {
createDriveList,
parseDriveListSnapshot,
} from './primitives/drive-list';
import {
createDriveRouteState,
createDriveViewState,
type DriveRouteState,
} from './primitives/drive-route-state';
import {
availableDriveState,
createDriveState,
} from './primitives/drive-state';
import { createDriveDataSource } from './queries/drive-data-source';
import { createDriveSidebarSource } from './queries/drive-sidebar-source';
import { documentsTour } from './tour';
import { DriveLoading, DriveWorkspace } from './views/drive-workspace';
import { SignedOutDriveWorkspace } from './views/signed-out-drive-workspace';
export type DriveViewProps = { initialFacets?: FacetSelection };
/** Constructs production sources and injects app capabilities into the workspace. */
function DriveComposition(props: { route: DriveRouteState }) {
const panel = useSplitPanelOrThrow();
const navigation = useDriveDetailNavigation();
const userId = useUserId();
const databasesFlag = useFeatureFlag(enableDatabases);
const formsFlag = useFeatureFlag(enableForms);
const notificationSource = useGlobalNotificationSource();
const tagSets = useTagSets();
const tagSetsReady = useTagSetsReady();
const view = createDriveViewState(props.route, () => list.reset());
const viewState = createMemo(() =>
availableDriveState(
view.value(),
databasesFlag().enabled,
formsFlag().enabled
)
);
const searchText = () => view.value().search;
const selection = () => ({ ...viewState(), search: searchText() });
const sidebar = createDriveSidebarSource({
databasesEnabled: () => databasesFlag().enabled,
formsEnabled: () => formsFlag().enabled,
});
const source = withSplitPanelOwner(listOwnedSlotName('data-source'), () =>
createDriveDataSource({
selection,
databasesEnabled: () => databasesFlag().enabled,
formsEnabled: () => formsFlag().enabled,
userId,
tagSets,
tagSetsReady,
notificationSource,
})
);
usePending('drive.items', source.isLoading);
const actions = createDriveHostActions({
projectId: () => state.projectId(),
selectFolder: (id) => state.selectFolder(id),
});
const list = withSplitPanelOwner(listOwnedSlotName('controller'), () =>
createDriveList({
source,
initial: parseDriveListSnapshot(
panel.handle.content().state?.['drive.listState']
),
onActivate: (row, metadata) =>
actions.openEntity(
row.entity,
metadata?.event,
undefined,
metadata?.newSplit
),
})
);
const state = createDriveState({
state: viewState,
setState: view.setValue,
folders: sidebar.folders,
list,
showList: () => {
navigation.clear();
},
});
createEffect(
on(
() => {
const location = view.value().location;
if (
sidebar.foldersLoading() ||
sidebar.foldersError() ||
location.kind !== 'folder' ||
!location.id
)
return;
return sidebar.folders().some((folder) => folder.id === location.id)
? undefined
: location.id;
},
(unavailableFolderId) => {
if (!unavailableFolderId) return;
view.update(
{
...view.value(),
location: { kind: 'folder', id: null },
scope: 'default',
search: '',
facets: {},
},
{ replace: true }
);
toast.alert('Folder unavailable', {
subtext: 'It may have moved, been deleted, or no longer be shared.',
});
}
)
);
onCleanup(
panel.handle.registerEntryStateCaptor('drive.returnLabel', () =>
driveLocationLabel(view.value().location, sidebar.folders())
)
);
onCleanup(
panel.handle.registerEntryStateCaptor('drive.listState', list.snapshot)
);
withSplitPanelOwner(listOwnedSlotName('navigation-hotkeys'), () => {
useSoupListNavigationHotkeys({
splitHotkeyScope: panel.splitHotkeyScope,
viewId: 'documents',
dataSource: source,
controller: list.controller,
handle: panel.handle,
openEntityInSplit: (entity, options) => {
markRowNotificationsSeenOnOpen(entity, notificationSource);
fireAndForget(
openEntityInSplitFromUnifiedList(entity, {
splitHandle: panel.handle,
...options,
}),
'open-entity-in-split'
);
},
});
});
onMount(() => panel.handle.setDisplayName('Drive'));
return (
<DriveProvider
value={{
state: { ...state, value: selection },
source,
list,
sidebar,
actions,
}}
>
<DriveWorkspace tour={<ViewTour tour={documentsTour} />} />
</DriveProvider>
);
}
/** Public details use the same viewer without constructing private Drive sources. */
export function SignedOutDriveView() {
const route = createDriveRouteState(() => undefined);
return (
<DriveDetailNavigationProvider location={route.location}>
<SignedOutDriveWorkspace />
</DriveDetailNavigationProvider>
);
}
export function DriveView(props: DriveViewProps) {
const route = createDriveRouteState(() => props.initialFacets);
return (
<DriveDetailNavigationProvider location={route.location}>
<ListEntityMetadataQueryProvider>
<Suspense fallback={<DriveLoading />}>
<DriveComposition route={route} />
</Suspense>
</ListEntityMetadataQueryProvider>
</DriveDetailNavigationProvider>
);
}