All files / src/apps/user/pages/swarms ThreadRow.tsx

100% Statements 11/11
92.59% Branches 25/27
100% Functions 4/4
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107                                5x       4x 4x 4x 4x 4x 4x 4x                                                                             1x                               3x                                                  
import { formatUsd } from "@/components/cost";
import { SWARM_RUN_STATUS, StatusBadge } from "@/components/detail";
import type { SwarmThread } from "@/objects/Swarm";
import { swarmRunHref } from "@/routes/resourceHref";
import { parseServerDate, timeAgo } from "@/util/time";
import { ChevronDown, ChevronRight, MessagesSquare, PlayCircle } from "lucide-react";
import React, { useState } from "react";
import { Link } from "react-router-dom";
import { taskExcerpt } from "./swarmDisplay";
 
/**
 * A run and its follow-ups, as a list of runs shows them: one row. It reads
 * as its first request — what the thread is about — and opens its latest,
 * where the next follow-up is sent; its status is the latest's. A thread of
 * more than one request opens below to each of them, in order.
 */
const ThreadRow: React.FC<{ thread: SwarmThread; showSwarm?: boolean }> = ({
  thread,
  showSwarm = true,
}) => {
  const [open, setOpen] = useState(false);
  const runs = thread.runs.length ? thread.runs : [thread.latest];
  const first = runs[0];
  const latest = thread.latest;
  const followUps = Math.max(thread.requests - 1, 0);
  const Icon = followUps ? MessagesSquare : PlayCircle;
  return (
    <div
      role="group"
      aria-label={`Thread: ${taskExcerpt(first.task, 80)}`}
      className="border-b border-gray-100 dark:border-gray-700 last:border-0"
    >
      <div className="flex items-center gap-1 pr-2 hover:bg-gray-50 dark:hover:bg-gray-700/40 group">
        <Link
          to={swarmRunHref(latest.id)}
          className="flex min-w-0 flex-1 items-center gap-3 px-4 py-3"
        >
          <Icon className="size-4 text-gray-400 shrink-0" />
          <div className="flex-1 min-w-0">
            <div className="flex min-w-0 items-center gap-2">
              <span className="truncate text-sm text-gray-900 dark:text-gray-100">
                {taskExcerpt(first.task)}
              </span>
              {followUps > 0 && (
                <span className="shrink-0 rounded-full bg-gray-100 dark:bg-gray-700 px-2 py-0.5 text-[10px] font-medium text-gray-600 dark:text-gray-300">
                  {thread.requests} requests
                </span>
              )}
            </div>
            <div className="truncate text-xs text-gray-400 dark:text-gray-500">
              {showSwarm ? `${thread.swarm_name} · ` : ""}
              {timeAgo(parseServerDate(thread.updated_at))} ·{" "}
              {followUps > 0
                ? `${formatUsd(thread.spent_usd)} in all`
                : `${formatUsd(latest.spent_usd)} of ${formatUsd(latest.budget_usd)}`}
            </div>
          </div>
          <StatusBadge status={latest.status} scale={SWARM_RUN_STATUS} size="xs" />
          {followUps === 0 && (
            <ChevronRight className="size-3.5 text-gray-300 dark:text-gray-600 opacity-0 group-hover:opacity-100" />
          )}
        </Link>
        {followUps > 0 && (
          <button
            type="button"
            onClick={() => setOpen((v) => !v)}
            aria-expanded={open}
            aria-label={`${open ? "Hide" : "Show"} ${followUps} follow-up${followUps === 1 ? "" : "s"}`}
            title={open ? "Hide the requests" : "Show every request"}
            className="shrink-0 rounded-md p-1.5 text-gray-400 hover:bg-gray-100 hover:text-gray-700 dark:hover:bg-gray-700 dark:hover:text-gray-200"
          >
            <ChevronDown className={open ? "size-4 rotate-180 transition-transform" : "size-4 transition-transform"} />
          </button>
        )}
      </div>
      {open && (
        <ol
          aria-label="Requests in this thread"
          className="mb-3 ml-11 mr-4 space-y-0.5 border-l-2 border-gray-100 dark:border-gray-700 pl-3"
        >
          {runs.map((run) => (
            <li key={run.id}>
              <Link
                to={swarmRunHref(run.id)}
                className="flex items-center gap-2 rounded-md px-2 py-1.5 text-xs hover:bg-gray-50 dark:hover:bg-gray-700/40"
              >
                <span className="w-5 shrink-0 tabular-nums text-gray-400">
                  {run.thread_seq ?? 1}
                </span>
                <span className="min-w-0 flex-1 truncate text-gray-700 dark:text-gray-200">
                  {taskExcerpt(run.task, 90)}
                </span>
                <span className="shrink-0 text-gray-400 dark:text-gray-500">
                  {timeAgo(parseServerDate(run.created_at))} · {formatUsd(run.spent_usd)}
                </span>
                <StatusBadge status={run.status} scale={SWARM_RUN_STATUS} size="xs" />
              </Link>
            </li>
          ))}
        </ol>
      )}
    </div>
  );
};
 
export default ThreadRow;