refactor: 抽离QuickSwitcher所有操作到独立文件

pull/81/head
moonrailgun 3 years ago
parent d82279f911
commit 86c2456fbf

@ -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;
}

@ -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(

Loading…
Cancel
Save