diff --git a/web/src/components/QuickSwitcher/useQuickSwitcherAllAction.ts b/web/src/components/QuickSwitcher/useQuickSwitcherAllAction.ts new file mode 100644 index 00000000..28049894 --- /dev/null +++ b/web/src/components/QuickSwitcher/useQuickSwitcherAllAction.ts @@ -0,0 +1,79 @@ +import { + isValidStr, + t, + useAppSelector, + useAsync, + useUserId, +} from 'tailchat-shared'; +import { useDebugValue } from 'react'; +import type { QuickActionContext } from './useQuickSwitcherActionContext'; +import { getDMConverseName } from 'tailchat-shared'; +import { ChatConverseType } from 'tailchat-shared/model/converse'; + +export interface QuickAction { + key: string; + source: string; + label: string; + action: (context: QuickActionContext) => void; +} + +const builtinActions: QuickAction[] = [ + { + key: 'personal', + source: 'core', + label: t('个人主页'), + action({ navigate }) { + navigate('/main/personal/friends'); + }, + }, + { + key: 'plugins', + source: 'core', + label: t('插件中心'), + action({ navigate }) { + navigate('/main/personal/plugins'); + }, + }, +]; + +function useDMConverseActions(): QuickAction[] { + const userId = useUserId(); + const dmConverses = useAppSelector((state) => + Object.values(state.chat.converses).filter( + (converse) => converse.type === ChatConverseType.DM + ) + ); + const { value: personalConverseActions = [] } = useAsync(async () => { + if (!isValidStr(userId)) { + return []; + } + + return Promise.all( + dmConverses.map((converse) => + getDMConverseName(userId, converse).then( + (converseName): QuickAction => ({ + key: `qs#converse#${converse._id}`, + label: `${t('私信')} ${converseName}`, + source: 'core', + action: ({ navigate }) => { + navigate(`/main/personal/converse/${converse._id}`); + }, + }) + ) + ) + ); + }, [userId, dmConverses.map((converse) => converse._id).join(',')]); + + useDebugValue(personalConverseActions); + + return personalConverseActions; +} + +/** + * @returns 返回所有的快速操作 + */ +export function useQuickSwitcherAllActions() { + const allActions = [...builtinActions, ...useDMConverseActions()]; + + return allActions; +} diff --git a/web/src/components/QuickSwitcher/useQuickSwitcherFilteredActions.tsx b/web/src/components/QuickSwitcher/useQuickSwitcherFilteredActions.tsx index 23842324..fc2642f7 100644 --- a/web/src/components/QuickSwitcher/useQuickSwitcherFilteredActions.tsx +++ b/web/src/components/QuickSwitcher/useQuickSwitcherFilteredActions.tsx @@ -1,81 +1,19 @@ -import { - isValidStr, - t, - useAppSelector, - useAsync, - useUserId, -} from 'tailchat-shared'; import _take from 'lodash/take'; -import { useDebugValue, useMemo } from 'react'; -import type { QuickActionContext } from './useQuickSwitcherActionContext'; -import { getDMConverseName } from 'tailchat-shared'; -import { ChatConverseType } from 'tailchat-shared/model/converse'; - -interface QuickAction { - key: string; - source: string; - label: string; - action: (context: QuickActionContext) => void; -} - -const builtinActions: QuickAction[] = [ - { - key: 'personal', - source: 'core', - label: t('个人主页'), - action({ navigate }) { - navigate('/main/personal/friends'); - }, - }, - { - key: 'plugins', - source: 'core', - label: t('插件中心'), - action({ navigate }) { - navigate('/main/personal/plugins'); - }, - }, -]; - -function useDMConverseActions(): QuickAction[] { - const userId = useUserId(); - const dmConverses = useAppSelector((state) => - Object.values(state.chat.converses).filter( - (converse) => converse.type === ChatConverseType.DM - ) - ); - const { value: personalConverseActions = [] } = useAsync(async () => { - if (!isValidStr(userId)) { - return []; - } - - return Promise.all( - dmConverses.map((converse) => - getDMConverseName(userId, converse).then( - (converseName): QuickAction => ({ - key: `qs#converse#${converse._id}`, - label: `${t('私信')} ${converseName}`, - source: 'core', - action: ({ navigate }) => { - navigate(`/main/personal/converse/${converse._id}`); - }, - }) - ) - ) - ); - }, [userId, dmConverses.map((converse) => converse._id).join(',')]); - - useDebugValue(personalConverseActions); - - return personalConverseActions; -} +import { useMemo } from 'react'; +import { + QuickAction, + useQuickSwitcherAllActions, +} from './useQuickSwitcherAllAction'; /** * 过滤快速搜索操作 + * 仅获取前5个 * @param keyword 关键字 */ -export function useQuickSwitcherFilteredActions(keyword: string) { - const allActions = [...builtinActions, ...useDMConverseActions()]; +export function useQuickSwitcherFilteredActions( + keyword: string +): QuickAction[] { + const allActions = useQuickSwitcherAllActions(); const filteredActions = useMemo(() => { return _take(