Skip to content

Commit 1f4cdd5

Browse files
committed
refactor: adapt AttachmentPreviewList to LLC attachment types
1 parent 373ec98 commit 1f4cdd5

File tree

12 files changed

+83
-78
lines changed

12 files changed

+83
-78
lines changed

src/components/Attachment/Attachment.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import React, { useMemo } from 'react';
2-
import type { ReactPlayerProps } from 'react-player';
3-
import type { Attachment as StreamAttachment } from 'stream-chat';
4-
52
import {
63
isAudioAttachment,
7-
isFileAttachment,
8-
isMediaAttachment,
4+
isImageAttachment,
95
isScrapedContent,
10-
isUploadedImage,
116
isVoiceRecordingAttachment,
12-
} from './utils';
7+
} from 'stream-chat';
8+
139
import {
1410
AudioContainer,
1511
CardContainer,
@@ -20,6 +16,10 @@ import {
2016
UnsupportedAttachmentContainer,
2117
VoiceRecordingContainer,
2218
} from './AttachmentContainer';
19+
import { isFileAttachment, isMediaAttachment } from './utils';
20+
21+
import type { ReactPlayerProps } from 'react-player';
22+
import type { Attachment as StreamAttachment } from 'stream-chat';
2323
import type { AttachmentActionsProps } from './AttachmentActions';
2424
import type { AudioProps } from './Audio';
2525
import type { VoiceRecordingProps } from './VoiceRecording';
@@ -104,11 +104,11 @@ const renderGroupedAttachments = ({
104104
...rest
105105
}: AttachmentProps): GroupedRenderedAttachment => {
106106
const uploadedImages: StreamAttachment[] = attachments.filter((attachment) =>
107-
isUploadedImage(attachment),
107+
isImageAttachment(attachment),
108108
);
109109

110110
const containers = attachments
111-
.filter((attachment) => !isUploadedImage(attachment))
111+
.filter((attachment) => !isImageAttachment(attachment))
112112
.reduce<GroupedRenderedAttachment>(
113113
(typeMap, attachment) => {
114114
const attachmentType = getAttachmentType(attachment);

src/components/Attachment/utils.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import type { ReactNode } from 'react';
2-
import type { Attachment } from 'stream-chat';
3-
4-
import type { ATTACHMENT_GROUPS_ORDER, AttachmentProps } from './Attachment';
52
import type {
3+
Attachment,
64
LocalAttachment,
75
LocalAudioAttachment,
8-
LocalFileAttachment,
96
LocalImageAttachment,
10-
LocalVideoAttachment,
117
LocalVoiceRecordingAttachment,
128
VoiceRecordingAttachment,
13-
} from '../MessageInput';
9+
} from 'stream-chat';
10+
11+
import type { ATTACHMENT_GROUPS_ORDER, AttachmentProps } from './Attachment';
12+
import type { LocalFileAttachment, LocalVideoAttachment } from '../MessageInput';
1413

1514
export const SUPPORTED_VIDEO_FORMATS = [
1615
'video/mp4',

src/components/MediaRecorder/classes/MediaRecorderController.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import fixWebmDuration from 'fix-webm-duration';
22
import { nanoid } from 'nanoid';
3-
import type { AmplitudeRecorderConfig } from './AmplitudeRecorder';
43
import {
54
AmplitudeRecorder,
65
DEFAULT_AMPLITUDE_RECORDER_CONFIG,
@@ -16,11 +15,11 @@ import {
1615
getExtensionFromMimeType,
1716
getRecordedMediaTypeFromMimeType,
1817
} from '../../ReactFileUtilities';
19-
import type { TranslationContextValue } from '../../../context';
2018
import { defaultTranslatorFunction } from '../../../i18n';
2119
import { mergeDeepUndefined } from '../../../utils/mergeDeep';
22-
23-
import type { LocalVoiceRecordingAttachment } from '../../MessageInput';
20+
import type { LocalVoiceRecordingAttachment } from 'stream-chat';
21+
import type { AmplitudeRecorderConfig } from './AmplitudeRecorder';
22+
import type { TranslationContextValue } from '../../../context';
2423

2524
export const RECORDED_MIME_TYPE_BY_BROWSER = {
2625
audio: {
@@ -188,7 +187,7 @@ export class MediaRecorderController {
188187
this.amplitudeRecorder?.amplitudes.value ?? [],
189188
this.amplitudeRecorderConfig.sampleCount,
190189
),
191-
};
190+
} as LocalVoiceRecordingAttachment;
192191
};
193192

194193
handleErrorEvent = (e: Event) => {

src/components/MediaRecorder/hooks/useMediaRecorder.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { useCallback, useEffect, useMemo, useState } from 'react';
2-
import type { MessageInputContextValue } from '../../../context';
3-
import { useTranslationContext } from '../../../context';
4-
import type { CustomAudioRecordingConfig, MediaRecordingState } from '../classes';
52
import { MediaRecorderController } from '../classes';
6-
7-
import type { LocalVoiceRecordingAttachment } from '../../MessageInput';
3+
import { useTranslationContext } from '../../../context';
84
import { useMessageComposer } from '../../MessageInput/hooks/messageComposer/useMessageComposer';
95

6+
import type { LocalVoiceRecordingAttachment } from 'stream-chat';
7+
import type { CustomAudioRecordingConfig, MediaRecordingState } from '../classes';
8+
import type { MessageInputContextValue } from '../../../context';
9+
1010
export type RecordingController = {
1111
completeRecording: () => void;
1212
permissionState?: PermissionState;

src/components/MessageInput/AttachmentPreviewList/AttachmentPreviewList.tsx

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
import type { ComponentType } from 'react';
22
import React from 'react';
3-
import type { UnsupportedAttachmentPreviewProps } from './UnsupportedAttachmentPreview';
4-
import { UnsupportedAttachmentPreview as DefaultUnknownAttachmentPreview } from './UnsupportedAttachmentPreview';
5-
import type { VoiceRecordingPreviewProps } from './VoiceRecordingPreview';
6-
import { VoiceRecordingPreview as DefaultVoiceRecordingPreview } from './VoiceRecordingPreview';
7-
import type { FileAttachmentPreviewProps } from './FileAttachmentPreview';
8-
import { FileAttachmentPreview as DefaultFilePreview } from './FileAttachmentPreview';
9-
import type { ImageAttachmentPreviewProps } from './ImageAttachmentPreview';
10-
import { ImageAttachmentPreview as DefaultImagePreview } from './ImageAttachmentPreview';
113
import {
124
isLocalAttachment,
135
isLocalAudioAttachment,
146
isLocalFileAttachment,
157
isLocalImageAttachment,
16-
isLocalMediaAttachment,
8+
isLocalVideoAttachment,
179
isLocalVoiceRecordingAttachment,
1810
isScrapedContent,
19-
} from '../../Attachment';
11+
} from 'stream-chat';
12+
import { UnsupportedAttachmentPreview as DefaultUnknownAttachmentPreview } from './UnsupportedAttachmentPreview';
13+
import { VoiceRecordingPreview as DefaultVoiceRecordingPreview } from './VoiceRecordingPreview';
14+
import { FileAttachmentPreview as DefaultFilePreview } from './FileAttachmentPreview';
15+
import { ImageAttachmentPreview as DefaultImagePreview } from './ImageAttachmentPreview';
16+
import { useMessageComposer } from '../hooks/messageComposer/useMessageComposer';
17+
import { useStateStore } from '../../../store';
2018

2119
import type { AttachmentManagerState } from 'stream-chat';
22-
import { useStateStore } from '../../../store';
23-
import { useMessageComposer } from '../hooks/messageComposer/useMessageComposer';
20+
import type { UnsupportedAttachmentPreviewProps } from './UnsupportedAttachmentPreview';
21+
import type { VoiceRecordingPreviewProps } from './VoiceRecordingPreview';
22+
import type { FileAttachmentPreviewProps } from './FileAttachmentPreview';
23+
import type { ImageAttachmentPreviewProps } from './ImageAttachmentPreview';
2424

2525
const attachmentManagerStateSelector = (state: AttachmentManagerState) => ({
2626
attachments: state.attachments,
@@ -76,7 +76,7 @@ export const AttachmentPreviewList = ({
7676
removeAttachments={messageComposer.attachmentManager.removeAttachments}
7777
/>
7878
);
79-
} else if (isLocalMediaAttachment(attachment)) {
79+
} else if (isLocalVideoAttachment(attachment)) {
8080
return (
8181
<VideoAttachmentPreview
8282
attachment={attachment}

src/components/MessageInput/AttachmentPreviewList/FileAttachmentPreview.tsx

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
import React from 'react';
2+
import { useTranslationContext } from '../../../context';
23
import { FileIcon } from '../../ReactFileUtilities';
34
import { CloseIcon, DownloadIcon, LoadingIndicatorIcon, RetryIcon } from '../icons';
4-
import { useTranslationContext } from '../../../context';
55

6-
import type { AttachmentPreviewProps } from './types';
7-
import type { LocalAttachmentCast, LocalAttachmentUploadMetadata } from '../types';
8-
import type { Attachment } from 'stream-chat';
6+
import type {
7+
LocalAudioAttachment,
8+
LocalFileAttachment,
9+
LocalVideoAttachment,
10+
} from 'stream-chat';
11+
import type { UploadAttachmentPreviewProps } from './types';
912

10-
type FileLikeAttachment = Partial<
11-
Pick<Attachment, 'title' | 'file_size' | 'asset_url' | 'mime_type'>
12-
>;
13-
14-
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
15-
export type FileAttachmentPreviewProps<CustomLocalMetadata = {}> = AttachmentPreviewProps<
16-
LocalAttachmentCast<
17-
FileLikeAttachment,
18-
LocalAttachmentUploadMetadata & CustomLocalMetadata
19-
>
20-
>;
13+
export type FileAttachmentPreviewProps<CustomLocalMetadata = unknown> =
14+
UploadAttachmentPreviewProps<
15+
| LocalFileAttachment<CustomLocalMetadata>
16+
| LocalAudioAttachment<CustomLocalMetadata>
17+
| LocalVideoAttachment<CustomLocalMetadata>
18+
>;
2119

2220
export const FileAttachmentPreview = ({
2321
attachment,
@@ -26,6 +24,7 @@ export const FileAttachmentPreview = ({
2624
}: FileAttachmentPreviewProps) => {
2725
const { t } = useTranslationContext('FilePreview');
2826
const uploadState = attachment.localMetadata?.uploadState;
27+
2928
return (
3029
<div
3130
className='str-chat__attachment-preview-file'

src/components/MessageInput/AttachmentPreviewList/ImageAttachmentPreview.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import clsx from 'clsx';
2-
import { CloseIcon, LoadingIndicatorIcon, RetryIcon } from '../icons';
32
import React, { useCallback, useState } from 'react';
3+
import { CloseIcon, LoadingIndicatorIcon, RetryIcon } from '../icons';
44
import { BaseImage as DefaultBaseImage } from '../../Gallery';
55
import { useComponentContext, useTranslationContext } from '../../../context';
6-
import type { AttachmentPreviewProps } from './types';
7-
import type { LocalImageAttachment } from '../types';
6+
import type { LocalImageAttachment } from 'stream-chat';
7+
import type { UploadAttachmentPreviewProps } from './types';
88

99
export type ImageAttachmentPreviewProps<CustomLocalMetadata = Record<string, unknown>> =
10-
AttachmentPreviewProps<LocalImageAttachment<CustomLocalMetadata>>;
10+
UploadAttachmentPreviewProps<LocalImageAttachment<CustomLocalMetadata>>;
1111

1212
export const ImageAttachmentPreview = ({
1313
attachment,

src/components/MessageInput/AttachmentPreviewList/UnsupportedAttachmentPreview.tsx

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import React from 'react';
2+
import { isLocalUploadAttachment } from 'stream-chat';
23
import { CloseIcon, DownloadIcon, LoadingIndicatorIcon, RetryIcon } from '../icons';
34
import { FileIcon } from '../../ReactFileUtilities';
45
import { useTranslationContext } from '../../../context';
5-
import type { AttachmentPreviewProps } from './types';
6-
import type { AnyLocalAttachment } from '../types';
6+
import type { AnyLocalAttachment, LocalUploadAttachment } from 'stream-chat';
77

88
export type UnsupportedAttachmentPreviewProps<
99
CustomLocalMetadata = Record<string, unknown>,
10-
> = AttachmentPreviewProps<AnyLocalAttachment<CustomLocalMetadata>>;
10+
> = {
11+
attachment: AnyLocalAttachment<CustomLocalMetadata>;
12+
handleRetry: (
13+
attachment: LocalUploadAttachment,
14+
) => void | Promise<LocalUploadAttachment | undefined>;
15+
removeAttachments: (ids: string[]) => void;
16+
};
1117

1218
export const UnsupportedAttachmentPreview = ({
1319
attachment,
@@ -37,15 +43,17 @@ export const UnsupportedAttachmentPreview = ({
3743
<CloseIcon />
3844
</button>
3945

40-
{attachment.localMetadata?.uploadState === 'failed' && !!handleRetry && (
41-
<button
42-
className='str-chat__attachment-preview-error str-chat__attachment-preview-error-file'
43-
data-testid='file-preview-item-retry-button'
44-
onClick={() => handleRetry(attachment)}
45-
>
46-
<RetryIcon />
47-
</button>
48-
)}
46+
{isLocalUploadAttachment(attachment) &&
47+
attachment.localMetadata?.uploadState === 'failed' &&
48+
!!handleRetry && (
49+
<button
50+
className='str-chat__attachment-preview-error str-chat__attachment-preview-error-file'
51+
data-testid='file-preview-item-retry-button'
52+
onClick={() => handleRetry(attachment)}
53+
>
54+
<RetryIcon />
55+
</button>
56+
)}
4957

5058
<div className='str-chat__attachment-preview-metadata'>
5159
<div className='str-chat__attachment-preview-title' title={title}>

src/components/MessageInput/AttachmentPreviewList/VoiceRecordingPreview.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import { RecordingTimer } from '../../MediaRecorder';
44
import { CloseIcon, LoadingIndicatorIcon, RetryIcon } from '../icons';
55
import { FileIcon } from '../../ReactFileUtilities';
66
import { useAudioController } from '../../Attachment/hooks/useAudioController';
7-
import type { AttachmentPreviewProps } from './types';
8-
import type { LocalVoiceRecordingAttachment } from '../types';
7+
import type { LocalVoiceRecordingAttachment } from 'stream-chat';
8+
import type { UploadAttachmentPreviewProps } from './types';
99

1010
export type VoiceRecordingPreviewProps<CustomLocalMetadata = Record<string, unknown>> =
11-
AttachmentPreviewProps<LocalVoiceRecordingAttachment<CustomLocalMetadata>>;
11+
UploadAttachmentPreviewProps<LocalVoiceRecordingAttachment<CustomLocalMetadata>>;
1212

1313
export const VoiceRecordingPreview = ({
1414
attachment,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export * from './AttachmentPreviewList';
22
export type { FileAttachmentPreviewProps } from './FileAttachmentPreview';
33
export type { ImageAttachmentPreviewProps } from './ImageAttachmentPreview';
4-
export type { AttachmentPreviewProps } from './types';
4+
export type { UploadAttachmentPreviewProps as AttachmentPreviewProps } from './types';
55
export type { UnsupportedAttachmentPreviewProps } from './UnsupportedAttachmentPreview';
66
export type { VoiceRecordingPreviewProps } from './VoiceRecordingPreview';

0 commit comments

Comments
 (0)