fix(web): scope and label clarification shortcuts (#1847)
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||||
import { useCallback, useEffect, useMemo, useRef, useState, type RefObject } from "react";
|
||||
import { IconX, IconMessageQuestion, IconInfoCircle, IconCheck } from "@tabler/icons-react";
|
||||
import { cn } from "@/lib/utils";
|
||||
import ReactMarkdown from "react-markdown";
|
||||
@@ -12,6 +12,8 @@ import type {
|
||||
ClarificationQuestion,
|
||||
} from "@/lib/types/http";
|
||||
import { useClarificationGroup } from "@/hooks/domains/session/use-clarification-group";
|
||||
import { KeyboardShortcutTooltip } from "@/components/keyboard-shortcut-tooltip";
|
||||
import { SHORTCUTS } from "@/lib/keyboard/constants";
|
||||
import {
|
||||
ClarificationCarouselNav,
|
||||
ClarificationCustomInput,
|
||||
@@ -22,6 +24,7 @@ import {
|
||||
type ClarificationInputOverlayProps = {
|
||||
messages: readonly Message[] | null | undefined;
|
||||
onResolved: () => void;
|
||||
shortcutScopeRef: RefObject<HTMLElement | null>;
|
||||
keyboardShortcutsEnabled?: boolean;
|
||||
};
|
||||
|
||||
@@ -169,6 +172,7 @@ function useResolveCallback(
|
||||
|
||||
type CarouselShortcutArgs = {
|
||||
enabled: boolean;
|
||||
scopeRef: RefObject<HTMLElement | null>;
|
||||
meta: SingleQuestionMeta;
|
||||
activeIndex: number;
|
||||
total: number;
|
||||
@@ -211,13 +215,14 @@ function tryHandleMetaEnter(e: KeyboardEvent, canSubmit: boolean, onSubmit: () =
|
||||
}
|
||||
|
||||
function CarouselKeyboardShortcuts(args: CarouselShortcutArgs) {
|
||||
const { enabled } = args;
|
||||
const { enabled, scopeRef } = args;
|
||||
const optionsCount = args.meta.question.options.length;
|
||||
const isLast = args.activeIndex === args.total - 1;
|
||||
const { canSubmit, onPick, onPrev, onNext, onSkip, onSubmit } = args;
|
||||
useEffect(() => {
|
||||
if (!enabled) return;
|
||||
const onKey = (e: KeyboardEvent) => {
|
||||
if (!(e.target instanceof Node) || !scopeRef.current?.contains(e.target)) return;
|
||||
if (tryHandleMetaEnter(e, canSubmit, onSubmit)) return;
|
||||
if (shouldIgnoreShortcut(e)) return;
|
||||
if (e.key === "Escape") {
|
||||
@@ -244,7 +249,18 @@ function CarouselKeyboardShortcuts(args: CarouselShortcutArgs) {
|
||||
};
|
||||
window.addEventListener("keydown", onKey);
|
||||
return () => window.removeEventListener("keydown", onKey);
|
||||
}, [enabled, optionsCount, isLast, canSubmit, onPick, onPrev, onNext, onSkip, onSubmit]);
|
||||
}, [
|
||||
enabled,
|
||||
scopeRef,
|
||||
optionsCount,
|
||||
isLast,
|
||||
canSubmit,
|
||||
onPick,
|
||||
onPrev,
|
||||
onNext,
|
||||
onSkip,
|
||||
onSubmit,
|
||||
]);
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -257,6 +273,7 @@ type CarouselBodyProps = {
|
||||
setCustomDrafts: React.Dispatch<React.SetStateAction<Record<string, string>>>;
|
||||
allAnswered: boolean;
|
||||
isSubmitting: boolean;
|
||||
shortcutScopeRef: RefObject<HTMLElement | null>;
|
||||
keyboardShortcutsEnabled: boolean;
|
||||
onSubmit: () => void;
|
||||
};
|
||||
@@ -362,6 +379,7 @@ function ClarificationCarouselBody({
|
||||
setCustomDrafts,
|
||||
allAnswered,
|
||||
isSubmitting,
|
||||
shortcutScopeRef,
|
||||
keyboardShortcutsEnabled,
|
||||
onSubmit,
|
||||
}: CarouselBodyProps) {
|
||||
@@ -418,7 +436,8 @@ function ClarificationCarouselBody({
|
||||
/>
|
||||
)}
|
||||
<CarouselKeyboardShortcuts
|
||||
enabled={keyboardShortcutsEnabled}
|
||||
enabled={keyboardShortcutsEnabled && !isSubmitting}
|
||||
scopeRef={shortcutScopeRef}
|
||||
meta={meta}
|
||||
activeIndex={activeIndex}
|
||||
total={total}
|
||||
@@ -433,9 +452,76 @@ function ClarificationCarouselBody({
|
||||
);
|
||||
}
|
||||
|
||||
function ClarificationHeaderActions({
|
||||
total,
|
||||
allAnswered,
|
||||
isSubmitting,
|
||||
onSubmit,
|
||||
onSkip,
|
||||
}: {
|
||||
total: number;
|
||||
allAnswered: boolean;
|
||||
isSubmitting: boolean;
|
||||
onSubmit: () => void;
|
||||
onSkip: () => void;
|
||||
}) {
|
||||
return (
|
||||
<div className="flex items-center gap-2">
|
||||
{total > 1 && (
|
||||
<KeyboardShortcutTooltip
|
||||
shortcut={SHORTCUTS.SUBMIT}
|
||||
description="Submit answers"
|
||||
enabled={!isSubmitting}
|
||||
>
|
||||
<span
|
||||
className="inline-flex"
|
||||
data-testid="clarification-submit-shortcut"
|
||||
tabIndex={!allAnswered && !isSubmitting ? 0 : undefined}
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
onClick={onSubmit}
|
||||
disabled={!allAnswered || isSubmitting}
|
||||
data-testid="clarification-submit"
|
||||
className={cn(
|
||||
"inline-flex items-center gap-1 text-xs px-3 py-1 rounded font-medium transition-colors",
|
||||
allAnswered && !isSubmitting
|
||||
? "bg-blue-500 text-white hover:bg-blue-500/90 cursor-pointer"
|
||||
: "bg-muted text-muted-foreground cursor-not-allowed",
|
||||
)}
|
||||
>
|
||||
{isSubmitting ? "Submitting…" : "Submit"}
|
||||
<IconCheck className="h-3 w-3" />
|
||||
</button>
|
||||
</span>
|
||||
</KeyboardShortcutTooltip>
|
||||
)}
|
||||
<KeyboardShortcutTooltip
|
||||
shortcut={SHORTCUTS.CANCEL}
|
||||
description="Skip all questions"
|
||||
enabled={!isSubmitting}
|
||||
>
|
||||
<span className="inline-flex" data-testid="clarification-skip-shortcut">
|
||||
<button
|
||||
type="button"
|
||||
onClick={onSkip}
|
||||
disabled={isSubmitting}
|
||||
className="text-muted-foreground hover:text-foreground cursor-pointer disabled:opacity-50"
|
||||
data-testid="clarification-skip"
|
||||
aria-label="Skip all questions"
|
||||
>
|
||||
<IconX className="h-4 w-4" />
|
||||
</button>
|
||||
</span>
|
||||
</KeyboardShortcutTooltip>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function ClarificationInputOverlay({
|
||||
messages,
|
||||
onResolved,
|
||||
shortcutScopeRef,
|
||||
keyboardShortcutsEnabled = true,
|
||||
}: ClarificationInputOverlayProps) {
|
||||
const sortedMessages = useMemo(
|
||||
@@ -492,35 +578,13 @@ export function ClarificationInputOverlay({
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
{total > 1 && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleSubmit}
|
||||
disabled={!allAnswered || isSubmitting}
|
||||
data-testid="clarification-submit"
|
||||
className={cn(
|
||||
"inline-flex items-center gap-1 text-xs px-3 py-1 rounded font-medium transition-colors",
|
||||
allAnswered && !isSubmitting
|
||||
? "bg-blue-500 text-white hover:bg-blue-500/90 cursor-pointer"
|
||||
: "bg-muted text-muted-foreground cursor-not-allowed",
|
||||
)}
|
||||
>
|
||||
{isSubmitting ? "Submitting…" : "Submit"}
|
||||
<IconCheck className="h-3 w-3" />
|
||||
</button>
|
||||
)}
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => void group.skipAll("User skipped")}
|
||||
disabled={isSubmitting}
|
||||
className="text-muted-foreground hover:text-foreground cursor-pointer disabled:opacity-50"
|
||||
data-testid="clarification-skip"
|
||||
aria-label="Skip all questions"
|
||||
>
|
||||
<IconX className="h-4 w-4" />
|
||||
</button>
|
||||
</div>
|
||||
<ClarificationHeaderActions
|
||||
total={total}
|
||||
allAnswered={allAnswered}
|
||||
isSubmitting={isSubmitting}
|
||||
onSubmit={handleSubmit}
|
||||
onSkip={() => void group.skipAll("User skipped")}
|
||||
/>
|
||||
</div>
|
||||
<ClarificationCarouselBody
|
||||
sortedMessages={sortedMessages}
|
||||
@@ -531,6 +595,7 @@ export function ClarificationInputOverlay({
|
||||
setCustomDrafts={setCustomDrafts}
|
||||
allAnswered={allAnswered}
|
||||
isSubmitting={isSubmitting}
|
||||
shortcutScopeRef={shortcutScopeRef}
|
||||
keyboardShortcutsEnabled={keyboardShortcutsEnabled}
|
||||
onSubmit={handleSubmit}
|
||||
/>
|
||||
|
||||
@@ -6,6 +6,8 @@ import { Textarea } from "@kandev/ui/textarea";
|
||||
import { useResponsiveBreakpoint } from "@/hooks/use-responsive-breakpoint";
|
||||
import { cn } from "@/lib/utils";
|
||||
import type { ClarificationOption } from "@/lib/types/http";
|
||||
import { KeyboardShortcutTooltip } from "@/components/keyboard-shortcut-tooltip";
|
||||
import { KEYS } from "@/lib/keyboard/constants";
|
||||
|
||||
// Grow the custom-answer box up to ~6 lines, then scroll internally so the
|
||||
// clarification overlay stays compact.
|
||||
@@ -328,36 +330,52 @@ export function ClarificationCarouselNav({
|
||||
const isLast = activeIndex === total - 1;
|
||||
return (
|
||||
<div className="flex items-center justify-between gap-2 px-4 pb-3">
|
||||
<button
|
||||
type="button"
|
||||
onClick={onPrev}
|
||||
disabled={isFirst || isSubmitting}
|
||||
data-testid="clarification-prev"
|
||||
className={cn(
|
||||
"inline-flex items-center gap-1 text-xs px-2 py-1 rounded border",
|
||||
isFirst
|
||||
? "border-transparent text-muted-foreground/40 cursor-not-allowed"
|
||||
: "border-border text-foreground/80 hover:bg-muted/50 cursor-pointer",
|
||||
)}
|
||||
<KeyboardShortcutTooltip
|
||||
shortcut={{ key: KEYS.ARROW_LEFT }}
|
||||
description="Previous question"
|
||||
enabled={!isFirst && !isSubmitting}
|
||||
>
|
||||
<IconArrowLeft className="h-3 w-3" />
|
||||
Back
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={onNext}
|
||||
disabled={isLast || isSubmitting}
|
||||
data-testid="clarification-next"
|
||||
className={cn(
|
||||
"inline-flex items-center gap-1 text-xs px-2 py-1 rounded border",
|
||||
isLast
|
||||
? "border-transparent text-muted-foreground/40 cursor-not-allowed"
|
||||
: "border-border text-foreground/80 hover:bg-muted/50 cursor-pointer",
|
||||
)}
|
||||
<span className="inline-flex">
|
||||
<button
|
||||
type="button"
|
||||
onClick={onPrev}
|
||||
disabled={isFirst || isSubmitting}
|
||||
data-testid="clarification-prev"
|
||||
className={cn(
|
||||
"inline-flex items-center gap-1 text-xs px-2 py-1 rounded border",
|
||||
isFirst
|
||||
? "border-transparent text-muted-foreground/40 cursor-not-allowed"
|
||||
: "border-border text-foreground/80 hover:bg-muted/50 cursor-pointer",
|
||||
)}
|
||||
>
|
||||
<IconArrowLeft className="h-3 w-3" />
|
||||
Back
|
||||
</button>
|
||||
</span>
|
||||
</KeyboardShortcutTooltip>
|
||||
<KeyboardShortcutTooltip
|
||||
shortcut={{ key: KEYS.ARROW_RIGHT }}
|
||||
description="Next question"
|
||||
enabled={!isLast && !isSubmitting}
|
||||
>
|
||||
Next
|
||||
<IconArrowRight className="h-3 w-3" />
|
||||
</button>
|
||||
<span className="inline-flex">
|
||||
<button
|
||||
type="button"
|
||||
onClick={onNext}
|
||||
disabled={isLast || isSubmitting}
|
||||
data-testid="clarification-next"
|
||||
className={cn(
|
||||
"inline-flex items-center gap-1 text-xs px-2 py-1 rounded border",
|
||||
isLast
|
||||
? "border-transparent text-muted-foreground/40 cursor-not-allowed"
|
||||
: "border-border text-foreground/80 hover:bg-muted/50 cursor-pointer",
|
||||
)}
|
||||
>
|
||||
Next
|
||||
<IconArrowRight className="h-3 w-3" />
|
||||
</button>
|
||||
</span>
|
||||
</KeyboardShortcutTooltip>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import type { MouseEvent, RefObject } from "react";
|
||||
|
||||
const interactiveSelector =
|
||||
"input, textarea, select, button, a, [contenteditable], [tabindex]:not([tabindex='-1'])";
|
||||
|
||||
export function routePanelMouseDown(
|
||||
event: MouseEvent<HTMLDivElement>,
|
||||
ref: RefObject<HTMLDivElement | null>,
|
||||
): void {
|
||||
const target = event.target as HTMLElement | null;
|
||||
if (!target || target.closest(interactiveSelector)) return;
|
||||
ref.current?.focus({ preventScroll: true });
|
||||
}
|
||||
@@ -18,6 +18,7 @@ import { useSessionSearch } from "@/hooks/domains/session/use-session-search";
|
||||
import { useLazyLoadMessages } from "@/hooks/use-lazy-load-messages";
|
||||
import { useAppStore } from "@/components/state-provider";
|
||||
import type { Message } from "@/lib/types/http";
|
||||
import { routePanelMouseDown } from "./chat/route-panel-mouse-down";
|
||||
|
||||
function useClarificationKey(agentMessageCount: number) {
|
||||
const lastCountRef = useRef(agentMessageCount);
|
||||
@@ -236,6 +237,7 @@ export const TaskChatPanel = memo(function TaskChatPanel({
|
||||
height={clarificationHeight}
|
||||
messages={pendingClarificationGroup}
|
||||
onResolved={handleClarificationResolved}
|
||||
shortcutScopeRef={panelRef}
|
||||
/>
|
||||
<ChatFooter
|
||||
isArchived={isArchived}
|
||||
@@ -262,6 +264,7 @@ type ClarificationSectionProps = {
|
||||
height: number | null;
|
||||
messages: readonly Message[] | null | undefined;
|
||||
onResolved: () => void;
|
||||
shortcutScopeRef: RefObject<HTMLElement | null>;
|
||||
};
|
||||
|
||||
function ClarificationSection({
|
||||
@@ -272,6 +275,7 @@ function ClarificationSection({
|
||||
height,
|
||||
messages,
|
||||
onResolved,
|
||||
shortcutScopeRef,
|
||||
}: ClarificationSectionProps) {
|
||||
if (!pendingClarification || isArchived) return null;
|
||||
return (
|
||||
@@ -283,7 +287,11 @@ function ClarificationSection({
|
||||
className="px-1 overflow-y-scroll overscroll-contain max-h-[50vh]"
|
||||
style={height === null ? undefined : { height }}
|
||||
>
|
||||
<ClarificationInputOverlay messages={messages} onResolved={onResolved} />
|
||||
<ClarificationInputOverlay
|
||||
messages={messages}
|
||||
onResolved={onResolved}
|
||||
shortcutScopeRef={shortcutScopeRef}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
@@ -340,21 +348,3 @@ function ChatFooter({
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
// routePanelMouseDown routes non-interactive clicks to the panel root so that
|
||||
// Ctrl+F can detect focus within the session panel. Extracted to keep
|
||||
// TaskChatPanel's cyclomatic complexity within limits.
|
||||
const interactiveSelector =
|
||||
"input, textarea, select, button, a, [contenteditable], [tabindex]:not([tabindex='-1'])";
|
||||
|
||||
function routePanelMouseDown(
|
||||
e: React.MouseEvent<HTMLDivElement>,
|
||||
ref: React.RefObject<HTMLDivElement | null>,
|
||||
): void {
|
||||
const target = e.target as HTMLElement | null;
|
||||
if (!target) return;
|
||||
// Exclude `tabindex="-1"` so we don't match PanelRoot itself (which is
|
||||
// marked focus-receivable but shouldn't short-circuit this handler).
|
||||
if (target.closest(interactiveSelector)) return;
|
||||
ref.current?.focus({ preventScroll: true });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user