Files
supabase/apps/studio/components/interfaces/Workers/DeployWorkerDialog.tsx
Gildas Garcia 3146650a5a Fix FormItemLayout usages for a11y (#49761)
Follow up of #49637. Usages that impacted tests were fixed in the
previous PR. This PR fixes the other usages so that label are correctly
linked to their inputs.

No visual changes

## How to test

1. Design system: [Form
examples](https://design-system-git-fix-form-item-layout-usages-supabase.vercel.app/design-system/docs/ui-patterns/forms):
moved `FormControl` around the `SelectTrigger` so that the label is
linked to the button (It's actually done like this in the [Select Form
example](https://design-system-git-fix-form-item-layout-usages-supabase.vercel.app/design-system/docs/components/select#form)
and Radix recommend targeting the button too in their
[documentation](https://www.radix-ui.com/primitives/docs/components/select#labelling))
2. [Access
tokens](https://studio-staging-463111oii-supabase.vercel.app/dashboard/account/tokens):
updated usage to fallback on generated ids and fixed the select just
like _1_
3. [New TOTP
factor](https://studio-staging-463111oii-supabase.vercel.app/dashboard/account/security):
updated usage to fallback on generated ids
4. _Studio/Database/Extensions_
(`https://studio-staging-463111oii-supabase.vercel.app/dashboard/project/[PROJECT]/database/extensions`):
updated the extension enabling modal to fallback on generated ids
5. _Studio/Integrations/Vault
(`https://studio-staging-463111oii-supabase.vercel.app/dashboard/project/[PROJECT]/integrations/vault/secrets`):
updated the secret edition modal to fallback on generated ids
6.
_Studio/Observability(`https://studio-staging-463111oii-supabase.vercel.app/dashboard/project/[PROJECT]/observability`):
updated the report creation and edition modals to fallback on generated
ids
7. _Studio/SQL
Editor(`https://studio-staging-463111oii-supabase.vercel.app/dashboard/project/[PROJECT]/sql/new`):
updated the query renaming modal to fallback on generated ids
8.
_Studio/Storage/Analytics(`https://studio-staging-463111oii-supabase.vercel.app/dashboard/project/[PROJECT]/storage/analytics`):
updated the table creation sheet to fallback on generated ids (you must
have a bucket first)
9.
_Studio/Workers(`https://studio-staging-463111oii-supabase.vercel.app/dashboard/project/[PROJECT]/workers`):
updated the worker creation modal to fallback on generated ids (you must
have a bucket first)
10. Updated
[Signup](https://studio-staging-463111oii-supabase.vercel.app/dashboard/sign-up?returnTo=%2Fnew),
[Signin](https://studio-staging-463111oii-supabase.vercel.app/dashboard/sign-in)
and [SSO
Signin](https://studio-staging-463111oii-supabase.vercel.app/dashboard/sign-in-sso)
forms to fallback on generated ids

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **Improvements**
- Standardized form field presentation across access tokens,
authentication, reports, integrations, database extensions, SQL editor,
storage, and worker deployment workflows.
- Updated password fields and visibility toggles for more consistent
input behavior.
- Refined token expiration selection, verification code entry, and
dropdown layouts.
- Preserved existing labels, validation, options, and form functionality
while simplifying the interface structure.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-09-01 15:22:55 +02:00

237 lines
7.6 KiB
TypeScript

import { zodResolver } from '@hookform/resolvers/zod'
import { useForm, useWatch } from 'react-hook-form'
import {
Button,
Dialog,
DialogContent,
DialogFooter,
DialogHeader,
DialogSection,
DialogSectionSeparator,
DialogTitle,
Form,
FormControl,
FormField,
Input,
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from 'ui'
import { Admonition } from 'ui-patterns/Admonition'
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
import * as z from 'zod'
import { RuntimeBadge } from './RuntimeBadge'
import { WORKER_DEPLOYABLE_RUNTIMES, WORKER_SIZES, WORKERS_REGION } from './Workers.constants'
import type { WorkerAccess } from './Workers.types'
import { formatSize, generateWorkerName } from './Workers.utils'
import { WorkerSnippetTabs } from './WorkerSnippetTabs'
const FORM_ID = 'deploy-worker-form'
const DeployWorkerFormSchema = z.object({
name: z
.string()
.trim()
.min(1, 'Name is required')
.regex(/^[a-z0-9-]+$/, 'Lowercase letters, numbers, and hyphens only'),
runtime: z.enum(WORKER_DEPLOYABLE_RUNTIMES),
size: z.enum(WORKER_SIZES),
access: z.enum(['private', 'public']),
instances: z
.union([z.literal(''), z.coerce.number().int().gte(1).lte(10)])
.refine((value) => value !== '', 'Instances is required'),
})
type DeployWorkerFormValues = z.infer<typeof DeployWorkerFormSchema>
const DEFAULT_VALUES: DeployWorkerFormValues = {
name: '',
runtime: 'deno',
size: WORKER_SIZES[0],
access: 'private',
instances: 1,
}
const ACCESS_OPTIONS: { value: WorkerAccess; label: string }[] = [
{ value: 'private', label: 'Private' },
{ value: 'public', label: 'Public' },
]
interface DeployWorkerDialogProps {
open: boolean
onOpenChange: (open: boolean) => void
}
export const DeployWorkerDialog = ({ open, onOpenChange }: DeployWorkerDialogProps) => {
const form = useForm<DeployWorkerFormValues>({
mode: 'onBlur',
resolver: zodResolver(DeployWorkerFormSchema),
defaultValues: { ...DEFAULT_VALUES, name: generateWorkerName() },
})
const [name, runtime, size, access, instances] = useWatch({
control: form.control,
name: ['name', 'runtime', 'size', 'access', 'instances'],
})
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent size="large">
<DialogHeader>
<DialogTitle>Deploy a worker</DialogTitle>
</DialogHeader>
<DialogSectionSeparator />
<Admonition
type="note"
title="This dashboard is read-only during the Private Alpha"
description={`Configure a worker below, then deploy it locally. Workers only deploy to ${WORKERS_REGION} during alpha.`}
className="border-x-0 border-y-0 rounded-none"
/>
<DialogSectionSeparator />
<DialogSection>
<Form {...form}>
<form id={FORM_ID} className="flex flex-col gap-y-4">
<FormField
control={form.control}
name="name"
render={({ field }) => (
<FormItemLayout label="Name">
<FormControl>
<Input {...field} placeholder="my-worker" />
</FormControl>
</FormItemLayout>
)}
/>
<FormField
control={form.control}
name="runtime"
render={({ field }) => (
<FormItemLayout label="Runtime">
<Select value={field.value} onValueChange={field.onChange}>
<FormControl>
<SelectTrigger>
<SelectValue />
</SelectTrigger>
</FormControl>
<SelectContent>
{WORKER_DEPLOYABLE_RUNTIMES.map((value) => (
<SelectItem key={value} value={value}>
<RuntimeBadge runtime={value} />
</SelectItem>
))}
</SelectContent>
</Select>
</FormItemLayout>
)}
/>
<FormField
control={form.control}
name="size"
render={({ field }) => (
<FormItemLayout
label="Size"
description="Fixed at deploy time and cannot be changed later"
>
<Select value={field.value} onValueChange={field.onChange}>
<FormControl>
<SelectTrigger>
<SelectValue />
</SelectTrigger>
</FormControl>
<SelectContent>
{WORKER_SIZES.map((value) => (
<SelectItem key={value} value={value}>
{formatSize(value)}
</SelectItem>
))}
</SelectContent>
</Select>
</FormItemLayout>
)}
/>
<FormField
control={form.control}
name="access"
render={({ field }) => (
<FormItemLayout
label="Access"
description="Public workers accept requests with a publishable key"
>
<Select value={field.value} onValueChange={field.onChange}>
<FormControl>
<SelectTrigger>
<SelectValue />
</SelectTrigger>
</FormControl>
<SelectContent>
{ACCESS_OPTIONS.map((option) => (
<SelectItem key={option.value} value={option.value}>
{option.label}
</SelectItem>
))}
</SelectContent>
</Select>
</FormItemLayout>
)}
/>
<FormField
control={form.control}
name="instances"
render={({ field }) => (
<FormItemLayout label="Instances" description="1 to 10">
<FormControl>
<Input
{...field}
type="number"
min={1}
max={10}
onChange={(e) =>
field.onChange(
Number.isNaN(e.target.valueAsNumber) ? '' : e.target.valueAsNumber
)
}
/>
</FormControl>
</FormItemLayout>
)}
/>
</form>
</Form>
</DialogSection>
<DialogSectionSeparator />
<DialogSection>
<WorkerSnippetTabs
input={{
name: name ?? '',
runtime: runtime ?? DEFAULT_VALUES.runtime,
size: size ?? DEFAULT_VALUES.size,
access: access ?? DEFAULT_VALUES.access,
instances: typeof instances === 'number' ? instances : DEFAULT_VALUES.instances,
}}
tabs={['ai', 'cli', 'config']}
/>
</DialogSection>
<DialogFooter>
<Button variant="default" onClick={() => onOpenChange(false)}>
Cancel
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
)
}