fix(frontend): update search API types and fix IMEditor stale closure
Add room/pn search params to SearchMessagesData type definition. Fix stale closure issue in IMEditor useEditor's onUpdate callback.
This commit is contained in:
parent
5a90a475a4
commit
a34cd73fdf
@ -17765,6 +17765,14 @@ export type SearchMessagesData = {
|
||||
* Results per page, default 20, max 100
|
||||
*/
|
||||
per_page?: number;
|
||||
/**
|
||||
* Scope search to a specific room by UUID
|
||||
*/
|
||||
room?: string;
|
||||
/**
|
||||
* Scope search to a specific project by name
|
||||
*/
|
||||
pn?: string;
|
||||
};
|
||||
url: '/api/search/messages';
|
||||
};
|
||||
|
||||
@ -366,6 +366,14 @@ export const IMEditor = forwardRef<IMEditorHandle, IMEditorProps>(function IMEdi
|
||||
|
||||
const slashPool = useMemo(() => [...mentionItems.commands], [mentionItems.commands]);
|
||||
|
||||
// Refs to avoid stale closure in useEditor's onUpdate
|
||||
const atPoolRef = useRef(atPool);
|
||||
const hashPoolRef = useRef(hashPool);
|
||||
const slashPoolRef = useRef(slashPool);
|
||||
useEffect(() => { atPoolRef.current = atPool; }, [atPool]);
|
||||
useEffect(() => { hashPoolRef.current = hashPool; }, [hashPool]);
|
||||
useEffect(() => { slashPoolRef.current = slashPool; }, [slashPool]);
|
||||
|
||||
const selectMention = useCallback((item: MentionItem) => {
|
||||
const editor = editorRef.current;
|
||||
if (!editor) return;
|
||||
@ -518,7 +526,7 @@ export const IMEditor = forwardRef<IMEditorHandle, IMEditorProps>(function IMEdi
|
||||
const q = text.slice(ts - 1, from);
|
||||
|
||||
if (trigger === '@' && q.length >= 1) {
|
||||
const results = filterMentionItems(atPool, q.slice(1));
|
||||
const results = filterMentionItems(atPoolRef.current, q.slice(1));
|
||||
setMentionQuery(q.slice(1));
|
||||
setMentionItems2(results);
|
||||
setMentionIdx(0);
|
||||
@ -533,7 +541,7 @@ export const IMEditor = forwardRef<IMEditorHandle, IMEditorProps>(function IMEdi
|
||||
}
|
||||
}
|
||||
} else if (trigger === '#' && q.length >= 1) {
|
||||
const results = filterMentionItems(hashPool, q.slice(1));
|
||||
const results = filterMentionItems(hashPoolRef.current, q.slice(1));
|
||||
setMentionQuery(q.slice(1));
|
||||
setMentionItems2(results);
|
||||
setMentionIdx(0);
|
||||
@ -548,7 +556,7 @@ export const IMEditor = forwardRef<IMEditorHandle, IMEditorProps>(function IMEdi
|
||||
}
|
||||
}
|
||||
} else if (trigger === '/' && q.length >= 1) {
|
||||
const results = filterMentionItems(slashPool, q.slice(1));
|
||||
const results = filterMentionItems(slashPoolRef.current, q.slice(1));
|
||||
setMentionQuery(q.slice(1));
|
||||
setMentionItems2(results);
|
||||
setMentionIdx(0);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user