Skip to content

Commit 2216856

Browse files
authored
fix(ui): prevent useSlashCompletion effects from running during @ completion (#8986)
1 parent 468db87 commit 2216856

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

packages/cli/src/ui/hooks/useSlashCompletion.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,4 +807,47 @@ describe('useSlashCompletion', () => {
807807
});
808808
});
809809
});
810+
811+
it('should not call shared callbacks when disabled', () => {
812+
const mockSetSuggestions = vi.fn();
813+
const mockSetIsLoadingSuggestions = vi.fn();
814+
const mockSetIsPerfectMatch = vi.fn();
815+
816+
const slashCommands = [
817+
createTestCommand({
818+
name: 'help',
819+
description: 'Show help',
820+
}),
821+
];
822+
823+
const { rerender } = renderHook(
824+
({ enabled, query }) =>
825+
useSlashCompletion({
826+
enabled,
827+
query,
828+
slashCommands,
829+
commandContext: mockCommandContext,
830+
setSuggestions: mockSetSuggestions,
831+
setIsLoadingSuggestions: mockSetIsLoadingSuggestions,
832+
setIsPerfectMatch: mockSetIsPerfectMatch,
833+
}),
834+
{
835+
initialProps: { enabled: false, query: '@src/file' },
836+
},
837+
);
838+
839+
// Clear any initial calls
840+
mockSetSuggestions.mockClear();
841+
mockSetIsLoadingSuggestions.mockClear();
842+
mockSetIsPerfectMatch.mockClear();
843+
844+
// Change query while disabled (simulating @ completion typing)
845+
rerender({ enabled: false, query: '@src/file.ts' });
846+
rerender({ enabled: false, query: '@src/file.tsx' });
847+
848+
// Should not have called shared callbacks during @ completion typing
849+
expect(mockSetSuggestions).not.toHaveBeenCalled();
850+
expect(mockSetIsLoadingSuggestions).not.toHaveBeenCalled();
851+
expect(mockSetIsPerfectMatch).not.toHaveBeenCalled();
852+
});
810853
});

packages/cli/src/ui/hooks/useSlashCompletion.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,14 +485,20 @@ export function useSlashCompletion(props: UseSlashCompletionProps): {
485485
);
486486
const { isPerfectMatch } = usePerfectMatch(parserResult);
487487

488-
// Update external state - this is now much simpler and focused
488+
// Clear internal state when disabled
489489
useEffect(() => {
490-
if (!enabled || query === null) {
490+
if (!enabled) {
491491
setSuggestions([]);
492492
setIsLoadingSuggestions(false);
493493
setIsPerfectMatch(false);
494494
setCompletionStart(-1);
495495
setCompletionEnd(-1);
496+
}
497+
}, [enabled, setSuggestions, setIsLoadingSuggestions, setIsPerfectMatch]);
498+
499+
// Update external state only when enabled
500+
useEffect(() => {
501+
if (!enabled || query === null) {
496502
return;
497503
}
498504

0 commit comments

Comments
 (0)