|
| 1 | +--- |
| 2 | +id: custom-attachments |
| 3 | +title: Custom attachments |
| 4 | +--- |
| 5 | + |
| 6 | +import SupportedAttachments from "../_common/supported-attachments.mdx"; |
| 7 | +import PaymentLink from "../assets/payment-link.png"; |
| 8 | +import PaymentPreview from "../assets/payment-preview.png"; |
| 9 | +import PaymentAttachment from "../assets/payment-attachment.png"; |
| 10 | + |
| 11 | +The Stream API allows you to add any attachment to a message. The SDK supports some common types (such as images, videos, etc.) out-of-the-box, but you have to provide your own template to display others. |
| 12 | + |
| 13 | +The Angular SDK has out-of-the-box support for the following types: |
| 14 | + |
| 15 | +<SupportedAttachments /> |
| 16 | + |
| 17 | +This guide will show you how to create custom attachments. In the example, we'll allow users to make payment links to send money to each other, but the same logic works for any attachment. |
| 18 | + |
| 19 | +## Creating the attachment |
| 20 | + |
| 21 | +Let's add a button to the message input component to make a payment link. How we create the attachment doesn't matter; the important part is to create an `Attachment` object we can provide to the [`AttachmentService`](../../services/AttachmentService). |
| 22 | + |
| 23 | +```html showLineNumbers |
| 24 | +<!-- Each message input component has it's own instance of the AttachmentService --> |
| 25 | +<stream-message-input #input> |
| 26 | + <button |
| 27 | + message-input-start |
| 28 | + (click)="createPaymentLink(input.attachmentService)" |
| 29 | + > |
| 30 | + Payment link |
| 31 | + </button> |
| 32 | +</stream-message-input> |
| 33 | +``` |
| 34 | + |
| 35 | +```typescript showLineNumbers {18-22} |
| 36 | +// Optionally, you can define the shape of your custom attachments to get proper compile checks |
| 37 | +type MyGenerics = DefaultStreamChatGenerics & { |
| 38 | + attachmentType: { |
| 39 | + type: 'custom'; |
| 40 | + subtype: 'payment'; |
| 41 | + value: string; |
| 42 | + paymentLink: string; |
| 43 | + }; |
| 44 | +}; |
| 45 | + |
| 46 | +createPaymentLink(attachmentService: AttachmentService) { |
| 47 | + const attachment: Attachment<MyGenerics> = { |
| 48 | + type: 'custom', |
| 49 | + subtype: 'payment', |
| 50 | + value: `${Math.ceil(Math.random() * 99)}$`, |
| 51 | + paymentLink: 'pay/me/or/else', |
| 52 | + }; |
| 53 | + // Insert the attachment to the list of custom attachments |
| 54 | + attachmentService.customAttachments$.next([ |
| 55 | + ...attachmentService.customAttachments$.value, |
| 56 | + attachment, |
| 57 | + ]); |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +<img src={PaymentLink} width="500" /> |
| 62 | + |
| 63 | +Clicking the "Payment link" will add the attachment to the message, but we don't yet have any visual indicator of this. |
| 64 | + |
| 65 | +## Custom attachment preview |
| 66 | + |
| 67 | +Let's add a preview of the payment attachment. |
| 68 | + |
| 69 | +To do this, we define the HTML template code for the preview that uses the `AttachmentService` to display the previews of the custom attachments: |
| 70 | + |
| 71 | +```html showLineNumbers |
| 72 | +<ng-template #customAttachmentPreviews let-service="service"> |
| 73 | + <div |
| 74 | + style="padding: 8px; background-color: azure; border-radius: inherit" |
| 75 | + class="custom-attachment-container" |
| 76 | + *ngFor="let attachment of service.customAttachments$ | async" |
| 77 | + > |
| 78 | + <ng-container [ngSwitch]="attachment.subtype"> |
| 79 | + <div *ngSwitchCase="'payment'" class="payment-link"> |
| 80 | + 🤑 {{ attachment.value }} |
| 81 | + </div> |
| 82 | + </ng-container> |
| 83 | + </div> |
| 84 | +</ng-template> |
| 85 | +``` |
| 86 | + |
| 87 | +If you have multiple different types of custom attachments, you can display them all here. |
| 88 | + |
| 89 | +Next, we register the template for the `customAttachmentPreviewListTemplate$` field of the [`CustomTemplatesService`](../../services/CustomTemplatesService): |
| 90 | + |
| 91 | +```typescript showLineNumbers {8-10} |
| 92 | +export class AppComponent implements AfterViewInit { |
| 93 | + @ViewChild("customAttachmentPreviews") |
| 94 | + customAttachmentPreviewsTemplate!: TemplateRef<CustomAttachmentPreviewListContext>; |
| 95 | + |
| 96 | + constructor(private customTemplateService: CustomTemplatesService) {} |
| 97 | + |
| 98 | + ngAfterViewInit(): void { |
| 99 | + this.customTemplateService.customAttachmentPreviewListTemplate$.next( |
| 100 | + this.customAttachmentPreviewsTemplate |
| 101 | + ); |
| 102 | + } |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +If we click the "Payment link" button, the preview is now visible: |
| 107 | + |
| 108 | +<img src={PaymentPreview} width="500" /> |
| 109 | + |
| 110 | +## Delete attachment preview |
| 111 | + |
| 112 | +Let's allow deleting a payment link by extending the template of the preview: |
| 113 | + |
| 114 | +```html showLineNumbers {10-11} |
| 115 | +<ng-template #customAttachmentPreviews let-service="service"> |
| 116 | + <div |
| 117 | + style="padding: 8px; background-color: azure; border-radius: inherit" |
| 118 | + class="custom-attachment-container" |
| 119 | + *ngFor="let attachment of service.customAttachments$ | async" |
| 120 | + > |
| 121 | + <ng-container [ngSwitch]="attachment.subtype"> |
| 122 | + <div *ngSwitchCase="'payment'" class="payment-link"> |
| 123 | + 🤑 {{ attachment.value }} |
| 124 | + <!-- Add a delete button --> |
| 125 | + <button (click)="deletePaymentLink(attachment, service)">X</button> |
| 126 | + </div> |
| 127 | + </ng-container> |
| 128 | + </div> |
| 129 | +</ng-template> |
| 130 | +``` |
| 131 | + |
| 132 | +This is the implementation of the delete payment attachment method: |
| 133 | + |
| 134 | +```typescript showLineNumbers {5-8} |
| 135 | +deletePaymentLink( |
| 136 | + attachment: Attachment<MyGenerics>, |
| 137 | + attachmentService: AttachmentService<MyGenerics> |
| 138 | +) { |
| 139 | + attachmentService.customAttachments$.next( |
| 140 | + attachmentService.customAttachments$.value.filter( |
| 141 | + (a) => a.paymentLink !== attachment.paymentLink |
| 142 | + ) |
| 143 | + ); |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | +## Loading state |
| 148 | + |
| 149 | +Sometimes, creating attachments happens asynchronously. If that's the case, you should disable message sending while the attachment is processing. |
| 150 | + |
| 151 | +Here is how you can do that by extending the `createPaymentLink` method: |
| 152 | + |
| 153 | +```typescript showLineNumbers {2-5,23-26} |
| 154 | +async createPaymentLink(attachmentService: AttachmentService) { |
| 155 | + // Increment the upload counter to disable message send |
| 156 | + attachmentService.attachmentUploadInProgressCounter$.next( |
| 157 | + attachmentService.attachmentUploadInProgressCounter$.value + 1 |
| 158 | + ); |
| 159 | + const attachment: Attachment<MyGenerics> = { |
| 160 | + type: 'custom', |
| 161 | + subtype: 'payment', |
| 162 | + value: `${Math.ceil(Math.random() * 99)}$`, |
| 163 | + paymentLink: '', |
| 164 | + }; |
| 165 | + // simulate network call |
| 166 | + await new Promise<void>((resolve) => { |
| 167 | + setTimeout(() => { |
| 168 | + attachment.paymentLink = 'pay/me/or/else'; |
| 169 | + resolve(); |
| 170 | + }, 2000); |
| 171 | + }); |
| 172 | + attachmentService.customAttachments$.next([ |
| 173 | + ...attachmentService.customAttachments$.value, |
| 174 | + attachment, |
| 175 | + ]); |
| 176 | + // Attachment is ready, decrease the upload counter |
| 177 | + attachmentService.attachmentUploadInProgressCounter$.next( |
| 178 | + attachmentService.attachmentUploadInProgressCounter$.value - 1 |
| 179 | + ); |
| 180 | + } |
| 181 | +``` |
| 182 | + |
| 183 | +## Custom attachment inside the message list |
| 184 | + |
| 185 | +The last missing step is to display the payment link inside the message list. This will be very similar to how we created the attachment preview. |
| 186 | + |
| 187 | +First, let's define an HTML template: |
| 188 | + |
| 189 | +```html showLineNumbers |
| 190 | +<ng-template #customAttachments let-attachments="attachments"> |
| 191 | + <div |
| 192 | + class="custom-attachment-container" |
| 193 | + *ngFor="let attachment of attachments" |
| 194 | + > |
| 195 | + <ng-container [ngSwitch]="attachment.subtype"> |
| 196 | + <div |
| 197 | + style="margin-inline: 16px; margin-top: 8px" |
| 198 | + *ngSwitchCase="'payment'" |
| 199 | + class="payment-link" |
| 200 | + > |
| 201 | + 💵 |
| 202 | + <a [href]="attachment.link" target="_blank">{{ attachment.value }}</a> |
| 203 | + </div> |
| 204 | + </ng-container> |
| 205 | + </div> |
| 206 | +</ng-template> |
| 207 | +``` |
| 208 | + |
| 209 | +The `attachments` template variable will contain the list of custom attachments. |
| 210 | + |
| 211 | +:::note |
| 212 | +By default the SDK will treat all `image`, `file`, `giphy`, `video` and `voiceRecording` attachments as built-in. All other type of attachments are treated as custom attachments. |
| 213 | + |
| 214 | +If you want to change the filtering logic, provide your own implementation using the `filterCustomAttachment` method of the [`MessageService`](../../services/MessageService/#filtercustomattachment). |
| 215 | +::: |
| 216 | + |
| 217 | +Next, we register the template for the `customAttachmentListTemplate$` field of the [`CustomTemplatesService`](../../services/CustomTemplatesService): |
| 218 | + |
| 219 | +```typescript showLineNumbers {8-10} |
| 220 | +export class AppComponent implements AfterViewInit { |
| 221 | + @ViewChild("customAttachments") |
| 222 | + customAttachmentsTemplate!: TemplateRef<CustomAttachmentListContext>; |
| 223 | + |
| 224 | + constructor(private customTemplateService: CustomTemplatesService) {} |
| 225 | + |
| 226 | + ngAfterViewInit(): void { |
| 227 | + this.customTemplateService.customAttachmentListTemplate$.next( |
| 228 | + this.customAttachmentsTemplate |
| 229 | + ); |
| 230 | + } |
| 231 | +} |
| 232 | +``` |
| 233 | + |
| 234 | +This is how the attachment looks like inside the message list: |
| 235 | + |
| 236 | +<img src={PaymentAttachment} width="500" /> |
| 237 | + |
| 238 | +If you need reference to the message ID (and parent message ID for thread replies) the attachments belong to, this is how you can access them: |
| 239 | + |
| 240 | +```html showLineNumbers {4-5} |
| 241 | +<ng-template |
| 242 | + #customAttachments |
| 243 | + let-attachments="attachments" |
| 244 | + let-messageId="messageId" |
| 245 | + let-parentMessageId="parentMessageId" |
| 246 | +> |
| 247 | + <div *ngFor="let attachment of attachments"> |
| 248 | + {{ messageId }} {{ parentMessageId }} {{ attachment | json }} |
| 249 | + </div> |
| 250 | +</ng-template> |
| 251 | +``` |
0 commit comments