how-to.tsx
This file
Path
apps/web/src/features/front/how-to.tsx
163 lines · 4.9 KiB · TypeScript
Last changed by
Merge redesign into mainoscabrielbefore adoptioncaedd21
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164/** * The strip of three steps above the asks, which a reader may dismiss. The * dismissal is kept in this browser's localStorage, never a cookie: a cookie * would take the page out of the edge cache (ADR 0042 says the same of the * theme). The server has no storage and draws the strip; a reader who * dismissed it sees it go once the page has read the storage. */import { pushCommand } from "@repo/agent-kit/install";import { Brand } from "@repo/brand";import { Result } from "effect";import { X } from "lucide-react";import { useCallback, useId, useSyncExternalStore } from "react";
import { Button } from "../../components/ui/button.tsx";import { CommandLine } from "../shared/command.tsx";import { AppLink } from "../shared/link.tsx";import { INSTALL_PATH } from "../shared/paths.ts";import { browserStorage } from "../shared/storage.ts";
const DISMISSED = "asks.how-to";
export const readHowToDismissed = (storage?: Storage): boolean => storage !== undefined && Result.getOrElse( Result.try(() => storage.getItem(DISMISSED) === "dismissed"), () => false );
export const dismissHowTo = (storage?: Storage) => { Result.try(() => storage?.setItem(DISMISSED, "dismissed"));};
export const restoreHowTo = (storage?: Storage) => { Result.try(() => storage?.removeItem(DISMISSED));};
/** Said on this page when it changes the storage; another tab says `storage`. */const CHANGED = "asks-how-to";
const subscribe = (onChange: () => void) => { globalThis.addEventListener(CHANGED, onChange); globalThis.addEventListener("storage", onChange);
return () => { globalThis.removeEventListener(CHANGED, onChange); globalThis.removeEventListener("storage", onChange); };};
const dismissedHere = () => readHowToDismissed(browserStorage());
const shownOnServer = () => false;
export const useHowTo = () => { const dismissed = useSyncExternalStore( subscribe, dismissedHere, shownOnServer );
const dismiss = useCallback(() => { dismissHowTo(browserStorage()); globalThis.dispatchEvent(new Event(CHANGED)); }, []);
const restore = useCallback(() => { restoreHowTo(browserStorage()); globalThis.dispatchEvent(new Event(CHANGED)); }, []);
return { dismiss, dismissed, restore };};
export const HOW_TO_TITLE = "How to take an ask";
const STEPS = [ { command: `${Brand.cli} claim <ask> --project <owner>/<name>`, label: "Claim one you may take", }, { command: `${Brand.cli} check`, label: "Check your answer before you push" }, { command: pushCommand, label: "Push it to your fork", },] as const;
const LINK = "decoration-rule underline hover:decoration-current";
/** * The strip itself. Its onward line links Install through the router and * `llms.txt` as a plain anchor, since `llms.txt` is a server route. On a phone * only the first step is a command box; the other two read as code in the * list, so the strip never becomes a column of code blocks (ADR 0041). */export const HowToTake = ({ onDismiss,}: { readonly onDismiss: () => void;}) => { const heading = useId();
return ( <section aria-labelledby={heading} className="border-rule bg-card flex flex-col gap-4 border p-4" data-how-to="" > <div className="flex items-center justify-between gap-3"> <h2 className="text-ui font-semibold" id={heading}> {HOW_TO_TITLE} </h2> <Button aria-label={`Dismiss ${HOW_TO_TITLE.toLowerCase()}`} onClick={onDismiss} size="icon-sm" title="Dismiss" type="button" variant="ghost" > <X aria-hidden="true" strokeWidth={1.75} /> </Button> </div> <ol className="columns:grid-cols-3 tablet:gap-4 grid gap-3"> {STEPS.map(({ command, label }, index) => ( <li className="flex min-w-0 flex-col gap-1" key={command}> <p className="text-label text-muted-foreground"> {index + 1}. {label} </p> {index === 0 ? ( <CommandLine command={command} numbered /> ) : ( <> <code className="text-mono tablet:hidden font-mono [overflow-wrap:anywhere]" data-step-text="" > {command} </code> <div className="max-tablet:hidden"> <CommandLine command={command} numbered /> </div> </> )} </li> ))} </ol> <p className="text-ui text-muted-foreground"> New here?{" "} <AppLink className={`text-foreground ${LINK}`} to={INSTALL_PATH}> Install the command line </AppLink>{" "} first. For agents:{" "} <a className={`text-foreground text-mono font-mono ${LINK}`} href="/llms.txt" > llms.txt </a> </p> </section> );};