Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

2.3 KB
Raw Blame
import type { BlockName } from '@core/block';
import { itemToResolvedBlockName } from '@core/constant/allBlocks';
import { getProfilePictureUrl } from '@core/signal/profilePicture';
import type { EntityType } from '@core/types';
import { getDisplayName, macroIdToEmail, tryMacroId } from '@core/user';
import { getItemPreview, isAccessiblePreviewItem } from '@queries/preview';
import { stringToItemType } from '@service-storage/client';
import { raceTimeout, until } from '@solid-primitives/promise';

export type UserNameResolver = (id: string) => Promise<string | undefined>;
export type UserAvatarResolver = (id: string) => Promise<string | undefined>;
export type DocumentNameResolver = (
  id: string,
  type: EntityType
) => Promise<string | undefined>;

export type NotificationBlockNameResolver = (
  entityId: string,
  entityType: EntityType
) => Promise<BlockName | undefined>;

const RESOLVER_TIMEOUT = 1000;

export const DefaultUserNameResolver: UserNameResolver = async (id: string) => {
  const macroId = tryMacroId(id);
  const resolvedName = await raceTimeout(
    until(() => getDisplayName(macroId)),
    RESOLVER_TIMEOUT
  );
  if (resolvedName) return resolvedName;
  return macroId ? macroIdToEmail(macroId) : id || undefined;
};

export const DefaultUserAvatarResolver: UserAvatarResolver = async (
  id: string
) => await raceTimeout(getProfilePictureUrl(id), RESOLVER_TIMEOUT);

const getPreview = async (id: string, type: string) => {
  const itemType = stringToItemType(type);
  if (!itemType) return undefined;
  return await raceTimeout(
    getItemPreview({ id, type: itemType }),
    RESOLVER_TIMEOUT
  );
};

export const DefaultDocumentNameResolver: DocumentNameResolver = async (
  id: string,
  type: string
) => {
  const preview = await getPreview(id, type);
  if (!preview || !isAccessiblePreviewItem(preview)) return undefined;
  return preview.name;
};

export const DefaultNotificationBlockNameResolver: NotificationBlockNameResolver =
  async (entityId: string, entityType: EntityType) => {
    const preview = await getPreview(entityId, entityType);
    if (!preview || !isAccessiblePreviewItem(preview)) return undefined;

    return itemToResolvedBlockName({
      type: preview.type,
      fileType: preview.fileType,
      name: preview.name,
    });
  };