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.

10.7 KB
Raw Blame
import type { DatabaseOp } from '@core/database-sql/generated/types';
import { throwOnErr } from '@core/util/result';
import { Telemetry } from '@macro-inc/observability';
import type { CardPosition } from '@service-storage/generated/schemas/cardPosition';
import type { DatabaseView } from '@service-storage/generated/schemas/databaseView';
import type { OpResult } from '@service-storage/generated/schemas/opResult';
import type { TableDetail } from '@service-storage/generated/schemas/tableDetail';
import { queryOptions, useQuery } from '@tanstack/solid-query';
import { errAsync, okAsync, ResultAsync } from 'neverthrow';
import type { Accessor } from 'solid-js';
import { match } from 'ts-pattern';
import { v7 as uuidv7 } from 'uuid';
import {
  boardWithStatusColumn,
  newBoardWithStatusColumn,
} from '../../database/core/board-grouping';
import { type CardMove, withPositions } from '../../database/core/board-moves';
import type { DatabaseViewColumn } from '../../database/core/database-view';
import { createKeyedSerializer } from '../../database/core/keyed-serializer';
import {
  newTimelineWithTimeRangeColumn,
  timelineWithTimeRangeColumn,
} from '../../database/core/timeline-range';
import type { NewView } from '../../database/core/view-creation';
import type { CardMoved, ViewChange } from '../../database/core/view-state';
import { boardLayout, timelineLayout } from '../../database/core/views';
import type { DatabaseOpFailure } from '../../database/core/write-failure';
import type { Pipeline } from '../core/pipeline';
import type { CrmRecordDependencies } from './dependencies';
import { crmKeys } from './keys';

type ViewOpResult = Extract<OpResult, { kind: 'view' }>;
type PositionsStorage = Pick<
  CrmRecordDependencies['storage'],
  'getCrmPipelineViewPositions'
>;

function pipelinePositionsOptions(
  storage: PositionsStorage,
  pipelineId: string,
  viewId: string | undefined
) {
  return {
    queryKey: crmKeys.pipelinePositions(pipelineId, viewId).queryKey,
    enabled: !!viewId,
    queryFn: () =>
      viewId
        ? Telemetry.span('crm.board.positions', async (span) => {
            span.setAttr('crm.pipeline_id', pipelineId);
            span.setAttr('database.view_id', viewId);
            return throwOnErr(() =>
              storage.getCrmPipelineViewPositions(pipelineId, viewId)
            );
          })
        : Promise.resolve([]),
  };
}

/** Saved ordering is scoped to the pipeline and the selected board. */
export function usePipelinePositionsQuery(
  deps: {
    client: CrmRecordDependencies['client'];
    storage: PositionsStorage;
  },
  pipelineId: string,
  viewId: Accessor<string | undefined>
) {
  return useQuery(
    () => pipelinePositionsOptions(deps.storage, pipelineId, viewId()),
    () => deps.client
  );
}

function pipelineTableQueryOptions(
  deps: CrmRecordDependencies,
  pipelineId: string
) {
  return queryOptions({
    queryKey: crmKeys.pipelineTable(pipelineId).queryKey,
    queryFn: () =>
      throwOnErr(() => deps.storage.getCrmPipelineTable(pipelineId)),
  });
}

/** The pipeline's single table, whose stored views the toolbar lists. */
export function usePipelineTableQuery(
  deps: CrmRecordDependencies,
  pipeline: Pipeline
) {
  return useQuery(
    () => pipelineTableQueryOptions(deps, pipeline.id),
    () => deps.client
  );
}

/**
 * Stored views of a pipeline's table, written as database view ops through the
 * pipeline's own endpoint. Its storage database is not the viewer's to address.
 */
