iterate.com / cloud

git clone https://git.iterate.com/repos/cloud

cloud / internal-packages / installer / src / parts.tsxmain at aacbc74

// The wizard's compositions of iterate's shadcn kit (@iterate-com/ui): a labelled field, choice cards,
// a notice with an action, a button that shows it's busy. Everything else comes from the kit as is.
import { useId, type ComponentProps, type ReactNode } from "react";
import type { VariantProps } from "class-variance-authority";
import { cn } from "cn";
import { Button, buttonVariants } from "@iterate-com/ui/components/ui/button";
import { Input } from "@iterate-com/ui/components/ui/input";
import {
  Field,
  FieldContent,
  FieldDescription,
  FieldLabel,
  FieldTitle,
} from "@iterate-com/ui/components/ui/field";
import { NativeSelect, NativeSelectOption } from "@iterate-com/ui/components/ui/native-select";
import { RadioGroup, RadioGroupItem } from "@iterate-com/ui/components/ui/radio-group";
import { Alert, AlertDescription, AlertTitle } from "@iterate-com/ui/components/ui/alert";
import { Spinner } from "@iterate-com/ui/components/ui/spinner";

type ButtonProps = ComponentProps<typeof Button> & { loading?: boolean };

/** shadcn's Button, with a spinner while `loading`: not pressable then, but it keeps the focus. */
export function Btn({ loading, disabled, children, className, ...props }: ButtonProps) {
  return (
    <Button
      disabled={disabled || loading}
      focusableWhenDisabled={loading || undefined}
      aria-busy={loading || undefined}
      className={cn(loading && "pointer-events-none opacity-70", className)}
      {...props}
    >
      {loading && <Spinner data-icon="inline-start" aria-hidden />}
      {children}
    </Button>
  );
}

/** A link that looks like a Button. */
export function LinkBtn({
  variant = "outline",
  size = "sm",
  className,
  ...props
}: ComponentProps<"a"> & VariantProps<typeof buttonVariants>) {
  return (
    <a data-slot="button" className={cn(buttonVariants({ variant, size }), className)} {...props} />
  );
}

/** A labelled text input. Its `error` shows under it and is read with it. */
export function TextField({
  label,
  id,
  className,
  error,
  ...props
}: ComponentProps<typeof Input> & { label: string; error?: string | null }) {
  const auto = useId();
  const field = id || auto;
  return (
    <Field className={className} data-invalid={!!error || undefined}>
      <FieldLabel htmlFor={field}>{label}</FieldLabel>
      <Input
        id={field}
        className="h-9 bg-background"
        aria-invalid={!!error || undefined}
        aria-describedby={error ? `${field}-error` : undefined}
        {...props}
      />
      {error && (
        <p id={`${field}-error`} className="text-sm text-destructive">
          {error}
        </p>
      )}
    </Field>
  );
}

/** A labelled native select. */
export function SelectField({
  label,
  value,
  onChange,
  items,
  autoComplete,
}: {
  label: string;
  value: string;
  onChange: (v: string) => void;
  items: Record<string, string>;
  autoComplete?: string;
}) {
  const id = useId();
  return (
    <Field>
      <FieldLabel htmlFor={id}>{label}</FieldLabel>
      <NativeSelect
        id={id}
        value={value}
        autoComplete={autoComplete}
        onChange={(e) => onChange(e.target.value)}
        className="w-full [&_select]:h-9 [&_select]:bg-background [&_select]:text-base md:[&_select]:text-sm"
      >
        {Object.entries(items).map(([k, v]) => (
          <NativeSelectOption key={k} value={k}>
            {v}
          </NativeSelectOption>
        ))}
      </NativeSelect>
    </Field>
  );
}

export type Choice = { value: string; label: ReactNode; description?: ReactNode };

/** One choice of several, as cards: shadcn's choice-card pattern (a RadioGroup in FieldLabels). */
export function ChoiceCards({
  label,
  value,
  onValueChange,
  choices,
}: {
  label: string;
  value: string;
  onValueChange: (v: string) => void;
  choices: Choice[];
}) {
  const base = useId();
  return (
    <RadioGroup
      aria-label={label}
      value={value}
      onValueChange={(v) => onValueChange(String(v))}
      className="gap-3"
    >
      {choices.map((c) => (
        <FieldLabel
          key={c.value}
          htmlFor={`${base}-${c.value}`}
          className="bg-card has-data-checked:border-primary has-data-checked:bg-card *:data-[slot=field]:px-4 *:data-[slot=field]:py-3"
        >
          <Field orientation="horizontal">
            <FieldContent>
              <FieldTitle>{c.label}</FieldTitle>
              {c.description && <FieldDescription>{c.description}</FieldDescription>}
            </FieldContent>
            <RadioGroupItem value={c.value} id={`${base}-${c.value}`} />
          </Field>
        </FieldLabel>
      ))}
    </RadioGroup>
  );
}

/** shadcn's Alert with a title, text and an optional action: the action beside the text, or under
 *  it on a phone (never over it). Only a problem interrupts a screen reader; the rest wait their turn. */
export function Notice({
  variant = "default",
  icon,
  title,
  description,
  action,
  className,
}: {
  variant?: "default" | "destructive";
  icon?: ReactNode;
  title: ReactNode;
  description?: ReactNode;
  action?: ReactNode;
  className?: string;
}) {
  return (
    <Alert
      variant={variant}
      role={variant === "destructive" ? "alert" : "status"}
      className={cn("block bg-card px-4 py-3", className)}
    >
      <div className="flex flex-col gap-3 sm:flex-row sm:items-center">
        <div className="flex min-w-0 flex-1 gap-2.5">
          {icon && (
            <span aria-hidden className="mt-0.5 shrink-0 [&>svg]:size-4">
              {icon}
            </span>
          )}
          <div className="min-w-0">
            <AlertTitle>{title}</AlertTitle>
            {description && (
              <AlertDescription
                className={variant === "destructive" ? "text-destructive/90" : undefined}
              >
                {description}
              </AlertDescription>
            )}
          </div>
        </div>
        {action && <div className={cn("shrink-0", icon && "max-sm:pl-[1.625rem]")}>{action}</div>}
      </div>
    </Alert>
  );
}

/** iterate's waiting list: the form on iterate.com's homepage. */
const WAITING_LIST = "https://iterate.com/#waiting-list";

/** FOR A PERSON WITH NO DEPLOYMENT, on the wizard's first step and on an empty list: a new
 *  deployment is set up in a personal walkthrough, and the way to one is the waiting list. The
 *  wizard stays open under it, because the walkthrough goes through it. `lead` stands before the
 *  words about the walkthrough. */
export function WalkthroughOnly({
  title,
  lead,
  className,
}: {
  title: string;
  lead?: string;
  className?: string;
}) {
  return (
    <Notice
      className={className}
      title={<span data-walkthrough-only>{title}</span>}
      description={`${lead ? `${lead} ` : ""}For now, Jonas from iterate sets up each new deployment together with its owner. Join the waiting list on iterate.com and he will get in touch.`}
      action={
        <LinkBtn variant="default" href={WAITING_LIST} data-waiting-list>
          Join the waiting list
        </LinkBtn>
      }
    />
  );
}