mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-20 00:53:46 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - Routines start agent work on a schedule, from a webhook, or on demand > - The routine detail page split one routine across eight sections behind a section sidebar (overview, triggers, variables, secrets, delivery, runs, activity, history) > - People had to hunt through sections to see what a routine does and to change it, and a finished webhook could not be tested again without starting the routine > - This pull request makes the approved two-column routine page, one Edit dialog and one Webhook dialog the real routed page, and lets a live webhook go back into test mode > - The benefit is one page that answers what the routine does, when it runs, who runs it and what it made, with every change in one dialog ## Linked Issues or Issue Description **Problem** The routine detail page spreads one routine over eight sections and a section sidebar. Changing a routine means visiting several sections, each with its own save bar. A webhook that is already live cannot be tested again: the API only accepts `setupPending: false`, so testing a live webhook starts the routine. **Proposed solution** Ship the reviewed design (stories `routine-page-design`, `routine-edit-dialog-design`, `routine-webhook-dialog-design`) as the routed page in both UI shells, wired to the real API. Let `PATCH /api/routine-triggers/:id` accept `setupPending: true` for a live webhook so the next events are checked but never start the routine. **Alternatives considered** Keep the sections and restyle them. The owner rejected this: the section sidebar should go. ## What Changed - `/routines/:id` renders `RoutinePage` in both shells (`RoutineDetail.production.tsx` is removed). Status switch, Repeats rows, default agent, responsible user, project, Asks for, Secrets, Run rules, Managed by plugin, Instructions with Show all, copy and the comment count, and History. - History is paged: 20 tasks per page from `GET /companies/:id/issues?originKind=routine_execution&originId=…&sortField=created&sortDir=desc&limit=21&offset=…`, with a real Load more. Coalesced runs show as "folded into a task". - A failed refetch keeps the loaded page on screen (`useQueryView`); a History failure shows `QueryErrorState` in place. - Header: Edit, "…" (Version history, Audit log, Archive / Restore) and Run now. In the streamlined desktop shell they sit in the breadcrumb toolbar; elsewhere above the page. - Version history (`/routines/:id/history`, restore and restored-webhook keys kept) and Audit log (`/routines/:id/activity`, `RoutineAuditActivity`) open as sub-views with a way back. Old section addresses and `?tab=` redirect to `/routines/:id`. - Edit dialog saves title, instructions, variables, secrets, agent, project and responsible user in one request against the revision the dialog opened with (save conflict shows "changed while you were editing"). The first schedule is created, changed or removed after that. Priority and delivery settings are never sent, so saved values stay. - Webhooks are created, changed, tested and removed (with Undo) right away from the Edit dialog, with the Webhook dialog nested inside it. Closing the Webhook dialog ends setup or test mode. A webhook left in test mode shows "Testing · events don’t run" on the page and offers Finish testing. - Responsibility (only with `enableResponsibilities`): the page shows direct and through-project responsibilities from the link lookup; the dialog links or unlinks the direct one. With the flag off nothing is fetched or shown. - The routines list creates routines with the same dialog in create mode, filed in the open folder, and opens the new routine page. - Server: `setupPending: true` on a live webhook enters test mode (planned by a pure `planTriggerUpdate`, which also gives the activity action) (422 for other trigger kinds; 409 for a disabled or removed webhook or an archived routine). Test deliveries keep the last real delivery as `lastWebhookDelivery.previous`, and finishing testing restores it. Activity `routine.trigger_test_started`. - Additions to the reviewed design, from review, so no capability is lost: the schedule popover has Pause/Resume; standing-check and API rows open a small popover with Pause/Resume and Remove; removing a trigger from the page leaves a "… removed. Undo" line under Repeats. Stories: `StandingCheckOpened`, `ScheduleTriggerPaused(Opened)`, `RemovedWithUndo`. - Save is several requests (routine, schedule, responsibility). It changes only the schedule the form opened with, records each finished step, and a partial failure says what was saved and that saving again finishes it. The dialog cannot be dismissed while saving. Failed reads show a Retry state, never empty choices. - Removed: the routine section sidebar and contextual sidebar, the trigger wizard, trigger cards, save bar, section components and their tests. - Storybook: the three routine stories moved to `stories/routines` as current production stories, a new story mounts the routed page against a fixture API, and stories that used the old page were updated or removed. ## Verification - `pnpm --filter @paperclipai/server exec vitest run src/__tests__/routines-e2e.test.ts` (17/17) and `src/__tests__/routines-service.test.ts` (105/105). - `pnpm exec vitest run` on the routine page, routines list, routine form, webhook instructions, Layout, shell navigation, audit and editor tests: 275 tests pass. - `cd ui && npx tsc -p tsconfig.json --noEmit`, `node scripts/check-token-gates.mjs`, `node scripts/check-maps.mjs`, `node scripts/storybook-catalog.mjs --check`: pass. - `pnpm verify` exits 2 (global CI input changed); required CI supplies the broad coverage. - Manual: an isolated worktree instance (`pnpm paperclipai worktree init --empty`, `pnpm dev`) with a seeded routine. Headless Playwright: an old `/triggers` address redirects, no section sidebar, History shows 20 then 23 after Load more, the schedule popover opens, pause and resume, Edit → Add webhook → created key → Finish, title save, Test webhook → Finish testing, Version history, Audit log shows "Started testing a webhook", phone width has no horizontal overflow, and Create routine from the list opens the new page. No console errors or failed API calls. ## Risks - Behaviour change: the section pages are gone. Old links redirect to the single page. - The delivery-setting editors (concurrency, catch-up, activity gate) and priority are removed from the UI. Saved values stay, and the API still accepts them. - The create dialog no longer offers a folder picker or delivery settings. A new routine goes into the folder the list is showing and takes the default settings. - Replace key in the Webhook dialog acts at once, as in the reviewed design. The old confirmation is gone. - No migration. `lastWebhookDelivery.previous` is an optional field inside the existing jsonb column. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, in Claude Code with tool use (shell, file edits, headless browser) and subagents for the server change and the Storybook move. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
682 lines
31 KiB
TypeScript
682 lines
31 KiB
TypeScript
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||
import { useInfiniteQuery, useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||
import type { Issue, RoutineDetail as RoutineDetailType, RoutineTrigger } from "@paperclipai/shared";
|
||
import { AlertCircle, Repeat } from "lucide-react";
|
||
import { ApiError } from "@/api/client";
|
||
import { documentAnnotationsApi } from "@/api/document-annotations";
|
||
import { describeError } from "@/api/errors";
|
||
import { issuesApi } from "@/api/issues";
|
||
import { responsibilitiesApi } from "@/api/responsibilities";
|
||
import { routinesApi, type RestoreRoutineRevisionResponse } from "@/api/routines";
|
||
import { EmptyState } from "@/components/EmptyState";
|
||
import { IssueDocumentAnnotations } from "@/components/IssueDocumentAnnotations";
|
||
import { PageSkeleton } from "@/components/PageSkeleton";
|
||
import { QueryErrorState, useQueryView } from "@/components/QueryView";
|
||
import { RoutineHistoryTab } from "@/components/RoutineHistoryTab";
|
||
import { RoutineRunVariablesDialog, type RoutineRunDialogSubmitData } from "@/components/RoutineRunVariablesDialog";
|
||
import { RoutineEditDialog, type RoutineFormDraft } from "@/components/routine-page/RoutineEditDialog";
|
||
import {
|
||
RoutinePage,
|
||
RoutinePageActions,
|
||
RoutineSubpage,
|
||
type RoutinePageAgent,
|
||
type RoutinePageProject,
|
||
} from "@/components/routine-page/RoutinePage";
|
||
import { RoutineWebhookDialog } from "@/components/routine-page/RoutineWebhookDialog";
|
||
import {
|
||
PartialRoutineSaveError,
|
||
primarySchedule,
|
||
type RoutineSavePart,
|
||
buildRoutineResponsibilityChoices,
|
||
routineFieldsChanged,
|
||
routineFieldsPayload,
|
||
routineToDraft,
|
||
syncRoutineResponsibility,
|
||
syncRoutineSchedule,
|
||
} from "@/components/routine-page/routine-form";
|
||
import { resolveRoutineAddress, routinePath, type RoutineView } from "@/components/routine-page/routine-routes";
|
||
import { routineAutomationState } from "@/components/routine-page/routine-summary";
|
||
import { useRoutineFormOptions } from "@/components/routine-page/useRoutineFormOptions";
|
||
import { useRoutineWebhookDialog } from "@/components/routine-page/useRoutineWebhookDialog";
|
||
import { RoutineAuditActivity } from "@/pages/audit/RoutineAuditActivity";
|
||
import { useBreadcrumbs } from "@/context/BreadcrumbContext";
|
||
import { useCompany } from "@/context/CompanyContext";
|
||
import { useSidebar } from "@/context/SidebarContext";
|
||
import { useToastActions } from "@/context/ToastContext";
|
||
import { useStreamlinedUiEnabled } from "@/hooks/useStreamlinedUiEnabled";
|
||
import { configurationWarningMessages } from "@/lib/configuration-warnings";
|
||
import { createIssueDetailLocationState } from "@/lib/issueDetailBreadcrumb";
|
||
import { queryKeys } from "@/lib/queryKeys";
|
||
import { Navigate, useLocation, useNavigate, useParams, useSearchParams } from "@/lib/router";
|
||
|
||
/** Tasks shown per History page; "Load more" fetches the next page. */
|
||
export const ROUTINE_HISTORY_PAGE_SIZE = 20;
|
||
|
||
export function routineHistoryQueryKey(companyId: string, routineId: string) {
|
||
return [...queryKeys.issues.list(companyId), "routine", routineId, "history"] as const;
|
||
}
|
||
|
||
/** `/routines/:routineId` and its two sub-views; every older section address redirects here. */
|
||
export function RoutineDetail() {
|
||
const { routineId, section } = useParams<{ routineId: string; section?: string }>();
|
||
const [searchParams] = useSearchParams();
|
||
const { selectedCompanyId } = useCompany();
|
||
if (!selectedCompanyId) return <EmptyState icon={Repeat} message="Select an organization to view routines." />;
|
||
if (!routineId) return <Navigate to="/routines" replace />;
|
||
const address = resolveRoutineAddress({ routineId, section, legacyTab: searchParams.get("tab") });
|
||
if (address.redirectTo) return <Navigate to={address.redirectTo} replace />;
|
||
return <RoutineDetailPage key={routineId} companyId={selectedCompanyId} routineId={routineId} view={address.view} />;
|
||
}
|
||
|
||
function withCurrent<T extends { id: string }>(options: readonly T[], all: readonly T[], currentId: string | null | undefined): T[] {
|
||
if (!currentId || options.some((option) => option.id === currentId)) return [...options];
|
||
const current = all.find((option) => option.id === currentId);
|
||
return current ? [...options, current] : [...options];
|
||
}
|
||
|
||
function RoutineDetailPage({ companyId, routineId, view }: { companyId: string; routineId: string; view: RoutineView }) {
|
||
const queryClient = useQueryClient();
|
||
const navigate = useNavigate();
|
||
const { pushToast } = useToastActions();
|
||
const { setBreadcrumbs, setBreadcrumbToolbar } = useBreadcrumbs();
|
||
const { isMobile } = useSidebar();
|
||
const { enabled: streamlinedUiEnabled } = useStreamlinedUiEnabled();
|
||
const options = useRoutineFormOptions(companyId);
|
||
|
||
const [editOpen, setEditOpen] = useState(false);
|
||
const [editInitial, setEditInitial] = useState<RoutineFormDraft | null>(null);
|
||
const [saveConflict, setSaveConflict] = useState(false);
|
||
const [saveError, setSaveError] = useState<string | null>(null);
|
||
const [removedWebhooks, setRemovedWebhooks] = useState<RoutineTrigger[]>([]);
|
||
const [runOpen, setRunOpen] = useState(false);
|
||
// A link to one instruction comment opens the whole text with its comments,
|
||
// also when only the fragment changes on a routine already on screen.
|
||
const { hash: locationHash } = useLocation();
|
||
const [commentsOpen, setCommentsOpen] = useState(() => locationHash.startsWith("#document-description"));
|
||
useEffect(() => {
|
||
if (locationHash.startsWith("#document-description")) setCommentsOpen(true);
|
||
}, [locationHash]);
|
||
/** The revision the open form started from; refreshed after this person's own changes. */
|
||
const baseRevisionRef = useRef<{ id: string | null; number: number } | null>(null);
|
||
/** The direct responsibility when the form opened; Save only moves the link when the person changed it. */
|
||
const editResponsibilityRef = useRef("");
|
||
/** The schedule the form opened with, by id; Save changes only this one. */
|
||
const editScheduleIdRef = useRef<string | null>(null);
|
||
const [pageRemoved, setPageRemoved] = useState<RoutineTrigger[]>([]);
|
||
const editOpenRef = useRef(false);
|
||
editOpenRef.current = editOpen;
|
||
const [webhookDialogOpen, setWebhookDialogOpen] = useState(false);
|
||
|
||
const detailKey = queryKeys.routines.detail(routineId);
|
||
const routineQuery = useQuery({
|
||
queryKey: detailKey,
|
||
queryFn: () => routinesApi.get(routineId),
|
||
// Live webhook status while its dialog is open (test events, first delivery).
|
||
refetchInterval: webhookDialogOpen ? 2000 : false,
|
||
});
|
||
const routineView = useQueryView(routineQuery);
|
||
const routine = routineView.data;
|
||
const routineRef = useRef(routine);
|
||
routineRef.current = routine;
|
||
|
||
const historyQuery = useInfiniteQuery({
|
||
queryKey: routineHistoryQueryKey(companyId, routineId),
|
||
queryFn: ({ pageParam }) =>
|
||
issuesApi.list(companyId, {
|
||
originKind: "routine_execution",
|
||
originId: routineId,
|
||
limit: ROUTINE_HISTORY_PAGE_SIZE + 1,
|
||
offset: pageParam,
|
||
sortField: "created",
|
||
sortDir: "desc",
|
||
}),
|
||
initialPageParam: 0,
|
||
getNextPageParam: (last: Issue[], pages: Issue[][]) =>
|
||
last.length > ROUTINE_HISTORY_PAGE_SIZE ? pages.length * ROUTINE_HISTORY_PAGE_SIZE : undefined,
|
||
enabled: view === "routine",
|
||
});
|
||
const historyView = useQueryView(historyQuery);
|
||
const history = useMemo(
|
||
() => (historyQuery.data?.pages ?? []).flatMap((page) => page.slice(0, ROUTINE_HISTORY_PAGE_SIZE)),
|
||
[historyQuery.data],
|
||
);
|
||
|
||
const linksQuery = useQuery({
|
||
queryKey: queryKeys.responsibilities.routineLinks(companyId, routineId),
|
||
queryFn: () => responsibilitiesApi.lookupRoutineLinks(companyId, routineId),
|
||
enabled: options.responsibilitiesOn,
|
||
});
|
||
const linkEntries = linksQuery.data?.responsibilities;
|
||
const linksView = useQueryView(linksQuery);
|
||
|
||
const annotationTarget = { kind: "routine" as const, routineId, documentKey: "description" as const };
|
||
const annotationsQuery = useQuery({
|
||
queryKey: queryKeys.routines.documentAnnotations(routineId, "description", "all"),
|
||
queryFn: () => documentAnnotationsApi.listForTarget(annotationTarget, { status: "all", includeComments: true }),
|
||
enabled: Boolean(routine?.descriptionDocument),
|
||
staleTime: 30_000,
|
||
});
|
||
const openCommentCount = (annotationsQuery.data ?? []).filter(
|
||
(thread) => thread.status === "open" && thread.anchorState !== "orphaned",
|
||
).length;
|
||
|
||
/** Refetches the routine after a change; an open form adopts the new revision as its own. */
|
||
const refreshRoutine = useCallback(async () => {
|
||
const fresh = await queryClient.fetchQuery({ queryKey: detailKey, queryFn: () => routinesApi.get(routineId), staleTime: 0 });
|
||
// Each change made from the open form adds one revision. Adopt it only when
|
||
// nothing else changed the routine, so a save still reports someone else's edit.
|
||
const base = baseRevisionRef.current;
|
||
if (editOpenRef.current && base && fresh.latestRevisionNumber - base.number <= 1) {
|
||
baseRevisionRef.current = { id: fresh.latestRevisionId, number: fresh.latestRevisionNumber };
|
||
}
|
||
void queryClient.invalidateQueries({ queryKey: queryKeys.routines.list(companyId) });
|
||
void queryClient.invalidateQueries({ queryKey: queryKeys.routines.revisions(routineId) });
|
||
void queryClient.invalidateQueries({ queryKey: queryKeys.routines.activity(companyId, routineId) });
|
||
return fresh;
|
||
}, [companyId, detailKey, queryClient, routineId]);
|
||
|
||
const webhook = useRoutineWebhookDialog({
|
||
routineId,
|
||
triggers: routine?.triggers ?? [],
|
||
onChanged: refreshRoutine,
|
||
onRemoved: (trigger) => {
|
||
if (editOpenRef.current) setRemovedWebhooks((current) => [...current, trigger]);
|
||
else setPageRemoved((current) => [...current, trigger]);
|
||
},
|
||
});
|
||
useEffect(() => setWebhookDialogOpen(webhook.open), [webhook.open]);
|
||
|
||
const toastError = useCallback(
|
||
(title: string, error: unknown, action: string) => pushToast({ title, body: describeError(error, { action }).body, tone: "error" }),
|
||
[pushToast],
|
||
);
|
||
|
||
const setStatus = useMutation({
|
||
mutationFn: (status: "active" | "paused" | "archived") => routinesApi.update(routineId, { status }),
|
||
onSuccess: async (_saved, status) => {
|
||
pushToast({
|
||
title: status === "active" ? "Routine is on" : status === "paused" ? "Routine paused" : "Routine archived",
|
||
tone: "success",
|
||
});
|
||
await refreshRoutine();
|
||
},
|
||
onError: (error) => toastError("Couldn’t change the routine", error, "change the routine"),
|
||
});
|
||
|
||
const runRoutine = useMutation({
|
||
mutationFn: (data?: RoutineRunDialogSubmitData) =>
|
||
routinesApi.run(routineId, {
|
||
...(data?.variables && Object.keys(data.variables).length > 0 ? { variables: data.variables } : {}),
|
||
...(data?.assigneeAgentId !== undefined ? { assigneeAgentId: data.assigneeAgentId } : {}),
|
||
...(data?.projectId !== undefined ? { projectId: data.projectId } : {}),
|
||
...(data?.executionWorkspaceId !== undefined ? { executionWorkspaceId: data.executionWorkspaceId } : {}),
|
||
...(data?.executionWorkspacePreference !== undefined ? { executionWorkspacePreference: data.executionWorkspacePreference } : {}),
|
||
...(data?.executionWorkspaceSettings !== undefined ? { executionWorkspaceSettings: data.executionWorkspaceSettings } : {}),
|
||
}),
|
||
onSuccess: async () => {
|
||
pushToast({ title: "Routine run started", tone: "success" });
|
||
setRunOpen(false);
|
||
await Promise.all([
|
||
queryClient.invalidateQueries({ queryKey: routineHistoryQueryKey(companyId, routineId) }),
|
||
queryClient.invalidateQueries({ queryKey: queryKeys.routines.runs(routineId) }),
|
||
refreshRoutine(),
|
||
]);
|
||
},
|
||
onError: (error) => toastError("Routine run failed", error, "start the routine run"),
|
||
});
|
||
|
||
/** Saves, pauses, resumes, removes (with Undo on the page) or restores one trigger from the page. */
|
||
const changeTrigger = useMutation({
|
||
mutationFn: ({ trigger, patch }: { trigger: RoutineTrigger; patch: Record<string, unknown>; success: string }) =>
|
||
routinesApi.updateTrigger(trigger.id, patch),
|
||
onSuccess: async (_saved, { trigger, patch, success }) => {
|
||
if (patch.archived === true) setPageRemoved((current) => [...current, trigger]);
|
||
else if (patch.archived === false) setPageRemoved((current) => current.filter((item) => item.id !== trigger.id));
|
||
else pushToast({ title: success, tone: "success" });
|
||
await refreshRoutine();
|
||
},
|
||
onError: (error) => toastError("Couldn’t change this trigger", error, "change this trigger"),
|
||
});
|
||
const triggerById = (id: string) => routineRef.current?.triggers.find((trigger) => trigger.id === id);
|
||
|
||
const saveEdits = useMutation({
|
||
mutationFn: async (draft: RoutineFormDraft) => {
|
||
const latest = routineRef.current;
|
||
if (!latest) throw new Error("This routine is still loading. Try again in a moment.");
|
||
const saved = routineToDraft(latest, editResponsibilityRef.current);
|
||
const responsibleUserChanged = Boolean(draft.responsibleUserId)
|
||
&& draft.responsibleUserId !== saved.responsibleUserId
|
||
&& latest.canChangeResponsibleUser !== false;
|
||
let warnings: string | null = null;
|
||
// Each step records what it saved, so a retry after a partial failure only does what is left.
|
||
const savedParts: RoutineSavePart[] = [];
|
||
if (routineFieldsChanged(draft, saved) || responsibleUserChanged) {
|
||
const updated = await routinesApi.update(routineId, {
|
||
...routineFieldsPayload(draft),
|
||
...(responsibleUserChanged ? { responsibleUserId: draft.responsibleUserId } : {}),
|
||
...(baseRevisionRef.current?.id ? { baseRevisionId: baseRevisionRef.current.id } : {}),
|
||
});
|
||
baseRevisionRef.current = { id: updated.latestRevisionId, number: updated.latestRevisionNumber };
|
||
warnings = configurationWarningMessages(updated);
|
||
savedParts.push("details");
|
||
}
|
||
try {
|
||
const schedule = await syncRoutineSchedule(routineId, latest.triggers, editScheduleIdRef.current, draft.schedule);
|
||
editScheduleIdRef.current = schedule.scheduleId;
|
||
if (schedule.changed) {
|
||
savedParts.push("schedule");
|
||
await refreshRoutine();
|
||
}
|
||
} catch (error) {
|
||
if (savedParts.length > 0) throw new PartialRoutineSaveError(savedParts, "schedule", error);
|
||
throw error;
|
||
}
|
||
if (options.responsibilitiesOn) {
|
||
try {
|
||
if (await syncRoutineResponsibility(routineId, editResponsibilityRef.current, draft.responsibilityId)) {
|
||
await queryClient.invalidateQueries({ queryKey: queryKeys.responsibilities.all });
|
||
}
|
||
editResponsibilityRef.current = draft.responsibilityId;
|
||
} catch (error) {
|
||
if (savedParts.length > 0) throw new PartialRoutineSaveError(savedParts, "responsibility", error);
|
||
throw error;
|
||
}
|
||
}
|
||
return warnings;
|
||
},
|
||
onMutate: () => {
|
||
setSaveError(null);
|
||
setSaveConflict(false);
|
||
},
|
||
onSuccess: async (warnings) => {
|
||
setEditOpen(false);
|
||
setRemovedWebhooks([]);
|
||
pushToast(warnings ? { title: "Routine saved", body: warnings, tone: "warn" } : { title: "Routine saved", tone: "success" });
|
||
await refreshRoutine();
|
||
},
|
||
onError: (error) => {
|
||
if (error instanceof ApiError && error.status === 409) {
|
||
setSaveConflict(true);
|
||
return;
|
||
}
|
||
setSaveError(error instanceof PartialRoutineSaveError ? error.message : describeError(error, { action: "save the routine" }).body);
|
||
void refreshRoutine().catch(() => undefined);
|
||
},
|
||
});
|
||
|
||
/** The routine's direct responsibility, read before the form opens so Save never unlinks one it had not loaded. */
|
||
const loadDirectResponsibility = useCallback(async () => {
|
||
if (!options.responsibilitiesOn) return "";
|
||
const links = await queryClient.fetchQuery({
|
||
queryKey: queryKeys.responsibilities.routineLinks(companyId, routineId),
|
||
queryFn: () => responsibilitiesApi.lookupRoutineLinks(companyId, routineId),
|
||
});
|
||
return links.responsibilities.find((entry) => entry.via === "direct")?.id ?? "";
|
||
}, [companyId, options.responsibilitiesOn, queryClient, routineId]);
|
||
|
||
const openEdit = useCallback(async () => {
|
||
const current = routineRef.current;
|
||
if (!current) return;
|
||
let responsibilityId = "";
|
||
try {
|
||
responsibilityId = await loadDirectResponsibility();
|
||
} catch (error) {
|
||
toastError("Couldn’t open the editor", error, "load this routine’s responsibility");
|
||
return;
|
||
}
|
||
editResponsibilityRef.current = responsibilityId;
|
||
editScheduleIdRef.current = primarySchedule(current.triggers)?.id ?? null;
|
||
setEditInitial(routineToDraft(current, responsibilityId));
|
||
baseRevisionRef.current = { id: current.latestRevisionId, number: current.latestRevisionNumber };
|
||
setSaveConflict(false);
|
||
setSaveError(null);
|
||
setRemovedWebhooks([]);
|
||
setEditOpen(true);
|
||
}, [loadDirectResponsibility, toastError]);
|
||
|
||
const reloadLatest = useCallback(async () => {
|
||
try {
|
||
const fresh = await refreshRoutine();
|
||
const responsibilityId = await loadDirectResponsibility();
|
||
editResponsibilityRef.current = responsibilityId;
|
||
editScheduleIdRef.current = primarySchedule(fresh.triggers)?.id ?? null;
|
||
baseRevisionRef.current = { id: fresh.latestRevisionId, number: fresh.latestRevisionNumber };
|
||
setEditInitial(routineToDraft(fresh, responsibilityId));
|
||
setSaveConflict(false);
|
||
setSaveError(null);
|
||
} catch (error) {
|
||
setSaveError(describeError(error, { action: "reload the routine" }).body);
|
||
}
|
||
}, [loadDirectResponsibility, refreshRoutine]);
|
||
|
||
const restoreWebhook = useMutation({
|
||
mutationFn: (trigger: RoutineTrigger) => routinesApi.updateTrigger(trigger.id, { archived: false }),
|
||
onSuccess: async (_saved, trigger) => {
|
||
setRemovedWebhooks((current) => current.filter((item) => item.id !== trigger.id));
|
||
await refreshRoutine();
|
||
},
|
||
onError: (error) => setSaveError(describeError(error, { action: "restore the webhook" }).body),
|
||
});
|
||
const removeWebhook = useMutation({
|
||
mutationFn: (trigger: RoutineTrigger) => routinesApi.updateTrigger(trigger.id, { archived: true }),
|
||
onSuccess: async (_saved, trigger) => {
|
||
setRemovedWebhooks((current) => [...current, trigger]);
|
||
await refreshRoutine();
|
||
},
|
||
onError: (error) => setSaveError(describeError(error, { action: "remove the webhook" }).body),
|
||
});
|
||
|
||
const onRestoreSecretMaterials = useCallback(
|
||
(response: RestoreRoutineRevisionResponse) => {
|
||
if (response.secretMaterials.length === 0) return;
|
||
navigate(routinePath(routineId));
|
||
const restoredTriggers = new Map(response.revision.snapshot.triggers.map((trigger) => [trigger.id, trigger]));
|
||
webhook.showKeys(response.secretMaterials.map((material) => ({
|
||
triggerId: material.triggerId,
|
||
webhookUrl: material.webhookUrl,
|
||
webhookSecret: material.webhookSecret,
|
||
signingMode: restoredTriggers.get(material.triggerId)?.signingMode ?? null,
|
||
})));
|
||
},
|
||
[navigate, routineId, webhook],
|
||
);
|
||
const onRestored = useCallback(
|
||
(response: RestoreRoutineRevisionResponse) => {
|
||
queryClient.setQueryData<RoutineDetailType | undefined>(detailKey, (previous) =>
|
||
previous
|
||
? { ...previous, ...response.routine, latestRevisionId: response.revision.id, latestRevisionNumber: response.revision.revisionNumber }
|
||
: previous,
|
||
);
|
||
void refreshRoutine().catch(() => undefined);
|
||
},
|
||
[detailKey, queryClient, refreshRoutine],
|
||
);
|
||
|
||
const routineTitle = routine?.title;
|
||
useEffect(() => {
|
||
if (!routineTitle) return;
|
||
setBreadcrumbs(
|
||
view === "routine"
|
||
? [{ label: "Routines", href: "/routines" }, { label: routineTitle }]
|
||
: [
|
||
{ label: "Routines", href: "/routines" },
|
||
{ label: routineTitle, href: routinePath(routineId) },
|
||
{ label: view === "history" ? "Version history" : "Audit log" },
|
||
],
|
||
);
|
||
}, [routineId, routineTitle, setBreadcrumbs, view]);
|
||
|
||
// Header actions sit in the shell header where it has a page toolbar; elsewhere above the page.
|
||
const archived = routine ? routineAutomationState(routine) === "archived" : false;
|
||
const actionsInToolbar = streamlinedUiEnabled && !isMobile;
|
||
const handlers = useRef({
|
||
edit: openEdit,
|
||
run: () => setRunOpen(true),
|
||
versions: () => navigate(routinePath(routineId, "history")),
|
||
audit: () => navigate(routinePath(routineId, "activity")),
|
||
archive: () => setStatus.mutate("archived"),
|
||
restore: () => setStatus.mutate("paused"),
|
||
});
|
||
handlers.current = {
|
||
edit: openEdit,
|
||
run: () => setRunOpen(true),
|
||
versions: () => navigate(routinePath(routineId, "history")),
|
||
audit: () => navigate(routinePath(routineId, "activity")),
|
||
archive: () => setStatus.mutate("archived"),
|
||
restore: () => setStatus.mutate("paused"),
|
||
};
|
||
const hasRoutine = Boolean(routine);
|
||
const runPending = runRoutine.isPending;
|
||
const actions = useMemo(
|
||
() =>
|
||
hasRoutine && view === "routine" ? (
|
||
<RoutinePageActions
|
||
archived={archived}
|
||
runPending={runPending}
|
||
onEdit={() => handlers.current.edit()}
|
||
onRunNow={() => handlers.current.run()}
|
||
onOpenVersionHistory={() => handlers.current.versions()}
|
||
onOpenAuditLog={() => handlers.current.audit()}
|
||
onArchive={() => handlers.current.archive()}
|
||
onRestore={() => handlers.current.restore()}
|
||
/>
|
||
) : null,
|
||
[archived, hasRoutine, runPending, view],
|
||
);
|
||
useEffect(() => {
|
||
if (!actionsInToolbar) return;
|
||
setBreadcrumbToolbar(actions);
|
||
return () => setBreadcrumbToolbar(null);
|
||
}, [actions, actionsInToolbar, setBreadcrumbToolbar]);
|
||
|
||
const routineProjectId = routine?.projectId ?? null;
|
||
const routineAgentId = routine?.assigneeAgentId ?? null;
|
||
// Stable between refreshes, so the form's pickers do not rebuild their indexes on every poll.
|
||
const agents = useMemo(
|
||
() => withCurrent<RoutinePageAgent>(options.agents, options.allAgents, routineAgentId),
|
||
[options.agents, options.allAgents, routineAgentId],
|
||
);
|
||
const projects = useMemo(
|
||
() => withCurrent<RoutinePageProject>(options.projects, options.allProjects, routineProjectId),
|
||
[options.projects, options.allProjects, routineProjectId],
|
||
);
|
||
const responsibilityChoices = useMemo(
|
||
() => buildRoutineResponsibilityChoices({
|
||
links: linkEntries ?? [],
|
||
options: options.responsibilities ?? [],
|
||
areaTitles: options.areaTitles,
|
||
projectId: routineProjectId,
|
||
}),
|
||
[linkEntries, options.areaTitles, options.responsibilities, routineProjectId],
|
||
);
|
||
|
||
// Keep a loaded routine on screen through a failed refetch; only a real
|
||
// failure, or an outage before the first load, replaces the page.
|
||
if (routineView.kind === "loading" || routineView.kind === "reconnecting") {
|
||
return <PageSkeleton variant="issues-list" />;
|
||
}
|
||
if (routineView.kind === "error" && !routine) {
|
||
return (
|
||
<QueryErrorState
|
||
size="page"
|
||
error={routineView.error}
|
||
action="load this routine"
|
||
onRetry={routineView.retry}
|
||
retrying={routineView.isFetching}
|
||
/>
|
||
);
|
||
}
|
||
if (!routine) return <EmptyState icon={AlertCircle} message="We couldn't load this routine." />;
|
||
|
||
const agent = options.allAgents.find((candidate) => candidate.id === routine.assigneeAgentId)
|
||
?? (routine.assignee ? { id: routine.assignee.id, name: routine.assignee.name, urlKey: routine.assignee.urlKey } : null);
|
||
const project = options.allProjects.find((candidate) => candidate.id === routine.projectId)
|
||
?? (routine.project ? { id: routine.project.id, name: routine.project.name } : null);
|
||
const responsibleUser = routine.responsibleUserId
|
||
? options.people.find((person) => person.id === routine.responsibleUserId)
|
||
?? (options.peopleFailed || options.people.length === 0 ? { id: routine.responsibleUserId, name: options.peopleFailed ? "Couldn’t load this person" : "Loading…" } : { id: routine.responsibleUserId, name: "Former member" })
|
||
: null;
|
||
const pageResponsibilities = options.responsibilitiesOn ? responsibilityChoices.onPage : undefined;
|
||
const responsibilityOptions = options.responsibilities ? responsibilityChoices.choices : undefined;
|
||
const loadErrors = [
|
||
// The routine stays on screen through a failed refresh; say so instead of showing stale data silently.
|
||
...(routineView.kind === "error"
|
||
? [{ action: "refresh this routine", error: routineView.error, retry: routineView.retry, retrying: routineView.isFetching }]
|
||
: []),
|
||
...options.loadErrors,
|
||
...(linksView.kind === "error"
|
||
? [{ action: "load this routine’s responsibilities", error: linksView.error, retry: linksView.retry, retrying: linksView.isFetching }]
|
||
: []),
|
||
];
|
||
const webhooks = routine.triggers.filter((trigger) => trigger.kind === "webhook");
|
||
const routineActive = routineAutomationState(routine) === "active";
|
||
const webhookDialog = (
|
||
<RoutineWebhookDialog {...webhook.dialogProps} routineTitle={routine.title} routineActive={routineActive} />
|
||
);
|
||
const descriptionDocument = routine.descriptionDocument;
|
||
|
||
const page =
|
||
view === "routine" ? (
|
||
<RoutinePage
|
||
routine={routine}
|
||
agent={agent}
|
||
project={project}
|
||
responsibleUser={responsibleUser}
|
||
responsibilities={pageResponsibilities}
|
||
history={history}
|
||
historyLoading={historyView.kind === "loading" || historyView.kind === "reconnecting"}
|
||
historyError={
|
||
historyView.kind === "error" ? (
|
||
<QueryErrorState
|
||
size="inline"
|
||
error={historyView.error}
|
||
action="load this routine’s tasks"
|
||
onRetry={historyView.retry}
|
||
retrying={historyView.isFetching}
|
||
/>
|
||
) : null
|
||
}
|
||
hasMoreHistory={Boolean(historyQuery.hasNextPage)}
|
||
loadingMoreHistory={historyQuery.isFetchingNextPage}
|
||
onLoadMoreHistory={() => void historyQuery.fetchNextPage()}
|
||
historyLinkState={createIssueDetailLocationState(routine.title, routinePath(routine.id), "issues")}
|
||
skippedRunCount={routine.recentRuns.filter((run) => run.status === "coalesced").length}
|
||
instructionCommentCount={openCommentCount}
|
||
onOpenInstructionComments={() => setCommentsOpen(true)}
|
||
instructionsExpanded={commentsOpen}
|
||
renderInstructionsBody={
|
||
descriptionDocument
|
||
? (body) => (
|
||
<IssueDocumentAnnotations
|
||
issueId={routine.id}
|
||
doc={descriptionDocument}
|
||
target={annotationTarget}
|
||
bodyMarkdown={routine.description ?? ""}
|
||
draftDirty={false}
|
||
draftConflicted={false}
|
||
historicalPreview={false}
|
||
locationHash={locationHash}
|
||
panelOpen={commentsOpen}
|
||
onPanelOpenChange={setCommentsOpen}
|
||
>
|
||
{body}
|
||
</IssueDocumentAnnotations>
|
||
)
|
||
: undefined
|
||
}
|
||
statusPending={setStatus.isPending}
|
||
onToggleActive={(active) => setStatus.mutate(active ? "active" : "paused")}
|
||
onEdit={openEdit}
|
||
onSaveSchedule={(id, change) => {
|
||
const trigger = triggerById(id);
|
||
if (trigger) changeTrigger.mutate({ trigger, patch: change, success: "Schedule saved" });
|
||
}}
|
||
onRemoveSchedule={(id) => {
|
||
const trigger = triggerById(id);
|
||
if (trigger) changeTrigger.mutate({ trigger, patch: { archived: true }, success: "Schedule removed" });
|
||
}}
|
||
onSetTriggerPaused={(id, paused) => {
|
||
const trigger = triggerById(id);
|
||
if (trigger) changeTrigger.mutate({ trigger, patch: { enabled: !paused }, success: paused ? "Paused" : "Resumed" });
|
||
}}
|
||
onRemoveTrigger={(trigger) => changeTrigger.mutate({ trigger, patch: { archived: true }, success: "Removed" })}
|
||
removedTriggers={pageRemoved}
|
||
onRestoreTrigger={(trigger) => changeTrigger.mutate({ trigger, patch: { archived: false }, success: "Restored" })}
|
||
onOpenWebhook={(trigger) => webhook.show(trigger)}
|
||
/>
|
||
) : (
|
||
<RoutineSubpage
|
||
routineTitle={routine.title}
|
||
title={view === "history" ? "Version history" : "Audit log"}
|
||
onBack={() => navigate(routinePath(routine.id))}
|
||
>
|
||
{view === "history" ? (
|
||
<RoutineHistoryTab
|
||
routine={routine}
|
||
isEditDirty={false}
|
||
dirtyFields={[]}
|
||
onDiscardEdits={() => {}}
|
||
onSaveEdits={() => {}}
|
||
agents={new Map(options.allAgents.map((candidate) => [candidate.id, candidate]))}
|
||
projects={new Map(options.allProjects.map((candidate) => [candidate.id, candidate]))}
|
||
secrets={options.secrets}
|
||
onRestoreSecretMaterials={onRestoreSecretMaterials}
|
||
onRestored={onRestored}
|
||
/>
|
||
) : (
|
||
<RoutineAuditActivity companyId={companyId} routineId={routine.id} />
|
||
)}
|
||
</RoutineSubpage>
|
||
);
|
||
|
||
return (
|
||
<div className="-m-4 md:-m-6">
|
||
{!actionsInToolbar && actions ? <div className="flex justify-end px-4 pt-3 md:px-8">{actions}</div> : null}
|
||
{loadErrors.length > 0 ? (
|
||
<div className="mx-auto flex w-full max-w-6xl flex-col gap-2 px-4 pt-4 md:px-8">
|
||
{loadErrors.map((failure) => (
|
||
<QueryErrorState
|
||
key={failure.action}
|
||
size="inline"
|
||
error={failure.error}
|
||
action={failure.action}
|
||
onRetry={failure.retry}
|
||
retrying={failure.retrying}
|
||
/>
|
||
))}
|
||
</div>
|
||
) : null}
|
||
{page}
|
||
|
||
{editInitial ? (
|
||
<RoutineEditDialog
|
||
open={editOpen}
|
||
onOpenChange={(open) => {
|
||
if (open) return;
|
||
if (webhook.open) void webhook.close();
|
||
setEditOpen(false);
|
||
setRemovedWebhooks([]);
|
||
}}
|
||
initial={editInitial}
|
||
agents={agents}
|
||
projects={projects}
|
||
people={options.people}
|
||
secrets={options.secrets}
|
||
responsibilities={responsibilityOptions}
|
||
mentions={options.mentions}
|
||
canChangeResponsibleUser={routine.canChangeResponsibleUser !== false}
|
||
saving={saveEdits.isPending}
|
||
saveConflict={saveConflict}
|
||
error={saveError ?? (loadErrors.length > 0 ? "Some choices couldn’t be loaded. Close this and use Retry on the page." : null)}
|
||
onSave={(draft) => saveEdits.mutate(draft)}
|
||
onReloadLatest={() => void reloadLatest()}
|
||
onCreateSecret={options.createSecret}
|
||
webhooks={webhooks}
|
||
onOpenWebhook={(trigger) => webhook.show(trigger)}
|
||
onRemoveWebhook={(trigger) => removeWebhook.mutate(trigger)}
|
||
removedWebhooks={removedWebhooks}
|
||
onRestoreWebhook={(trigger) => restoreWebhook.mutate(trigger)}
|
||
>
|
||
{editOpen ? webhookDialog : null}
|
||
</RoutineEditDialog>
|
||
) : null}
|
||
|
||
{editOpen ? null : webhookDialog}
|
||
|
||
<RoutineRunVariablesDialog
|
||
open={runOpen}
|
||
onOpenChange={setRunOpen}
|
||
companyId={routine.companyId}
|
||
routineName={routine.title}
|
||
agents={options.allAgents}
|
||
projects={options.allProjects}
|
||
defaultProjectId={routine.projectId}
|
||
defaultAssigneeAgentId={routine.assigneeAgentId}
|
||
variables={routine.variables ?? []}
|
||
isPending={runRoutine.isPending}
|
||
onSubmit={(data) => runRoutine.mutate(data)}
|
||
/>
|
||
</div>
|
||
);
|
||
}
|