export function createPipelineViews(
  deps: {
    client: CrmRecordDependencies['client'];
    storage: Pick<CrmRecordDependencies['storage'], 'applyCrmPipelineOps'>;
  },
  pipeline: Pipeline
) {
  const key = crmKeys.pipelineTable(pipeline.id).queryKey;
  // Each view's writes, and the view list's, reach the server in order.
  const writes = createKeyedSerializer();
  const inOrder = <Value>(
    order: string,
    write: () => ResultAsync<Value, DatabaseOpFailure>
  ) => new ResultAsync(writes.run(order, async () => await write()));
  const patchViews = (change: (views: DatabaseView[]) => DatabaseView[]) =>
    deps.client.setQueryData<TableDetail>(
      key,
      (detail) => detail && { ...detail, views: change(detail.views) }
    );
  const upsert = (view: DatabaseView) =>
    patchViews((views) =>
      views.some((existing) => existing.id === view.id)
        ? views.map((existing) => (existing.id === view.id ? view : existing))
        : [...views, view]
    );
  const apply = (ops: DatabaseOp[]) =>
    deps.storage
      .applyCrmPipelineOps(pipeline.id, { ops })
      .map((response) => response.results)
      .mapErr(([error]): DatabaseOpFailure => ({ kind: 'ops', error }))
      .orElse((failure) => {
        // A refusal shows what is stored instead of what was patched in.
        deps.client.invalidateQueries({ queryKey: key });
        return errAsync(failure);
      });
  const lastView = (results: OpResult[]) => {
    const result = results.at(-1) as ViewOpResult | undefined;
    return result?.kind === 'view' && 'view' in result.change
      ? okAsync(result.change.view)
      : errAsync<DatabaseView, DatabaseOpFailure>({
          kind: 'unexpected-result',
        });
  };
  const listKey = `table:${pipeline.tableId}`;
  const status = () => ({
    column: uuidv7(),
    options: [uuidv7(), uuidv7(), uuidv7()] as [string, string, string],
  });

  /** A batch adding a column and then changing `view`. */
  const withColumn = (view: DatabaseView, ops: DatabaseOp[]) =>
    inOrder(view.id, () =>
      apply(ops)
        .andThen(lastView)
        .map((stored) => {
          upsert(stored);
          // The new column arrives with the table read.
          deps.client.invalidateQueries({ queryKey: key });
        })
    );

  return {
    /**
     * Add a view that starts from `current`; a board without a grouping
     * column adds a Status column, and a timeline without a time range a Time range column.
     */
    create(
      current: DatabaseView,
      input: NewView,
      columns: readonly DatabaseViewColumn[]
    ) {
      const viewId = uuidv7();
      const created = (layout: DatabaseView['layout']): DatabaseOp[] => [
        {
          kind: 'view',
          table: pipeline.tableId,
          view: viewId,
          change: {
            kind: 'create',
            view: { name: input.name, query: current.query, layout },
          },
        },
      ];
      const creation = {
        tableId: pipeline.tableId,
        viewId,
        name: input.name,
        query: current.query,
        columns,
      };
      const ops = match(input)
        .with({ layout: 'table' }, () =>
          created(
            current.layout.kind === 'table'
              ? current.layout
              : { kind: 'table', columns: [] }
          )
        )
        .with({ layout: 'board', groupBy: { kind: 'column' } }, ({ groupBy }) =>
          created(boardLayout(groupBy.columnId, columns))
        )
        .with({ layout: 'board', groupBy: { kind: 'new-status' } }, () =>
          newBoardWithStatusColumn({ ...creation, status: status() })
        )
        .with({ layout: 'timeline', range: { kind: 'column' } }, ({ range }) =>
          created(timelineLayout(range.columnId, columns))
        )
        .with({ layout: 'timeline', range: { kind: 'new-column' } }, () =>
          newTimelineWithTimeRangeColumn({ ...creation, column: uuidv7() })
        )
        .exhaustive(() => created({ kind: 'table', columns: [] }));
      return inOrder(listKey, () => apply(ops).andThen(lastView)).map(
        (view) => {
          upsert(view);
          return view;
        }
      );
    },
    update(view: DatabaseView, change: ViewChange) {
      patchViews((views) =>
        views.map((existing) =>
          existing.id === view.id ? { ...existing, ...change } : existing
        )
      );
      return inOrder(view.id, () =>
        apply([
          {
            kind: 'view',
            table: view.tableId,
            view: view.id,
            change: { kind: 'update', ...change },
          },
        ]).andThen(lastView)
      ).map(upsert);
    },
    /** Lay a view out as a board grouped by a new Status column, in one batch. */
    showAsBoardWithStatusColumn(
      view: DatabaseView,
      columns: readonly DatabaseViewColumn[]
    ) {
      return withColumn(
        view,
        boardWithStatusColumn({ view, columns, status: status() })
      );
    },
    /** Lay a view out as a timeline along a new Time range column, in one batch. */
    showAsTimelineWithTimeRangeColumn(
      view: DatabaseView,
      columns: readonly DatabaseViewColumn[]
    ) {
      return withColumn(
        view,
        timelineWithTimeRangeColumn({ view, columns, column: uuidv7() })
      );
    },
    remove(view: DatabaseView) {
      patchViews((views) =>
        views.filter((existing) => existing.id !== view.id)
      );
      return inOrder(view.id, () =>
        apply([
          {
            kind: 'view',
            table: view.tableId,
            view: view.id,
            change: { kind: 'delete' },
          },
        ])
      ).map(() => undefined);
    },
    /** Move a stored board's card, which also sets its row's grouping cell. */
    moveCard(
      view: DatabaseView,
      move: CardMove
    ): ResultAsync<CardMoved, DatabaseOpFailure> {
      const positionsKey = crmKeys.pipelinePositions(
        pipeline.id,
        view.id
      ).queryKey;
      return inOrder(view.id, () =>
        ResultAsync.fromSafePromise(
          deps.client.cancelQueries({ queryKey: positionsKey })
        ).andThen(() =>
          apply([
            {
              kind: 'view',
              table: view.tableId,
              view: view.id,
              change: {
                kind: 'move_card',
                row: move.row,
                lane: move.lane,
                before: move.before,
                after: move.after,
              },
            },
          ])
        )
      ).andThen((results) => {
        const result = results.at(-1) as ViewOpResult | undefined;
        if (
          result?.kind !== 'view' ||
          result.change.kind !== 'card_moved' ||
          result.tableVersion == null
        )
          return errAsync<CardMoved, DatabaseOpFailure>({
            kind: 'unexpected-result',
          });
        const moved = {
          positions: result.change.positions,
          tableVersion: result.tableVersion,
        };
        // A focus refetch may have started while the write was in flight.
        return ResultAsync.fromSafePromise(
          deps.client.cancelQueries({ queryKey: positionsKey })
        ).map(() => {
          deps.client.setQueryData<CardPosition[]>(positionsKey, (current) =>
            withPositions(current ?? [], moved.positions)
          );
          return moved;
        });
      });
    },
  };
}

export type PipelineViews = ReturnType<typeof createPipelineViews>;