Skip to content
Myr

prefill.ts

This file

Path

apps/web/src/features/import/prefill.ts

78 lines · 2.3 KiB · TypeScript

Last changed by

Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72

History of this file

Take it

Merge build/1411-web-import into mainoscabrielbefore adoption059c75c

lines 1–78why, one step back
1import type { InitProject } from "@repo/protocol/contracts";
2import { ImportSource } from "@repo/protocol/domain/policy";3import {4 Owner,5 projectNameOf,6 ShortName,7 sourceNamespaceOf,8} from "@repo/protocol/ids";9import type { Handle, ProjectName, SourceNamespace } from "@repo/protocol/ids";10import { Array, Option, Schema } from "effect";1112export type ImportPayload = typeof InitProject.payloadSchema.Type;1314export interface Prefill {15 readonly owner: Option.Option<SourceNamespace>;16 readonly name: Option.Option<ShortName>;17}1819const HTTPS_URL = /^https:\/\/[^/?#\s]+\/(?<path>[^?#\s]+)$/u;2021const decodeShortName = Schema.decodeUnknownOption(ShortName);2223const decodeOwner = Schema.decodeUnknownOption(Owner);2425const decodeSource = Schema.decodeUnknownOption(ImportSource);2627const nameIn = (url: string): Option.Option<ShortName> => {28 const path = HTTPS_URL.exec(url.trim())?.groups?.path ?? "";29 const last = path.split("/").findLast((segment) => segment !== "") ?? "";3031 return decodeShortName(last.replace(/\.git$/u, "").toLowerCase());32};3334export const prefillFrom = (url: string): Prefill => ({35 name: nameIn(url),36 owner: sourceNamespaceOf(url.trim()),37});3839export const ownerOfNamespace = (40 namespace: SourceNamespace41): Option.Option<Owner> => decodeOwner(namespace);4243export const ownerOfHandle = (handle: Handle): Option.Option<Owner> =>44 decodeOwner(handle.toLowerCase());4546export const ownerChoices = (47 handle: Option.Option<Handle>,48 url: string49): readonly Owner[] =>50 Array.dedupe([51 ...Option.toArray(Option.flatMap(prefillFrom(url).owner, ownerOfNamespace)),52 ...Option.toArray(Option.flatMap(handle, ownerOfHandle)),53 ]);5455export const sourceUrlOf = (project: ProjectName) => `https://${project}`;5657export const importSourceOf = (form: {58 readonly url: string;59 readonly branch: string;60}): Option.Option<ImportSource> => {61 const branch = form.branch.trim();62 const url = form.url.trim();6364 return decodeSource(branch === "" ? { url } : { branch, url });65};6667export const importPayload = (form: {68 readonly url: string;69 readonly branch: string;70 readonly owner: Owner;71 readonly name: string;72}): Option.Option<ImportPayload> =>73 Option.flatMap(importSourceOf(form), (source) =>74 Option.map(decodeShortName(form.name.trim()), (short) => ({75 import: source,76 project: projectNameOf(form.owner, short),77 }))78 );