Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | 69x 69x 69x 69x | import { useState, forwardRef, useImperativeHandle, useEffect } from 'react'
import { EntitySelect } from '@/shared/components/EntitySelect'
import { useCreatePipeline, useUpdatePipeline } from '@/features/pipelines/pipelinesApi'
import { useRepositories } from '@/features/repositories/repositoriesApi'
import type { SelectionOption } from '@/shared/components/EntitySelect'
export interface CrudFormRef {
submit: () => Promise<void>
}
export interface PipelineFormProps {
projectId: string
id?: string
defaults?: { name?: string; output_branch?: string; enabled?: boolean; code_repo_id?: string; manifest_repo_id?: string; manifest_path?: string }
onSuccess: (result: { id: string }) => void
onClose: () => void
onDirtyChange?: (dirty: boolean) => void
onPendingChange?: (pending: boolean) => void
}
interface FormState {
name: string
description: string
code_repo_id: string | undefined
manifest_repo_id: string | undefined
manifest_path: string
output_branch: string
enabled: boolean
}
const BRANCH_REGEX = /^[a-zA-Z0-9_\-./]+$/
export const PipelineForm = forwardRef<CrudFormRef, PipelineFormProps>(function PipelineForm(
{ projectId, id, defaults, onSuccess, onClose: _onClose, onDirtyChange, onPendingChange },
ref,
) {
const [form, setForm] = useState<FormState>({
name: defaults?.name ?? '',
description: '',
code_repo_id: defaults?.code_repo_id,
manifest_repo_id: defaults?.manifest_repo_id,
manifest_path: defaults?.manifest_path ?? '',
output_branch: defaults?.output_branch ?? '',
enabled: defaults?.enabled ?? true,
})
const [errors, setErrors] = useState<Record<string, string>>({})
const [submitted, setSubmitted] = useState(false)
const isEdit = !!id
const { mutateAsync: createPipeline, isPending: isCreatePending } = useCreatePipeline(projectId)
const { mutateAsync: updatePipeline, isPending: isUpdatePending } = useUpdatePipeline(id ?? '')
const isPending = isCreatePending || isUpdatePending
const { data: reposData, isLoading: reposLoading } = useRepositories()
const isDirty =
!submitted && (
form.name !== (defaults?.name ?? '') ||
form.code_repo_id !== defaults?.code_repo_id ||
form.manifest_repo_id !== defaults?.manifest_repo_id ||
form.manifest_path !== (defaults?.manifest_path ?? '') ||
form.output_branch !== (defaults?.output_branch ?? '') ||
form.enabled !== (defaults?.enabled ?? true)
)
useEffect(() => { onDirtyChange?.(isDirty) }, [isDirty, onDirtyChange])
useEffect(() => { onPendingChange?.(isPending) }, [isPending, onPendingChange])
function validate(): Record<string, string> {
const errs: Record<string, string> = {}
if (!form.name) errs.name = 'Name is required'
if (!form.code_repo_id) errs.code_repo_id = 'Code repository is required'
if (!form.manifest_repo_id) errs.manifest_repo_id = 'Manifest repository is required'
if (!form.manifest_path) errs.manifest_path = 'Manifest path is required'
if (!form.output_branch) {
errs.output_branch = 'Output branch is required'
} else if (!BRANCH_REGEX.test(form.output_branch)) {
errs.output_branch = 'Invalid branch name format'
}
return errs
}
async function handleSubmit() {
if (isPending) return
const errs = validate()
setErrors(errs)
if (Object.keys(errs).length > 0) return
setSubmitted(true)
try {
if (isEdit) {
await updatePipeline({
name: form.name,
code_repo_id: form.code_repo_id!,
manifest_repo_id: form.manifest_repo_id!,
manifest_path: form.manifest_path,
output_branch: form.output_branch,
enabled: form.enabled,
description: form.description || null,
})
onSuccess({ id: id! })
} else {
const result = await createPipeline({
name: form.name,
code_repo_id: form.code_repo_id!,
manifest_repo_id: form.manifest_repo_id!,
manifest_path: form.manifest_path,
output_branch: form.output_branch,
enabled: form.enabled,
description: form.description || null,
})
onSuccess({ id: result.id })
}
} catch {
setSubmitted(false)
}
}
useImperativeHandle(ref, () => ({
submit: handleSubmit,
}))
const repoOptions: SelectionOption[] = (reposData?.data ?? []).map((r) => ({
value: r.id,
label: r.name,
description: r.url,
}))
return (
<fieldset disabled={isPending} className="flex flex-col gap-4">
<label className="flex flex-col gap-1 text-sm font-medium">
Name
<input
className="rounded-md border border-input bg-background px-3 py-2 text-sm"
value={form.name}
onChange={(e) => {
setForm((prev) => ({ ...prev, name: e.target.value }))
setErrors((prev) => ({ ...prev, name: '' }))
}}
placeholder="Pipeline name"
/>
{errors.name && <p className="text-sm text-destructive">{errors.name}</p>}
</label>
<label className="flex flex-col gap-1 text-sm font-medium">
Description
<textarea
className="rounded-md border border-input bg-background px-3 py-2 text-sm"
value={form.description}
onChange={(e) => setForm((prev) => ({ ...prev, description: e.target.value }))}
placeholder="Description (optional)"
rows={3}
/>
</label>
<div className="flex flex-col gap-1">
<label className="text-sm font-medium">Code Repository</label>
<EntitySelect
options={repoOptions}
value={form.code_repo_id}
onChange={(val) => {
setForm((prev) => ({ ...prev, code_repo_id: val as string | undefined }))
setErrors((prev) => ({ ...prev, code_repo_id: '' }))
}}
placeholder="Select repository..."
isLoading={reposLoading}
/>
{errors.code_repo_id && (
<p className="text-sm text-destructive">{errors.code_repo_id}</p>
)}
</div>
<label className="flex flex-col gap-1 text-sm font-medium">
Output Branch
<input
className="rounded-md border border-input bg-background px-3 py-2 text-sm font-mono"
value={form.output_branch}
onChange={(e) => {
setForm((prev) => ({ ...prev, output_branch: e.target.value }))
setErrors((prev) => ({ ...prev, output_branch: '' }))
}}
placeholder="output-branch"
/>
{errors.output_branch && (
<p className="text-sm text-destructive">{errors.output_branch}</p>
)}
</label>
<div className="flex flex-col gap-1">
<label className="text-sm font-medium">Manifest Repository</label>
<EntitySelect
options={repoOptions}
value={form.manifest_repo_id}
onChange={(val) => {
setForm((prev) => ({ ...prev, manifest_repo_id: val as string | undefined }))
setErrors((prev) => ({ ...prev, manifest_repo_id: '' }))
}}
placeholder="Select repository..."
isLoading={reposLoading}
/>
{errors.manifest_repo_id && (
<p className="text-sm text-destructive">{errors.manifest_repo_id}</p>
)}
</div>
<label className="flex flex-col gap-1 text-sm font-medium">
Manifest Path
<input
className="rounded-md border border-input bg-background px-3 py-2 text-sm font-mono"
value={form.manifest_path}
onChange={(e) => {
setForm((prev) => ({ ...prev, manifest_path: e.target.value }))
setErrors((prev) => ({ ...prev, manifest_path: '' }))
}}
placeholder="manifests/prod"
/>
{errors.manifest_path && (
<p className="text-sm text-destructive">{errors.manifest_path}</p>
)}
</label>
<label className="flex items-center gap-2 text-sm font-medium">
<input
type="checkbox"
checked={form.enabled}
onChange={(e) => setForm((prev) => ({ ...prev, enabled: e.target.checked }))}
className="size-4 accent-primary"
/>
Enabled
</label>
</fieldset>
)
})
|