|
1 | 1 | import "./App.css"; |
2 | 2 |
|
3 | | -import { Session, Chatbox } from "../lib/main"; |
| 3 | +import { Session, Chatbox, useUnreads } from "../lib/main"; |
4 | 4 | import Talk from "talkjs"; |
5 | | -import { ChangeEvent, useCallback, useMemo, useRef, useState } from "react"; |
| 5 | +import { ChangeEvent, useCallback, useMemo, useRef, useState, ReactElement } from "react"; |
6 | 6 |
|
7 | 7 | const convIds = ["talk-react-94872948u429843", "talk-react-194872948u429843"]; |
8 | 8 | const users = [ |
@@ -151,6 +151,7 @@ function App() { |
151 | 151 | {...(blur ? { onBlur } : {})} |
152 | 152 | style={{ width: 500, height: 600 }} |
153 | 153 | /> |
| 154 | + <UnreadsDisplay /> |
154 | 155 | </Session> |
155 | 156 | <button onClick={otherMe}>switch user (new session)</button> |
156 | 157 | <br /> |
@@ -195,4 +196,26 @@ function App() { |
195 | 196 | ); |
196 | 197 | } |
197 | 198 |
|
| 199 | +function UnreadsDisplay() { |
| 200 | + const unreads = useUnreads(); |
| 201 | + let content: ReactElement | null = null; |
| 202 | + |
| 203 | + if (unreads === undefined) { |
| 204 | + content = <p>unreads is undefined (no session)</p>; |
| 205 | + } else if (unreads.length === 0) { |
| 206 | + content = <p>No unread messages</p> |
| 207 | + } else { |
| 208 | + content = <ul> |
| 209 | + {unreads.map(u => { |
| 210 | + return <li key={u.conversation.id}>{u.conversation.id} - {u.lastMessage.sender?.name || "system"}: {u.lastMessage.body}</li> |
| 211 | + })} |
| 212 | + </ul> |
| 213 | + } |
| 214 | + |
| 215 | + return <details> |
| 216 | + <summary><strong>Unreads rendered with useUnreads</strong></summary> |
| 217 | + {content} |
| 218 | + </details> |
| 219 | +} |
| 220 | + |
198 | 221 | export default App; |
0 commit comments