Skip to content

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

Go
15.3 KB
Raw Blame
"use client";

import { useMemo, useState } from "react";
import { Keyboard, RotateCcw, Search, X } from "@multica/ui/icons";
import { Button } from "@multica/ui/components/ui/button";
import { IconButton } from "@multica/ui/components/common/icon-button";
import { Input } from "@multica/ui/components/ui/input";
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
} from "@multica/ui/components/ui/alert-dialog";
import { cn } from "@multica/ui/lib/utils";
import {
  canStartShortcutSequence,
  findShortcutConflict,
  createShortcutChord,
  isReservedShortcut,
  isShortcutAllowedForAction,
  isShortcutModifierKey,
  isPlainShortcut,
  resolveShortcut,
  shortcutFirstStep,
  shortcutFromEvent,
  SHORTCUT_ACTIONS,
  useShortcutStore,
  type ShortcutActionDefinition,
  type ShortcutActionId,
  type ShortcutCategory,
  type ShortcutChord,
} from "@multica/core/shortcuts";
import { isImeComposing } from "@multica/core/utils";
import { useT } from "../../i18n";
import { ShortcutKeycaps } from "../../common/shortcut-keycaps";
import {
  SettingsCard,
  SettingsRow,
  SettingsSection,
  SettingsTab,
} from "./settings-layout";

type CaptureError =
  | { kind: "conflict"; actionId: ShortcutActionId }
  | { kind: "reserved" }
  | { kind: "send" }
  | { kind: "unsafe" }
  | { kind: "unrecognized" }
  | null;

