|
| 1 | +--- |
| 2 | +id: pin-messages |
| 3 | +title: Pin messages |
| 4 | +--- |
| 5 | + |
| 6 | +import PinActionScreenshot from "../assets/pin-action-screenshot.png"; |
| 7 | +import PinnedMessageScreenshot from "../assets/pinned-message-screenshot.png"; |
| 8 | + |
| 9 | +Pinning messages can be a useful feature inside your chat application, `stream-chat-angular` supports this feature, but you have to provide the UI template for the pinned messages. This guide shows you how you can set up this functionality. |
| 10 | + |
| 11 | +## Pin and unpin actions |
| 12 | + |
| 13 | +The default message action box contains the pin/unpin action, this action marks a message as pinned/unpinned. |
| 14 | + |
| 15 | +<img src={PinActionScreenshot} width="500" /> |
| 16 | + |
| 17 | +:::note |
| 18 | +If you don't see the action, you might have [authorize the pin action](https://getstream.io/chat/docs/javascript/chat_permission_policies/?language=javascript). |
| 19 | +::: |
| 20 | + |
| 21 | +## Pinned messages stream |
| 22 | + |
| 23 | +The [`activeChannelPinnedMessages$`](../services/ChannelService.mdx/#activechannelpinnedmessages) stream of the [`ChannelService`](../services/ChannelService.mdx) emits the list of currently pinned messages inside the active channel. |
| 24 | + |
| 25 | +```typescript |
| 26 | +this.channelService.activeChannelPinnedMessages$.subscribe(console.log); |
| 27 | +``` |
| 28 | + |
| 29 | +- The initial value is retrived from the [channel query response](https://getstream.io/chat/docs/javascript/query_channels/?language=javascript&q=pin#channelstate-response) |
| 30 | +- After that, all pin and unpin actions are reflected in the emitted list |
| 31 | + |
| 32 | +## Pinned messages UI |
| 33 | + |
| 34 | +Once we know the list of pinned messages, we should display them somewhere users can easily spot them. You can display pinned messages anywhere you want to, however a common place could be the top/bottom of the message list, this example will display pinned messages at the bottom of the message list. |
| 35 | + |
| 36 | +You have to provide the template for the pinned messages: |
| 37 | + |
| 38 | +```html |
| 39 | +<stream-notification-list></stream-notification-list> |
| 40 | +<div |
| 41 | + style="padding: 8px; background: #e1f5fe" |
| 42 | + *ngFor=" |
| 43 | + let message of channelService.activeChannelPinnedMessages$ | async |
| 44 | + " |
| 45 | +> |
| 46 | + {{ message.text }} |
| 47 | +</div> |
| 48 | +<stream-message-input></stream-message-input> |
| 49 | +``` |
| 50 | + |
| 51 | +The `message` variable has [`StreamMessage`](../types/stream-message.mdx) type, so you can access all fields defined there inside your template. |
| 52 | + |
| 53 | +<img src={PinnedMessageScreenshot} width="500" /> |
| 54 | + |
| 55 | +## Jump to a pinned message |
| 56 | + |
| 57 | +Let's add a click event handler to the pinned message, and jump to the message: |
| 58 | + |
| 59 | +```typescript |
| 60 | +jumpToMessage(message: StreamMessage) { |
| 61 | + this.channelService.jumpToMessage(message.id); |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +```html |
| 66 | +<div |
| 67 | + style="padding: 8px; background: #e1f5fe" |
| 68 | + *ngFor=" |
| 69 | + let message of channelService.activeChannelPinnedMessages$ | async |
| 70 | + " |
| 71 | + (click)="jumpToMessage(message)" |
| 72 | +> |
| 73 | + {{ message.text }} |
| 74 | +</div> |
| 75 | +``` |
0 commit comments