Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

4.5 KB
Raw Blame
import { Alert, Box, Chip, CircularProgress } from "@mui/material";
import type { GridColDef } from "@mui/x-data-grid";
import { useEffect, useMemo, useState } from "react";
import { useNavigate } from "react-router";
import { AccessChip } from "../../components/AccessControls";
import DataTable from "../../components/DataTable";
import type { TemplateSummary } from "../../data/api";
import { useTemplates } from "../../data/hooks";
import ResourceFiltersBar from "./ResourceFiltersBar";
import { formatBytes } from "./templateFormat";
import { formatDate } from "../../theme/hootFormat";

interface Props {
  refreshKey: number;
}

export default function TemplatesTab({ refreshKey }: Props) {
  const navigate = useNavigate();
  const [search, setSearch] = useState("");
  // The server searches name and description. The request waits until
  // typing has paused for a moment.
  const [searchSent, setSearchSent] = useState("");
  useEffect(() => {
    const timer = window.setTimeout(() => setSearchSent(search.trim()), 250);
    return () => window.clearTimeout(timer);
  }, [search]);
  const [ownerId, setOwnerId] = useState<string>("");
  const [kind, setKind] = useState<string>("");
  // Every filter is the server's. The whole catalogue is fetched once more,
  // unfiltered, so the owner and kind options do not shrink to what the
  // current search happens to match and hide the value that is selected.
  const { data: catalogue, refetch: refetchCatalogue } = useTemplates();
  const {
    data: templates,
    loading,
    error,
    refetch,
  } = useTemplates({ q: searchSent, kind, owner_id: ownerId });

  useEffect(() => {
    refetch();
    refetchCatalogue();
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [refreshKey]);

  const owners = useMemo(() => {
    const m = new Map<string, string>();
    for (const t of catalogue ?? []) {
      // A template that comes with Switch has no owner to filter by.
      if (t.owner_id !== null) m.set(t.owner_id, t.owner_name ?? t.owner_id);
    }
    return [...m.entries()].map(([id, name]) => ({ id, name }));
  }, [catalogue]);

  // Kinds are free text on the server, so the filter offers whatever is
  // actually in the registry rather than a list this page would have to grow.
  const kinds = useMemo(() => {
    const seen = new Set((catalogue ?? []).map((t) => t.kind));
    return [...seen].sort().map((k) => ({ value: k, label: k }));
  }, [catalogue]);

  const filtered = templates ?? [];

  const columns = useMemo<GridColDef<TemplateSummary>[]>(
    () => [
      { field: "name", headerName: "Name", flex: 1, minWidth: 180 },
      {
        field: "description",
        headerName: "Description",
        flex: 2,
        minWidth: 260,
      },
      {
        field: "kind",
        headerName: "Kind",
        width: 110,
        renderCell: ({ value }) => (
          <Chip size="small" variant="outlined" label={value as string} />
        ),
      },
      {
        field: "read_visibility",
        headerName: "Access",
        width: 110,
        renderCell: ({ row }) => <AccessChip pair={row} />,
      },
      {
        field: "owner_name",
        headerName: "Owner",
        width: 150,
        renderCell: ({ value, row }) => value ?? row.owner_id,
      },
      { field: "version", headerName: "Rev", width: 70, type: "number" },
      {
        field: "size_bytes",
        headerName: "Size",
        width: 90,
        valueFormatter: (value) => formatBytes(value as number),
      },
      {
        field: "updated_at",
        headerName: "Updated",
        width: 116,
        valueFormatter: (value) => formatDate(value as string),
      },
    ],
    [],
  );

  return (
    <Box
      sx={{ display: "flex", flexDirection: "column", flexGrow: 1, minHeight: 0 }}
    >
      <ResourceFiltersBar
        search={search}
        onSearchChange={setSearch}
        ownerId={ownerId}
        onOwnerChange={setOwnerId}
        owners={owners}
        typeFilter={kind}
        onTypeChange={setKind}
        types={kinds}
        typeLabel="Kind"
        searchPlaceholder="Search by name or description…"
      />

      {error && (
        <Alert severity="error" sx={{ mb: 2 }}>
          {error}
        </Alert>
      )}

      {loading ? (
        <CircularProgress />
      ) : (
        <DataTable
          rows={filtered}
          columns={columns}
          fillHeight
          onRowClick={(params) => navigate(`/resources/templates/${params.id}`)}
          sx={{ "& .MuiDataGrid-row": { cursor: "pointer" } }}
        />
      )}
    </Box>
  );
}