Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
15.5 KB
Raw Blame
"use client";

import {
  createContext,
  useContext,
  useEffect,
  useId,
  type ReactNode,
} from "react";
import { createPortal } from "react-dom";
import { AnimatePresence, motion } from "motion/react";
import { UI_EASE_IN, UI_EASE_OUT, UI_MOTION_DURATION } from "@multica/ui/lib/motion";
import {
  AlertCircle,
  Building2,
  Check,
  ChevronRight,
  Eye,
  Globe,
  Laptop,
  Loader2,
  UserRound,
} from "@multica/ui/icons";
import { useQuery } from "@tanstack/react-query";
import { useReducedMotion } from "@multica/core/appearance";
import { Card, CardContent } from "@multica/ui/components/ui/card";
import { UI_INDICATOR_SPRING } from "@multica/ui/lib/motion";
import { cn } from "@multica/ui/lib/utils";
import { useCurrentWorkspace } from "@multica/core/paths";
import { memberListOptions } from "@multica/core/workspace/queries";
import { WorkspaceAvatar } from "../../workspace/workspace-avatar";
import { useT } from "../../i18n";

export type SettingsSaveStatus = "idle" | "saving" | "saved" | "error";

/**
 * Where a setting takes effect. Settings mix three storage scopes — the
 * account, the current workspace, and this device — and the page chrome says
 * which one applies so nobody has to guess whether a change follows them.
 */
export type SettingsScope =
  | "account"
  | "all-workspaces"
  | "device"
  | "device-workspace"
  | "workspace"
  | "workspace-only";

/** Anchor attribute search results and legacy `?section=` links scroll to. */
export const SETTINGS_ANCHOR_ATTR = "data-settings-anchor";

const SCOPE_BADGE_CLASS =
  "inline-flex h-5 min-w-0 shrink-0 items-center gap-1 rounded-full bg-muted px-2 text-caption font-medium text-muted-foreground";

export function SettingsScopeBadge({ scope }: { scope: SettingsScope }) {
  const { t } = useT("settings");
  // Only the workspace-bound scopes read the workspace, so account and device
  // pages render without the workspace query.
  if (scope === "workspace" || scope === "workspace-only" || scope === "device-workspace") {
    return <WorkspaceScopeBadge scope={scope} />;
  }
  const Icon = scope === "account" ? UserRound : scope === "all-workspaces" ? Globe : Laptop;
  return (
    <span data-slot="settings-scope" className={SCOPE_BADGE_CLASS}>
      <Icon aria-hidden="true" className="size-3" />
      <span className="truncate">
        {scope === "account"
          ? t(($) => $.layout.scope.account)
          : scope === "all-workspaces"
            ? t(($) => $.layout.scope.all_workspaces)
            : t(($) => $.layout.scope.device)}
      </span>
    </span>
  );
}

function WorkspaceScopeBadge({
  scope,
}: {
  scope: "workspace" | "workspace-only" | "device-workspace";
}) {
  const { t } = useT("settings");
  const workspace = useCurrentWorkspace();
  const name = workspace?.name ?? t(($) => $.page.workspace_fallback);
  // A letter avatar is illegible at chip size, so only a real logo is shown.
  const mark =
    scope === "device-workspace" ? (
      <Laptop aria-hidden="true" className="size-3" />
    ) : workspace?.avatar_url ? (
      <WorkspaceAvatar
        name={name}
        avatarUrl={workspace.avatar_url}
        radius="xs"
        className="size-3 border-0"
      />
    ) : (
      <Building2 aria-hidden="true" className="size-3" />
    );
  const label =
    scope === "workspace"
      ? t(($) => $.layout.scope.workspace, { name })
      : scope === "workspace-only"
        ? t(($) => $.layout.scope.workspace_only, { name })
        : t(($) => $.layout.scope.device_workspace, { name });
  return (
    <span data-slot="settings-scope" className={SCOPE_BADGE_CLASS}>
      {mark}
      <span className="truncate">{label}</span>
    </span>
  );
}

