ExportRows

tcmts.studio.ExportRows · #oj4hfbxt · term · runs in the browser

uses document

Download a table's rows as the page has them (searched, filtered, sorted): JSON or CSV (exportRows).

import { toasts } from "#codebase/tcmts/share";
import { exportRows } from "#codebase/tcmts/studio";
import { useState } from "react";
import { Button } from "react-aria-components";

export function ExportRows({
  table,
  q,
  where,
  sort,
  desc,
}: {
  table: string;
  q: string;
  where: Record<string, string>;
  sort: string;
  desc: boolean;
}) {
  const [busy, setBusy] = useState("");
  const get = async (format: "json" | "csv") => {
    setBusy(format);
    try {
      const text = await exportRows(table, q, where, sort, desc, format);
      const url = URL.createObjectURL(
        new Blob([text], { type: format === "json" ? "application/json" : "text/csv" }),
      );
      const a = document.createElement("a");
      a.href = url;
      a.download = `${table}.${format}`;
      a.click();
      setTimeout(() => URL.revokeObjectURL(url), 1000);
    } catch (e) {
      toasts.add({
        title: `Couldn't export ${table}`,
        description: (e as Error).message,
        tone: "bad",
      });
    }
    setBusy("");
  };
  return (
    <span className="seg">
      <Button className="btn small" isPending={busy === "json"} onPress={() => get("json")}>
        JSON
      </Button>
      <Button className="btn small" isPending={busy === "csv"} onPress={() => get("csv")}>
        CSV
      </Button>
    </span>
  );
}

uses

used by