Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
24.5 KB
Raw Blame
"use client";

import { useState } from "react";
import {
  Check,
  ChevronRight,
  ExternalLink,
  Loader2,
  Plus,
  RotateCw,
  Search,
  Trash2,
} from "@multica/ui/icons";
import { useQueryClient } from "@tanstack/react-query";
import { toast } from "sonner";
import type { Agent, SkillSummary } from "@multica/core/types";
import { api } from "@multica/core/api";
import { workspaceKeys } from "@multica/core/workspace/queries";
import { useWorkspacePaths } from "@multica/core/paths";
import { resolvePublicFileUrl } from "@multica/core/workspace/avatar-url";
import { Button } from "@multica/ui/components/ui/button";
import { Checkbox } from "@multica/ui/components/ui/checkbox";
import { Input } from "@multica/ui/components/ui/input";
import {
  Collapsible,
  CollapsibleContent,
  CollapsibleTrigger,
} from "@multica/ui/components/ui/collapsible";
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from "@multica/ui/components/ui/dialog";
import {
  DropdownMenuItem,
  DropdownMenuSeparator,
} from "@multica/ui/components/ui/dropdown-menu";
import {
  Tooltip,
  TooltipContent,
  TooltipTrigger,
} from "@multica/ui/components/ui/tooltip";
import { ActorAvatar } from "@multica/ui/components/common/actor-avatar";
import { cn } from "@multica/ui/lib/utils";
import { useT } from "../../i18n";
import { useIntentNavigate } from "../../navigation";
import {
  CollectionBatchBar,
  CollectionRowMenu,
} from "../../layout/collection-list";
import { isRefreshableOrigin, readOrigin } from "../lib/origin";
import { RefreshSkillDialog } from "./refresh-skill-dialog";
import type { SkillRow } from "./skill-list-filter";

// Shared context the row kebab and the batch toolbar both need. Assembled
// once at the page level.
export interface SkillActionsContext {
  wsId: string;
  agents: Agent[];
  currentUserId: string | null;
  /** Workspace owner/admin — may manage every agent, not only their own. */
  isAdmin: boolean;
}

// Add-to-agent dialog (multi-select; shared by row kebab and batch toolbar)

// Attaching is permission-gated by the TARGET agent (its owner, or a
// workspace owner/admin — server/internal/handler/agent.go canManageAgent).
// Members therefore only see their own agents; admins additionally see
// everyone else's. Agents that already have every selected skill render
// checked-out and disabled; partial overlap stays selectable because the
// endpoint is additive ("ensure these skills") and idempotent.
function partitionAgents(ctx: SkillActionsContext) {
  const active = ctx.agents.filter((a) => !a.archived_at);
  const mine = active.filter(
    (a) => a.owner_id !== null && a.owner_id === ctx.currentUserId,
  );
  const others = ctx.isAdmin
    ? active.filter(
        (a) => a.owner_id === null || a.owner_id !== ctx.currentUserId,
      )
    : [];
  return { mine, others };
}

function AgentPickerRow({
  agent,
  skillIds,
  selected,
  onToggle,
}: {
  agent: Agent;
  skillIds: string[];
  selected: boolean;
  onToggle: (agent: Agent) => void;
}) {
  const { t } = useT("skills");
  const owned = skillIds.filter((id) =>
    agent.skills.some((s) => s.id === id),
  ).length;
  const hasAll = owned === skillIds.length;
  return (
    <button
      type="button"
      disabled={hasAll}
      onClick={() => onToggle(agent)}
      aria-pressed={selected}
      className={cn(
        "flex w-full items-center gap-2.5 rounded-md px-2.5 py-2 text-left transition-colors",
        hasAll
          ? "opacity-50"
          : selected
            ? "bg-accent"
            : "hover:bg-accent/50",
      )}
    >
      {/* Indicator only — the wrapping <button> handles clicks. */}
      <Checkbox
        checked={hasAll || selected}
        tabIndex={-1}
        className="pointer-events-none"
      />
      <ActorAvatar
        name={agent.name}
        initials={agent.name.slice(0, 2).toUpperCase()}
        avatarUrl={resolvePublicFileUrl(agent.avatar_url)}
        isAgent
        size="md"
      />
      <span className="min-w-0 flex-1 truncate text-body">{agent.name}</span>
      {hasAll ? (
        <Check className="size-3.5 shrink-0 text-muted-foreground" />
      ) : owned > 0 ? (
        <span className="shrink-0 text-caption text-muted-foreground">
          {t(($) => $.actions.has_partial, { owned, total: skillIds.length })}
        </span>
      ) : null}
    </button>
  );
}

