| 1 | "use client"; |
| 2 | |
| 3 | import { zodResolver } from |
| 4 | import Link from "next/link |
| 5 | import { useAction } |
| 6 | import { useState } |
| 7 | import { useForm } |
| 8 | |
| 9 | import { Button } |
| 10 | import { |
| 11 | Field, |
| 12 | FieldError, |
| 13 | FieldGroup, |
| 14 | FieldLabel, |
| 15 | } from "@/components/ui/field"; |
| 16 | import { Input } |
| 17 | import { Spinner } |
| 18 | import { Textarea } |
| 19 | import type { IssueLabel |
| 20 | import { cn } |
| 21 | import { createIssue } |
| 22 | import { type CreateIssueInput, |
| 23 | |
| 24 | export function CreateIssueForm({ |
| 25 | username, |
| 26 | repo, |
| 27 | labels, |
| 28 | }: { |
| 29 | username: string |
| 30 | repo: string |
| 31 | labels: IssueLabel |
| 32 | }) { |
| 33 | const { |
| 34 | register, |
| 35 | handleSubmit, |
| 36 | formState: { errors |
| 37 | } = useForm<CreateIssueInput |
| 38 | resolver: zodResolver(createIssueSchema |
| 39 | defaultValues: { title: "" |
| 40 | }); |
| 41 | |
| 42 | const [selected, |
| 43 | const [assignees, |
| 44 | |
| 45 | const { execute, |
| 46 | |
| 47 | function toggleLabel(name |
| 48 | setSelected((current) |
| 49 | current.includes(name |
| 50 | ? current.filter( |
| 51 | : [...current, |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | return ( |
| 56 | <form |
| 57 | onSubmit={handleSubmit(( |
| 58 | execute({ |
| 59 | ...input, |
| 60 | username, |
| 61 | repo, |
| 62 | labels: selected, |
| 63 | assignees: assignees |
| 64 | .split(",") |
| 65 | .map((name) |
| 66 | .filter(Boolean), |
| 67 | }), |
| 68 | )} |
| 69 | className="contents" |
| 70 | > |
| 71 | <FieldGroup> |
| 72 | <Field> |
| 73 | <FieldLabel htmlFor=" |
| 74 | <Input |
| 75 | id="issue-title" |
| 76 | placeholder="Login fails with 500 on Safari" |
| 77 | autoComplete="off" |
| 78 | aria-invalid={Boolean(errors |
| 79 | {...register("title |
| 80 | /> |
| 81 | <FieldError errors={ |
| 82 | </Field> |
| 83 | |
| 84 | <Field> |
| 85 | <FieldLabel htmlFor=" |
| 86 | Description |
| 87 | <span className="font-normal text-muted-foreground |
| 88 | (optional) |
| 89 | </span> |
| 90 | </FieldLabel> |
| 91 | <Textarea |
| 92 | id="issue-body" |
| 93 | rows={8} |
| 94 | placeholder="Steps to reproduce, expected behavior, screenshots… Markdown is supported." |
| 95 | aria-invalid={Boolean(errors |
| 96 | {...register("body |
| 97 | /> |
| 98 | <FieldError errors={ |
| 99 | </Field> |
| 100 | |
| 101 | {labels.length > |
| 102 | <Field> |
| 103 | <FieldLabel>Labels (optional)</FieldLabel |
| 104 | <div className="flex flex-wrap gap-1.5 |
| 105 | {labels.map(( |
| 106 | const active = |
| 107 | return ( |
| 108 | <button |
| 109 | key={label.id |
| 110 | type="button" |
| 111 | onClick={() => |
| 112 | aria-pressed={active} |
| 113 | title={label.description |
| 114 | className={cn( |
| 115 | "flex items-center gap-1.5 rounded-full border px-2.5 py-1 text-xs transition-colors", |
| 116 | active ? "border-primary bg-muted" |
| 117 | )} |
| 118 | > |
| 119 | <span |
| 120 | className="size-2.5 rounded-full" |
| 121 | style={{ backgroundColor: |
| 122 | /> |
| 123 | {label.name} |
| 124 | </button> |
| 125 | ); |
| 126 | })} |
| 127 | </div> |
| 128 | </Field> |
| 129 | )} |
| 130 | |
| 131 | <Field> |
| 132 | <FieldLabel htmlFor=" |
| 133 | Assignees |
| 134 | <span className="font-normal text-muted-foreground |
| 135 | (optional, comma-separated usernames) |
| 136 | </span> |
| 137 | </FieldLabel> |
| 138 | <Input |
| 139 | id="issue-assignees" |
| 140 | placeholder="octocat" |
| 141 | autoComplete="off" |
| 142 | value={assignees} |
| 143 | onChange={(event) |
| 144 | /> |
| 145 | </Field> |
| 146 | |
| 147 | {result.serverError && |
| 148 | </FieldGroup> |
| 149 | |
| 150 | <div className="mt-6 flex flex-col-reverse gap-2 sm:flex-row sm:justify-end |
| 151 | <Button type=" |
| 152 | <Link href={ |
| 153 | </Button> |
| 154 | <Button type=" |
| 155 | {isExecuting && <Spinner |
| 156 | Create issue |
| 157 | </Button> |
| 158 | </div> |
| 159 | </form> |
| 160 | ); |
| 161 | } |