Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
import { queryOptions } from "@tanstack/react-query";
import { api } from "../api";
import type { IssueWorkflow, IssueWorkflowStep, Project } from "../types";
/**
* Workspace workflows (MUL-7420).
*
* A project's board is its workflow's columns — the named one it picked, or
* the workspace Default — with that workflow's handoffs on them. Helpers
* return null only while workflows are unknown (still loading, or a server
* without workflows), and callers then keep their pre-workflow behavior.
*/
export const issueWorkflowKeys = {
all: (wsId: string) => ["issue-workflows", wsId] as const,
list: (wsId: string) => [...issueWorkflowKeys.all(wsId), "list"] as const,
handoffPreview: (wsId: string, issueId: string, status: string, projectId = "") =>
[...issueWorkflowKeys.all(wsId), "handoff-preview", issueId, status, projectId] as const,
};
/**
* What moving an issue to `status` — or into project `projectId` — would do.
* Short-lived: the answer depends on the issue's assignee and runs, which
* change under it.
*/
export function workflowHandoffPreviewOptions(wsId: string, issueId: string, status: string, projectId?: string) {
return queryOptions({
queryKey: issueWorkflowKeys.handoffPreview(wsId, issueId, status, projectId),
queryFn: () => api.previewWorkflowHandoff(issueId, status, projectId),
staleTime: 0,
gcTime: 30_000,
});
}
export function issueWorkflowListOptions(wsId: string) {
return queryOptions({
queryKey: issueWorkflowKeys.list(wsId),
queryFn: () => api.listIssueWorkflows(),
select: (data) => data.workflows,
// Workflows change only when an admin edits one; the
// `issue_workflow:changed` event refreshes every client when that happens.
staleTime: 5 * 60_000,
});
}
/** The workspace Default workflow, once loaded. */
export function defaultWorkflow(workflows: readonly IssueWorkflow[] | undefined): IssueWorkflow | null {
return workflows?.find((w) => w.is_default) ?? null;
}
/** Statuses with fixed places at the ends of a workflow, in the order they sit. */
export const WORKFLOW_HEAD_STATUSES = ["backlog", "todo"] as const;
export const WORKFLOW_TAIL_STATUSES = ["blocked", "done", "cancelled"] as const;
/** Fixed statuses a workflow may leave out; the others every workflow lists. */
export const WORKFLOW_OPTIONAL_STATUSES = ["backlog", "blocked"] as const;
const FIXED_STATUSES = new Set<string>([...WORKFLOW_HEAD_STATUSES, ...WORKFLOW_TAIL_STATUSES]);
const OPTIONAL_STATUSES = new Set<string>(WORKFLOW_OPTIONAL_STATUSES);
const NO_HANDOFF_STATUSES = new Set(["backlog", "done", "cancelled"]);
/** Whether a status has a fixed place when a workflow lists it. */
export function isFixedWorkflowStatus(statusKey: string): boolean {
return FIXED_STATUSES.has(statusKey);
}
/** Whether a fixed status may be left out of a workflow. */
export function isOptionalWorkflowStatus(statusKey: string): boolean {
return OPTIONAL_STATUSES.has(statusKey);
}
/** Whether a column can have a handler: parking and closing columns cannot. */
export function statusAcceptsHandoff(statusKey: string): boolean {
return !NO_HANDOFF_STATUSES.has(statusKey);
}
/** The columns between the fixed head and tail, in order. */
export function workflowMiddleKeys(workflow: Pick<IssueWorkflow, "steps">): string[] {
return workflow.steps.map((s) => s.status_key).filter((k) => !FIXED_STATUSES.has(k));
}
/**
* The column a step's handler is told to move to when done: the step's own
* choice, else the next column, skipping blocked and closed ones; empty at the
* end. With `categoryOf`, it matches the agent's brief for custom statuses
* too: a closed column such as Won't Fix is never where finished work goes,
* and a done or closed column routes only where its author chose (MUL-9913).
*/
export function workflowNextStatus(
workflow: Pick<IssueWorkflow, "steps">,
statusKey: string,
categoryOf?: (statusKey: string) => string,
): string {
const index = workflow.steps.findIndex((s) => s.status_key === statusKey);
if (index < 0) return "";
const own = workflow.steps[index]?.next_status_key;
if (own) return own;
const category = categoryOf?.(statusKey);
if (category === "done" || category === "closed") return "";
for (const s of workflow.steps.slice(index + 1)) {
if (s.status_key !== "blocked" && categoryOf?.(s.status_key) !== "closed") return s.status_key;
}
return "";
}
/**
* The workflow a project uses — its named one, else Default; null while
* workflows are unknown.
*/
export function resolveProjectWorkflow(
workflows: readonly IssueWorkflow[] | undefined,
project: Pick<Project, "workflow_id"> | null | undefined,
): IssueWorkflow | null {
if (!project || !workflows) return null;
const named = project.workflow_id ? workflows.find((w) => w.id === project.workflow_id) : undefined;
return named ?? defaultWorkflow(workflows);
}
/**
* The workflow whose statuses an issue may take: its project's, or Default
* for an issue outside any project. Only for statuses — outside a project
* nothing hands off, so handoffs still come from `resolveProjectWorkflow`.
* Null while workflows or the issue's project are unknown. (MUL-8250)
*/
export function issueStatusWorkflow(
workflows: readonly IssueWorkflow[] | undefined,
projects: readonly Pick<Project, "id" | "workflow_id">[] | undefined,
issue: { project_id: string | null },
): IssueWorkflow | null {
if (!issue.project_id) return defaultWorkflow(workflows);
return resolveProjectWorkflow(workflows, projects?.find((p) => p.id === issue.project_id));
}
/**
* The statuses every one of `issues` may take, or null when none is limited.
* Each issue takes its workflow's statuses, Default's outside a project, as
* the server enforces. A batch status change offers this set, so it never
* shows a status the server would skip.
*/
export function sharedWorkflowStatusKeys(
workflows: readonly IssueWorkflow[] | undefined,
projects: readonly Pick<Project, "id" | "workflow_id">[] | undefined,
issues: readonly { project_id: string | null }[],
): Set<string> | null {
let shared = null as Set<string> | null;
for (const issue of issues) {
const workflow = issueStatusWorkflow(workflows, projects, issue);
if (!workflow) continue;
const keys = new Set(workflow.steps.map((s) => s.status_key));
shared = shared ? new Set([...shared].filter((key) => keys.has(key))) : keys;
}
return shared;
}
export function workflowStep(
workflow: IssueWorkflow | null | undefined,
statusKey: string,
): IssueWorkflowStep | undefined {
return workflow?.steps.find((s) => s.status_key === statusKey);
}
/** Whether entering the step reassigns the issue. */
export function stepHandsOff(step: IssueWorkflowStep | undefined): boolean {
return !!step && step.handler.type !== "none";
}
/**
* Whether issues on `workflow` may enter a status: the ones it lists, Default
* included (MUL-8250). An issue already on a status its workflow no longer
* lists keeps it, so callers exempt the issue's current status. Null —
* workflows still loading — allows every status.
*/
export function workflowAllowsStatus(
workflow: IssueWorkflow | null | undefined,
statusKey: string,
): boolean {
if (!workflow) return true;
return workflow.steps.some((s) => s.status_key === statusKey);
}
/**
* Whether moving an issue on `statusKey` into a project using `workflow` may
* hand it off, when the move enters the column it lands on: the column it
* keeps hands off, or it lands on another column and some column does. The
* server preview has the final word.
*/
export function projectMoveMayHandOff(workflow: IssueWorkflow | null | undefined, statusKey: string): boolean {
if (!workflow) return false;
// A move into a Default project keeps the issue's status, listed or not.
if (workflow.is_default || workflowAllowsStatus(workflow, statusKey)) {
return stepHandsOff(workflowStep(workflow, statusKey));
}
return workflow.steps.some(stepHandsOff);
}
/** Steps whose entry reassigns the issue. */
export function handoffStepCount(workflow: IssueWorkflow): number {
return workflow.steps.filter((s) => s.handler.type !== "none").length;
}