export function KeyboardShortcutsTab() {
  const { t } = useT("settings");
  const [query, setQuery] = useState("");
  const [recording, setRecording] = useState<ShortcutActionId | null>(null);
  // The first key of a sequence being recorded, waiting for the second.
  const [pendingFirst, setPendingFirst] = useState<ShortcutChord | null>(null);
  const [captureError, setCaptureError] = useState<CaptureError>(null);
  const [resetConfirmOpen, setResetConfirmOpen] = useState(false);
  const overrides = useShortcutStore((state) => state.overrides);
  const setShortcut = useShortcutStore((state) => state.setShortcut);
  const resetShortcut = useShortcutStore((state) => state.resetShortcut);
  const resetAll = useShortcutStore((state) => state.resetAll);

  const visibleActions = useMemo(() => {
    const needle = query.trim().toLocaleLowerCase();
    if (!needle) return SHORTCUT_ACTIONS;
    return SHORTCUT_ACTIONS.filter((action) => {
      const label = t(($) => $.shortcuts.actions[action.id].label);
      const description = t(
        ($) => $.shortcuts.actions[action.id].description,
      );
      return `${label} ${description}`
        .toLocaleLowerCase()
        .includes(needle);
    });
  }, [query, t]);

  const groups: readonly ShortcutCategory[] = ["general", "issue", "navigation"];

  const stopRecording = () => {
    setRecording(null);
    setPendingFirst(null);
    setCaptureError(null);
  };

  const capture = (actionId: ShortcutActionId, event: React.KeyboardEvent) => {
    event.preventDefault();
    event.stopPropagation();
    if (event.repeat || isImeComposing(event)) return;
    if (event.key === "Escape") {
      stopRecording();
      return;
    }

    if (pendingFirst) {
      // Backspace takes the first key back; Enter keeps it as a single key.
      if (event.key === "Backspace" || event.key === "Delete") {
        setPendingFirst(null);
        setCaptureError(null);
        return;
      }
      if (event.key === "Enter") {
        commit(actionId, pendingFirst);
        return;
      }
      const then = shortcutFromEvent(event.nativeEvent);
      if (!then) {
        if (!isShortcutModifierKey(event.key)) setCaptureError({ kind: "unrecognized" });
        return;
      }
      commit(actionId, { ...pendingFirst, then });
      return;
    }

    if (event.key === "Backspace" || event.key === "Delete") {
      setShortcut(actionId, null);
      stopRecording();
      return;
    }

    const shortcut = shortcutFromEvent(event.nativeEvent);
    if (!shortcut) {
      if (!isShortcutModifierKey(event.key)) {
        setCaptureError({ kind: "unrecognized" });
      }
      return;
    }
    if (canStartShortcutSequence(actionId, shortcut)) {
      setPendingFirst(shortcut);
      setCaptureError(null);
      return;
    }
    commit(actionId, shortcut, event.nativeEvent);
  };

  const commit = (actionId: ShortcutActionId, shortcut: ShortcutChord, event?: KeyboardEvent) => {
    if (isReservedShortcut(shortcutFirstStep(shortcut))) {
      setCaptureError({ kind: "reserved" });
      return;
    }
    if (!isShortcutAllowedForAction(actionId, shortcut)) {
      setCaptureError({ kind: actionId === "send" ? "send" : "unsafe" });
      return;
    }
    const conflict = findShortcutConflict(actionId, shortcut, event);
    if (conflict) {
      setCaptureError({ kind: "conflict", actionId: conflict });
      return;
    }

    setShortcut(actionId, shortcut);
    stopRecording();
  };

  return (
    <SettingsTab
      title={t(($) => $.shortcuts.title)}
      description={t(($) => $.shortcuts.description)}
      scope="device"
    >
      <div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
        <div className="relative min-w-0 flex-1 sm:max-w-sm">
          <Search className="pointer-events-none absolute left-2.5 top-1/2 size-4 -translate-y-1/2 text-muted-foreground" />
          <Input
            value={query}
            onChange={(event) => setQuery(event.target.value)}
            placeholder={t(($) => $.shortcuts.search_placeholder)}
            aria-label={t(($) => $.shortcuts.search_placeholder)}
            className="pl-8"
          />
        </div>
        <Button
          variant="outline"
          size="sm"
          onClick={() => setResetConfirmOpen(true)}
          disabled={Object.keys(overrides).length === 0}
        >
          <RotateCcw className="size-3.5" />
          {t(($) => $.shortcuts.reset_all)}
        </Button>
      </div>

      {groups.map((category) => {
        const actions = visibleActions.filter(
          (action) => action.category === category,
        );
        if (actions.length === 0) return null;
        return (
          <SettingsSection
            key={category}
            title={t(($) => $.shortcuts.categories[category])}
            description={category === "issue" ? t(($) => $.shortcuts.issue_scope) : undefined}
          >
            <SettingsCard>
              {actions.map((action) => (
                <ShortcutRow
                  key={action.id}
                  action={action}
                  shortcut={resolveShortcut(overrides, action.id)}
                  customized={Object.prototype.hasOwnProperty.call(
                    overrides,
                    action.id,
                  )}
                  recording={recording === action.id}
                  pendingFirst={recording === action.id ? pendingFirst : null}
                  error={recording === action.id ? captureError : null}
                  onStartRecording={() => {
                    setRecording(action.id);
                    setPendingFirst(null);
                    setCaptureError(null);
                  }}
                  onCancelRecording={stopRecording}
                  onCapture={(event) => capture(action.id, event)}
                  onDisable={() => {
                    setShortcut(action.id, null);
                    setCaptureError(null);
                  }}
                  onReset={() => {
                    resetShortcut(action.id);
                    setCaptureError(null);
                  }}
                />
              ))}
            </SettingsCard>
          </SettingsSection>
        );
      })}

      {visibleActions.length === 0 ? (
        <div className="rounded-lg border border-dashed px-4 py-10 text-center text-body text-muted-foreground">
          {t(($) => $.shortcuts.no_results)}
        </div>
      ) : null}

      <SettingsSection
        title={t(($) => $.shortcuts.fixed.title)}
        description={t(($) => $.shortcuts.fixed.description)}
      >
        <SettingsCard>
          <FixedShortcutRow label={t(($) => $.shortcuts.fixed.open_settings)} shortcut={createShortcutChord(",", { primary: true })} />
          <FixedShortcutRow label={t(($) => $.shortcuts.fixed.close_tab)} shortcut={createShortcutChord("W", { primary: true })} />
          <FixedShortcutRow label={t(($) => $.shortcuts.fixed.select_tab_1_to_8)} shortcut={createShortcutChord("1–8", { primary: true })} />
          <FixedShortcutRow label={t(($) => $.shortcuts.fixed.select_last_tab)} shortcut={createShortcutChord("9", { primary: true })} />
          <FixedShortcutRow label={t(($) => $.shortcuts.fixed.zoom_in)} shortcut={createShortcutChord("Plus", { primary: true })} />
          <FixedShortcutRow label={t(($) => $.shortcuts.fixed.zoom_out)} shortcut={createShortcutChord("Minus", { primary: true })} />
          <FixedShortcutRow label={t(($) => $.shortcuts.fixed.reset_zoom)} shortcut={createShortcutChord("0", { primary: true })} />
          <FixedShortcutRow label={t(($) => $.shortcuts.fixed.close_dialog)} shortcut={createShortcutChord("Escape")} />
        </SettingsCard>
      </SettingsSection>

      <AlertDialog open={resetConfirmOpen} onOpenChange={setResetConfirmOpen}>
        <AlertDialogContent>
          <AlertDialogHeader>
            <AlertDialogTitle>
              {t(($) => $.shortcuts.reset_confirm.title)}
            </AlertDialogTitle>
            <AlertDialogDescription>
              {t(($) => $.shortcuts.reset_confirm.description)}
            </AlertDialogDescription>
          </AlertDialogHeader>
          <AlertDialogFooter>
            <AlertDialogCancel>
              {t(($) => $.shortcuts.reset_confirm.cancel)}
            </AlertDialogCancel>
            <AlertDialogAction
              variant="destructive"
              onClick={() => {
                resetAll();
                stopRecording();
                setResetConfirmOpen(false);
              }}
            >
              {t(($) => $.shortcuts.reset_confirm.confirm)}
            </AlertDialogAction>
          </AlertDialogFooter>
        </AlertDialogContent>
      </AlertDialog>
    </SettingsTab>
  );
}

