Skip to content

Commit 19f7223

Browse files
Merge pull request #6 from talkjs/feat/useUnreads
feat: add useUnreads
2 parents 4c95ecc + aea6c08 commit 19f7223

File tree

5 files changed

+63
-5
lines changed

5 files changed

+63
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## v0.1.4
2+
3+
- Added `useUnreads` hook
4+
15
## v0.1.3
26

37
- Add `signature?: string` prop to `Session`

example/App.tsx

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import "./App.css";
22

3-
import { Session, Chatbox } from "../lib/main";
3+
import { Session, Chatbox, useUnreads } from "../lib/main";
44
import Talk from "talkjs";
5-
import { ChangeEvent, useCallback, useMemo, useRef, useState } from "react";
5+
import { ChangeEvent, useCallback, useMemo, useRef, useState, ReactElement } from "react";
66

77
const convIds = ["talk-react-94872948u429843", "talk-react-194872948u429843"];
88
const users = [
@@ -151,6 +151,7 @@ function App() {
151151
{...(blur ? { onBlur } : {})}
152152
style={{ width: 500, height: 600 }}
153153
/>
154+
<UnreadsDisplay />
154155
</Session>
155156
<button onClick={otherMe}>switch user (new session)</button>
156157
<br />
@@ -195,4 +196,26 @@ function App() {
195196
);
196197
}
197198

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+
198221
export default App;

lib/SessionContext.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createContext, useContext } from "react";
1+
import { createContext, useContext, useEffect, useState } from "react";
22
import type Talk from "talkjs";
33

44
export const SessionContext = createContext<Talk.Session | undefined>(
@@ -31,3 +31,34 @@ export function useSession() {
3131
const session = useContext(SessionContext);
3232
return session?.isAlive ? session : undefined;
3333
}
34+
35+
/**
36+
* Returns conversations with unread messages.
37+
*
38+
* @remarks
39+
* Can only be used in child components of <Session>.
40+
*
41+
* @returns A list of {@link https://talkjs.com/docs/Reference/JavaScript_Chat_SDK/Session/#UnreadConversation | UnreadConversation} objects,
42+
* or undefined if this hook is used outside of the <Session> component, or the session is not alive for any other reason.
43+
* During development in particular, sessions can be destroyed and recreated a
44+
* lot, eg due to React.StrictMode or due to hot-module reloading.
45+
*/
46+
export function useUnreads() {
47+
const session = useSession();
48+
const [unreads, setUnreads] = useState<Talk.UnreadConversation[] | undefined>();
49+
50+
useEffect(() => {
51+
if (!session || !session.isAlive) {
52+
setUnreads(undefined);
53+
return;
54+
}
55+
56+
const sub = session.unreads.onChange( unreads => {
57+
setUnreads(unreads);
58+
});
59+
60+
return () => sub.unsubscribe();
61+
}, [session]);
62+
63+
return unreads;
64+
}

lib/main.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ export { Session } from "./Session";
33
export { Chatbox } from "./ui/Chatbox";
44
export { Inbox } from "./ui/Inbox";
55
export { Popup } from "./ui/Popup";
6-
export { useSession } from "./SessionContext";
6+
export { useSession, useUnreads } from "./SessionContext";

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"url": "https://github.com/talkjs/talkjs-react/issues"
2929
},
3030
"homepage": "https://talkjs.com",
31-
"version": "0.1.3",
31+
"version": "0.1.4",
3232
"type": "module",
3333
"files": [
3434
"dist"

0 commit comments

Comments
 (0)