/**
 * The settings page's one header, as a set of slots the open tab fills. The
 * page names the section itself, so it stays put while a tab loads; the tab
 * contributes what only it knows — where its setting takes effect, its
 * page-level actions, and the name of a detail page opened inside it.
 */
export interface SettingsHeaderSlots {
  trail: HTMLElement | null;
  meta: HTMLElement | null;
  actions: HTMLElement | null;
  /** Tells the page a detail is open, so the section name links back. */
  onDetailChange: (open: boolean) => void;
}

export const SettingsHeaderContext = createContext<SettingsHeaderSlots | null>(null);

export function SettingsTab({
  title,
  description,
  scope,
  actions,
  breadcrumb,
  children,
}: {
  title: ReactNode;
  description?: ReactNode;
  scope?: SettingsScope;
  /** Page-level actions, at the right of the page header. */
  actions?: ReactNode;
  /**
   * Parent location for detail pages. Inside the settings page the header
   * shows the trail itself; standalone, this renders above the title.
   */
  breadcrumb?: ReactNode;
  children: ReactNode;
}) {
  const header = useContext(SettingsHeaderContext);
  const detail = breadcrumb != null && breadcrumb !== false;
  const onDetailChange = header?.onDetailChange;
  useEffect(() => {
    if (!onDetailChange || !detail) return;
    onDetailChange(true);
    return () => onDetailChange(false);
  }, [onDetailChange, detail]);

  if (header) {
    return (
      <div className="space-y-8">
        {header.trail && detail
          ? createPortal(
              <>
                <ChevronRight aria-hidden="true" className="size-3 shrink-0 text-faint-foreground" />
                <span aria-current="page" className="min-w-0 truncate">
                  {title}
                </span>
              </>,
              header.trail,
            )
          : null}
        {header.meta && scope
          ? createPortal(<SettingsScopeBadge scope={scope} />, header.meta)
          : null}
        {header.actions && actions ? createPortal(actions, header.actions) : null}
        {description ? (
          <p className="max-w-2xl text-body leading-6 text-muted-foreground">
            {description}
          </p>
        ) : null}
        {children}
      </div>
    );
  }

  return (
    <div className="space-y-8">
      <header>
        {breadcrumb ? <div className="mb-2">{breadcrumb}</div> : null}
        <div className="flex min-w-0 items-start gap-4">
          <div className="min-w-0 flex-1">
            <div className="flex min-w-0 flex-wrap items-center gap-x-2.5 gap-y-1">
              <h2 className="text-title-lg font-semibold tracking-tight">{title}</h2>
              {scope ? <SettingsScopeBadge scope={scope} /> : null}
            </div>
            {description ? (
              <p className="mt-1 max-w-2xl text-body leading-6 text-muted-foreground">
                {description}
              </p>
            ) : null}
          </div>
          {actions ? <div className="shrink-0">{actions}</div> : null}
        </div>
      </header>
      {children}
    </div>
  );
}

export function SettingsSection({
  title,
  description,
  scope,
  action,
  anchor,
  children,
  className,
}: {
  title?: ReactNode;
  description?: ReactNode;
  scope?: SettingsScope;
  action?: ReactNode;
  /** Target for search results and deep links (`?section=<anchor>`). */
  anchor?: string;
  children: ReactNode;
  className?: string;
}) {
  const headingId = useId();
  return (
    <section
      className={cn("scroll-mt-6 space-y-3", className)}
      aria-labelledby={title ? headingId : undefined}
      {...(anchor ? { [SETTINGS_ANCHOR_ATTR]: anchor } : {})}
    >
      {title || description || action ? (
        <div className="flex min-w-0 items-end justify-between gap-4 px-0.5">
          <div className="min-w-0">
            {title ? (
              <div className="flex min-w-0 flex-wrap items-center gap-x-2 gap-y-1">
                <h3 id={headingId} className="text-body font-semibold">
                  {title}
                </h3>
                {scope ? <SettingsScopeBadge scope={scope} /> : null}
              </div>
            ) : null}
            {description ? (
              <p className="mt-1 text-caption leading-5 text-muted-foreground">
                {description}
              </p>
            ) : null}
          </div>
          {action ? <div className="shrink-0">{action}</div> : null}
        </div>
      ) : null}
      {children}
    </section>
  );
}

