-
Notifications
You must be signed in to change notification settings - Fork 48
feat: Cancel specific run from dashboard #313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,111 @@ | ||||||||||||||
| import { | ||||||||||||||
| AlertDialog, | ||||||||||||||
| AlertDialogAction, | ||||||||||||||
| AlertDialogCancel, | ||||||||||||||
| AlertDialogContent, | ||||||||||||||
| AlertDialogDescription, | ||||||||||||||
| AlertDialogFooter, | ||||||||||||||
| AlertDialogHeader, | ||||||||||||||
| AlertDialogTitle, | ||||||||||||||
| } from "@/components/ui/alert-dialog"; | ||||||||||||||
| import { Button } from "@/components/ui/button"; | ||||||||||||||
| import { cancelWorkflowRunServerFn } from "@/lib/api"; | ||||||||||||||
| import { isRunCancelableStatus } from "@/lib/status"; | ||||||||||||||
| import type { WorkflowRunStatus } from "openworkflow/internal"; | ||||||||||||||
| import { useState } from "react"; | ||||||||||||||
|
|
||||||||||||||
| interface RunCancelActionProps { | ||||||||||||||
| runId: string; | ||||||||||||||
| status: WorkflowRunStatus; | ||||||||||||||
| onCanceled?: (() => Promise<void>) | (() => void); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| function getErrorMessage(error: unknown): string { | ||||||||||||||
| if (error instanceof Error && error.message) { | ||||||||||||||
| return error.message; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| return "Unable to cancel workflow run"; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| export function RunCancelAction({ | ||||||||||||||
| runId, | ||||||||||||||
| status, | ||||||||||||||
| onCanceled, | ||||||||||||||
| }: RunCancelActionProps) { | ||||||||||||||
| const [isOpen, setIsOpen] = useState(false); | ||||||||||||||
| const [isCanceling, setIsCanceling] = useState(false); | ||||||||||||||
| const [error, setError] = useState<string | null>(null); | ||||||||||||||
|
|
||||||||||||||
| if (!isRunCancelableStatus(status)) { | ||||||||||||||
| return null; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| async function cancelRun() { | ||||||||||||||
| setIsCanceling(true); | ||||||||||||||
| setError(null); | ||||||||||||||
|
|
||||||||||||||
| try { | ||||||||||||||
| await cancelWorkflowRunServerFn({ | ||||||||||||||
| data: { | ||||||||||||||
| workflowRunId: runId, | ||||||||||||||
| }, | ||||||||||||||
| }); | ||||||||||||||
| await onCanceled?.(); | ||||||||||||||
| setIsOpen(false); | ||||||||||||||
| } catch (caughtError) { | ||||||||||||||
| setError(getErrorMessage(caughtError)); | ||||||||||||||
| } finally { | ||||||||||||||
| setIsCanceling(false); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| return ( | ||||||||||||||
| <AlertDialog | ||||||||||||||
| open={isOpen} | ||||||||||||||
| onOpenChange={(nextOpen) => { | ||||||||||||||
| setIsOpen(nextOpen); | ||||||||||||||
| if (!nextOpen) { | ||||||||||||||
| setError(null); | ||||||||||||||
| } | ||||||||||||||
| }} | ||||||||||||||
| > | ||||||||||||||
| <Button | ||||||||||||||
| type="button" | ||||||||||||||
| variant="destructive" | ||||||||||||||
| onClick={() => { | ||||||||||||||
| setIsOpen(true); | ||||||||||||||
| }} | ||||||||||||||
| disabled={isCanceling} | ||||||||||||||
| > | ||||||||||||||
| Cancel Run | ||||||||||||||
| </Button> | ||||||||||||||
|
|
||||||||||||||
| <AlertDialogContent> | ||||||||||||||
| <AlertDialogHeader> | ||||||||||||||
| <AlertDialogTitle>Cancel this run?</AlertDialogTitle> | ||||||||||||||
| <AlertDialogDescription> | ||||||||||||||
| This will stop any future progress for this workflow run. | ||||||||||||||
| </AlertDialogDescription> | ||||||||||||||
| </AlertDialogHeader> | ||||||||||||||
|
|
||||||||||||||
| {error && <p className="text-destructive text-xs">{error}</p>} | ||||||||||||||
|
||||||||||||||
| {error && <p className="text-destructive text-xs">{error}</p>} | |
| {error && ( | |
| <p role="alert" aria-live="assertive" className="text-destructive text-xs"> | |
| {error} | |
| </p> | |
| )} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
onCanceled?: (() => Promise<void>) | (() => void)can be simplified to a single signature like() => void | Promise<void>, which is easier to read and avoids an unnecessary union of function types.