Files
multica/packages/views/issues/components/batch-action-toolbar.tsx
Naiyuan Qing ade6b34e5f MUL-3903: Extract shared issue surfaces (#4774)
* MUL-3903 refactor project issue surface state

Co-authored-by: multica-agent <github@multica.ai>

* Refactor project issue surface ownership

Co-authored-by: multica-agent <github@multica.ai>

* Extract shared issue surface entrypoints

Co-authored-by: multica-agent <github@multica.ai>

* Fix issue surface create defaults and selection reset

Co-authored-by: multica-agent <github@multica.ai>

* test(editor): add missing AbortSignal to suggestion items() calls

The suggestion items() contract gained a required signal param; the
mention/slash test call sites were never updated, breaking pnpm typecheck
for @multica/views.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(issues): server-side assignee_types filter on ListIssues

ListGroupedIssues has taken assignee_types since squads shipped, but
ListIssues never did — so the workspace Members/Agents tabs had to fetch
the unfiltered workspace list and post-filter loaded pages client-side,
which made column totals and load-more pagination reflect the unfiltered
counts.

Add the same parse + WHERE clause to ListIssues (count query shares the
WHERE, so totals agree), thread the param through the TS client, and
widen MyIssuesFilter so scoped list caches can carry it.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* refactor(issues): route issue cache writes through a membership-aware coordinator

useUpdateIssue, useBatchUpdateIssues, and the WS issue:updated handler
each maintained their own similar-but-diverging patch/invalidate rules.
Consolidate them into cache-coordinator.ts (applyIssueChange /
rollbackIssueChange / invalidateIssueDerivatives) so local writes and
remote echoes follow one rules table by construction.

The coordinator is membership-aware via surface/membership.ts
(true | false | unknown against each list cache's own filter contract):

- a change that moves an issue off a filtered surface removes the card
  surgically (bucket total decremented) — fixes assignee changes leaving
  stale cards on My Assigned with no local safety net (previously only
  the WS echo recovered it), and replaces the blanket invalidate-myAll
  net for project moves (MUL-3669) with per-key precision
- possible entry into a loaded list marks that key stale — never
  hard-insert; page/slot is server knowledge
- stale keys flush on settle for mutations (a mid-flight refetch would
  stomp the optimistic state) and immediately for WS
- batch updates now patch detail + inbox like single updates; the
  off-screen bucket-count recovery previously exclusive to the WS path
  now covers local mutations too

Preserved invariants: synchronous optimistic patches (dnd-kit), MUL-3375
control-field stripping, and no refetch of surgically reconciled lists
(the drag-flicker fix).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* refactor(issues): resolve surfaces via core query plan/repository with window-keyed remount

Read-path convergence and the loading/empty semantics that fall out of
it:

- scope -> API params moves from scope.ts helpers into
  surface/query-plan.ts; workspace members/agents become server-filtered
  scoped plans (assignee_types) and the client postFilter machinery is
  deleted — tab counts and load-more are now exact
- query selection moves behind surface/repository.ts; the views data
  hook no longer branches on workspace-vs-scoped plumbing
- IssueSurfaceContent remounts on data-window change (wsId + scope):
  keepPreviousData placeholders keep sort/filter changes flicker-free
  within one window but must never let project A's (or workspace A's)
  cards impersonate B's with no loading state — cold window shows the
  skeleton, warm window hits cache instantly
- isEmpty is only asserted from full-window data; the gantt
  scheduled-only projection can't prove the window is empty, so GanttView's
  own "no scheduled issues" empty state renders instead of the generic
  create-issue one
- per-card project lookups hoist into a surface-level projectMap (drops
  a per-card useQuery), create-defaults typing tightens to
  IssueCreateDefaults

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* perf(issues): count-only arithmetic for off-window status/membership changes

An issue beyond a list's loaded page window used to force a full
first-page refetch just to fix two column counts. When the change is
CERTAIN (base entity known, membership definitive) the coordinator now
does the arithmetic locally:

- stayed a member + status changed: move one unit of total between the
  two buckets (loaded arrays untouched; hasMore stays consistent)
- left the list (reassigned / re-projected): old status bucket total -1
- member-to-member reassignment: counts unaffected, not even a stale key

Entering a list and any uncertainty (no base, unknown membership) still
refetch — the right page/slot is server knowledge. Branches on membership
OUTCOMES, not on which field changed, so future dimensions (team) join
automatically. Biggest win is the WS path: agents flipping off-screen
statuses no longer trigger refetch storms.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(issues): deferred view-refresh indicator during placeholder revalidation

Sort/date changes (and any grouped-board filter change) revalidate behind
the previous snapshot — correct, but on a slow network the click felt
dead: content stays put and isLoading never fires. Surface the state as
isRefreshing (isPlaceholderData of the active query) and render a shared
ViewRefreshIndicator in every issues header: a fixed-width slot (zero
layout shift) whose spinner fades in after 300ms, so sub-second responses
show nothing (NN/g) while slow ones get a working signal.

Bound to the revalidation STATE, not to any particular control — any
current or future server-side view change lights it automatically.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: multica-agent <github@multica.ai>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-02 14:11:10 +08:00

248 lines
9.0 KiB
TypeScript

"use client";
import { useMemo, useState } from "react";
import { X, Trash2 } from "lucide-react";
import { toast } from "sonner";
import { Button } from "@multica/ui/components/ui/button";
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from "@multica/ui/components/ui/alert-dialog";
import type { Issue, UpdateIssueRequest } from "@multica/core/types";
import { commonIssueFields } from "@multica/core/issues/batch";
import { useBatchUpdateIssues, useBatchDeleteIssues } from "@multica/core/issues/mutations";
import { useModalStore } from "@multica/core/modals";
import { StatusPicker, PriorityPicker, AssigneePicker } from "./pickers";
import { useT } from "../../i18n";
import { cn } from "@multica/ui/lib/utils";
import { useIssueSurfaceActionsOptional } from "../surface/actions-context";
import { useIssueSurfaceSelection } from "../surface/selection-context";
export function BatchActionToolbar({
issues,
placement = "fixed-bottom",
}: {
/**
* The universe of selectable issues at this call site (the same list the
* rows are rendered from). The toolbar filters it by the active surface
* selection to reflect the real common status / priority / assignee of the
* selected issues, mirroring how the skill list filters rows by `selectedIds`.
*/
issues: Issue[];
/**
* "fixed-bottom" — floats at the bottom of the viewport (default; used by
* full-screen issue lists).
* "inline" — renders in normal flow so callers can place it adjacent to
* the selected rows (used inside scrollable sections like sub-issues).
*/
placement?: "fixed-bottom" | "inline";
}) {
const { t } = useT("issues");
const selection = useIssueSurfaceSelection();
const selectedIds = selection.selectedIds;
const clear = selection.clear;
const count = selectedIds.size;
// Reflect the real shared value of the selected issues in each picker; fall
// back to an empty (no-checkmark) state when the selection is mixed, instead
// of asserting a hardcoded default.
const common = useMemo(
() => commonIssueFields(issues.filter((i) => selectedIds.has(i.id))),
[issues, selectedIds],
);
const [statusOpen, setStatusOpen] = useState(false);
const [priorityOpen, setPriorityOpen] = useState(false);
const [assigneeOpen, setAssigneeOpen] = useState(false);
const [deleteOpen, setDeleteOpen] = useState(false);
const surfaceActions = useIssueSurfaceActionsOptional();
const batchUpdate = useBatchUpdateIssues();
const batchDelete = useBatchDeleteIssues();
const openModal = useModalStore((s) => s.open);
const loading =
surfaceActions?.isPending ?? (batchUpdate.isPending || batchDelete.isPending);
if (count === 0) return null;
const ids = Array.from(selectedIds);
const handleBatchUpdate = async (updates: Partial<UpdateIssueRequest>) => {
try {
if (surfaceActions) {
await surfaceActions.batchUpdate(ids, updates);
} else {
await batchUpdate.mutateAsync({ ids, updates });
}
toast.success(t(($) => $.batch.update_success, { count }));
} catch (err) {
toast.error(
err instanceof Error && err.message
? err.message
: t(($) => $.batch.update_failed),
);
}
};
// Status and agent/squad assignment can fan out runs across the selection, so
// route them through the pre-trigger confirm modal (aggregate "将启动 N 个" +
// collective handoff note for assign + 暂不开始). The modal applies the batch
// itself. Priority, member assign, and unassign never start a run — direct.
const handleBatchStatus = (updates: Partial<UpdateIssueRequest>) => {
if (!updates.status) return;
// Backlog is the parking lot — a move into backlog never starts a run
// (server/internal/service/issue_trigger.go), so the confirm modal would
// only render an empty "won't start" box with a single Apply button. Apply
// directly, matching the single-issue status path.
if (updates.status === "backlog") {
void handleBatchUpdate(updates);
return;
}
openModal("issue-run-confirm", { issueIds: ids, mode: "status", status: updates.status });
};
const handleBatchAssignee = (updates: Partial<UpdateIssueRequest>) => {
if ((updates.assignee_type === "agent" || updates.assignee_type === "squad") && updates.assignee_id) {
// Backlog never starts a run on assign (parking lot), so if every selected
// issue is in backlog the confirm modal would only render an empty "won't
// start" box — apply directly, matching handleBatchStatus's backlog short-
// circuit. A mixed selection still routes through the modal: the non-backlog
// issues will trigger and need confirmation. An empty intersection (selected
// ids not in `issues`) falls through to the modal — safer than skipping.
const selected = issues.filter((i) => selectedIds.has(i.id));
const allBacklog = selected.length > 0 && selected.every((i) => i.status === "backlog");
if (!allBacklog) {
openModal("issue-run-confirm", {
issueIds: ids,
mode: "assign",
assigneeType: updates.assignee_type,
assigneeId: updates.assignee_id,
});
return;
}
}
void handleBatchUpdate(updates);
};
const handleBatchDelete = async () => {
try {
if (surfaceActions) {
await surfaceActions.batchDelete(ids);
} else {
await batchDelete.mutateAsync(ids);
}
clear();
toast.success(t(($) => $.batch.delete_success, { count }));
} catch (err) {
toast.error(
err instanceof Error && err.message
? err.message
: t(($) => $.batch.delete_failed),
);
} finally {
setDeleteOpen(false);
}
};
return (
<>
<div
className={cn(
"z-50 flex items-center gap-1 rounded-lg border bg-background px-2 py-1.5 shadow-lg",
placement === "fixed-bottom"
? "fixed bottom-6 left-1/2 -translate-x-1/2"
: "mb-2 w-fit",
)}
>
<div className="flex items-center gap-1.5 pl-1 pr-2 border-r mr-1">
<span className="text-sm font-medium">{t(($) => $.batch.selected, { count })}</span>
<button
type="button"
onClick={clear}
className="rounded p-0.5 hover:bg-accent transition-colors"
>
<X className="size-3.5 text-muted-foreground" />
</button>
</div>
{/* Status */}
<StatusPicker
status={common.status}
onUpdate={handleBatchStatus}
open={statusOpen}
onOpenChange={setStatusOpen}
triggerRender={<Button variant="ghost" size="sm" disabled={loading} />}
trigger={t(($) => $.batch.status)}
align="center"
/>
{/* Priority */}
<PriorityPicker
priority={common.priority}
onUpdate={handleBatchUpdate}
open={priorityOpen}
onOpenChange={setPriorityOpen}
triggerRender={<Button variant="ghost" size="sm" disabled={loading} />}
trigger={t(($) => $.batch.priority)}
align="center"
/>
{/* Assignee */}
<AssigneePicker
assigneeType={common.assignee?.type ?? null}
assigneeId={common.assignee?.id ?? null}
mixed={common.assignee === null}
onUpdate={handleBatchAssignee}
open={assigneeOpen}
onOpenChange={setAssigneeOpen}
triggerRender={<Button variant="ghost" size="sm" disabled={loading} />}
trigger={t(($) => $.batch.assignee)}
align="center"
/>
{/* Delete */}
<Button
variant="ghost"
size="sm"
disabled={loading}
onClick={() => setDeleteOpen(true)}
className="text-destructive hover:text-destructive"
>
<Trash2 className="size-3.5 mr-1" />
{t(($) => $.batch.delete)}
</Button>
</div>
<AlertDialog open={deleteOpen} onOpenChange={setDeleteOpen}>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>
{t(($) => $.batch.delete_dialog_title, { count })}
</AlertDialogTitle>
<AlertDialogDescription>
{t(($) => $.batch.delete_dialog_desc, { count })}
<span className="mt-2 block text-xs text-muted-foreground/80">
{t(($) => $.batch.delete_dialog_warning)}
</span>
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>{t(($) => $.batch.cancel)}</AlertDialogCancel>
<AlertDialogAction
onClick={handleBatchDelete}
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
>
{t(($) => $.batch.delete)}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</>
);
}