// Shows the first few skill names as chips so the dialog says WHAT is being
// added; the overflow collapses into a "+N" badge with the remaining names
// in a tooltip. Number-agnostic, so single-row and batch share the layout.
const MAX_SKILL_CHIPS = 3;
const MAX_TOOLTIP_NAMES = 10;

function SkillChips({ skills }: { skills: SkillSummary[] }) {
  const visible = skills.slice(0, MAX_SKILL_CHIPS);
  const overflow = skills.slice(MAX_SKILL_CHIPS);
  const chipClass =
    "max-w-[10rem] truncate rounded-xs bg-muted px-1.5 py-0.5 text-caption text-muted-foreground";
  return (
    <div className="flex flex-wrap items-center gap-1">
      {visible.map((s) => (
        <span key={s.id} className={chipClass}>
          {s.name}
        </span>
      ))}
      {overflow.length > 0 && (
        <Tooltip>
          <TooltipTrigger
            render={
              <span className={cn(chipClass, "font-medium")}>
                +{overflow.length}
              </span>
            }
          />
          <TooltipContent side="bottom" className="max-w-64">
            {overflow
              .slice(0, MAX_TOOLTIP_NAMES)
              .map((s) => s.name)
              .join(", ")}
            {overflow.length > MAX_TOOLTIP_NAMES ? "…" : ""}
          </TooltipContent>
        </Tooltip>
      )}
    </div>
  );
}

// Collapsible agent group; "my agents" opens by default, other people's
// agents start collapsed.
function AgentGroup({
  label,
  agents,
  defaultOpen,
  skillIds,
  selectedIds,
  onToggle,
}: {
  label: string;
  agents: Agent[];
  defaultOpen: boolean;
  skillIds: string[];
  selectedIds: ReadonlySet<string>;
  onToggle: (agent: Agent) => void;
}) {
  if (agents.length === 0) return null;
  return (
    <Collapsible defaultOpen={defaultOpen}>
      <CollapsibleTrigger className="group/trigger flex w-full items-center gap-1 rounded-md px-2.5 py-1.5 text-caption font-medium text-muted-foreground transition-colors hover:bg-accent/50">
        <ChevronRight className="size-3 stroke-[2.5] transition-transform duration-200 group-data-[panel-open]/trigger:rotate-90" />
        <span>{label}</span>
        <span className="text-muted-foreground">{agents.length}</span>
      </CollapsibleTrigger>
      <CollapsibleContent className="space-y-0.5">
        {agents.map((agent) => (
          <AgentPickerRow
            key={agent.id}
            agent={agent}
            skillIds={skillIds}
            selected={selectedIds.has(agent.id)}
            onToggle={onToggle}
          />
        ))}
      </CollapsibleContent>
    </Collapsible>
  );
}

