signal-desktop/ts/components/conversation/media-gallery/AttachmentSection.tsx

70 lines
2.1 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2018 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
2018-04-12 20:23:26 +00:00
import React from 'react';
import type { ItemClickEvent } from './types/ItemClickEvent';
import type { LocalizerType } from '../../../types/Util';
2022-12-20 17:50:23 +00:00
import type { MediaItemType } from '../../../types/MediaItem';
import { DocumentListItem } from './DocumentListItem';
import { MediaGridItem } from './MediaGridItem';
2021-03-04 21:44:57 +00:00
import { getMessageTimestamp } from '../../../util/getMessageTimestamp';
2022-12-20 17:50:23 +00:00
import { missingCaseError } from '../../../util/missingCaseError';
2018-04-12 20:23:26 +00:00
export type Props = {
2018-04-12 20:23:26 +00:00
header?: string;
2022-12-20 17:50:23 +00:00
i18n: LocalizerType;
mediaItems: ReadonlyArray<MediaItemType>;
2022-12-20 17:50:23 +00:00
onItemClick: (event: ItemClickEvent) => unknown;
type: 'media' | 'documents';
};
2018-04-12 20:23:26 +00:00
2022-12-20 17:50:23 +00:00
export function AttachmentSection({
i18n,
header,
type,
mediaItems,
onItemClick,
}: Props): JSX.Element {
return (
<div className="module-attachment-section">
<h2 className="module-attachment-section__header">{header}</h2>
<div className="module-attachment-section__items">
{mediaItems.map((mediaItem, position, array) => {
const shouldShowSeparator = position < array.length - 1;
const { message, index, attachment } = mediaItem;
2018-04-13 00:56:05 +00:00
2022-12-20 17:50:23 +00:00
const onClick = () => {
onItemClick({ type, message, attachment, index: mediaItem.index });
2022-12-20 17:50:23 +00:00
};
2018-04-13 00:56:05 +00:00
2022-12-20 17:50:23 +00:00
switch (type) {
case 'media':
return (
<MediaGridItem
key={`${message.id}-${index}`}
mediaItem={mediaItem}
onClick={onClick}
i18n={i18n}
/>
);
case 'documents':
return (
<DocumentListItem
key={`${message.id}-${index}`}
fileName={attachment.fileName}
fileSize={attachment.size}
shouldShowSeparator={shouldShowSeparator}
onClick={onClick}
timestamp={getMessageTimestamp(message)}
/>
);
default:
return missingCaseError(type);
}
})}
</div>
</div>
);
2018-04-12 20:23:26 +00:00
}