Skip to content

Commit 9156f6f

Browse files
authored
feat: redesign UnsupportedAttachment (#3099)
1 parent 759cadf commit 9156f6f

9 files changed

Lines changed: 397 additions & 33 deletions

File tree

β€Žsrc/components/Attachment/AttachmentContainer.tsxβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ export const UnsupportedAttachmentContainer = ({
330330
attachment,
331331
UnsupportedAttachment = DefaultUnsupportedAttachment,
332332
}: RenderAttachmentProps) => (
333-
<>
333+
<AttachmentWithinContainer attachment={attachment} componentType='unsupported'>
334334
<UnsupportedAttachment attachment={attachment} />
335-
</>
335+
</AttachmentWithinContainer>
336336
);

β€Žsrc/components/Attachment/UnsupportedAttachment.tsxβ€Ž

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,26 @@ import React from 'react';
22
import type { Attachment } from 'stream-chat';
33

44
import { FileIcon } from '../FileIcon';
5-
import { useTranslationContext } from '../../context';
5+
import { useComponentContext, useTranslationContext } from '../../context';
66

77
export type UnsupportedAttachmentProps = {
88
attachment: Attachment;
99
};
1010

1111
export const UnsupportedAttachment = ({ attachment }: UnsupportedAttachmentProps) => {
1212
const { t } = useTranslationContext('UnsupportedAttachment');
13+
const { AttachmentFileIcon } = useComponentContext();
14+
const FileIconComponent = AttachmentFileIcon ?? FileIcon;
1315
return (
1416
<div
1517
className='str-chat__message-attachment-unsupported'
1618
data-testid='attachment-unsupported'
1719
>
18-
<FileIcon className='str-chat__file-icon' />
20+
<FileIconComponent
21+
className='str-chat__file-icon'
22+
fileName={attachment.title}
23+
mimeType={attachment.mime_type}
24+
/>
1925
<div className='str-chat__message-attachment-unsupported__metadata'>
2026
<div
2127
className='str-chat__message-attachment-unsupported__title'
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import React from 'react';
2+
import { render, screen } from '@testing-library/react';
3+
import type { Attachment } from 'stream-chat';
4+
5+
import { UnsupportedAttachment } from '../UnsupportedAttachment';
6+
import { ComponentProvider } from '../../../context/ComponentContext';
7+
import { TranslationProvider } from '../../../context';
8+
import {
9+
mockComponentContext,
10+
mockTranslationContextValue,
11+
} from '../../../mock-builders';
12+
import type { FileIconProps } from '../../FileIcon/FileIcon';
13+
14+
const renderUnsupported = (
15+
attachment: Attachment,
16+
componentOverrides: Record<string, unknown> = {},
17+
) =>
18+
render(
19+
<TranslationProvider value={mockTranslationContextValue()}>
20+
<ComponentProvider value={mockComponentContext(componentOverrides)}>
21+
<UnsupportedAttachment attachment={attachment} />
22+
</ComponentProvider>
23+
</TranslationProvider>,
24+
);
25+
26+
describe('UnsupportedAttachment', () => {
27+
it('renders attachment title when title is set', () => {
28+
renderUnsupported({
29+
mime_type: 'application/x-custom',
30+
title: 'Custom payload',
31+
type: 'unknown',
32+
} as Attachment);
33+
34+
expect(screen.getByTestId('unsupported-attachment-title')).toHaveTextContent(
35+
'Custom payload',
36+
);
37+
});
38+
39+
it('falls back to translated label when title is missing', () => {
40+
renderUnsupported({ type: 'weird' } as Attachment);
41+
42+
expect(screen.getByTestId('unsupported-attachment-title')).toHaveTextContent(
43+
'Unsupported attachment',
44+
);
45+
});
46+
47+
it('uses AttachmentFileIcon from context when provided', () => {
48+
const CustomAttachmentFileIcon = ({ fileName, mimeType }: FileIconProps) => (
49+
<span
50+
data-file-name={fileName}
51+
data-mime-type={mimeType}
52+
data-testid='custom-attachment-file-icon'
53+
/>
54+
);
55+
56+
renderUnsupported(
57+
{
58+
mime_type: 'application/octet-stream',
59+
title: 'data.bin',
60+
type: 'file',
61+
} as Attachment,
62+
{ AttachmentFileIcon: CustomAttachmentFileIcon },
63+
);
64+
65+
const icon = screen.getByTestId('custom-attachment-file-icon');
66+
expect(icon).toHaveAttribute('data-file-name', 'data.bin');
67+
expect(icon).toHaveAttribute('data-mime-type', 'application/octet-stream');
68+
});
69+
});

β€Žsrc/components/Attachment/styling/Attachment.scssβ€Ž

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -338,34 +338,6 @@
338338
}
339339
}
340340

341-
.str-chat__message-attachment-unsupported {
342-
@include utils.component-layer-overrides('file-attachment');
343-
display: flex;
344-
align-items: center;
345-
justify-content: center;
346-
padding: var(--space-8);
347-
column-gap: var(--space-16);
348-
//margin: var(--str-chat__attachment-margin);
349-
350-
.str-chat__file-icon {
351-
width: calc(var(--str-chat__spacing-px) * 30);
352-
}
353-
354-
.str-chat__message-attachment-unsupported__metadata {
355-
min-width: 0;
356-
flex: 1;
357-
display: flex;
358-
flex-direction: column;
359-
align-items: flex-start;
360-
justify-content: center;
361-
}
362-
363-
.str-chat__message-attachment-unsupported__title {
364-
@include utils.ellipsis-text;
365-
max-width: 100%;
366-
}
367-
}
368-
369341
.str-chat__message-attachment-file--item,
370342
.str-chat__message-attachment-audio-widget {
371343
@include utils.flex-row-center;
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
@use '../../../styling/utils';
2+
@use '../../../styling/variables/font' as font;
3+
4+
.str-chat {
5+
/* The border radius used for the borders of file attachments */
6+
--str-chat__unsupported-attachment-border-radius: calc(
7+
var(--str-chat__message-bubble-border-radius) - var(--str-chat__attachment-margin)
8+
);
9+
10+
/* The text/icon color of file attachments */
11+
--str-chat__unsupported-attachment-color: var(--str-chat__text-color);
12+
13+
/* The text/icon color of file attachments for low emphasis texts (for example file size) */
14+
--str-chat__unsupported-attachment-secondary-color: var(
15+
--str-chat__text-low-emphasis-color
16+
);
17+
18+
/* The background color of file attachments */
19+
--str-chat__unsupported-attachment-background-color: transparent;
20+
21+
/* Top border of file attachments */
22+
--str-chat__unsupported-attachment-border-block-start: none;
23+
24+
/* Bottom border of file attachments */
25+
--str-chat__unsupported-attachment-border-block-end: none;
26+
27+
/* Left (right in RTL layout) border of file attachments */
28+
--str-chat__unsupported-attachment-border-inline-start: none;
29+
30+
/* Right (left in RTL layout) border of file attachments */
31+
--str-chat__unsupported-attachment-border-inline-end: none;
32+
33+
/* Box shadow applied to file attachments */
34+
--str-chat__unsupported-attachment-box-shadow: none;
35+
}
36+
37+
.str-chat__message-attachment-unsupported {
38+
@include utils.component-layer-overrides('unsupported-attachment');
39+
display: flex;
40+
align-items: center;
41+
justify-content: center;
42+
padding: var(--spacing-sm);
43+
column-gap: var(--spacing-sm);
44+
// todo: keep a single variable for attachment min width
45+
min-width: 300px;
46+
47+
.str-chat__file-icon {
48+
width: calc(var(--str-chat__spacing-px) * 30);
49+
}
50+
51+
.str-chat__message-attachment-unsupported__metadata {
52+
min-width: 0;
53+
flex: 1;
54+
height: stretch;
55+
display: flex;
56+
flex-direction: column;
57+
align-items: flex-start;
58+
justify-content: flex-start;
59+
}
60+
61+
.str-chat__message-attachment-unsupported__title {
62+
@include utils.ellipsis-text;
63+
@include font.text-caption-emphasis;
64+
max-width: 100%;
65+
}
66+
}

β€Žsrc/components/Attachment/styling/index.scssβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
@use 'Giphy';
77
@use 'LinkPreview';
88
@use 'ModalGallery';
9+
@use 'UnsupportedAttachment';

β€Žsrc/styling/index.scssβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
@use 'global-layout-variables';
55
@use 'global-theme-variables';
66
@use 'palette-variables';
7-
@use './variables.css';
7+
@use './variables-tokens.css';
8+
@use 'variables/font';
89
@use 'base';
910
@use 'fonts';
1011

0 commit comments

Comments
Β (0)