blob: f64012e7865c3cc20d5ed69fe7afe130b020e890 [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,
idillon07d31cc2022-12-06 22:40:14 -050028 conversationInfos: ConversationInfos | undefined,
idillon9e542ca2022-12-15 17:54:07 -050029 members: ConversationMember[] | undefined = []
idillon07d31cc2022-12-06 22:40:14 -050030) => {
31 const { t } = useTranslation();
32
33 const adminTitle = conversationInfos?.title as string;
34
35 return useMemo(() => {
36 if (adminTitle) {
37 return adminTitle;
38 }
39
idillon07d31cc2022-12-06 22:40:14 -050040 const options: TranslateEnumerationOptions<ConversationMember> = {
41 elementPartialKey: 'member',
42 getElementValue: (member) => member.getDisplayName(),
43 translaters: [
44 () =>
45 // The user is chatting with themself
idillon6e8ca412022-12-16 11:22:42 -050046 t('conversation_title_1', { member0: account?.getDisplayName() }),
47 (interpolations) => t('conversation_title_1', interpolations),
48 (interpolations) => t('conversation_title_2', interpolations),
49 (interpolations) => t('conversation_title_3', interpolations),
50 (interpolations) => t('conversation_title_4', interpolations),
idillon07d31cc2022-12-06 22:40:14 -050051 (interpolations) => t('conversation_title_more', interpolations),
52 ],
53 };
54
55 return translateEnumeration<ConversationMember>(members, options);
idillon9e542ca2022-12-15 17:54:07 -050056 }, [account, adminTitle, members, t]);
idillon07d31cc2022-12-06 22:40:14 -050057};
idillon18283ac2023-01-07 12:06:42 -050058
59export const useConversationDisplayNameShort = (
60 account: Account | null,
61 title: string | undefined,
62 membersNames: string[]
63): string => {
64 return useMemo(() => {
65 if (title) {
66 return title;
67 }
68
69 if (membersNames.length === 0) {
70 return account?.getDisplayName() || '';
71 }
72
73 const twoFirstMembers = membersNames.slice(0, 2);
74 const baseName = twoFirstMembers.join(', ');
75 const excess = membersNames.length - twoFirstMembers.length;
76
77 if (excess > 0) {
78 return baseName + ' +' + excess;
79 } else {
80 return baseName;
81 }
82 }, [account, title, membersNames]);
83};