Skip to content
Myr

import-form.tsx

This file

Path

apps/web/src/features/import/import-form.tsx

398 lines · 10.7 KiB · TypeScript

Last changed by

Merge redesign into mainoscabrielbefore adoptioncaedd21

History of this file

Take it

Merge build/1411-web-import into mainoscabrielbefore adoption059c75c

lines 1–112why, one step back
1import { Brand } from "@repo/brand";
2import type {3 Handle,4 Owner,5 ShortName,6 SourceNamespace,7} from "@repo/protocol/ids";8import { Option } from "effect";9import { LogIn } from "lucide-react";10import { useId, useState } from "react";11import type { ReactNode } from "react";1213import { Button } from "../../components/ui/button.tsx";14import { Input } from "../../components/ui/input.tsx";15import { Spinner } from "../../components/ui/spinner.tsx";16import { signInHref } from "../auth/session-slot.tsx";17import { CommandLine } from "../shared/command.tsx";18import { ClientOwnerSelect } from "./client-owner.tsx";19import {20 importPayload,21 importSourceOf,22 ownerChoices,23 ownerOfNamespace,24 prefillFrom,25} from "./prefill.ts";26import type { ImportPayload } from "./prefill.ts";2728export interface ImportPrefill {29 readonly url: string;30 readonly owner?: SourceNamespace | undefined;31 readonly name?: ShortName | undefined;32}3334export interface Importer {35 readonly handle: Option.Option<Handle>;36 readonly checking?: boolean | undefined;37 readonly busy?: boolean | undefined;38 readonly onImport?: ((payload: ImportPayload) => void) | undefined;39 readonly refusal?: ReactNode | undefined;40}4142export interface ImportFormProps extends Importer {43 readonly prefill?: ImportPrefill | undefined;44 readonly fixedOwner?: SourceNamespace | undefined;45 readonly here: string;46}4748export const importCommand = (project = "<owner>/<name>", url = "<url>") =>49 `${Brand.cli} init --project ${project} --import ${url}`;5051const LINK = "decoration-rule font-medium underline hover:decoration-current";5253const LABEL = "text-ui font-medium";5455const HINT = "text-label text-muted-foreground font-normal";5657const MONO_FIELD =58 "tablet:h-9 tablet:text-mono h-11 font-mono text-base [font-feature-settings:'calt'_0,'liga'_0]";5960const IMPORT_WORDS = {61 busy: "Importing…",62 noName: "Name the project",63 noOwner: "No owner you may import under",64 noUrl: "Paste a repo's URL first",65 ready: "Import",66} as const;6768type FormState = keyof typeof IMPORT_WORDS;6970const formState = ({71 branch,72 busy,73 owner,74 payload,75 url,76}: {77 readonly busy: boolean;78 readonly owner: Owner | undefined;79 readonly payload: Option.Option<ImportPayload>;80 readonly url: string;81 readonly branch: string;82}): FormState => {83 if (busy) {84 return "busy";85 }8687 if (Option.isNone(importSourceOf({ branch, url }))) {88 return "noUrl";89 }9091 if (owner === undefined) {92 return "noOwner";93 }9495 return Option.isSome(payload) ? "ready" : "noName";96};9798const SignInToImport = ({99 fixedOwner,100 here,101 prefill,102}: Pick<ImportFormProps, "fixedOwner" | "here" | "prefill">) => {103 const name = prefill?.name;104105 const project =106 fixedOwner === undefined || name === undefined107 ? undefined108 : `${fixedOwner}/${name}`;109110 return (111 <section112 aria-label="Sign in to import"

Merge redesign into mainoscabrielbefore adoptioncaedd21

line 113why, one step back
113 className="flex max-w-(--measure) flex-col items-start gap-4"

Merge build/1411-web-import into mainoscabrielbefore adoption059c75c

lines 114–236why, one step back
114 >
115 {prefill === undefined || prefill.url === "" ? null : (116 <dl className="border-rule flex w-full flex-col border-y">117 <div className="flex flex-col gap-0.5 py-2.5">118 <dt className={HINT}>Repo URL</dt>119 <dd className="text-mono font-mono [overflow-wrap:anywhere]">120 <code>{prefill.url}</code>121 </dd>122 </div>123 {project === undefined ? null : (124 <div className="border-rule flex flex-col gap-0.5 border-t py-2.5">125 <dt className={HINT}>Project</dt>126 <dd className="text-mono font-mono [overflow-wrap:anywhere]">127 <code>{project}</code>128 </dd>129 </div>130 )}131 </dl>132 )}133 <p className="text-ui flex items-start gap-2">134 <LogIn135 aria-hidden="true"136 className="mt-0.5 size-4 shrink-0"137 strokeWidth={1.75}138 />139 <span>140 Importing is for signed-in people.{" "}141 <a className={LINK} href={signInHref(here)}>142 Sign in143 </a>{" "}144 to import it here, or import from a terminal:145 </span>146 </p>147 <CommandLine148 command={importCommand(149 project,150 prefill === undefined || prefill.url === "" ? undefined : prefill.url151 )}152 />153 </section>154 );155};156157const FixedOwner = ({158 hintId,159 owner,160}: {161 readonly hintId: string;162 readonly owner: SourceNamespace;163}) => (164 <>165 <p aria-describedby={hintId} className="text-want font-medium">166 {owner}167 </p>168 <input name="owner" type="hidden" value={owner} />169 </>170);171172const ownerHint = (fixed: boolean, choices: readonly Owner[]) => {173 if (fixed) {174 return "The address names it: the project keeps its source's account.";175 }176177 return choices.length > 1178 ? "The URL's own account, or your handle."179 : "Your handle: this URL's host is not one a project may be owned by.";180};181182const NameHint = ({183 payload,184}: {185 readonly payload: Option.Option<ImportPayload>;186}) =>187 Option.match(payload, {188 onNone: () =>189 "Lowercase letters and digits, joined by single dots, dashes or underscores.",190 onSome: ({ project }) => (191 <>192 The project&apos;s address:{" "}193 <code className="text-mono text-foreground font-mono [overflow-wrap:anywhere]">194 /{project}195 </code>196 </>197 ),198 });199200const Form = ({201 busy = false,202 fixedOwner,203 handle,204 onImport,205 prefill,206 refusal,207}: Pick<208 ImportFormProps,209 "busy" | "fixedOwner" | "handle" | "onImport" | "prefill" | "refusal"210>) => {211 const id = useId();212 const [url, setUrl] = useState(prefill?.url ?? "");213 const [branch, setBranch] = useState("");214 const [chosen, setChosen] = useState<string | undefined>(prefill?.owner);215 const [typed, setTyped] = useState<string | undefined>(prefill?.name);216217 const choices =218 fixedOwner === undefined219 ? ownerChoices(handle, url)220 : Option.toArray(ownerOfNamespace(fixedOwner));221222 const owner = choices.find((choice) => choice === chosen) ?? choices[0];223224 const name: string =225 typed ?? Option.getOrElse(prefillFrom(url).name, () => "");226227 const payload =228 owner === undefined229 ? Option.none()230 : importPayload({ branch, name, owner, url });231232 const state = formState({ branch, busy, owner, payload, url });233234 return (235 <form236 aria-label="Import a repo"

Merge redesign into mainoscabrielbefore adoptioncaedd21

line 237why, one step back
237 className="flex max-w-(--measure) flex-col gap-6"

Merge build/1411-web-import into mainoscabrielbefore adoption059c75c

lines 238–398why, one step back
238 data-import=""
239 onSubmit={(event) => {240 event.preventDefault();241242 if (!busy && Option.isSome(payload)) {243 onImport?.(payload.value);244 }245 }}246 >247 <div className="flex flex-col gap-1.5">248 <label className={LABEL} htmlFor={`${id}-url`}>249 Repo URL250 </label>251 <Input252 aria-describedby={`${id}-url-hint`}253 autoCapitalize="off"254 autoComplete="off"255 className={MONO_FIELD}256 id={`${id}-url`}257 inputMode="url"258 name="url"259 onChange={(event) => {260 setUrl(event.target.value);261 }}262 placeholder="https://github.com/pallets/click"263 readOnly={busy || fixedOwner !== undefined}264 required265 spellCheck={false}266 type="url"267 value={url}268 />269 <p className={HINT} id={`${id}-url-hint`}>270 {fixedOwner === undefined271 ? "Any public repo over HTTPS: GitHub, GitLab, Codeberg, Bitbucket or another host."272 : "Rebuilt from this page's address."}273 </p>274 </div>275276 <div className="flex flex-col gap-1.5">277 <label className={LABEL} htmlFor={`${id}-branch`}>278 Branch <span className={HINT}>(optional)</span>279 </label>280 <Input281 aria-describedby={`${id}-branch-hint`}282 autoCapitalize="off"283 autoComplete="off"284 className={`${MONO_FIELD} max-w-sm`}285 id={`${id}-branch`}286 name="branch"287 onChange={(event) => {288 setBranch(event.target.value);289 }}290 readOnly={busy}291 spellCheck={false}292 value={branch}293 />294 <p className={HINT} id={`${id}-branch-hint`}>295 Left empty, the repo&apos;s default branch.296 </p>297 </div>298299 <div className="flex flex-col gap-1.5">300 <span className={LABEL} id={`${id}-owner`}>301 Owner302 </span>303 {fixedOwner === undefined ? (304 <ClientOwnerSelect305 choices={choices}306 describedBy={`${id}-owner-hint`}307 labelledBy={`${id}-owner`}308 onChange={setChosen}309 value={owner}310 />311 ) : (312 <FixedOwner hintId={`${id}-owner-hint`} owner={fixedOwner} />313 )}314 <p className={HINT} id={`${id}-owner-hint`}>315 {ownerHint(fixedOwner !== undefined, choices)}316 </p>317 </div>318319 <div className="flex flex-col gap-1.5">320 <label className={LABEL} htmlFor={`${id}-name`}>321 Name322 </label>323 <Input324 aria-describedby={`${id}-name-hint`}325 autoCapitalize="off"326 autoComplete="off"327 className={`${MONO_FIELD} max-w-sm`}328 id={`${id}-name`}329 name="name"330 onChange={(event) => {331 setTyped(event.target.value);332 }}333 readOnly={busy}334 required335 spellCheck={false}336 value={name}337 />338 <p className={HINT} id={`${id}-name-hint`}>339 <NameHint payload={payload} />340 </p>341 </div>342343 {refusal}344345 <div className="flex flex-col gap-2">346 <Button347 className={`tablet:h-9 tablet:w-fit h-11 w-full min-w-28 px-4 ${state === "ready" || state === "busy" ? "" : "text-muted-foreground border-dashed disabled:opacity-100"}`}348 data-state={state}349 disabled={state !== "ready"}350 type="submit"351 variant={352 state === "ready" || state === "busy" ? "default" : "outline"353 }354 >355 {busy ? <Spinner /> : null}356 {IMPORT_WORDS[state]}357 </Button>358 <output aria-live="polite" className={HINT}>359 {busy360 ? "The platform is cloning the repo; an import takes up to half a minute. Stay on this page."361 : null}362 </output>363 </div>364 </form>365 );366};367368export const ImportForm = ({369 busy,370 checking = false,371 fixedOwner,372 handle,373 here,374 onImport,375 prefill,376 refusal,377}: ImportFormProps): ReactNode => {378 if (checking) {379 return (380 <output className="text-ui text-muted-foreground block">381 Checking who is signed in382 </output>383 );384 }385386 return Option.isSome(handle) ? (387 <Form388 busy={busy}389 fixedOwner={fixedOwner}390 handle={handle}391 onImport={onImport}392 prefill={prefill}393 refusal={refusal}394 />395 ) : (396 <SignInToImport fixedOwner={fixedOwner} here={here} prefill={prefill} />397 );398};