export function SettingsCard({
  children,
  className,
}: {
  children: ReactNode;
  className?: string;
}) {
  return (
    <Card className={cn("gap-0 py-0 shadow-none", className)}>
      <CardContent className="divide-y divide-surface-border px-0">
        {children}
      </CardContent>
    </Card>
  );
}

/**
 * Width tiers for the control column. Within a card, every text-entry
 * control shares the `text` tier so their edges align; a row may only
 * drop to a smaller tier when the field is deliberately short (a code,
 * an enum select) — the difference must read as intentional. Pick a
 * tier instead of adding per-row ad-hoc widths.
 */
const SETTINGS_CONTROL_WIDTHS = {
  /** Text inputs and textareas — the standard control column. */
  text: "sm:w-96",
  /** Selects/pickers with long option labels (timezone, model). */
  "select-wide": "sm:w-72",
  /** Compact enum selects (theme, language). */
  select: "sm:w-48",
  /** Short fixed-format codes (issue prefix). */
  code: "sm:w-40",
  /** Unconstrained — non-input content like avatar uploads. */
  none: "sm:max-w-none",
} as const;

export type SettingsControlSize = keyof typeof SETTINGS_CONTROL_WIDTHS;

export function SettingsRow({
  label,
  description,
  children,
  className,
  size,
  align = "center",
  anchor,
}: {
  label: ReactNode;
  description?: ReactNode;
  children: ReactNode;
  className?: string;
  /** Control column width tier; omit for content-hugging controls (buttons, switches). */
  size?: SettingsControlSize;
  align?: "center" | "start";
  /** Target for search results and deep links (`?section=<anchor>`). */
  anchor?: string;
}) {
  return (
    <div
      {...(anchor ? { [SETTINGS_ANCHOR_ATTR]: anchor } : {})}
      className={cn(
        "flex min-h-16 scroll-mt-6 gap-4 px-4 py-3.5 transition-colors duration-700 data-[flash=true]:bg-surface-selected sm:justify-between sm:gap-8",
        size ? "flex-col sm:flex-row" : "flex-row justify-between",
        align === "center"
          ? size
            ? "sm:items-center"
            : "items-center"
          : size
            ? "sm:items-start"
            : "items-start",
        className,
      )}
    >
      <div className="min-w-0 flex-1">
        <div className="text-body font-medium">{label}</div>
        {description ? (
          <div className="mt-0.5 text-caption leading-5 text-muted-foreground">
            {description}
          </div>
        ) : null}
      </div>
      <div
        className={cn(
          "shrink-0 sm:w-auto sm:max-w-[56%]",
          size ? "w-full" : "w-auto max-w-[56%]",
          size ? SETTINGS_CONTROL_WIDTHS[size] : undefined,
        )}
      >
        {children}
      </div>
    </div>
  );
}