function ShortcutRow({
  action,
  shortcut,
  customized,
  recording,
  pendingFirst,
  error,
  onStartRecording,
  onCancelRecording,
  onCapture,
  onDisable,
  onReset,
}: {
  action: ShortcutActionDefinition;
  shortcut: ShortcutChord | null;
  customized: boolean;
  recording: boolean;
  pendingFirst: ShortcutChord | null;
  error: CaptureError;
  onStartRecording: () => void;
  onCancelRecording: () => void;
  onCapture: (event: React.KeyboardEvent) => void;
  onDisable: () => void;
  onReset: () => void;
}) {
  const { t } = useT("settings");
  const label = t(($) => $.shortcuts.actions[action.id].label);
  // Keep descriptions in the search index, but only show non-obvious behavior.
  const description = [
    "openSearch",
    "toggleRightSidebar",
    "archiveInboxItem",
    "unsubscribeArchiveInboxItem",
    "snoozeInboxItem",
    "markInboxUnread",
    "nextInboxItem",
    "prevInboxItem",
    "undoInboxAction",
    "runIssueQuickAction",
    "replyToIssue",
    "send",
  ].includes(action.id)
    ? t(($) => $.shortcuts.actions[action.id].description)
    : undefined;
  const errorText = error?.kind === "reserved"
    ? t(($) => $.shortcuts.reserved_error)
    : error?.kind === "send"
      ? t(($) => $.shortcuts.send_error)
    : error?.kind === "unsafe"
      ? t(($) => $.shortcuts.unsafe_error)
    : error?.kind === "unrecognized"
      ? t(($) => $.shortcuts.unrecognized_error)
    : error?.kind === "conflict"
      ? t(($) => $.shortcuts.conflict_error, {
          action: t(($) => $.shortcuts.actions[error.actionId].label),
        })
      : null;

  return (
    <SettingsRow
      label={label}
      description={
        action.id === "send" && isPlainShortcut(shortcut, "Enter") ? (
          <>
            {description}
            <span className="mt-1 block text-brand">
              {t(($) => $.shortcuts.send_enter_hint)}
            </span>
          </>
        ) : description
      }
      align="start"
      size="select-wide"
    >
      <div className="flex flex-col items-stretch gap-1.5 sm:items-end">
        <div className="flex items-center justify-end gap-1.5">
          <button
            type="button"
            onClick={onStartRecording}
            onKeyDown={recording ? onCapture : undefined}
            onBlur={onCancelRecording}
            className={cn(
              "inline-flex h-8 min-w-28 items-center justify-center rounded-md border bg-background px-2.5 font-mono text-caption font-medium shadow-xs outline-none transition-colors hover:bg-surface-hover focus-visible:ring-2 focus-visible:ring-ring",
              recording && "border-brand bg-brand/5 text-brand ring-2 ring-brand/20",
              error && "border-destructive text-destructive ring-destructive/20",
            )}
            aria-label={t(($) => $.shortcuts.record_aria, { action: label })}
            aria-pressed={recording}
            data-shortcut-recorder={recording ? "" : undefined}
          >
            {pendingFirst ? (
              <span className="inline-flex items-center gap-1.5 font-sans">
                <ShortcutKeycaps shortcut={pendingFirst} layout="split" decorative />
                {t(($) => $.shortcuts.then)}…
              </span>
            ) : recording ? (
              <span className="inline-flex items-center gap-1.5 font-sans">
                <Keyboard className="size-3.5" />
                {t(($) => $.shortcuts.recording)}
              </span>
            ) : shortcut ? (
              <ShortcutKeycaps shortcut={shortcut} layout="split" decorative thenLabel={t(($) => $.shortcuts.then)} />
            ) : (
              <span className="font-sans font-normal text-muted-foreground">
                {t(($) => $.shortcuts.unassigned)}
              </span>
            )}
          </button>
          <IconButton
            type="button"
            variant="ghost"
            size="icon-sm"
            onClick={onReset}
            disabled={!customized}
            label={t(($) => $.shortcuts.reset_action, { action: label })}
          >
            <RotateCcw className="size-3.5" />
          </IconButton>
          <IconButton
            type="button"
            variant="ghost"
            size="icon-sm"
            onClick={onDisable}
            disabled={shortcut === null}
            label={t(($) => $.shortcuts.disable_action, { action: label })}
          >
            <X className="size-3.5" />
          </IconButton>
        </div>
        {errorText ? (
          <span role="alert" className="max-w-72 text-right text-caption text-destructive">
            {errorText}
          </span>
        ) : pendingFirst ? (
          <span className="max-w-72 text-right text-micro text-muted-foreground">
            {t(($) => $.shortcuts.record_then_hint)}
          </span>
        ) : recording ? (
          <span className="max-w-72 text-right text-micro text-muted-foreground">
            {action.category === "navigation"
              ? t(($) => $.shortcuts.record_sequence_hint)
              : t(($) => $.shortcuts.record_hint)}
          </span>
        ) : null}
      </div>
    </SettingsRow>
  );
}

function FixedShortcutRow({ label, shortcut }: { label: string; shortcut: ShortcutChord }) {
  return (
    <SettingsRow label={label}>
      <ShortcutKeycaps shortcut={shortcut} size="md" layout="split" className="justify-end" />
    </SettingsRow>
  );
}