idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 18 | import { ConversationInfos } from 'jami-web-common'; |
| 19 | import { useMemo } from 'react'; |
| 20 | import { useTranslation } from 'react-i18next'; |
| 21 | |
| 22 | import { Account } from '../models/account'; |
| 23 | import { ConversationMember } from '../models/conversation-member'; |
| 24 | import { translateEnumeration, TranslateEnumerationOptions } from '../utils/translations'; |
| 25 | |
| 26 | export const useConversationDisplayName = ( |
| 27 | account: Account, |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 28 | conversationInfos: ConversationInfos | undefined, |
idillon | 9e542ca | 2022-12-15 17:54:07 -0500 | [diff] [blame] | 29 | members: ConversationMember[] | undefined = [] |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 30 | ) => { |
| 31 | const { t } = useTranslation(); |
| 32 | |
| 33 | const adminTitle = conversationInfos?.title as string; |
| 34 | |
| 35 | return useMemo(() => { |
| 36 | if (adminTitle) { |
| 37 | return adminTitle; |
| 38 | } |
| 39 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 40 | const options: TranslateEnumerationOptions<ConversationMember> = { |
| 41 | elementPartialKey: 'member', |
| 42 | getElementValue: (member) => member.getDisplayName(), |
| 43 | translaters: [ |
| 44 | () => |
| 45 | // The user is chatting with themself |
idillon | 6e8ca41 | 2022-12-16 11:22:42 -0500 | [diff] [blame] | 46 | 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), |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 51 | (interpolations) => t('conversation_title_more', interpolations), |
| 52 | ], |
| 53 | }; |
| 54 | |
| 55 | return translateEnumeration<ConversationMember>(members, options); |
idillon | 9e542ca | 2022-12-15 17:54:07 -0500 | [diff] [blame] | 56 | }, [account, adminTitle, members, t]); |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 57 | }; |