DELETE /runs?flow= removes every Run row of the flow but left its flow_run rows, so a deleted flow kept appearing in the history. history=true now drops those too, and the flow delete dialogs (editor and bulk) send it when "Also delete its runs" is ticked. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WgBfkm4rCmtd1T4vHWjEPx
216 lines
7.0 KiB
TypeScript
216 lines
7.0 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"
|
|
import { createFileRoute, useNavigate } from "@tanstack/react-router"
|
|
import { Workflow } from "lucide-react"
|
|
import { useState } from "react"
|
|
|
|
import { FlowsService, RunsService } from "@/client"
|
|
import { OverviewCard, useSelection } from "@/components/Common/OverviewCard"
|
|
import {
|
|
ConfirmDelete,
|
|
OverviewToolbar,
|
|
useDeleteSelected,
|
|
usePublishAll,
|
|
} from "@/components/Common/OverviewToolbar"
|
|
import { AlsoDeleteRuns } from "@/components/Flow/FlowPanel"
|
|
import { flowKeys, flowsQueryOptions } from "@/components/Flow/queries"
|
|
import { Button } from "@/components/ui/button"
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogDescription,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
} from "@/components/ui/dialog"
|
|
import { Input } from "@/components/ui/input"
|
|
import { Skeleton } from "@/components/ui/skeleton"
|
|
import useCustomToast from "@/hooks/useCustomToast"
|
|
import { handleError } from "@/utils"
|
|
|
|
export const Route = createFileRoute("/_layout/flows/")({
|
|
component: Flows,
|
|
head: () => ({ meta: [{ title: "Flows - Fluksio" }] }),
|
|
})
|
|
|
|
/** The shape the store accepts, so a bad name is caught before the request. */
|
|
const NAME = /^[a-z][a-z0-9_]*$/
|
|
|
|
function Flows() {
|
|
const { data, isPending } = useQuery(flowsQueryOptions())
|
|
const queryClient = useQueryClient()
|
|
const navigate = useNavigate()
|
|
const { showErrorToast } = useCustomToast()
|
|
const [name, setName] = useState("")
|
|
const [search, setSearch] = useState("")
|
|
const [dialogOpen, setDialogOpen] = useState(false)
|
|
const [deleteOpen, setDeleteOpen] = useState(false)
|
|
const [withRuns, setWithRuns] = useState(false)
|
|
|
|
const create = useMutation({
|
|
mutationFn: (flow: string) =>
|
|
FlowsService.saveFlow({
|
|
name: flow,
|
|
requestBody: { name: flow, nodes: [], inputs: [] },
|
|
}),
|
|
onSuccess: (_saved, flow) => {
|
|
queryClient.invalidateQueries({ queryKey: flowKeys.all })
|
|
setDialogOpen(false)
|
|
navigate({ to: "/flows/$flowName", params: { flowName: flow } })
|
|
},
|
|
onError: handleError.bind(showErrorToast),
|
|
})
|
|
|
|
// The summary carries the working copy's version, which is the one publishing
|
|
// is based on — so the list already holds everything this needs.
|
|
const publishAll = usePublishAll(
|
|
(flow) =>
|
|
FlowsService.publishFlow({
|
|
name: flow,
|
|
requestBody: {
|
|
version: data?.data.find((f) => f.name === flow)?.version ?? 1,
|
|
},
|
|
}),
|
|
"flow",
|
|
() => queryClient.invalidateQueries({ queryKey: flowKeys.all }),
|
|
)
|
|
|
|
const slug = name
|
|
.trim()
|
|
.toLowerCase()
|
|
.replace(/[^a-z0-9]+/g, "_")
|
|
const needle = search.trim().toLowerCase()
|
|
const flows = (data?.data ?? []).filter((flow) =>
|
|
`${flow.name} ${flow.title ?? ""}`.toLowerCase().includes(needle),
|
|
)
|
|
const drafts = (data?.data ?? [])
|
|
.filter((flow) => flow.has_draft)
|
|
.map((flow) => flow.name)
|
|
|
|
const selection = useSelection(flows.map((flow) => flow.name))
|
|
const remove = useDeleteSelected(
|
|
async (flow) => {
|
|
await FlowsService.deleteFlow({ name: flow })
|
|
if (withRuns) await RunsService.deleteRuns({ flow, history: true })
|
|
},
|
|
"flow",
|
|
() => queryClient.invalidateQueries({ queryKey: flowKeys.all }),
|
|
)
|
|
|
|
return (
|
|
<div className="grid gap-6">
|
|
<div className="grid gap-1">
|
|
<h1 className="text-2xl">Flows</h1>
|
|
<p className="text-sm text-muted-foreground">
|
|
A flow is a handful of nodes passing messages to each other. Keep them
|
|
small and name them after what they do.
|
|
</p>
|
|
</div>
|
|
|
|
<Dialog open={dialogOpen} onOpenChange={setDialogOpen}>
|
|
<OverviewToolbar
|
|
search={search}
|
|
onSearch={setSearch}
|
|
searchLabel="Search flows"
|
|
searchTestId="search-flows"
|
|
createLabel="New flow"
|
|
createTestId="new-flow"
|
|
draftCount={drafts.length}
|
|
publishing={publishAll.isPending}
|
|
onPublishAll={() => publishAll.mutate(drafts)}
|
|
selectedCount={selection.selected.length}
|
|
onDeleteSelected={() => {
|
|
setWithRuns(false)
|
|
setDeleteOpen(true)
|
|
}}
|
|
/>
|
|
<DialogContent>
|
|
<form
|
|
className="grid gap-4"
|
|
onSubmit={(event) => {
|
|
event.preventDefault()
|
|
if (NAME.test(slug)) create.mutate(slug)
|
|
}}
|
|
>
|
|
<DialogHeader>
|
|
<DialogTitle>New flow</DialogTitle>
|
|
<DialogDescription>
|
|
Flows are small on purpose. Name this one after what it does.
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
<Input
|
|
value={name}
|
|
placeholder="heating"
|
|
aria-label="New flow name"
|
|
autoComplete="off"
|
|
data-testid="new-flow-name"
|
|
onChange={(event) => setName(event.target.value)}
|
|
/>
|
|
<DialogFooter>
|
|
<Button
|
|
type="submit"
|
|
disabled={!NAME.test(slug) || create.isPending}
|
|
data-testid="create-flow"
|
|
>
|
|
Create flow
|
|
</Button>
|
|
</DialogFooter>
|
|
</form>
|
|
</DialogContent>
|
|
</Dialog>
|
|
|
|
<ConfirmDelete
|
|
open={deleteOpen}
|
|
onOpenChange={setDeleteOpen}
|
|
names={selection.selected}
|
|
noun="flow"
|
|
pending={remove.isPending}
|
|
onConfirm={() => {
|
|
setDeleteOpen(false)
|
|
remove.mutate(selection.selected)
|
|
}}
|
|
>
|
|
<AlsoDeleteRuns
|
|
checked={withRuns}
|
|
onCheckedChange={setWithRuns}
|
|
plural={selection.selected.length > 1}
|
|
/>
|
|
</ConfirmDelete>
|
|
|
|
{isPending ? (
|
|
<div className="grid gap-3 sm:grid-cols-2 lg:grid-cols-3">
|
|
{Array.from({ length: 3 }).map((_, index) => (
|
|
<Skeleton key={index} className="h-[5.5rem] rounded-lg" />
|
|
))}
|
|
</div>
|
|
) : flows.length === 0 ? (
|
|
<p className="text-sm text-muted-foreground">
|
|
{needle
|
|
? "No flow matches that."
|
|
: "No flows yet. Create one to start."}
|
|
</p>
|
|
) : (
|
|
<div className="grid gap-3 sm:grid-cols-2 lg:grid-cols-3">
|
|
{flows.map((flow) => (
|
|
<OverviewCard
|
|
key={flow.name}
|
|
link={{ to: "/flows/$flowName", params: { flowName: flow.name } }}
|
|
icon={Workflow}
|
|
title={flow.title || flow.name}
|
|
draft={flow.has_draft}
|
|
detail={`${flow.node_count ?? 0} node${
|
|
flow.node_count === 1 ? "" : "s"
|
|
} · ${flow.enabled === false ? "stopped" : "running"}${
|
|
flow.error_count ? ` · ${flow.error_count} to fix` : ""
|
|
}`}
|
|
selecting={selection.selecting}
|
|
selected={selection.selected.includes(flow.name)}
|
|
onToggle={() => selection.toggle(flow.name)}
|
|
testId="flow-card"
|
|
/>
|
|
))}
|
|
</div>
|
|
)}
|
|
</div>
|
|
)
|
|
}
|