Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
"use client";
import { useCallback, useMemo } from "react";
import { toast } from "sonner";
import type { BatchIssueLabelOps, Issue, UpdateIssueRequest } from "@multica/core/types";
import { errorCode } from "@multica/core/api";
import { useCommandBarContextStore, type CommandBarContext } from "@multica/core/command-bar";
import { noteInboxBarAction, type InboxBarAction } from "@multica/core/inbox/auto-archive";
import { useWorkspaceId } from "@multica/core/hooks";
import { useIssueStatuses } from "@multica/core/issue-statuses/hooks";
import { useBatchUpdateIssues, useUpdateIssue } from "@multica/core/issues/mutations";
import { useModalStore } from "@multica/core/modals";
import { runConfirmIntent } from "../issues/actions/run-confirm-gate";
import { useBatchResultToast } from "../issues/components/batch-result-toast";
import { useT } from "../i18n";
import type { IssueTarget } from "./types";
export function targetIssues(target: IssueTarget): Issue[] {
return target.kind === "single" ? [target.issue] : target.issues;
}
/** `contexts` (newest first) with a list cursor's issue moved behind the rest. */
export function withCursorLast(contexts: readonly CommandBarContext[]): readonly CommandBarContext[] {
if (!contexts.some(isCursorContext)) return contexts;
return [...contexts.filter((context) => !isCursorContext(context)), ...contexts.filter(isCursorContext)];
}
function isCursorContext(context: CommandBarContext): boolean {
return context.kind === "issue" && context.cursor === true;
}
/**
* What issue commands act on: the newest issue or selection a page
* registered, and a list cursor's issue only when there is neither.
*/
export function issueTargetFromContexts(contexts: readonly CommandBarContext[]): IssueTarget | null {
for (const context of withCursorLast(contexts)) {
if (context.kind === "issue") return { kind: "single", issue: context.issue };
if (context.kind === "issues") {
return { kind: "batch", issues: context.issues, clearSelection: context.clearSelection };
}
}
return null;
}
/**
* Record a palette write to the Inbox's open issue as its bar records one, so
* once the notification archives the bar names what the user did.
*/
export function noteInboxBarActionFromPalette(issueId: string, action: InboxBarAction) {
const inbox = useCommandBarContextStore
.getState()
.entries.find((entry) => entry.context.kind === "inbox")?.context;
if (inbox?.kind === "inbox" && inbox.item?.issue_id === issueId) noteInboxBarAction(issueId, action);
}
/**
* Field writes for an issue command, routed the way the matching on-page
* control routes them: a single issue passes the run-confirm gate that asks
* before handing work to an agent; a selection follows the batch toolbar's
* rules.
*
* The palette closes, and unmounts this hook, before a write settles, so
* outcomes are handled on the returned promise: per-call `mutate` callbacks
* would be dropped with the component and a rejected write would roll back
* silently.
*/
export function useIssueTargetWriter(target: IssueTarget) {
const { t } = useT("issues");
const wsId = useWorkspaceId();
const { entryOf } = useIssueStatuses(wsId);
const single = target.kind === "single" ? target.issue : null;
const updateIssue = useUpdateIssue();
const batchUpdate = useBatchUpdateIssues();
const openModal = useModalStore((s) => s.open);
const issues = useMemo(() => targetIssues(target), [target]);
const { report, reportError } = useBatchResultToast();
const applyBatch = useCallback(
async (updates: Partial<UpdateIssueRequest>, labels?: BatchIssueLabelOps, successMessage?: string) => {
if (target.kind !== "batch") return;
const ids = issues.map((issue) => issue.id);
try {
const response = await batchUpdate.mutateAsync({ ids, updates, labels });
report(response, ids.length, { successMessage });
} catch (err) {
reportError(err);
}
},
[batchUpdate, issues, report, reportError, target.kind],
);
const applySingle = useCallback(
(issue: Issue, updates: Partial<UpdateIssueRequest>) => {
const intent = runConfirmIntent(issue, updates, { entryOf });
if (intent) {
openModal("issue-run-confirm", intent);
return;
}
updateIssue.mutateAsync({ id: issue.id, ...updates }).catch((err: unknown) => {
toast.error(
errorCode(err) === "revision_conflict"
? t(($) => $.revision.conflict)
: err instanceof Error && err.message
? err.message
: t(($) => $.detail.update_failed),
);
});
},
[entryOf, openModal, t, updateIssue],
);
const apply = useCallback(
(updates: Partial<UpdateIssueRequest>) => {
if (single) {
applySingle(single, updates);
return;
}
void applyBatch(updates);
},
[applyBatch, applySingle, single],
);
const assign = useCallback(
(assigneeType: "member" | "agent" | "squad" | null, assigneeId: string | null) => {
const updates: Partial<UpdateIssueRequest> = {
assignee_type: assigneeType,
assignee_id: assigneeId,
};
if (target.kind === "batch" && (assigneeType === "agent" || assigneeType === "squad") && assigneeId) {
// Same rule as the batch toolbar: assigning agent work across a
// selection previews the run fan-out, unless everything is parked in
// Backlog and nothing would start.
const allBacklog = issues.every((issue) => issue.status === "backlog");
if (!allBacklog) {
openModal("issue-run-confirm", {
issueIds: issues.map((issue) => issue.id),
mode: "assign",
assigneeType,
assigneeId,
});
return;
}
}
apply(updates);
},
[apply, issues, openModal, target.kind],
);
/** Label add / remove across a selection, as one batch write. */
const applyLabels = useCallback(
(labels: BatchIssueLabelOps, successMessage: string) => void applyBatch({}, labels, successMessage),
[applyBatch],
);
return { issues, apply, assign, applyLabels };
}