-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add dropdown for components to submit playbook runs
Build on top of #1352 PR Closes #1353 fix: close modal on save fix: fix submit relaoding full page and wrong endpoint fix: fix playbook submission params
- Loading branch information
1 parent
7938b56
commit 367a9b6
Showing
9 changed files
with
321 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { useMemo } from "react"; | ||
import FormikTextInput from "../../Forms/Formik/FormikTextInput"; | ||
import { PlaybookSpec } from "../Settings/PlaybookSpecsTable"; | ||
|
||
type AddPlaybookToRunParamsProps = { | ||
playbookSpec: PlaybookSpec; | ||
}; | ||
|
||
export default function AddPlaybookToRunParams({ | ||
playbookSpec | ||
}: AddPlaybookToRunParamsProps) { | ||
const playbookParams = useMemo( | ||
() => | ||
(playbookSpec.spec.parameters as { name: string; label: string }[]) ?? [], | ||
[playbookSpec] | ||
); | ||
|
||
return ( | ||
<div className="flex flex-col gap-4"> | ||
<div className="font-bold">Playbook Run Params</div> | ||
<div className="flex flex-col gap-2"> | ||
{playbookParams.length > 0 ? ( | ||
playbookParams.map(({ name, label }) => ( | ||
<FormikTextInput name={`params.${name}`} label={label} /> | ||
)) | ||
) : ( | ||
<div className="text-gray-400">No parameters for this playbook.</div> | ||
)} | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { Fragment, useState } from "react"; | ||
import { FaChevronDown, FaChevronUp } from "react-icons/fa"; | ||
import { useGetPlaybooksToRun } from "../../../api/query-hooks/playbooks"; | ||
import { Button } from "../../Button"; | ||
import SubmitPlaybookRunForm from "./SubmitPlaybookRunForm"; | ||
import { Menu } from "../../Menu"; | ||
import { PlaybookSpec } from "../Settings/PlaybookSpecsTable"; | ||
|
||
type SelectPlaybookToRunProps = { | ||
component_id?: string; | ||
config_id?: string; | ||
check_id?: string; | ||
}; | ||
|
||
export default function SelectPlaybookToRun({ | ||
check_id, | ||
component_id, | ||
config_id | ||
}: SelectPlaybookToRunProps) { | ||
const [selectedPlaybookSpec, setSelectedPlaybookSpec] = | ||
useState<PlaybookSpec>(); | ||
|
||
const { | ||
data: playbooks, | ||
isLoading, | ||
error | ||
} = useGetPlaybooksToRun({ | ||
check_id, | ||
component_id, | ||
config_id | ||
}); | ||
|
||
if (error || playbooks?.length === 0 || isLoading) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div className="flex flex-col items-center p-1 relative"> | ||
<Menu> | ||
<Menu.Button as={Fragment}> | ||
{({ open }) => ( | ||
<div className="flex items-center"> | ||
<Button | ||
text={ | ||
<div className="flex flex-row gap-2"> | ||
<span>Playbooks</span> | ||
{!open ? <FaChevronDown /> : <FaChevronUp />} | ||
</div> | ||
} | ||
className="text-sm btn-white" | ||
disabled={isLoading} | ||
/> | ||
</div> | ||
)} | ||
</Menu.Button> | ||
<Menu.Items> | ||
{playbooks?.map((playbook) => ( | ||
<Menu.Item | ||
as="button" | ||
onClick={() => setSelectedPlaybookSpec(playbook)} | ||
key={playbook.id} | ||
> | ||
{playbook.name} | ||
</Menu.Item> | ||
))} | ||
</Menu.Items> | ||
</Menu> | ||
{selectedPlaybookSpec && ( | ||
<SubmitPlaybookRunForm | ||
type="component" | ||
componentId={component_id!} | ||
isOpen={!!selectedPlaybookSpec} | ||
onClose={() => setSelectedPlaybookSpec(undefined)} | ||
playbookSpec={selectedPlaybookSpec} | ||
/> | ||
)} | ||
</div> | ||
); | ||
} |
116 changes: 116 additions & 0 deletions
116
src/components/Playbooks/Runs/SubmitPlaybookRunForm.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { Form, Formik } from "formik"; | ||
import SelectPlaybookToRun from "./SelectPlaybookToRun"; | ||
import { useMemo } from "react"; | ||
import AddPlaybookToRunParams from "./AddPlaybookToRunParams"; | ||
import { Modal } from "../../Modal"; | ||
import { Button } from "../../Button"; | ||
import { useSubmitPlaybookRunMutation } from "../../../api/query-hooks/playbooks"; | ||
import { toastError } from "../../Toast/toast"; | ||
import { PlaybookSpec } from "../Settings/PlaybookSpecsTable"; | ||
|
||
export type SubmitPlaybookRunFormValues = { | ||
// if this is present in the form, we show step to add params | ||
id: string; | ||
component_id?: string; | ||
config_id?: string; | ||
params?: Record<string, any>; | ||
// do not send this to the backend | ||
playbook_spec?: Record<string, any>; | ||
}; | ||
|
||
type Props = { | ||
isOpen: boolean; | ||
onClose: () => void; | ||
playbookSpec: PlaybookSpec; | ||
}; | ||
|
||
type SubmitPlaybookRunFormForComponentProps = { | ||
type: "component"; | ||
componentId: string; | ||
} & Props; | ||
|
||
type SubmitPlaybookRunFormForConfigProps = { | ||
type: "config"; | ||
configId: string; | ||
} & Props; | ||
|
||
type SubmitPlaybookRunFormForCheckProps = { | ||
type: "check"; | ||
checkId: string; | ||
} & Props; | ||
|
||
type SubmitPlaybookRunFormProps = | ||
| SubmitPlaybookRunFormForComponentProps | ||
| SubmitPlaybookRunFormForConfigProps | ||
| SubmitPlaybookRunFormForCheckProps; | ||
|
||
export default function SubmitPlaybookRunForm({ | ||
isOpen, | ||
onClose, | ||
playbookSpec, | ||
...props | ||
}: SubmitPlaybookRunFormProps) { | ||
const initialValues: Partial<SubmitPlaybookRunFormValues> = useMemo( | ||
() => ({ | ||
playbook_id: playbookSpec.id, | ||
params: undefined, | ||
|
||
...(props.type === "component" | ||
? { component_id: props.componentId } | ||
: props.type === "check" | ||
? { | ||
check_id: props.checkId | ||
} | ||
: { | ||
config_id: props.configId | ||
}) | ||
}), | ||
[props, playbookSpec] | ||
); | ||
|
||
const { mutate: submitPlaybookRun } = useSubmitPlaybookRunMutation({ | ||
onSuccess: () => { | ||
toastError("Playbook run submitted successfully"); | ||
onClose(); | ||
}, | ||
onError: (error) => { | ||
toastError(error.message); | ||
} | ||
}); | ||
|
||
return ( | ||
<Modal | ||
title="Submit a Playbook Run" | ||
open={isOpen} | ||
onClose={onClose} | ||
size="slightly-small" | ||
bodyClass="" | ||
containerClassName="" | ||
> | ||
<Formik | ||
initialValues={initialValues} | ||
onSubmit={(values) => { | ||
const { playbook_spec, ...rest } = values; | ||
submitPlaybookRun(rest as SubmitPlaybookRunFormValues); | ||
}} | ||
> | ||
{({ values, handleSubmit }) => { | ||
return ( | ||
<Form onSubmit={handleSubmit} className="flex flex-col gap-2"> | ||
<div className="flex p-4 flex-col gap-2"> | ||
<AddPlaybookToRunParams playbookSpec={playbookSpec} /> | ||
</div> | ||
<div className="flex flex-row p-4 justify-end bg-gray-200 rounded-b rounded-md"> | ||
<Button | ||
disabled={values.id === undefined} | ||
text="Submit" | ||
type="submit" | ||
/> | ||
</div> | ||
</Form> | ||
); | ||
}} | ||
</Formik> | ||
</Modal> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.