export function AddToAgentDialog({
  skills,
  ctx,
  open,
  onOpenChange,
}: {
  skills: SkillSummary[];
  ctx: SkillActionsContext;
  open: boolean;
  onOpenChange: (open: boolean) => void;
}) {
  const { t } = useT("skills");
  const qc = useQueryClient();
  const [selectedIds, setSelectedIds] = useState<ReadonlySet<string>>(
    new Set(),
  );
  const [saving, setSaving] = useState(false);
  const [query, setQuery] = useState("");

  const skillIds = skills.map((s) => s.id);
  const { mine, others } = partitionAgents(ctx);
  const count = selectedIds.size;

  // Search across both groups by agent name. Filtering is applied per group
  // so the "My agents" / "Other agents" split is preserved; when a query is
  // active both groups are force-expanded (via the remount key below) so
  // matches in the normally-collapsed "Other agents" group stay visible.
  const trimmedQuery = query.trim().toLowerCase();
  const searching = trimmedQuery.length > 0;
  const matchesQuery = (a: Agent) =>
    !searching || a.name.toLowerCase().includes(trimmedQuery);
  const filteredMine = mine.filter(matchesQuery);
  const filteredOthers = others.filter(matchesQuery);
  const hasAnyAgent = mine.length + others.length > 0;
  const hasMatch = filteredMine.length + filteredOthers.length > 0;

  const handleOpenChange = (v: boolean) => {
    if (saving) return;
    if (!v) {
      setSelectedIds(new Set());
      setQuery("");
    }
    onOpenChange(v);
  };

  const handleToggle = (agent: Agent) => {
    setSelectedIds((prev) => {
      const next = new Set(prev);
      if (next.has(agent.id)) next.delete(agent.id);
      else next.add(agent.id);
      return next;
    });
  };

  const handleConfirm = async () => {
    const targets = [...mine, ...others].filter((a) => selectedIds.has(a.id));
    if (targets.length === 0) return;
    setSaving(true);
    try {
      for (const agent of targets) {
        const missing = skillIds.filter(
          (id) => !agent.skills.some((s) => s.id === id),
        );
        if (missing.length > 0) {
          await api.addAgentSkills(agent.id, { skill_ids: missing });
        }
      }
      qc.invalidateQueries({ queryKey: workspaceKeys.agents(ctx.wsId) });
      toast.success(
        targets.length === 1 && targets[0]
          ? t(($) => $.actions.added_toast, { name: targets[0].name })
          : t(($) => $.actions.added_multi_toast, { count: targets.length }),
      );
      setSelectedIds(new Set());
      onOpenChange(false);
    } catch (e) {
      toast.error(
        e instanceof Error && e.message
          ? e.message
          : t(($) => $.actions.add_failed_toast),
      );
    } finally {
      setSaving(false);
    }
  };

  return (
    <Dialog open={open} onOpenChange={handleOpenChange}>
      {/* Fixed-height dialog: header/chips/footer stay put, only the agent
          list scrolls (flex-1 + min-h-0). One size up from the default
          confirm dialogs — this is a working picker, not a prompt. */}
      <DialogContent className="flex h-[32rem] max-h-[85%] max-w-lg flex-col">
        <DialogHeader>
          <DialogTitle className="text-body">
            {t(($) => $.actions.add_to_agent)}
          </DialogTitle>
        </DialogHeader>

        <SkillChips skills={skills} />

        {hasAnyAgent && (
          <div className="relative">
            <Search className="pointer-events-none absolute left-2.5 top-1/2 h-3.5 w-3.5 -translate-y-1/2 text-muted-foreground" />
            <Input
              value={query}
              onChange={(e) => setQuery(e.target.value)}
              placeholder={t(($) => $.actions.search_placeholder)}
              className="h-8 pl-7 text-caption"
            />
          </div>
        )}

        <div className="min-h-0 flex-1 overflow-y-auto rounded-lg border bg-card p-1.5">
          {!hasAnyAgent ? (
            <div className="py-6 text-center text-caption text-muted-foreground">
              {t(($) => $.actions.no_agents)}
            </div>
          ) : !hasMatch ? (
            <div className="py-6 text-center text-caption text-muted-foreground">
              {t(($) => $.actions.no_agents_match)}
            </div>
          ) : (
            <>
              <AgentGroup
                key={`mine-${searching}`}
                label={t(($) => $.actions.my_agents)}
                agents={filteredMine}
                defaultOpen
                skillIds={skillIds}
                selectedIds={selectedIds}
                onToggle={handleToggle}
              />
              <AgentGroup
                key={`others-${searching}`}
                label={t(($) => $.actions.other_agents)}
                agents={filteredOthers}
                defaultOpen={searching}
                skillIds={skillIds}
                selectedIds={selectedIds}
                onToggle={handleToggle}
              />
            </>
          )}
        </div>

        <DialogFooter>
          <Button
            type="button"
            variant="ghost"
            onClick={() => handleOpenChange(false)}
            disabled={saving}
          >
            {t(($) => $.actions.cancel)}
          </Button>
          <Button
            type="button"
            onClick={handleConfirm}
            disabled={saving || count === 0}
          >
            {saving ? (
              <>
                <Loader2 className="h-3 w-3 animate-spin" />
                {t(($) => $.actions.adding)}
              </>
            ) : (
              t(($) => $.actions.add_confirm, { num: count })
            )}
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  );
}

