blob: c8aabae8e5d37f9acf0eedea1791b64c9630d09d [file] [log] [blame]
idillon07d31cc2022-12-06 22:40:14 -05001/*
2 * Copyright (C) 2022 Savoir-faire Linux Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation; either version 3 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
13 *
14 * You should have received a copy of the GNU Affero General Public
15 * License along with this program. If not, see
16 * <https://www.gnu.org/licenses/>.
17 */
18import { ConversationInfos } from 'jami-web-common';
19import { useMemo } from 'react';
20import { useTranslation } from 'react-i18next';
21
22import { Account } from '../models/account';
23import { ConversationMember } from '../models/conversation-member';
24import { translateEnumeration, TranslateEnumerationOptions } from '../utils/translations';
25
26export const useConversationDisplayName = (
27 account: Account,
28 conversationId: string | undefined,
29 conversationInfos: ConversationInfos | undefined,
30 members: ConversationMember[] | undefined
31) => {
32 const { t } = useTranslation();
33
34 const adminTitle = conversationInfos?.title as string;
35
36 return useMemo(() => {
37 if (adminTitle) {
38 return adminTitle;
39 }
40
41 if (!members) {
42 return conversationId;
43 }
44
45 const options: TranslateEnumerationOptions<ConversationMember> = {
46 elementPartialKey: 'member',
47 getElementValue: (member) => member.getDisplayName(),
48 translaters: [
49 () =>
50 // The user is chatting with themself
51 t('conversation_title_one', { member0: account?.getDisplayName() }),
52 (interpolations) => t('conversation_title_one', interpolations),
53 (interpolations) => t('conversation_title_two', interpolations),
54 (interpolations) => t('conversation_title_three', interpolations),
55 (interpolations) => t('conversation_title_four', interpolations),
56 (interpolations) => t('conversation_title_more', interpolations),
57 ],
58 };
59
60 return translateEnumeration<ConversationMember>(members, options);
61 }, [account, adminTitle, conversationId, members, t]);
62};