/** Segmented switch between the lists one settings page manages. */
export function SettingsViewTabs<T extends string>({
  label,
  items,
  value,
  onChange,
}: {
  /** Accessible name of the tab list. */
  label: string;
  items: readonly { value: T; label: ReactNode; count?: number }[];
  value: T;
  onChange: (value: T) => void;
}) {
  const indicatorId = useId();
  const prefersReducedMotion = useReducedMotion();
  const transition = prefersReducedMotion ? { duration: 0 } : UI_INDICATOR_SPRING;

  return (
    <div
      role="tablist"
      aria-label={label}
      className="inline-flex w-fit items-center gap-0.5 rounded-lg bg-muted p-0.5"
    >
      {items.map((item) => {
        const active = value === item.value;
        return (
          <button
            key={item.value}
            type="button"
            role="tab"
            aria-selected={active}
            onClick={() => onChange(item.value)}
            className={cn(
              "relative inline-flex h-7 items-center gap-1.5 rounded-md px-2.5 text-label font-medium transition-colors focus-visible:outline-2 focus-visible:outline-ring",
              active
                ? "text-foreground"
                : "text-muted-foreground hover:text-foreground",
            )}
          >
            {active && (
              <motion.span
                layoutId={indicatorId}
                transition={transition}
                aria-hidden="true"
                data-slot="settings-view-tabs-indicator"
                className="pointer-events-none absolute inset-0 rounded-md bg-background shadow-sm"
              />
            )}
            <span className="relative z-10 inline-flex items-center gap-1.5">
              {item.label}
              {item.count !== undefined ? (
                <>
                  {" "}
                  <span className="text-caption font-normal tabular-nums text-muted-foreground">
                    {item.count}
                  </span>
                </>
              ) : null}
            </span>
          </button>
        );
      })}
    </div>
  );
}

/**
 * The one read-only explanation for workspace pages a member can see but not
 * change. It names who can make the change so the next step is obvious.
 */
export function SettingsReadOnlyNotice({ wsId }: { wsId: string }) {
  const { t } = useT("settings");
  const { data: members = [] } = useQuery({
    ...memberListOptions(wsId),
    enabled: !!wsId,
  });
  const managers = members
    .filter((member) => member.role === "owner" || member.role === "admin")
    .map((member) => member.name)
    .filter(Boolean)
    .slice(0, 3);
  return (
    <div
      role="note"
      className="flex items-start gap-3 rounded-xl border border-surface-border bg-surface px-4 py-3 text-body"
    >
      <Eye aria-hidden="true" className="mt-0.5 size-4 shrink-0 text-muted-foreground" />
      <div className="min-w-0">
        <p className="font-medium">{t(($) => $.layout.read_only.title)}</p>
        <p className="mt-0.5 text-caption leading-5 text-muted-foreground">
          {managers.length > 0
            ? t(($) => $.layout.read_only.contact, {
                names: managers.join(t(($) => $.layout.read_only.separator)),
              })
            : t(($) => $.layout.read_only.description)}
        </p>
      </div>
    </div>
  );
}

export function SettingsSaveState({
  status,
  savingLabel,
  savedLabel,
  errorLabel,
}: {
  status: SettingsSaveStatus;
  savingLabel: string;
  savedLabel: string;
  errorLabel: string;
}) {
  const reduceMotion = useReducedMotion();

  const content =
    status === "saving" ? (
      <>
        <Loader2 className="size-3 animate-spin motion-reduce:animate-none" />
        {savingLabel}
      </>
    ) : status === "saved" ? (
      <>
        <Check className="size-3 text-success" />
        {savedLabel}
      </>
    ) : status === "error" ? (
      <>
        <AlertCircle className="size-3 text-destructive" />
        {errorLabel}
      </>
    ) : null;

  return (
    <AnimatePresence mode="wait" initial={false}>
      {status !== "idle" && (
        <motion.span
          key={status}
          role="status"
          initial={reduceMotion ? false : { opacity: 0, y: 2 }}
          animate={{
            opacity: 1,
            y: 0,
            transition: { duration: reduceMotion ? 0 : UI_MOTION_DURATION.fast, ease: UI_EASE_OUT },
          }}
          exit={{
            opacity: 0,
            transition: { duration: reduceMotion ? 0 : UI_MOTION_DURATION.micro, ease: UI_EASE_IN },
          }}
          className={cn(
            "inline-flex items-center gap-1.5 text-caption text-muted-foreground",
            status === "error" && "text-destructive",
          )}
        >
          {content}
        </motion.span>
      )}
    </AnimatePresence>
  );
}