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