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
/**
 * Creating a new Photoshop document: 1920 × 1080 with a white Background,
 * as Photoshop's default "Web" preset is laid out.
 */

import { analytics } from '@app/lib/analytics';
import { PsdEngine } from '@core/psd-engine/client';
import { fireAndForget } from '@core/util/fireAndForget';
import { contentHash } from '@core/util/hash';
import { Telemetry } from '@macro-inc/observability';
import { invalidateUserQuota } from '@queries/auth';
import { postNewHistoryItem } from '@queries/history/history';
import { setPreviewOnCreate } from '@queries/preview/preview';
import { refetchSoupEntity } from '@queries/soup/cache';
import { storageServiceClient } from '@service-storage/client';
import { uploadToPresignedUrl } from '@service-storage/util/uploadToPresignedUrl';
import { PSD_MIME } from '../definition';

/** The size of a new document. */
export const NEW_DOCUMENT = { width: 1920, height: 1080 };

export async function createPsdDocument(args: {
  title?: string;
  projectId?: string;
  /** UI surface the creation originated from, for analytics. */
  source?: string;
}): Promise<string | undefined> {
  const title = args.title ?? 'Untitled image';
  const bytes = await PsdEngine.blank(
    NEW_DOCUMENT.width,
    NEW_DOCUMENT.height,
    true
  );
  const sha = await contentHash(bytes);
  const created = await storageServiceClient.createDocument({
    documentName: title,
    fileType: 'psd',
    sha,
    projectId: args.projectId,
  });
  fireAndForget(invalidateUserQuota(), 'create-psd-document');
  if (created.isErr()) return undefined;
  const doc = created.value;
  const upload = await uploadToPresignedUrl({
    presignedUrl: doc.presignedUrl,
    buffer: bytes,
    sha,
    type: PSD_MIME,
  });
  if (upload.isErr()) return undefined;
  const documentId = doc.metadata.documentId;
  fireAndForget(
    postNewHistoryItem('document', documentId),
    'create-psd-document'
  );
  setPreviewOnCreate({
    itemId: documentId,
    itemType: 'document',
    name: title,
    fileType: 'psd',
  });
  refetchSoupEntity(documentId, 'document', {
    ownTouch: true,
    refreshGraphql: true,
  }).catch((error) =>
    Telemetry.error(error, { surface: 'create-psd-document' })
  );
  analytics.track('create_entity', {
    entityType: 'psd',
    entityId: documentId,
    projectId: args.projectId,
    source: args.source,
  });
  return documentId;
}