// Delete confirmation (single row and batch share one dialog)

export function DeleteSkillsDialog({
  rows,
  ctx,
  open,
  onOpenChange,
  onDeleted,
}: {
  rows: SkillRow[];
  ctx: SkillActionsContext;
  open: boolean;
  onOpenChange: (open: boolean) => void;
  onDeleted?: () => void;
}) {
  const { t } = useT("skills");
  const qc = useQueryClient();
  const [deleting, setDeleting] = useState(false);
  const single = rows.length === 1 ? rows[0] : null;
  const count = rows.length;

  const handleConfirm = async () => {
    setDeleting(true);
    try {
      for (const row of rows) {
        await api.deleteSkill(row.skill.id);
      }
      qc.invalidateQueries({ queryKey: workspaceKeys.skills(ctx.wsId) });
      qc.invalidateQueries({ queryKey: workspaceKeys.agents(ctx.wsId) });
      toast.success(t(($) => $.actions.deleted_toast, { count }));
      onOpenChange(false);
      onDeleted?.();
    } catch (e) {
      toast.error(
        e instanceof Error && e.message
          ? e.message
          : t(($) => $.actions.delete_failed_toast),
      );
    } finally {
      setDeleting(false);
    }
  };

  return (
    <Dialog
      open={open}
      onOpenChange={(v) => {
        if (!deleting) onOpenChange(v);
      }}
    >
      <DialogContent className="sm:max-w-md">
        <DialogHeader>
          <DialogTitle>
            {single
              ? t(($) => $.detail.delete_dialog.title)
              : t(($) => $.actions.delete_dialog_title, { count })}
          </DialogTitle>
          <DialogDescription>
            {single
              ? single.agents.length > 0
                ? t(($) => $.detail.delete_dialog.description_with_agents, {
                    name: single.skill.name,
                    count: single.agents.length,
                  })
                : t(($) => $.detail.delete_dialog.description_no_agents, {
                    name: single.skill.name,
                  })
              : t(($) => $.actions.delete_dialog_desc, { count })}
          </DialogDescription>
        </DialogHeader>
        <div className="rounded-md bg-destructive/10 px-3 py-2 text-caption text-destructive">
          {t(($) => $.detail.delete_dialog.warning)}
        </div>
        <DialogFooter>
          <Button
            type="button"
            variant="ghost"
            onClick={() => onOpenChange(false)}
            disabled={deleting}
          >
            {t(($) => $.detail.delete_dialog.cancel)}
          </Button>
          <Button
            type="button"
            variant="destructive"
            onClick={handleConfirm}
            disabled={deleting}
          >
            {deleting ? (
              <>
                <Loader2 className="h-3 w-3 animate-spin" />
                {t(($) => $.detail.delete_dialog.deleting)}
              </>
            ) : (
              <>
                <Trash2 className="h-3 w-3" />
                {t(($) => $.detail.delete_dialog.confirm)}
              </>
            )}
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  );
}

// Bulk update-from-source (batch toolbar)

/**
 * Confirm-and-run dialog for updating several imported skills from their
 * sources. Every updatable selection entry is re-downloaded — deliberately
 * no upstream freshness probe first (product call on #6930): re-fetching an
 * already-current skill costs one download on an explicit Update click,
 * which beats probing every source to find out it was current.
 *
 * Refreshes run sequentially with per-item failure tolerance (unlike bulk
 * delete's all-or-nothing loop): a single 409 from an upstream rename
 * collision must not strand the rest of the batch. A partial failure keeps
 * the selection (`onUpdated` only fires on a clean run) so retry is one
 * click away.
 */
