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.7 KB
Raw Blame
/** Webhooks that insert rows into a database's tables. */

import { throwOnErr } from '@core/util/result';
import { queryClient } from '@queries/client';
import { storageServiceClient } from '@service-storage/client';
import type { CreatedDatabaseWebhookResponse } from '@service-storage/generated/schemas/createdDatabaseWebhookResponse';
import type { DatabaseWebhookResponse } from '@service-storage/generated/schemas/databaseWebhookResponse';
import { queryOptions, useQuery } from '@tanstack/solid-query';
import type { ResultAsync } from 'neverthrow';
import type { Accessor } from 'solid-js';
import { databaseWebhookKeys } from './keys';

function databaseWebhooksQueryOptions(databaseId: string) {
  return queryOptions({
    queryKey: databaseWebhookKeys.list(databaseId).queryKey,
    refetchOnMount: 'always',
    queryFn: () =>
      throwOnErr(() =>
        storageServiceClient.databases.listWebhooks({ id: databaseId })
      ),
  });
}

/** A table's webhooks; `undefined` until they are read. */
export function useTableWebhooks(
  databaseId: Accessor<string>,
  tableId: Accessor<string>
): {
  webhooks: Accessor<DatabaseWebhookResponse[] | undefined>;
  failed: Accessor<boolean>;
  ready: Accessor<boolean>;
} {
  const query = useQuery(() => databaseWebhooksQueryOptions(databaseId()));
  return {
    webhooks: () =>
      query.isSuccess
        ? query.data.webhooks.filter((webhook) => webhook.tableId === tableId())
        : undefined,
    failed: () => query.isError,
    ready: () =>
      query.isFetchedAfterMount &&
      query.isSuccess &&
      query.fetchStatus === 'idle',
  };
}

function updateList(
  databaseId: string,
  update: (webhooks: DatabaseWebhookResponse[]) => DatabaseWebhookResponse[]
) {
  queryClient.setQueryData<{ webhooks: DatabaseWebhookResponse[] }>(
    databaseWebhookKeys.list(databaseId).queryKey,
    (current) => current && { webhooks: update(current.webhooks) }
  );
}

/** Create a webhook for the table; the answer holds the token, shown only now. */
export function createTableWebhook(
  databaseId: string,
  tableId: string
): ResultAsync<CreatedDatabaseWebhookResponse, unknown> {
  return storageServiceClient.databases
    .createWebhook({ id: databaseId, tableId })
    .map((created) => {
      updateList(databaseId, (webhooks) => [...webhooks, created.webhook]);
      return created;
    });
}

/** Delete a webhook; its URL stops working. */
export function deleteTableWebhook(
  databaseId: string,
  webhookId: string
): ResultAsync<void, unknown> {
  return storageServiceClient.databases
    .deleteWebhook({ id: databaseId, webhookId })
    .map(() =>
      updateList(databaseId, (webhooks) =>
        webhooks.filter((webhook) => webhook.id !== webhookId)
      )
    );
}