Skip to content
refs
History
66 lines · 3.8 KBRaw
1import { Banner, Button, Checkbox, Input, LayerCard, LinkButton, Radio, Select } from "@refs/design-system";
2import { useState } from "react";
3import { Form, useNavigation } from "react-router";
4import type { NamespaceRef, Visibility } from "@refs/backend/contract";
5
6export interface RepoFormValues {
7 owner?: string;
8 name?: string;
9 description?: string;
10 visibility?: Visibility;
11 importUrl?: string;
12 setUp?: boolean;
13}
14
15export function readRepoForm(form: FormData): { owner: string; name: string; description: string; visibility: Visibility } {
16 const text = (name: string) => String(form.get(name) ?? "").trim();
17 return { owner: text("owner"), name: text("name"), description: text("description"), visibility: text("visibility") === "public" ? "public" : "private" };
18}
19
20export function VisibilityField({ defaultValue, onChange }: { defaultValue: Visibility; onChange?: (value: Visibility) => void }) {
21 return (
22 <Radio.Group legend="Visibility" name="visibility" defaultValue={defaultValue} onValueChange={(value) => onChange?.(value === "public" ? "public" : "private")} appearance="card">
23 <Radio.Item value="private" label="Private" description="Only members of this namespace can see and clone it." />
24 <Radio.Item value="public" label="Public" description="Anyone with the link can see and clone it. Only members can push." />
25 </Radio.Group>
26 );
27}
28
29export function RepoForm({ submit, error, values, owners, allowImport }: { submit: string; error?: string; values?: RepoFormValues; owners: readonly NamespaceRef[]; allowImport?: boolean }) {
30 const [setUp, setSetUp] = useState(values?.setUp ?? true);
31 const submitting = useNavigation().state === "submitting";
32 return (
33 <Form key={JSON.stringify({ error, values })} method="post" className="flex max-w-xl flex-col gap-5">
34 {error && <Banner variant="error" description={error} />}
35 <LayerCard className="flex flex-col gap-4 p-5">
36 <div className="grid grid-cols-[minmax(0,10rem)_auto_minmax(0,1fr)] items-start gap-2">
37 <Select label="Owner" name="owner" defaultValue={values?.owner ?? owners[0]?.slug} items={Object.fromEntries(owners.map((owner) => [owner.slug, owner.slug]))} />
38 <span aria-hidden className="mt-8 text-lg text-faint">/</span>
39 <Input label="Repository name" name="name" defaultValue={values?.name} maxLength={100} pattern="[A-Za-z0-9][A-Za-z0-9._\-]*" description="Letters, numbers, '.', '-' and '_'." required autoFocus />
40 </div>
41 <Input label="Description" name="description" defaultValue={values?.description} maxLength={350} description="Optional." />
42 <VisibilityField defaultValue={values?.visibility ?? "private"} />
43 {allowImport && (
44 <>
45 <Checkbox checked={setUp} onCheckedChange={(checked) => setSetUp(checked === true)} label="Add AGENTS.md, CLAUDE.md and README.md so agents can start" />
46 <input type="hidden" name="set_up" value={setUp ? "1" : ""} />
47 <details open={Boolean(values?.importUrl)} className="group text-compact">
48 <summary className="w-fit cursor-pointer text-muted select-none hover:text-ink">Import an existing repository instead</summary>
49 <div className="mt-3">
50 <Input label="Git URL" name="import_url" type="url" defaultValue={values?.importUrl} placeholder="https://github.com/owner/repo.git" description="A public repository over HTTPS. Its files replace the starter files." />
51 </div>
52 </details>
53 </>
54 )}
55 </LayerCard>
56 <div className="flex items-center gap-2">
57 <Button type="submit" variant="primary" loading={submitting}>
58 {submit}
59 </Button>
60 <LinkButton href="/" variant="secondary">
61 Cancel
62 </LinkButton>
63 </div>
64 </Form>
65 );
66}