export function UpdateSkillsDialog({
  rows,
  skippedCount,
  ctx,
  open,
  onOpenChange,
  onUpdated,
}: {
  /** The updatable subset of the selection (canEdit + refreshable origin). */
  rows: SkillRow[];
  /** Selection entries that are not updatable (manual, runtime, no permission). */
  skippedCount: number;
  ctx: SkillActionsContext;
  open: boolean;
  onOpenChange: (open: boolean) => void;
  /** Fired only after a fully successful run, so a partial failure keeps the selection for retry. */
  onUpdated?: () => void;
}) {
  const { t } = useT("skills");
  const qc = useQueryClient();
  const [updating, setUpdating] = useState(false);
  const [progress, setProgress] = useState(0);

  const handleConfirm = async () => {
    setUpdating(true);
    let updated = 0;
    let failed = 0;
    try {
      for (const row of rows) {
        setProgress(updated + failed + 1);
        try {
          await api.refreshSkill(row.skill.id);
          updated++;
        } catch {
          failed++;
        }
      }
      qc.invalidateQueries({ queryKey: workspaceKeys.skills(ctx.wsId) });
      qc.invalidateQueries({ queryKey: workspaceKeys.agents(ctx.wsId) });
      if (failed === 0) {
        toast.success(t(($) => $.actions.updated_toast, { count: updated }));
        onOpenChange(false);
        onUpdated?.();
      } else {
        toast.error(
          t(($) => $.actions.update_partial_toast, { count: updated, failed }),
        );
        onOpenChange(false);
      }
    } finally {
      setUpdating(false);
      setProgress(0);
    }
  };

  return (
    <Dialog
      open={open}
      onOpenChange={(v) => {
        if (!updating) onOpenChange(v);
      }}
    >
      <DialogContent className="sm:max-w-md">
        <DialogHeader>
          <DialogTitle>{t(($) => $.actions.update_dialog_title)}</DialogTitle>
        </DialogHeader>
        <ul className="space-y-1 text-caption text-muted-foreground">
          <li>
            {t(($) => $.actions.update_summary_selected, {
              count: rows.length,
            })}
          </li>
          {skippedCount > 0 && (
            <li>
              {t(($) => $.actions.update_summary_skipped, {
                count: skippedCount,
              })}
            </li>
          )}
        </ul>
        <div className="rounded-md bg-warning/10 px-3 py-2 text-caption text-muted-foreground">
          {t(($) => $.detail.refresh.dialog.warning)}
        </div>
        <DialogFooter>
          <Button
            type="button"
            variant="ghost"
            onClick={() => onOpenChange(false)}
            disabled={updating}
          >
            {t(($) => $.actions.cancel)}
          </Button>
          <Button
            type="button"
            onClick={handleConfirm}
            disabled={updating || rows.length === 0}
          >
            {updating ? (
              <>
                <Loader2 className="h-3 w-3 animate-spin" />
                {t(($) => $.actions.update_updating, {
                  done: progress,
                  total: rows.length,
                })}
              </>
            ) : (
              <>
                <RotateCw className="h-3 w-3" />
                {t(($) => $.actions.update_confirm, { count: rows.length })}
              </>
            )}
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  );
}

// Row kebab

export function SkillRowActions({
  row,
  ctx,
}: {
  row: SkillRow;
  ctx: SkillActionsContext;
}) {
  const { t } = useT("skills");
  const { t: tCommon } = useT("common");
  const paths = useWorkspacePaths();
  const intentNavigate = useIntentNavigate();
  const [addOpen, setAddOpen] = useState(false);
  const [refreshOpen, setRefreshOpen] = useState(false);
  const [deleteOpen, setDeleteOpen] = useState(false);

  const origin = readOrigin(row.skill);
  const canRefresh = row.canEdit && isRefreshableOrigin(origin);

  return (
    <>
      <CollectionRowMenu label={t(($) => $.actions.row_menu)} contentClassName="w-52">
        <DropdownMenuItem
          onClick={() =>
            intentNavigate(
              paths.skillDetail(row.skill.id),
              "foreground-tab",
              row.skill.name,
            )
          }
        >
          <ExternalLink className="size-3.5" />
          {tCommon(($) => $.navigation.open_in_new_tab)}
        </DropdownMenuItem>
        <DropdownMenuSeparator />
        <DropdownMenuItem onClick={() => setAddOpen(true)}>
          <Plus className="size-3.5" />
          {t(($) => $.actions.add_to_agent)}
        </DropdownMenuItem>
        {canRefresh && (
          <DropdownMenuItem onClick={() => setRefreshOpen(true)}>
            <RotateCw className="size-3.5" />
            {t(($) => $.actions.refresh)}
          </DropdownMenuItem>
        )}
        {row.canEdit && (
          <>
            <DropdownMenuSeparator />
            <DropdownMenuItem
              variant="destructive"
              onClick={() => setDeleteOpen(true)}
            >
              <Trash2 className="size-3.5" />
              {t(($) => $.actions.delete)}
            </DropdownMenuItem>
          </>
        )}
      </CollectionRowMenu>
      <AddToAgentDialog
        skills={[row.skill]}
        ctx={ctx}
        open={addOpen}
        onOpenChange={setAddOpen}
      />
      {canRefresh && (
        <RefreshSkillDialog
          skill={row.skill}
          origin={origin}
          wsId={ctx.wsId}
          open={refreshOpen}
          onOpenChange={setRefreshOpen}
        />
      )}
      <DeleteSkillsDialog
        rows={[row]}
        ctx={ctx}
        open={deleteOpen}
        onOpenChange={setDeleteOpen}
      />
    </>
  );
}

