Skip to content

Fully autonomous AI Agents system capable of performing complex penetration testing tasks

refactor(frontend): make FormSubmitButton fully accept Button's API

Sergey Kozyrenko1eb3ae1389d5
diff --git a/frontend/src/components/ui/form-submit-button.tsx b/frontend/src/components/ui/form-submit-button.tsx
index c13ab5ce..6d501db5 100644
--- a/frontend/src/components/ui/form-submit-button.tsx
+++ b/frontend/src/components/ui/form-submit-button.tsx
@@ -5,30 +5,56 @@ import { Button } from '@/components/ui/button';
interface FormSubmitButtonProps extends React.ComponentProps<typeof Button> {
/**
- * When true (the default), the button is disabled as soon as the form has
- * been submitted once with invalid values — i.e. mirrors the "click first,
- * see errors, then re-disable" UX our forms already use. Pass `false` for
- * flows that must accept clicks even with invalid state (e.g. multi-step
- * forms where submit also runs validation manually).
+ * Icon rendered to the left of `children` while the form is idle. During
+ * submit it is swapped for a spinner so the button keeps its size. Omit
+ * for plain text buttons — the spinner is then prepended in front of
+ * `children` only while submitting.
+ */
+ icon?: React.ReactNode;
+ /**
+ * External submit-pending override. Wins over `formState.isSubmitting`
+ * from the react-hook-form context. Use when the submit lives outside
+ * `<FormProvider>` (attached via the HTML `form="…"` attribute) or when
+ * the parent juggles multiple mutation flags that can't be reduced to a
+ * single `isSubmitting`.
+ */
+ loading?: boolean;
+ /**
+ * When true (default), the button disables itself once the form has been
+ * submitted with invalid values — mirrors our "click first, see errors,
+ * then re-disable" UX. Pass `false` for flows that must accept further
+ * submits even when validity has not yet been recomputed (e.g. forms that
+ * run validation manually).
*/
requireValid?: boolean;
}
/**
- * Submit button that reads the pending/validity state of the nearest
- * react-hook-form context. Drops in next to a <Form {...form}> wrapper without
- * any prop drilling — the button shows a spinner while form.handleSubmit(...)
- * is awaiting and disables itself once the form is dirty-but-invalid.
+ * Submit button that subscribes to the nearest react-hook-form's
+ * `formState.isSubmitting` (and `isValid` / `isSubmitted`) on its own — no
+ * prop drilling. Accepts every prop the underlying `<Button>` does
+ * (`variant`, `size`, `className`, `onClick`, `aria-label`, `form`, …) so it
+ * works for plain text buttons, icon-only buttons, destructive actions, and
+ * the `form="…"`-attached submits that sit outside the FormProvider tree.
*
- * This is the RHF-flavoured analogue of React 19's useFormStatus — same idea
- * (child component subscribes to the form's status), different source
- * (useFormContext vs the form-action runtime) because our forms run through
- * react-hook-form's handleSubmit, not native <form action>.
+ * This is the react-hook-form-flavoured analogue of React 19's
+ * `useFormStatus`: same idea (child component subscribes to the form
+ * status), different source — our forms run through
+ * `form.handleSubmit(...)`, not native `<form action>`.
*/
-function FormSubmitButton({ children, requireValid = true, ...props }: FormSubmitButtonProps) {
- const { formState } = useFormContext();
- const { isSubmitted, isSubmitting, isValid } = formState;
- const isDisabled = isSubmitting || (requireValid && isSubmitted && !isValid);
+function FormSubmitButton({ children, icon, loading, requireValid = true, ...props }: FormSubmitButtonProps) {
+ // `useFormContext` is safe to call outside a FormProvider — it returns
+ // `null` instead of throwing. We branch on that so the same component
+ // can drive both in-tree submits (read state from context) and
+ // out-of-tree submits attached via `form="…"` (caller passes `loading`).
+ const ctx = useFormContext();
+ const formState = ctx?.formState;
+
+ const isSubmitting = loading ?? formState?.isSubmitting ?? false;
+ const isFormInvalid = !!formState && requireValid && formState.isSubmitted && !formState.isValid;
+ const isDisabled = isSubmitting || isFormInvalid;
+
+ const renderedIcon = isSubmitting ? <Loader2 className="size-4 animate-spin" /> : icon;
return (
<Button
@@ -36,7 +62,7 @@ function FormSubmitButton({ children, requireValid = true, ...props }: FormSubmi
type="submit"
{...props}
>
- {isSubmitting && <Loader2 className="size-4 animate-spin" />}
+ {renderedIcon}
{children}
</Button>
);
diff --git a/frontend/src/features/resources/resources-mkdir-dialog.tsx b/frontend/src/features/resources/resources-mkdir-dialog.tsx
index 64ece8d9..448a2f16 100644
--- a/frontend/src/features/resources/resources-mkdir-dialog.tsx
+++ b/frontend/src/features/resources/resources-mkdir-dialog.tsx
@@ -119,10 +119,7 @@ function ResourcesMkdirDialogForm({ defaultParentPath, onClose }: ResourcesMkdir
>
Cancel
</Button>
- <FormSubmitButton>
- <FolderPlus />
- Create
- </FormSubmitButton>
+ <FormSubmitButton icon={<FolderPlus />}>Create</FormSubmitButton>
</div>
</form>
</Form>
diff --git a/frontend/src/pages/settings/settings-prompt.tsx b/frontend/src/pages/settings/settings-prompt.tsx
index 0be6c081..06b3e729 100644
--- a/frontend/src/pages/settings/settings-prompt.tsx
+++ b/frontend/src/pages/settings/settings-prompt.tsx
@@ -26,6 +26,7 @@ import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert';
import { Button } from '@/components/ui/button';
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog';
import { Form, FormControl, FormItem, FormLabel, FormMessage } from '@/components/ui/form';
+import { FormSubmitButton } from '@/components/ui/form-submit-button';
import { StatusCard } from '@/components/ui/status-card';
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
import { Textarea } from '@/components/ui/textarea';
@@ -901,26 +902,24 @@ function SettingsPrompt() {
Cancel
</Button>
{activeTab === 'system' && (
- <Button
- disabled={isLoading}
+ <FormSubmitButton
form="system-prompt-form"
- type="submit"
+ icon={<Save className="size-4" />}
+ loading={isLoading}
variant="secondary"
>
- {isLoading ? <Loader2 className="size-4 animate-spin" /> : <Save className="size-4" />}
{isLoading ? 'Saving...' : 'Save Changes'}
- </Button>
+ </FormSubmitButton>
)}
{activeTab === 'human' && promptInfo?.type === 'agent' && promptInfo?.hasHuman && (
- <Button
- disabled={isLoading}
+ <FormSubmitButton
form="human-prompt-form"
- type="submit"
+ icon={<Save className="size-4" />}
+ loading={isLoading}
variant="secondary"
>
- {isLoading ? <Loader2 className="size-4 animate-spin" /> : <Save className="size-4" />}
{isLoading ? 'Saving...' : 'Save Changes'}
- </Button>
+ </FormSubmitButton>
)}
</div>
</div>
diff --git a/frontend/src/pages/settings/settings-provider.tsx b/frontend/src/pages/settings/settings-provider.tsx
index 9c023999..a4c8ad72 100644
--- a/frontend/src/pages/settings/settings-provider.tsx
+++ b/frontend/src/pages/settings/settings-provider.tsx
@@ -33,6 +33,7 @@ import { Button } from '@/components/ui/button';
import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from '@/components/ui/command';
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog';
import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form';
+import { FormSubmitButton } from '@/components/ui/form-submit-button';
import { Input } from '@/components/ui/input';
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
@@ -1802,15 +1803,14 @@ function SettingsProvider() {
>
Cancel
</Button>
- <Button
- disabled={isLoading}
+ <FormSubmitButton
form="provider-form"
- type="submit"
+ icon={<Save className="size-4" />}
+ loading={isLoading}
variant="secondary"
>
- {isLoading ? <Loader2 className="size-4 animate-spin" /> : <Save className="size-4" />}
{isLoading ? 'Saving...' : isNew ? 'Create Provider' : 'Update Provider'}
- </Button>
+ </FormSubmitButton>
</div>
</div>