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 { useFeatureFlag } from '@app/lib/analytics/posthog';
import { URL_PARAMS as CHANNEL_PARAMS } from '@block-channel/constants';
import {
EntityIcon as CoreEntityIcon,
type EntityIconProps,
} from '@core/component/EntityIcon';
import { UserIcon } from '@core/component/UserIcon';
import { fileTypeToBlockName } from '@core/constant/allBlocks';
import { enableProjects } from '@core/constant/featureFlags';
import { useChannelName } from '@core/context/channels';
import { useUserId } from '@core/context/user';
import ProjectIcon from '@icon/system/regular/cube.svg';
import { isAccessiblePreviewItem, useItemPreview } from '@queries/preview';
import type { EntityType } from '@service-properties/generated/schemas/entityType';
import { createLazyMemo } from '@solid-primitives/memo';
import { type Accessor, createMemo, type JSX, untrack } from 'solid-js';
import { match } from 'ts-pattern';
import { useProjectIdentityQuery } from '../../projects/queries/project-identity';
import { entityTypeToItemType } from '../utils';
import { usePropertyUserDisplay } from './usePropertyUserDisplay';
const PREVIEWABLE_ENTITY_TYPES: EntityType[] = [
'DOCUMENT',
'TASK',
'PROJECT',
'CHAT',
'CHANNEL',
'THREAD',
'COMPANY',
'CONTACT',
] as const;
type PreviewableEntityType = (typeof PREVIEWABLE_ENTITY_TYPES)[number];
const isPreviewable = (
type: EntityType | undefined
): type is PreviewableEntityType => {
if (!type) return false;
return PREVIEWABLE_ENTITY_TYPES.includes(type);
};
type PropertyEntityDisplayResult = {
/** Resolved display name for the entity */
name: Accessor<string>;
/** Native project identity, available only after an authorized read. */
nativeProjectId: Accessor<string | undefined>;
/** Icon JSX element for the entity */
icon: Accessor<JSX.Element>;
/** Whether preview data is still loading */
isLoading: Accessor<boolean>;
/** Whether the current preview confirms access to the referenced entity. */
hasPreviewAccess: Accessor<boolean>;
/** Block or file type for linking (null if not linkable) */
blockOrFileType: Accessor<string | null>;
/** URL params for navigation (e.g., message ID for threads) */
linkParams: Accessor<Record<string, string> | undefined>;
};
/**
* Hook to resolve entity display information (name, icon) for property entity values.
* Handles preview fetching, channel name resolution, and user display names.
*
* @param entityIdSource - The entity's unique identifier
* @param entityTypeSource - The type of entity (USER, CHANNEL, DOCUMENT, etc.)
* @param options - Optional configuration
* @returns Object with name, icon, isLoading, and blockOrFileType accessors
*/
export function usePropertyEntityDisplay(
entityIdSource: Accessor<string>,
entityTypeSource: Accessor<EntityType | undefined>,
options?: {
/** Custom fallback icon for unknown entity types (null to show nothing) */
fallbackIcon?: JSX.Element | null;
/** Size passed to the entity icon renderer; defaults to xs. */
iconSize?: EntityIconProps['size'];
/** Specific message ID for THREAD/CHANNEL/CHAT entities */
specificMessageId?: Accessor<string | null | undefined>;
/** Name to show while the entity's own read loads, such as a picker's label. */
loadingName?: Accessor<string | undefined>;
}
): PropertyEntityDisplayResult {
// Callers usually read these from a property value, which every properties
// emission replaces. Only a real change may rebuild or re-run the reads below.
const entityId = createMemo(entityIdSource);
const entityType = createMemo(entityTypeSource);
const projectsFlag = createMemo(() =>
entityType() === 'INITIATIVE' ? useFeatureFlag(enableProjects) : undefined
);
// Until Projects is enabled, a project stays the generic "Project" label.
const projectSource = createMemo(() => {
if (!projectsFlag()?.().enabled) return;
return untrack(() => {
const userId = useUserId();
return useProjectIdentityQuery(entityId, userId);
});
});
const nativeProjectId = () => {
const source = projectSource();
return source && !source.isError && !source.isPending
? source.data?.id
: undefined;
};
const previewSource = createMemo(() => {
const eType = entityType();
if (isPreviewable(eType)) {
const pType = entityTypeToItemType(eType);
return untrack(
() =>
useItemPreview(() => ({
id: entityId(),
type: pType,
}))[0]
);
}
});
// Keep subscription ownership separate from its value: a live result must
// not dispose and reacquire the preview that produced it.
const preview = () => previewSource()?.();
const hasPreviewAccess = () => {
const item = preview();
return !!item && isAccessiblePreviewItem(item);
};
const channelNameSource = createMemo(() => {
const eType = entityType();
if (eType === 'CHANNEL') {
return useChannelName(entityId());
}
});
const channelName = () => channelNameSource()?.();
const user = usePropertyUserDisplay(() =>
entityType() === 'USER' ? entityId() : ''
);
const isLoading = createMemo(() => {
if (entityType() === 'INITIATIVE')
return projectSource()?.isPending ?? false;
if (!isPreviewable(entityType())) return false;
const previewItem = preview();
return !previewItem || previewItem.loading;
});
const loadingName = () => options?.loadingName?.() || 'Loading...';
const name = createMemo(() =>
match(entityType())
.with('INITIATIVE', () => {
const source = projectSource();
if (!source) return 'Project';
if (source.isPending) return loadingName();
return (!source.isError && source.data?.name) || 'Project unavailable';
})
.with('USER', () => user.name())
.with('CHANNEL', () => channelName() || 'Channel')
.otherwise(() => {
const item = preview();
if (!item || item.loading) return loadingName();
if (isAccessiblePreviewItem(item)) {
return item.name;
}
return `Unknown ${entityType()?.toLowerCase() ?? 'entity'}`;
})
);
// Built on first read: a caller that draws its own icon pays nothing.
const icon = createLazyMemo(() =>
match(entityType())
.when(
(type) => type === 'INITIATIVE' && projectSource(),
() => <ProjectIcon class="size-4" />
)
.with('USER', () => (
<UserIcon id={entityId()} size="sm" photoUrl={user.photoUrl()} />
))
.with('CHANNEL', () => (
<CoreEntityIcon targetType="channel" size={options?.iconSize ?? 'xs'} />
))
.with('TASK', () => (
<CoreEntityIcon targetType="task" size={options?.iconSize ?? 'xs'} />
))
.with('DOCUMENT', () => {
const item = preview();
if (!item || !isAccessiblePreviewItem(item)) {
return (
<CoreEntityIcon
targetType="unknown"
size={options?.iconSize ?? 'xs'}
/>
);
}
const { subType, fileType } = item;
const blockName = fileTypeToBlockName(subType?.type ?? fileType, true);
return (
<CoreEntityIcon
targetType={blockName}
size={options?.iconSize ?? 'xs'}
/>
);
})
.with('PROJECT', () => (
<CoreEntityIcon targetType="project" size={options?.iconSize ?? 'xs'} />
))
.with('CHAT', () => (
<CoreEntityIcon targetType="chat" size={options?.iconSize ?? 'xs'} />
))
.with('COMPANY', () => (
<CoreEntityIcon
targetType="crm_company"
size={options?.iconSize ?? 'xs'}
/>
))
.with('CONTACT', () => (
<CoreEntityIcon targetType="contact" size={options?.iconSize ?? 'xs'} />
))
.with('THREAD', () => (
<CoreEntityIcon targetType="email" size={options?.iconSize ?? 'xs'} />
))
.otherwise(() => {
if (options && 'fallbackIcon' in options) {
return options.fallbackIcon;
}
return (
<CoreEntityIcon
targetType="unknown"
size={options?.iconSize ?? 'xs'}
/>
);
})
);
const blockOrFileType = createMemo(() =>
match(entityType())
.with('CHANNEL', () => 'channel')
.with('CHAT', () => 'chat')
.with('PROJECT', () => 'project')
.with('TASK', () => 'task')
.with('THREAD', () => 'email')
.with('COMPANY', () => 'company')
.with('CONTACT', () => 'contact')
.with('DOCUMENT', () => {
const item = preview();
if (!item || !isAccessiblePreviewItem(item)) {
return null;
}
if (item.subType?.type === 'task') {
return 'task';
}
return item.fileType || null;
})
.otherwise(() => null)
);
const linkParams = createMemo((): Record<string, string> | undefined => {
const messageId = options?.specificMessageId?.();
if (!messageId) return undefined;
return match(entityType())
.with('THREAD', () => ({ email_message_id: messageId }))
.with('CHANNEL', () => ({ [CHANNEL_PARAMS.message]: messageId }))
.with('CHAT', () => ({ message_id: messageId }))
.otherwise(() => undefined);
});
return {
name,
icon,
isLoading,
hasPreviewAccess,
blockOrFileType,
nativeProjectId,
linkParams,
};
}