Skip to content

Commit 860fb63

Browse files
committed
fix(*): Adding images for file attachments.
1 parent c201da2 commit 860fb63

File tree

4 files changed

+40
-11
lines changed

4 files changed

+40
-11
lines changed
686 Bytes
Loading
787 Bytes
Loading
839 Bytes
Loading

src/components/chat/message-attachments.ts

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import { registerComponent } from '../common/definitions/register.js';
77
import IgcExpansionPanelComponent from '../expansion-panel/expansion-panel.js';
88
import IgcIconComponent from '../icon/icon.js';
99
import { registerIconFromText } from '../icon/icon.registry.js';
10+
import defaultFileIcon from './assets/file.png';
11+
import jsonIcon from './assets/json.png';
12+
import linkIcon from './assets/link.png';
1013
import type { ChatState } from './chat-state.js';
1114
import { styles } from './themes/message-attachments.base.css.js';
1215
import { styles as shared } from './themes/shared/message-attachments.common.css.js';
@@ -76,6 +79,32 @@ export default class IgcMessageAttachmentsComponent extends LitElement {
7679
this._chatState?.emitEvent('igcAttachmentClick', { detail: attachment });
7780
}
7881

82+
private isImageAttachment(attachment: IgcMessageAttachment): boolean {
83+
return (
84+
attachment.type === 'image' ||
85+
!!attachment.file?.type.startsWith('image/')
86+
);
87+
}
88+
private _fileIconMap: Record<string, string> = {
89+
pdf: defaultFileIcon,
90+
doc: defaultFileIcon,
91+
docx: defaultFileIcon,
92+
xls: defaultFileIcon,
93+
xlsx: defaultFileIcon,
94+
txt: defaultFileIcon,
95+
json: jsonIcon,
96+
link: linkIcon,
97+
default: defaultFileIcon, // A fallback icon
98+
};
99+
100+
private getFileExtension(fileName: string): string {
101+
const parts = fileName.split('.');
102+
if (parts.length > 1) {
103+
return parts.pop()!.toLowerCase();
104+
}
105+
return '';
106+
}
107+
79108
private getURL(attachment: IgcMessageAttachment): string {
80109
if (attachment.url) {
81110
return attachment.url;
@@ -87,14 +116,13 @@ export default class IgcMessageAttachmentsComponent extends LitElement {
87116
}
88117

89118
private renderDefaultAttachmentContent(attachment: IgcMessageAttachment) {
90-
return html`${attachment.type === 'image' ||
91-
attachment.file?.type.startsWith('image/')
92-
? html`<img
93-
part="image-attachment"
94-
src=${this.getURL(attachment)}
95-
alt=${attachment.name}
96-
/>`
97-
: nothing}`;
119+
const ext = this.getFileExtension(attachment.name);
120+
const isImage = this.isImageAttachment(attachment);
121+
const url = isImage
122+
? this.getURL(attachment)
123+
: (this._fileIconMap[ext] ?? this._fileIconMap['default']);
124+
const partName = isImage ? 'image-attachment' : 'file-attachment';
125+
return html` <img part="${partName}" src=${url} alt=${attachment.name} />`;
98126
}
99127

100128
private renderAttachmentHeaderText(attachment: IgcMessageAttachment) {
@@ -146,6 +174,9 @@ export default class IgcMessageAttachmentsComponent extends LitElement {
146174
return html`${this.message?.attachments?.map(
147175
(attachment) =>
148176
html`<div part="attachment">
177+
${this.message?.sender === this._chatState?.currentUserId
178+
? this.renderAttachmentContent(attachment)
179+
: nothing}
149180
<div
150181
part="attachment-header"
151182
role="button"
@@ -155,9 +186,7 @@ export default class IgcMessageAttachmentsComponent extends LitElement {
155186
${this.renderAttachmentHeaderActions()}
156187
</div>
157188
158-
${attachment.type === 'image' ||
159-
attachment.file?.type.startsWith('image/') ||
160-
this._chatState?.options?.templates?.attachmentContentTemplate
189+
${this.message?.sender !== this._chatState?.currentUserId
161190
? this.renderAttachmentContent(attachment)
162191
: nothing}
163192
</div>`

0 commit comments

Comments
 (0)