Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
"use client";
import { IssueSidebarCollapsible, IssueSidebarSectionHeader } from "./issue-sidebar-section";
import { useState } from "react";
import { useQuery } from "@tanstack/react-query";
import { Bell, Clock3, ChevronRight, GitPullRequest, Link2, ListChecks, CircleDot, Play } from "@multica/ui/icons";
import { toast } from "sonner";
import {
issueWakeupRunsOptions,
issueWakeupsOptions,
useDeleteIssueWakeup,
useDisableIssueWakeup,
useEnableIssueWakeup,
useTriggerIssueWakeup,
issueTasksOptions,
} from "@multica/core/issues";
import type { AgentTask, IssueWakeup, WakeupCondition } from "@multica/core/types";
import { useCurrentWorkspace } from "@multica/core/paths";
import {
Popover,
PopoverTrigger,
PopoverContent,
PopoverTitle,
} from "@multica/ui/components/ui/popover";
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 { WakeupInstructionEditor } from "./wakeup-instruction-editor";
import { WakeupControl } from "./wakeup-control";
import { WakeupCreate } from "./wakeup-create";
import { useSidebarRevealMotion } from "./use-sidebar-reveal-motion";
import { TranscriptButton } from "../../common/task-transcript";
import { useViewingTimezone } from "../../common/use-viewing-timezone";
import { useLocale, useT } from "../../i18n";
import {
formatWakeupTime,
isCurrentWakeup,
isActiveWakeupRun,
useWakeupText,
wakeupRun,
} from "./wakeup-presentation";
/** The glyph a rule's condition reads with in lists and the timeline. */
export function conditionIcon(condition?: WakeupCondition | null) {
switch (condition?.type) {
case "children_done":
return ListChecks;
case "pull_request":
return GitPullRequest;
case "other_issue":
return Link2;
case "issue_field":
return CircleDot;
default:
return null;
}
}
/** The rule's latest runs: what fired, and what came of it. */
function WakeupHistory({ wakeup }: { wakeup: IssueWakeup }) {
const { t } = useT("issues");
const locale = useLocale();
const viewTZ = useViewingTimezone();
const workspaceId = useCurrentWorkspace()?.id ?? "";
const text = useWakeupText();
const { data: runs, isError } = useQuery(issueWakeupRunsOptions(workspaceId, wakeup.issue_id, wakeup.id));
return (
<div className="space-y-1.5">
<p className="text-caption font-medium">{t(($) => $.wakeups.detail.history_title)}</p>
{isError ? (
<p className="text-caption text-muted-foreground">{t(($) => $.wakeups.detail.history_error)}</p>
) : runs && runs.length === 0 ? (
<p className="text-caption text-muted-foreground">{t(($) => $.wakeups.detail.history_empty)}</p>
) : (
<ul className="space-y-1">
{(runs ?? []).map((run) => (
<li key={run.id} className="grid grid-cols-[auto_minmax(0,1fr)] gap-x-3 text-caption">
<span className="tabular-nums text-muted-foreground">{formatWakeupTime(run.created_at, locale, viewTZ)}</span>
<span className="min-w-0 break-words">
{run.checkin_note ? (
<>
<span>{t(($) => $.wakeups.detail.run_checkin)}</span>
<span className="text-muted-foreground"> · {run.checkin_note}</span>
</>
) : run.commented ? (
t(($) => $.wakeups.detail.run_commented, { state: text.runState(run.status) })
) : (
text.runState(run.status)
)}
</span>
</li>
))}
</ul>
)}
</div>
);
}
function DeleteWakeup({ wakeup, onDeleted }: { wakeup: IssueWakeup; onDeleted: () => void }) {
const { t } = useT("issues");
const text = useWakeupText();
const workspaceId = useCurrentWorkspace()?.id ?? "";
const remove = useDeleteIssueWakeup(workspaceId, wakeup.issue_id);
const [open, setOpen] = useState(false);
return (
<AlertDialog open={open} onOpenChange={(next) => !remove.isPending && setOpen(next)}>
<Button variant="ghost" size="sm" className="ml-auto text-destructive hover:text-destructive" onClick={() => setOpen(true)}>
{t(($) => $.wakeups.detail.delete)}
</Button>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>{t(($) => $.wakeups.detail.delete_title)}</AlertDialogTitle>
<AlertDialogDescription>{t(($) => $.wakeups.detail.delete_body, { agent: wakeup.agent_name })}</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel disabled={remove.isPending}>{t(($) => $.wakeups.create.cancel)}</AlertDialogCancel>
<AlertDialogAction
variant="destructive"
disabled={remove.isPending}
onClick={(event) => {
event.preventDefault();
remove.mutate(wakeup.id, {
onSuccess: () => {
setOpen(false);
onDeleted();
},
onError: (err) => toast.error(text.error(err, t(($) => $.wakeups.detail.delete_error))),
});
}}
>
{remove.isPending ? t(($) => $.wakeups.detail.deleting) : t(($) => $.wakeups.detail.delete)}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
);
}
function WakeupRow({
wakeup,
task,
sourceTask,
pending,
onDisable,
onEnable,
closed,
agentAssumed,
}: {
wakeup: IssueWakeup;
task?: AgentTask;
sourceTask?: AgentTask;
pending: boolean;
onDisable: () => void;
closed: boolean;
onEnable: (input?: { at?: string; rearm?: boolean }) => Promise<void>;
/** The woken agent is the issue's own, so the row need not name it. */
agentAssumed: boolean;
}) {
const { t } = useT("issues");
const workspaceId = useCurrentWorkspace()?.id ?? "";
const text = useWakeupText();
const viewTZ = useViewingTimezone();
const status = task?.status ?? wakeup.last_task_status;
const activeRun = isActiveWakeupRun(status);
const Icon = conditionIcon(wakeup.condition) ?? (wakeup.kind === "event" ? Bell : Clock3);
const [detailOpen, setDetailOpen] = useState(false);
const trigger = useTriggerIssueWakeup(workspaceId, wakeup.issue_id);
const paused = text.paused(wakeup);
const fires = text.fires(wakeup);
// A finished, successful run is the expected outcome and goes unsaid here.
const showRunState = !!status && (activeRun || status !== "completed");
return (
<div aria-busy={pending}>
<Popover open={detailOpen} onOpenChange={setDetailOpen}>
<PopoverTrigger
render={
<button
type="button"
className="flex w-full min-w-0 flex-col rounded-md py-2 text-left text-label hover:bg-accent/50 focus-visible:outline-2 focus-visible:outline-ring"
/>
}
>
<span className="flex min-h-7 min-w-0 items-center gap-2.5 px-2">
<Icon
className="mt-px size-3.5 shrink-0 text-muted-foreground"
aria-hidden="true"
/>
<span className="line-clamp-2 min-w-0 break-words">
{text.trigger(wakeup, true)}
</span>
</span>
<span className="min-w-0 break-words pl-8 pr-2 text-caption">
<span className="block text-muted-foreground empty:hidden">
{text.summary(wakeup, closed, agentAssumed)}
</span>
{showRunState && (
<span className="block text-muted-foreground">
{t(
($) =>
activeRun || wakeup.mode === "once"
? $.wakeups.execution_summary
: $.wakeups.recent_execution,
{ state: text.runState(status) },
)}
</span>
)}
{(wakeup.disabled_at || closed) && activeRun && (
<span className="block text-muted-foreground">
{t(($) => $.wakeups.stopped_running)}
</span>
)}
{paused && <span className="block text-warning">{paused}</span>}
{wakeup.last_error && (
<span className="block text-destructive">
{t(($) => $.wakeups.needs_attention)}
</span>
)}
</span>
</PopoverTrigger>
<PopoverContent
align="end"
className="max-h-[70dvh] w-80 max-w-[calc(100vw-2rem)] overflow-y-auto"
keepMounted
>
<PopoverTitle>{text.trigger(wakeup)}</PopoverTitle>
<p className="text-caption text-muted-foreground">
{t(($) => $.wakeups.wake_agent, { agent: wakeup.agent_name })} ·{" "}
{text.schedule(wakeup)}
</p>
<p className="text-caption text-muted-foreground">
{t(($) => $.wakeups.scope_title)}:{" "}
{t(($) => $.wakeups.scope_current)}
</p>
{wakeup.created_by_name && (
<p className="break-words text-caption text-muted-foreground">
{t(($) => $.wakeups.source_title)}: {text.source(wakeup)}
</p>
)}
{wakeup.expires_at && (
<p className="break-words text-caption text-muted-foreground">
{t(($) => $.wakeups.expiry_title)}: {text.expiry(wakeup)}
</p>
)}
{fires && <p className="text-caption text-muted-foreground">{fires}</p>}
{paused && (
<p className="break-words text-caption text-warning">
{paused} · {t(($) => $.wakeups.paused.hint)}
</p>
)}
<div className="flex items-center justify-between gap-2">
<p className="text-caption font-medium">{t(($) => $.wakeups.instruction_title)}</p>
<WakeupInstructionEditor workspaceId={workspaceId} issueId={wakeup.issue_id} wakeupId={wakeup.id} />
</div>
<p className="whitespace-pre-wrap break-words text-caption">
{wakeup.instruction}
</p>
{wakeup.kind === "event" && !wakeup.condition && (
<p className="break-words text-caption text-muted-foreground">
{t(($) => $.wakeups.any_event)}:{" "}
{wakeup.event_types
.map((event) =>
text.eventCondition(event, wakeup),
)
.join("; ")}
</p>
)}
{wakeup.filter_actor_type && (
<p className="break-words text-caption text-muted-foreground">
{t(($) => $.wakeups.source_actor)}: {text.actorName(wakeup)}
</p>
)}
{wakeup.filter_agent_id && (
<p className="break-all text-caption text-muted-foreground">
{t(($) => $.wakeups.source_agent)}:{" "}
{wakeup.filter_agent_name ?? wakeup.filter_agent_id}
</p>
)}
{sourceTask && (
<TranscriptButton
task={sourceTask}
agentName={wakeup.filter_agent_name ?? ""}
title={t(($) => $.wakeups.source_run)}
/>
)}
{wakeup.filter_task_id && (
<p className="break-all text-caption text-muted-foreground">
{t(($) => $.wakeups.source_run)}: {wakeup.filter_task_id}
</p>
)}
{wakeup.next_fire_at && (
<p className="text-caption text-muted-foreground">
{new Date(wakeup.next_fire_at).toLocaleString(undefined, {
timeZone: viewTZ,
})}{" "}
· {viewTZ}
</p>
)}
{wakeup.cron_expression && (
<code className="block text-caption text-muted-foreground">
{wakeup.cron_expression} · {wakeup.timezone}
</code>
)}
{wakeup.last_error && (
<p className="break-words text-caption text-destructive">
{wakeup.last_error}
</p>
)}
{task && (
<div className="flex items-center gap-1 text-caption text-muted-foreground">
<span>{t(($) => $.wakeups.last_run)}</span>
<TranscriptButton
task={task}
agentName={wakeup.agent_name}
title={t(($) => $.wakeups.last_run)}
/>
</div>
)}
{detailOpen && <WakeupHistory wakeup={wakeup} />}
<div className="flex items-center gap-1 border-t border-border pt-2.5">
<WakeupControl
size="xs"
wakeup={wakeup}
task={task}
pending={pending}
closed={closed}
onDisable={onDisable}
onEnable={onEnable}
/>
{!closed && !wakeup.disabled_at && (
<Button
variant="outline"
size="sm"
disabled={trigger.isPending}
onClick={() =>
trigger.mutate(wakeup.id, {
onSuccess: () => toast.success(t(($) => $.wakeups.detail.wake_now_done, { agent: wakeup.agent_name })),
onError: (err) => toast.error(text.error(err, t(($) => $.wakeups.detail.wake_now_error))),
})
}
>
<Play aria-hidden="true" />
{t(($) => $.wakeups.detail.wake_now)}
</Button>
)}
{!closed && <DeleteWakeup wakeup={wakeup} onDeleted={() => setDetailOpen(false)} />}
</div>
</PopoverContent>
</Popover>
</div>
);
}
export function WakeupsSection({
issueId,
closed = false,
defaultAgentId,
reveal = false,
onRevealEnd,
}: {
issueId: string;
closed?: boolean;
/** Preselected target for a new wakeup: the issue's agent assignee. */
defaultAgentId?: string;
/** Shows the section while empty with the create form open, until it closes. */
reveal?: boolean;
onRevealEnd?: () => void;
}) {
const { t } = useT("issues");
const workspaceId = useCurrentWorkspace()?.id ?? "";
const [open, setOpen] = useState(true);
const [historyOpen, setHistoryOpen] = useState(false);
const {
data = [],
isError,
refetch,
} = useQuery(issueWakeupsOptions(workspaceId, issueId));
const { data: tasks = [] } = useQuery(issueTasksOptions(issueId));
const hasContent = data.length > 0 || isError;
const text = useWakeupText();
const disable = useDisableIssueWakeup(workspaceId, issueId);
const enable = useEnableIssueWakeup(workspaceId, issueId);
const motion = useSidebarRevealMotion<HTMLElement>(reveal && !closed, hasContent, onRevealEnd);
// An empty section is offered by the sidebar's add row instead.
if (!motion.shown) return null;
const current = data.filter((w) => isCurrentWakeup(w, wakeupRun(w, tasks)));
const history = data.filter((w) => !isCurrentWakeup(w, wakeupRun(w, tasks)));
const row = (wakeup: IssueWakeup) => (
<WakeupRow
key={wakeup.id}
wakeup={wakeup}
task={wakeupRun(wakeup, tasks)}
sourceTask={tasks.find((task) => task.id === wakeup.filter_task_id)}
pending={disable.isPending || enable.isPending}
closed={closed}
agentAssumed={!!defaultAgentId && wakeup.agent_id === defaultAgentId}
onEnable={async (input = {}) => {
await enable.mutateAsync({
id: wakeup.id,
revision: wakeup.revision ?? 0,
...input,
});
}}
onDisable={() =>
disable.mutate(wakeup.id, {
onError: (err) =>
toast.error(
text.error(
err,
t(($) => $.wakeups.disable_error),
),
),
onSuccess: () => setHistoryOpen(true),
})
}
/>
);
return (
<section ref={motion.ref}>
<IssueSidebarSectionHeader
id={`issue-wakeups-${issueId}`}
title={t(($) => $.wakeups.title)} open={open} onOpenChange={setOpen}
count={current.length}
collapsible={hasContent}
actions={!closed ? (
<WakeupCreate
workspaceId={workspaceId}
issueId={issueId}
defaultAgentId={defaultAgentId}
defaultOpen={reveal}
onOpenChange={(next) => !next && reveal && motion.end()}
/>
) : undefined}
/>
<IssueSidebarCollapsible open={open && hasContent}>
<div className="px-2">
{isError && (
<button
type="button"
className="flex min-h-8 w-full items-center rounded-md px-2 py-1.5 text-left text-label text-muted-foreground transition-colors hover:bg-accent/50 hover:text-foreground focus-visible:outline-2 focus-visible:outline-ring"
onClick={() => void refetch()}
>
{t(($) => $.wakeups.retry)}
</button>
)}
{closed && (
<p className="px-2 text-label text-muted-foreground">
{t(($) => $.wakeups.closed_hint)}
</p>
)}
{current.map(row)}
{history.length > 0 && (
<>
<button
type="button"
aria-expanded={historyOpen}
onClick={() => setHistoryOpen(!historyOpen)}
className="flex min-h-9 w-full items-center gap-2.5 rounded-md px-2 py-1 text-label text-muted-foreground transition-colors hover:bg-accent/50 focus-visible:outline-2 focus-visible:outline-ring"
>
<span className="flex size-3.5 shrink-0 items-center justify-center" aria-hidden="true">
<ChevronRight
className={`size-3 transition-transform duration-150 ease-[cubic-bezier(0.23,1,0.32,1)] motion-reduce:transition-none ${historyOpen ? "rotate-90" : ""}`}
/>
</span>
{t(($) => $.wakeups.ended, { count: history.length })}
</button>
<IssueSidebarCollapsible open={historyOpen}>
{history.map(row)}
</IssueSidebarCollapsible>
</>
)}
</div>
</IssueSidebarCollapsible>
</section>
);
}