Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions apps/app/src/components/promptbox/PromptBoxInternal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2791,6 +2791,120 @@ describe("PromptBoxInternal mention triggers", () => {
replacement: "#42 Fix login bug",
};

it("applies the first result with Enter for a multiword mention query", async () => {
const { changes, onMentionQueryChange, onSubmit, promptBoxRef } =
renderPromptBox("Ask @fix login", {
mentionSuggestions: [githubIssueSuggestion],
});

await focusPromptEnd(promptBoxRef);
await waitFor(() =>
expect(onMentionQueryChange).toHaveBeenCalledWith("fix login", "@"),
);
await screen.findByRole("button", { name: /Fix login bug/u });

fireEvent.keyDown(getPromptEditorElement(), { key: "Enter" });

await waitFor(() =>
expect(latestValue(changes)).toBe("Ask @#42 Fix login bug "),
);
expect(latestChange(changes)?.mentions).toHaveLength(1);
expect(onSubmit).not.toHaveBeenCalled();
});

it("keeps a dismissed multiword occurrence closed as its query extends", async () => {
const { changes, promptBoxRef } = renderPromptBox("@asdf qwe", {
mentionSuggestions: [githubIssueSuggestion],
});
await focusPromptEnd(promptBoxRef);
await screen.findByRole("button", { name: /Fix login bug/u });

fireEvent.keyDown(getPromptEditorElement(), { key: "Escape" });
await act(async () => promptBoxRef.current?.insertTextAtCursor("rt"));

await waitFor(() => expect(latestValue(changes)).toBe("@asdf qwe rt"));
expect(screen.queryByRole("button", { name: /Fix login bug/u })).toBeNull();
});

it("dismisses a coarse-pointer occurrence from a 44px close target", async () => {
const restorePointer = mockPointerCoarse(true);
try {
const { onMentionQueryChange } = renderPromptBox("@fix", {
mentionSuggestions: [githubIssueSuggestion],
});

const closeButton = await screen.findByRole("button", {
name: "Close suggestions",
});
expect(closeButton.classList).toContain("size-11");
expect(closeButton.parentElement?.classList).toContain("h-11");
expect(closeButton.parentElement?.classList).not.toContain("absolute");

fireEvent.click(closeButton);

await waitFor(() =>
expect(
screen.queryByRole("button", { name: /Fix login bug/u }),
).toBeNull(),
);
expect(getPromptEditorElement().textContent).toBe("@fix");
expect(onMentionQueryChange).toHaveBeenLastCalledWith(null, null);
} finally {
restorePointer();
}
});

it("reopens after a touch-dismissed occurrence is removed and retyped", async () => {
const restorePointer = mockPointerCoarse(true);
const promptBoxRef = createRef<PromptBoxHandle>();

function RetriggerHarness() {
const [value, setValue] = useState("@fix");
return (
<>
<button type="button" onClick={() => setValue("")}>
Remove occurrence
</button>
<button type="button" onClick={() => setValue("@fix")}>
Retype occurrence
</button>
<PromptBoxInternal
{...createPromptBoxProps({
value,
onChange: (nextValue) => setValue(nextValue),
typeahead: buildTypeaheadConfig({
mentionSuggestions: [githubIssueSuggestion],
}),
})}
promptBoxRef={promptBoxRef}
/>
</>
);
}

try {
render(<RetriggerHarness />);
await screen.findByRole("button", { name: /Fix login bug/u });
fireEvent.click(
screen.getByRole("button", { name: "Close suggestions" }),
);

fireEvent.click(
screen.getByRole("button", { name: "Remove occurrence" }),
);
await waitFor(() =>
expect(getPromptEditorElement().textContent).toBe(""),
);
fireEvent.click(
screen.getByRole("button", { name: "Retype occurrence" }),
);

await screen.findByRole("button", { name: /Fix login bug/u });
} finally {
restorePointer();
}
});

it("reports the queued editor typeahead's open state and measured height", async () => {
const layouts: Array<{ height: number; isOpen: boolean }> = [];
const nativeGetBoundingClientRect =
Expand Down
76 changes: 55 additions & 21 deletions apps/app/src/components/promptbox/PromptBoxInternal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1619,11 +1619,23 @@ export function PromptBoxInternal({
const syncTriggerState = useCallback(
(editor: Editor) => {
const caretPosition = editor.state.selection.from;
const dismissedTrigger = dismissedTriggerRef.current;
let dismissedTrigger = dismissedTriggerRef.current;
const isRestoringAppliedMention =
isRestoringAppliedMentionRef.current && dismissedTrigger !== null;

const detectedTrigger = findActiveTrigger(editor, triggers);
if (dismissedTrigger && !isRestoringAppliedMention) {
if (
!dismissedTrigger.hasLeftRange &&
detectedTrigger?.from === dismissedTrigger.start
) {
// Continuing to type extends the same trigger occurrence. Grow the
// dismissed range before checking whether the caret left it.
dismissedTrigger = {
...dismissedTrigger,
end: Math.max(dismissedTrigger.end, caretPosition),
};
dismissedTriggerRef.current = dismissedTrigger;
}
const isWithinDismissedRange =
caretPosition >= dismissedTrigger.start &&
caretPosition <= dismissedTrigger.end;
Expand All @@ -1646,9 +1658,7 @@ export function PromptBoxInternal({
caretPosition <= dismissedTriggerRef.current.end)),
);

const nextTrigger = shouldSuppressTrigger
? null
: findActiveTrigger(editor, triggers);
const nextTrigger = shouldSuppressTrigger ? null : detectedTrigger;
const nextKey = nextTrigger
? `${nextTrigger.kind}:${nextTrigger.from}:${nextTrigger.to}:${nextTrigger.query}`
: "";
Expand Down Expand Up @@ -1896,6 +1906,24 @@ export function PromptBoxInternal({
},
onUpdate({ editor: updatedEditor, transaction }) {
if (skipEditorChangeRef.current) return;
const dismissedTrigger = dismissedTriggerRef.current;
if (
dismissedTrigger !== null &&
transaction.docChanged &&
!isRestoringAppliedMentionRef.current
) {
const mappedStart = transaction.mapping.mapResult(
dismissedTrigger.start,
1,
);
dismissedTriggerRef.current = mappedStart.deleted
? null
: {
...dismissedTrigger,
start: mappedStart.pos,
end: transaction.mapping.map(dismissedTrigger.end, -1),
};
}
const nextValue = promptEditorValueFromDoc(updatedEditor.state.doc);
lastSyncedEditorValueRef.current = nextValue;
onChangeRef.current(nextValue.text, nextValue.mentions);
Expand Down Expand Up @@ -2009,6 +2037,11 @@ export function PromptBoxInternal({
return;
}

// A controlled replacement is a new occurrence. Parent echoes of editor
// updates return above because `lastSyncedEditorValueRef` already matches.
dismissedTriggerRef.current = null;
triggerKeyRef.current = "";

try {
skipEditorChangeRef.current = true;
editor.commands.setContent(
Expand Down Expand Up @@ -2437,6 +2470,20 @@ export function PromptBoxInternal({
[applyCommandSuggestion, applyMentionSuggestion],
);

const dismissActiveTrigger = useCallback(() => {
triggerKeyRef.current = "";
if (activeTrigger) {
dismissedTriggerRef.current = {
start: activeTrigger.from,
end: activeTrigger.to,
hasLeftRange: false,
};
}
setActiveTrigger(null);
onMentionQueryChange(null, null);
onCommandQueryChange(null);
}, [activeTrigger, onCommandQueryChange, onMentionQueryChange]);

const focusEnd = useCallback(() => {
if (isPointerCoarse) {
pendingFocusEndRef.current = false;
Expand Down Expand Up @@ -2931,19 +2978,7 @@ export function PromptBoxInternal({
}
if (event.key === "Escape") {
event.preventDefault();
triggerKeyRef.current = "";
if (activeTrigger) {
// Escape dismisses the typed token span for both kinds — re-trigger
// stays suppressed while the caret remains inside `[from, to]`.
dismissedTriggerRef.current = {
start: activeTrigger.from,
end: activeTrigger.to,
hasLeftRange: false,
};
}
setActiveTrigger(null);
onMentionQueryChange(null, null);
onCommandQueryChange(null);
dismissActiveTrigger();
return true;
}
}
Expand Down Expand Up @@ -3076,7 +3111,6 @@ export function PromptBoxInternal({
[
activeHistoryIndex,
activeSuggestions,
activeTrigger,
activeTriggerKind,
applyHistoryDraft,
applyTrigger,
Expand All @@ -3085,12 +3119,11 @@ export function PromptBoxInternal({
commandHasMore,
commandIsLoadingMore,
dispatchAppCommandKey,
dismissActiveTrigger,
history,
isPointerCoarse,
loadMoreCommands,
onCommandQueryChange,
onEscape,
onMentionQueryChange,
onModifierSubmit,
postCompositionKeyDownEvents,
resetHistorySession,
Expand Down Expand Up @@ -3248,6 +3281,7 @@ export function PromptBoxInternal({
state={typeaheadMenuState}
selectedIndex={selectedIndex}
onApply={applyTrigger}
onDismiss={isPointerCoarse ? dismissActiveTrigger : undefined}
onCommandLoadMore={
canLoadMoreCommands ? loadMoreCommands : undefined
}
Expand Down
Loading
Loading