From c4e7cb75cd406b77d9db5ce7603374a81e2e0874 Mon Sep 17 00:00:00 2001 From: hjlarry Date: Sat, 4 Oct 2025 11:22:02 +0800 Subject: [PATCH] cache the mentioned users --- .../workflow/comment/mention-input.tsx | 27 ++++++++++++++++--- .../workflow/store/workflow/comment-slice.ts | 20 +++++++++++++- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/web/app/components/workflow/comment/mention-input.tsx b/web/app/components/workflow/comment/mention-input.tsx index 3213942572..8fa43d47a9 100644 --- a/web/app/components/workflow/comment/mention-input.tsx +++ b/web/app/components/workflow/comment/mention-input.tsx @@ -11,6 +11,7 @@ import Button from '@/app/components/base/button' import Avatar from '@/app/components/base/avatar' import cn from '@/utils/classnames' import { type UserProfile, fetchMentionableUsers } from '@/service/workflow-comment' +import { useStore, useWorkflowStore } from '../store' type MentionInputProps = { value: string @@ -42,7 +43,12 @@ export const MentionInput: FC = memo(({ const appId = params.appId as string const textareaRef = useRef(null) - const [mentionUsers, setMentionUsers] = useState([]) + const workflowStore = useWorkflowStore() + const mentionUsersFromStore = useStore(state => ( + appId ? state.mentionableUsersCache[appId] : undefined + )) + const mentionUsers = mentionUsersFromStore ?? [] + const [showMentionDropdown, setShowMentionDropdown] = useState(false) const [mentionQuery, setMentionQuery] = useState('') const [mentionPosition, setMentionPosition] = useState(0) @@ -121,15 +127,28 @@ export const MentionInput: FC = memo(({ }, [value, mentionNameList]) const loadMentionableUsers = useCallback(async () => { - if (!appId) return + if (!appId) + return + + const state = workflowStore.getState() + if (state.mentionableUsersCache[appId] !== undefined) + return + + if (state.mentionableUsersLoading[appId]) + return + + state.setMentionableUsersLoading(appId, true) try { const users = await fetchMentionableUsers(appId) - setMentionUsers(users) + workflowStore.getState().setMentionableUsersCache(appId, users) } catch (error) { console.error('Failed to load mentionable users:', error) } - }, [appId]) + finally { + workflowStore.getState().setMentionableUsersLoading(appId, false) + } + }, [appId, workflowStore]) useEffect(() => { loadMentionableUsers() diff --git a/web/app/components/workflow/store/workflow/comment-slice.ts b/web/app/components/workflow/store/workflow/comment-slice.ts index b870020ba4..c0e9a7a0c0 100644 --- a/web/app/components/workflow/store/workflow/comment-slice.ts +++ b/web/app/components/workflow/store/workflow/comment-slice.ts @@ -1,5 +1,5 @@ import type { StateCreator } from 'zustand' -import type { WorkflowCommentDetail, WorkflowCommentList } from '@/service/workflow-comment' +import type { UserProfile, WorkflowCommentDetail, WorkflowCommentList } from '@/service/workflow-comment' export type CommentSliceShape = { comments: WorkflowCommentList[] @@ -12,6 +12,10 @@ export type CommentSliceShape = { setActiveCommentDetailLoading: (loading: boolean) => void commentDetailCache: Record setCommentDetailCache: (cache: Record) => void + mentionableUsersCache: Record + setMentionableUsersCache: (appId: string, users: UserProfile[]) => void + mentionableUsersLoading: Record + setMentionableUsersLoading: (appId: string, loading: boolean) => void } export const createCommentSlice: StateCreator = set => ({ @@ -25,4 +29,18 @@ export const createCommentSlice: StateCreator = set => ({ setActiveCommentDetailLoading: activeCommentDetailLoading => set({ activeCommentDetailLoading }), commentDetailCache: {}, setCommentDetailCache: commentDetailCache => set({ commentDetailCache }), + mentionableUsersCache: {}, + setMentionableUsersCache: (appId, users) => set(state => ({ + mentionableUsersCache: { + ...state.mentionableUsersCache, + [appId]: users, + }, + })), + mentionableUsersLoading: {}, + setMentionableUsersLoading: (appId, loading) => set(state => ({ + mentionableUsersLoading: { + ...state.mentionableUsersLoading, + [appId]: loading, + }, + })), })