/* * Wire * Copyright (C) 2022 Wire Swiss GmbH * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. * */ import React, {Fragment} from 'react'; import {FadingScrollbar} from 'Components/FadingScrollbar'; import {ContentMessage} from 'src/script/entity/message/ContentMessage'; import {useKoSubscribableChildren} from 'Util/ComponentUtil'; import {t} from 'Util/LocalizerUtil'; import {formatLocale, isThisYear, isToday} from 'Util/TimeUtil'; import {noop} from 'Util/util'; import {CollectionItem} from './CollectionItem'; import {Conversation} from '../../../../entity/Conversation'; interface CollectionDetailsProps { conversation: Conversation; messages: ContentMessage[]; onClose?: () => void; onImageClick?: (message: ContentMessage) => void; } type GroupedCollection = [string, ContentMessage[]][]; const getTitleForHeader = (timestamp: number) => { if (isToday(timestamp)) { return t('conversationToday'); } return isThisYear(timestamp) ? formatLocale(timestamp, 'MMMM') : formatLocale(timestamp, 'MMMM y'); }; const groupByDate = (messages: ContentMessage[]): GroupedCollection => { return Object.entries( messages.reduce<{[group: string]: ContentMessage[]}>((groups, message) => { const group = getTitleForHeader(message.timestamp()); groups[group] = groups[group] || []; groups[group].unshift(message); return groups; }, {}), ); }; const CollectionDetails: React.FC = ({ conversation, messages, onClose = noop, onImageClick, }) => { const {display_name} = useKoSubscribableChildren(conversation, ['display_name']); return (
{display_name}
{groupByDate(messages).map(([groupName, groupMessages]) => { return (
{groupName}
{groupMessages.map(message => ( ))}
); })}
); }; export {CollectionDetails};