Skip to content

Commit 7367fd3

Browse files
committed
Merge branch 'master' into feat/htmlpanel
2 parents ac6d3ff + 3111bdb commit 7367fd3

File tree

7 files changed

+159
-272
lines changed

7 files changed

+159
-272
lines changed

CHANGELOG.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
## v0.1.5
2+
3+
- Output ES2015
4+
15
## v0.1.4
26

3-
- Add `<HtmlPanel>` component
7+
- Added `useUnreads` hook
8+
49

510
## v0.1.3
611

README.md

+81-266
Large diffs are not rendered by default.

example/App.tsx

+37-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import "./App.css";
22

3-
import { Session, Chatbox, HtmlPanel } from "../lib/main";
3+
import { Session, Chatbox, HtmlPanel, useUnreads } from "../lib/main";
44
import Talk from "talkjs";
5-
import React, {
5+
import {
66
ChangeEvent,
77
useCallback,
88
useMemo,
99
useRef,
1010
useState,
11+
ReactElement,
1112
} from "react";
1213

1314
const convIds = ["talk-react-94872948u429843", "talk-react-194872948u429843"];
@@ -178,6 +179,7 @@ function App() {
178179
</HtmlPanel>
179180
)}
180181
</Chatbox>
182+
<UnreadsDisplay />
181183
</Session>
182184

183185
<button
@@ -231,4 +233,37 @@ function App() {
231233
);
232234
}
233235

236+
function UnreadsDisplay() {
237+
const unreads = useUnreads();
238+
let content: ReactElement | null = null;
239+
240+
if (unreads === undefined) {
241+
content = <p>unreads is undefined (no session)</p>;
242+
} else if (unreads.length === 0) {
243+
content = <p>No unread messages</p>;
244+
} else {
245+
content = (
246+
<ul>
247+
{unreads.map((u) => {
248+
return (
249+
<li key={u.conversation.id}>
250+
{u.conversation.id} - {u.lastMessage.sender?.name || "system"}:{" "}
251+
{u.lastMessage.body}
252+
</li>
253+
);
254+
})}
255+
</ul>
256+
);
257+
}
258+
259+
return (
260+
<details>
261+
<summary>
262+
<strong>Unreads rendered with useUnreads</strong>
263+
</summary>
264+
{content}
265+
</details>
266+
);
267+
}
268+
234269
export default App;

lib/SessionContext.ts

+32-1
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ 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";
77
export { HtmlPanel } from "./HtmlPanel";

package.json

+1-1
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.4",
31+
"version": "0.1.5",
3232
"type": "module",
3333
"files": [
3434
"dist"

vite.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ async function renameJsx() {
2222
// https://vitejs.dev/config/
2323
export default defineConfig({
2424
build: {
25+
target: "es2015",
2526
lib: {
2627
entry: "lib/main.tsx",
2728
name: "TalkJSReact",

0 commit comments

Comments
 (0)