feat: 增加会话快速切换

pull/13/head
moonrailgun 4 years ago
parent 64be97d07f
commit c0c56734da

@ -1,4 +1,5 @@
import { getCachedConverseInfo } from '../cache/cache';
import { isValidStr } from '..';
import { getCachedConverseInfo, getCachedUserInfo } from '../cache/cache';
import { t } from '../i18n';
import type { ChatConverseInfo } from '../model/converse';
import { appendUserDMConverse } from '../model/user';
@ -18,3 +19,31 @@ export async function ensureDMConverse(
return converse;
}
/**
*
* @param userId ID()
* @param converse
*/
export async function getDMConverseName(
userId: string,
converse: Pick<ChatConverseInfo, 'name' | 'members'>
): Promise<string> {
if (isValidStr(converse.name)) {
return converse.name;
}
const otherConverseMembers = converse.members.filter((m) => m !== userId); // 成员Id
const len = otherConverseMembers.length;
const otherMembersInfo = await Promise.all(
otherConverseMembers.map((memberId) => getCachedUserInfo(memberId))
);
if (len === 1) {
return otherMembersInfo[0]?.nickname ?? '';
} else if (len === 2) {
return `${otherMembersInfo[0]?.nickname}, ${otherMembersInfo[1]?.nickname}`;
} else {
return `${otherMembersInfo[0]?.nickname}, ${otherMembersInfo[1]?.nickname} ...`;
}
}

@ -40,6 +40,8 @@ export {
} from './contexts/ChatBoxContext';
export { useColorScheme } from './contexts/ColorSchemeContext';
export { getDMConverseName } from './helper/converse-helper';
// i18n
export { t, setLanguage, useTranslation } from './i18n';
export { Trans } from './i18n/Trans';

@ -1,26 +1,17 @@
import { getCachedUserInfo, isValidStr, useAsync } from '../../index';
import { getDMConverseName } from '../../helper/converse-helper';
import { isValidStr, useAsync } from '../../index';
import type { ChatConverseState } from '../slices/chat';
import { useUserId } from './useUserInfo';
export function useDMConverseName(converse: ChatConverseState) {
const userId = useUserId();
const otherConverseMembers = converse.members.filter((m) => m !== userId); // 成员Id
const len = otherConverseMembers.length;
const { value: otherMembersInfo = [] } = useAsync(() => {
return Promise.all(
otherConverseMembers.map((userId) => getCachedUserInfo(userId))
);
}, [otherConverseMembers.join(',')]);
if (isValidStr(converse.name)) {
return converse.name;
const { value: name = '' } = useAsync(async () => {
if (!isValidStr(userId)) {
return '';
}
if (len === 1) {
return otherMembersInfo[0]?.nickname ?? '';
} else if (len === 2) {
return `${otherMembersInfo[0]?.nickname}, ${otherMembersInfo[1]?.nickname}`;
} else {
return `${otherMembersInfo[0]?.nickname}, ${otherMembersInfo[1]?.nickname} ...`;
}
return getDMConverseName(userId, converse);
}, [userId, converse.name, converse.members.join(',')]);
return name;
}

@ -1,7 +1,14 @@
import { t } from 'tailchat-shared';
import {
isValidStr,
t,
useAppSelector,
useAsync,
useUserId,
} from 'tailchat-shared';
import _take from 'lodash/take';
import { useMemo } from 'react';
import { useDebugValue, useMemo } from 'react';
import type { QuickActionContext } from './useQuickSwitcherActionContext';
import { getDMConverseName } from 'tailchat-shared';
interface QuickAction {
key: string;
@ -26,17 +33,49 @@ const builtinActions: QuickAction[] = [
},
];
function usePersonalConverseActions(): QuickAction[] {
const userId = useUserId();
const converses = useAppSelector((state) =>
Object.values(state.chat.converses)
);
const { value: personalConverseActions = [] } = useAsync(async () => {
if (!isValidStr(userId)) {
return [];
}
return Promise.all(
converses.map((converse) =>
getDMConverseName(userId, converse).then(
(converseName): QuickAction => ({
key: `qs#converse#${converse._id}`,
label: `${t('私信')} ${converseName}`,
action: ({ navigate }) => {
navigate(`/main/personal/converse/${converse._id}`);
},
})
)
)
);
}, [userId, converses.map((converse) => converse._id).join(',')]);
useDebugValue(personalConverseActions);
return personalConverseActions;
}
/**
*
* @param keyword
*/
export function useQuickSwitcherFilteredActions(keyword: string) {
const allActions = [...builtinActions, ...usePersonalConverseActions()];
const filteredActions = useMemo(() => {
return _take(
builtinActions.filter((action) => action.label.includes(keyword)),
allActions.filter((action) => action.label.includes(keyword)),
5
);
}, [keyword]);
}, [keyword, allActions.length]);
return filteredActions;
}

Loading…
Cancel
Save