// Batch toolbar (floating bottom bar, same shape as the issues batch bar)

export function SkillBatchToolbar({
  rows,
  ctx,
  onClear,
}: {
  rows: SkillRow[];
  ctx: SkillActionsContext;
  onClear: () => void;
}) {
  const { t } = useT("skills");
  const [addOpen, setAddOpen] = useState(false);
  const [updateOpen, setUpdateOpen] = useState(false);
  const [deleteOpen, setDeleteOpen] = useState(false);

  const allDeletable = rows.every((r) => r.canEdit);
  const updatable = rows.filter(
    (r) => r.canEdit && isRefreshableOrigin(readOrigin(r.skill)),
  );

  const updateButton = (
    <Button
      variant="ghost"
      size="sm"
      disabled={updatable.length === 0}
      onClick={() => setUpdateOpen(true)}
      className={cn(updatable.length === 0 && "pointer-events-none")}
    >
      <RotateCw className="mr-1 size-3.5" />
      {t(($) => $.actions.update)}
    </Button>
  );

  const deleteButton = (
    <Button
      variant="ghost"
      size="sm"
      disabled={!allDeletable}
      onClick={() => setDeleteOpen(true)}
      className={cn(
        "text-destructive hover:text-destructive",
        !allDeletable && "pointer-events-none",
      )}
    >
      <Trash2 className="mr-1 size-3.5" />
      {t(($) => $.actions.delete)}
    </Button>
  );

  return (
    <>
      <CollectionBatchBar count={rows.length} onClear={onClear}>
        <Button variant="ghost" size="sm" onClick={() => setAddOpen(true)}>
          <Plus className="mr-1 size-3.5" />
          {t(($) => $.actions.add_to_agent)}
        </Button>

        {updatable.length > 0 ? (
          updateButton
        ) : (
          <Tooltip>
            <TooltipTrigger
              render={<span className="inline-flex">{updateButton}</span>}
            />
            <TooltipContent side="top">
              {t(($) => $.actions.update_none)}
            </TooltipContent>
          </Tooltip>
        )}

        {allDeletable ? (
          deleteButton
        ) : (
          <Tooltip>
            <TooltipTrigger
              render={<span className="inline-flex">{deleteButton}</span>}
            />
            <TooltipContent side="top">
              {t(($) => $.actions.delete_no_permission)}
            </TooltipContent>
          </Tooltip>
        )}
      </CollectionBatchBar>

      <AddToAgentDialog
        skills={rows.map((r) => r.skill)}
        ctx={ctx}
        open={addOpen}
        onOpenChange={setAddOpen}
      />
      <UpdateSkillsDialog
        rows={updatable}
        skippedCount={rows.length - updatable.length}
        ctx={ctx}
        open={updateOpen}
        onOpenChange={setUpdateOpen}
        onUpdated={onClear}
      />
      <DeleteSkillsDialog
        rows={rows}
        ctx={ctx}
        open={deleteOpen}
        onOpenChange={setDeleteOpen}
        onDeleted={onClear}
      />
    </>
  );
}