propose-view.tsx
This file
Path
apps/web/src/features/propose/propose-view.tsx
334 lines · 9.3 KiB · TypeScript
Last changed by
Merge redesign into mainoscabrielbefore adoptioncaedd21
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335import { Brand } from "@repo/brand";import type { ProposeAsk } from "@repo/protocol/contracts";import type { CommitSha, ProjectName, RepoPath } from "@repo/protocol/ids";import { Option } from "effect";import { LogIn } from "lucide-react";import { useId, useState } from "react";import type { ReactNode } from "react";
import { Button } from "../../components/ui/button.tsx";import { Checkbox } from "../../components/ui/checkbox.tsx";import { Label } from "../../components/ui/label.tsx";import { Spinner } from "../../components/ui/spinner.tsx";import { Textarea } from "../../components/ui/textarea.tsx";import { signInHref } from "../auth/session-slot.tsx";import { CommandLine } from "../shared/command.tsx";import { FactBlock } from "../shared/facts.tsx";import { Workspace } from "../shared/shell/workspace.tsx";import { ClientTerritorySelect } from "./client-territory.tsx";
export type ProposePayload = typeof ProposeAsk.payloadSchema.Type;
export interface About { readonly path: RepoPath; readonly line: number; readonly at: CommitSha;}
/** * `draft: true` only when the box is checked: the contract takes no `draft: * false`. */export const proposalPayload = (form: { readonly project: ProjectName; readonly want: string; readonly territory?: RepoPath; readonly draft: boolean;}): ProposePayload => { const want = form.want.trim();
const named = form.territory === undefined ? { project: form.project, want } : { project: form.project, territory: form.territory, want };
return form.draft ? { ...named, draft: true } : named;};
export const wantAbout = ({ at, line, path }: About) => `In \`${path}\` line ${line} at ${at.slice(0, 7)}: `;
export const proposeFormPath = ( project: ProjectName, about: Option.Option<About> = Option.none()) => Option.match(about, { onNone: () => `/${project}/proposals/new`, onSome: ({ at, line, path }) => `/${project}/proposals/new?${new URLSearchParams({ about: path, at, line: String(line) }).toString()}`, });
const AGENT_CHOOSES = "";
export const territoryChoices = (modules: readonly RepoPath[]) => [ { label: "Let the agent choose", value: AGENT_CHOOSES }, ...modules.map((module) => ({ label: module, value: module })),];
const proposeWords = (written: boolean, busy: boolean) => { if (!written) { return "Write the want first"; }
return busy ? "Proposing" : "Propose";};
const PROPOSE_COMMAND = `${Brand.cli} propose --draft "<want>"`;
const SignInFirst = ({ about, project,}: { readonly about: Option.Option<About>; readonly project: ProjectName;}) => ( <section aria-label="Sign in to propose" className="flex max-w-(--measure) flex-col items-start gap-3" > <p className="text-body"> A proposal asks {project} to open an ask nobody wrote yet: a want in your words, which the platform's agent can draft into an ask file and a failing check for a maintainer to accept. </p> <p className="text-ui flex items-start gap-2"> <LogIn aria-hidden="true" className="mt-0.5 size-4 shrink-0" strokeWidth={1.75} /> <span> Proposing is for signed-in members.{" "} <a className="decoration-rule font-medium underline hover:decoration-current" href={signInHref(proposeFormPath(project, about))} > Sign in </a>{" "} to propose one here, or propose from your clone: </span> </p> <CommandLine command={PROPOSE_COMMAND} /> </section>);
const GoodWant = () => ( <> <FactBlock label="A good want"> <ul className="text-ui flex list-disc flex-col gap-1.5 pl-4"> <li>Says what the project should do, not how to change the code.</li> <li>Asks for one change, small enough to check on its own.</li> <li>Says how you would tell it is done: what a check would see.</li> </ul> </FactBlock> <FactBlock label="From a terminal"> <CommandLine command={PROPOSE_COMMAND} numbered /> </FactBlock> </>);
const Form = ({ about, busy, children, modules, onPropose, project,}: { readonly project: ProjectName; readonly about: Option.Option<About>; readonly modules: readonly RepoPath[]; readonly onPropose?: ((payload: ProposePayload) => void) | undefined; readonly busy: boolean; readonly children?: ReactNode;}) => { const id = useId();
const [want, setWant] = useState(() => Option.match(about, { onNone: () => "", onSome: wantAbout }) );
const [territory, setTerritory] = useState(AGENT_CHOOSES); const [draft, setDraft] = useState(true); const chosen = modules.find((module) => module === territory); const written = want.trim().length > 0; const choices = territoryChoices(modules);
return ( <form aria-label="Propose an ask" className="flex max-w-(--measure) flex-col gap-6" data-propose="" onSubmit={(event) => { event.preventDefault();
if (written) { onPropose?.( proposalPayload( chosen === undefined ? { draft, project, want } : { draft, project, territory: chosen, want } ) ); } }} > <div className="flex flex-col gap-1.5"> <label className="text-ui font-medium" htmlFor={`${id}-want`}> What should {project} do that it does not do now? </label> <Textarea className="min-h-32 text-base leading-6" id={`${id}-want`} name="want" onChange={(event) => { setWant(event.target.value); }} required value={want} /> </div>
<div className="flex flex-col gap-1.5"> <span className="text-ui font-medium" id={`${id}-territory`}> Territory </span> <ClientTerritorySelect choices={choices} labelledBy={`${id}-territory`} onChange={setTerritory} value={territory} /> <p className="text-label text-muted-foreground font-normal"> One of the project's top folders, or the agent picks from the want. </p> </div>
<div className="flex flex-col gap-1.5"> <div className="flex items-start gap-2"> <Checkbox checked={draft} className="mt-0.5" id={`${id}-draft`} name="draft" onCheckedChange={(checked) => { setDraft(checked); }} /> <Label className="text-ui leading-snug" htmlFor={`${id}-draft`}> Let the platform's agent draft the ask </Label> </div> <p className="text-label text-muted-foreground pl-6 font-normal"> It writes the ask file and a check that fails on main, runs it red first, and puts the draft to a maintainer. Clear the box to propose the want alone. </p> </div>
{children}
<Button className={`tablet:h-9 tablet:w-fit h-11 w-full min-w-28 px-4 ${written ? "" : "text-muted-foreground border-dashed disabled:opacity-100"}`} data-written={written} disabled={busy || !written} type="submit" variant={written ? "default" : "outline"} > {busy ? <Spinner /> : null} {proposeWords(written, busy)} </Button> </form> );};
const ProposeBody = ({ about, busy, checking, children, modules, onPropose, project, signedIn,}: { readonly project: ProjectName; readonly signedIn: boolean; readonly checking: boolean; readonly about: Option.Option<About>; readonly modules: readonly RepoPath[]; readonly onPropose?: ((payload: ProposePayload) => void) | undefined; readonly busy: boolean; readonly children?: ReactNode;}) => { if (checking) { return ( <output className="text-ui text-muted-foreground block"> Checking who is signed in </output> ); }
return signedIn ? ( <Form about={about} busy={busy} modules={modules} onPropose={onPropose} project={project} > {children} </Form> ) : ( <SignInFirst about={about} project={project} /> );};
export const ProposeView = ({ about, busy = false, checking = false, children, modules, onPropose, project, signedIn,}: { readonly project: ProjectName; readonly signedIn: boolean; readonly checking?: boolean; readonly about: Option.Option<About>; readonly modules: readonly RepoPath[]; readonly onPropose?: (payload: ProposePayload) => void; readonly busy?: boolean; readonly children?: ReactNode;}): ReactNode => ( <div className="contents [&_[data-facts=inline]]:hidden"> <Workspace crumbs={[ { label: project, to: `/${project}` }, { label: "Propose an ask" }, ]} facts={<GoodWant />} factsHeading="Proposing" meta="A maintainer decides whether the ask opens; nothing lands from here." title="Propose an ask" > <ProposeBody about={about} busy={busy} checking={checking} modules={modules} onPropose={onPropose} project={project} signedIn={signedIn} > {children} </ProposeBody> <div className="border-rule flex flex-col border-t pt-4"> <GoodWant /> </div> </Workspace> </div>);