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, useDuplicateSelected, 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 }), ) const duplicate = useDuplicateSelected( (flow) => FlowsService.duplicateFlow({ name: flow }), "flow", () => { selection.clear() queryClient.invalidateQueries({ queryKey: flowKeys.all }) }, ) return (
A flow is a handful of nodes passing messages to each other. Keep them small and name them after what they do.
{needle ? "No flow matches that." : "No flows yet. Create one to start."}
) : (