[WIKI-849] feat: debounce for mention search (#8380)

This commit is contained in:
Vipin Chaudhary 2025-12-18 18:44:16 +05:30 committed by GitHub
parent ba7b2a3e27
commit 59f024dad3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 31 additions and 7 deletions

View file

@ -62,6 +62,7 @@
"@tiptap/react": "^2.22.3", "@tiptap/react": "^2.22.3",
"@tiptap/starter-kit": "^2.22.3", "@tiptap/starter-kit": "^2.22.3",
"@tiptap/suggestion": "^2.22.3", "@tiptap/suggestion": "^2.22.3",
"lodash-es": "catalog:",
"buffer": "^6.0.3", "buffer": "^6.0.3",
"emoji-regex": "^10.3.0", "emoji-regex": "^10.3.0",
"highlight.js": "^11.8.0", "highlight.js": "^11.8.0",
@ -85,6 +86,7 @@
"@types/node": "catalog:", "@types/node": "catalog:",
"@types/react": "catalog:", "@types/react": "catalog:",
"@types/react-dom": "catalog:", "@types/react-dom": "catalog:",
"@types/lodash-es": "catalog:",
"postcss": "^8.4.38", "postcss": "^8.4.38",
"tsdown": "catalog:", "tsdown": "catalog:",
"typescript": "catalog:" "typescript": "catalog:"

View file

@ -2,6 +2,7 @@ import { FloatingOverlay } from "@floating-ui/react";
import type { SuggestionProps } from "@tiptap/suggestion"; import type { SuggestionProps } from "@tiptap/suggestion";
import { forwardRef, useCallback, useEffect, useImperativeHandle, useLayoutEffect, useRef, useState } from "react"; import { forwardRef, useCallback, useEffect, useImperativeHandle, useLayoutEffect, useRef, useState } from "react";
import { v4 as uuidv4 } from "uuid"; import { v4 as uuidv4 } from "uuid";
import { debounce } from "lodash-es";
// plane utils // plane utils
import { useOutsideClickDetector } from "@plane/hooks"; import { useOutsideClickDetector } from "@plane/hooks";
import { cn } from "@plane/utils"; import { cn } from "@plane/utils";
@ -75,12 +76,12 @@ export const MentionsListDropdown = forwardRef(function MentionsListDropdown(pro
}); });
}, [sections]); }, [sections]);
// fetch mention sections based on query // debounced search callback
useEffect(() => { const debouncedSearchCallback = useCallback(
const fetchSuggestions = async () => { debounce(async (searchQuery: string) => {
setIsLoading(true); setIsLoading(true);
try { try {
const sectionsResponse = await searchCallback?.(query); const sectionsResponse = await searchCallback?.(searchQuery);
if (sectionsResponse) { if (sectionsResponse) {
setSections(sectionsResponse); setSections(sectionsResponse);
} }
@ -89,9 +90,24 @@ export const MentionsListDropdown = forwardRef(function MentionsListDropdown(pro
} finally { } finally {
setIsLoading(false); setIsLoading(false);
} }
}; }, 300),
fetchSuggestions(); [searchCallback]
}, [query, searchCallback]); );
// trigger debounced search when query changes
useEffect(() => {
if (query) {
void debouncedSearchCallback(query);
}
}, [query, debouncedSearchCallback]);
// cancel pending debounced calls on unmount
useEffect(
() => () => {
debouncedSearchCallback.cancel();
},
[debouncedSearchCallback]
);
// scroll to the dropdown item when navigating via keyboard // scroll to the dropdown item when navigating via keyboard
useLayoutEffect(() => { useLayoutEffect(() => {

6
pnpm-lock.yaml generated
View file

@ -950,6 +950,9 @@ importers:
linkifyjs: linkifyjs:
specifier: ^4.3.2 specifier: ^4.3.2
version: 4.3.2 version: 4.3.2
lodash-es:
specifier: 'catalog:'
version: 4.17.21
lowlight: lowlight:
specifier: ^3.0.0 specifier: ^3.0.0
version: 3.3.0 version: 3.3.0
@ -993,6 +996,9 @@ importers:
'@plane/typescript-config': '@plane/typescript-config':
specifier: workspace:* specifier: workspace:*
version: link:../typescript-config version: link:../typescript-config
'@types/lodash-es':
specifier: 'catalog:'
version: 4.17.12
'@types/node': '@types/node':
specifier: 'catalog:' specifier: 'catalog:'
version: 22.12.0 version: 22.12.0