import { useNavigate } from "@tanstack/react-router"; import { useState } from "react"; import { createPull } from "~/fns/pulls"; import { useAction } from "~/hooks/use-action"; import { usePullSuggestion } from "~/hooks/use-pull-suggestion"; import { MarkdownEditor } from "~/components/MarkdownEditor"; import { Button } from "~/ui/Button"; import { CheckboxField, TextField } from "~/ui/Field"; import { ErrorBanner } from "~/ui/Callout"; import type { AiAccess } from "~/server/pull-describe/service"; type Props = { owner: string; repo: string; base: string; /** Head as the server expects it: a bare branch or `owner:branch` for a fork. */ head: string; defaultTitle: string; template: string; ai: AiAccess | null; }; function SparkleIcon() { return ( Sparkle ); } function usageHint(ai: AiAccess): string { if (!ai.usage) return "Uses one AI call with your own key"; const limit = ai.usage.limit === null ? "" : ` of ${ai.usage.limit}`; return `Uses one AI call (${ai.usage.used}${limit} used this month)`; } export function CreatePullForm({ owner, repo, base, head, defaultTitle, template, ai }: Props) { const navigate = useNavigate(); const create = useAction(createPull, { invalidate: false }); const [title, setTitle] = useState(defaultTitle); const [body, setBody] = useState(template); const suggestion = usePullSuggestion({ owner, repo, base, head, current: { title, body }, defaults: { title: defaultTitle, body: template }, onFill: (fields) => { setTitle(fields.title); setBody(fields.body); }, }); const suggestAccess = ai?.allowed ? ai : null; return (
{ event.preventDefault(); const form = new FormData(event.currentTarget); const result = await create.run({ data: { owner, repo, base, head, title, body, draft: form.get("draft") === "on" }, }); if (result) await navigate({ to: "/$owner/$repo/pull/$number", params: { owner, repo, number: String(result.number) } }); }} >
setTitle(event.target.value)} />
{suggestAccess && ( )}
); }