Skip to content

Commit 359bb0f

Browse files
committed
chore: v0.1.7-beta.0
Merge branch 'master' into feat/htmlpanel
2 parents e66cdb9 + 5963043 commit 359bb0f

File tree

8 files changed

+71
-29
lines changed

8 files changed

+71
-29
lines changed

CHANGELOG.md

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
## v0.1.7-beta.0
2+
3+
- Add `<HtmlPanel>` component
4+
5+
**Note:** This component is currently experimental.
6+
7+
## v0.1.6
8+
9+
- Add `asGuest?: boolean` prop to `Chatbox`, `Inbox` and `Popup`
10+
111
## v0.1.6-beta.0
212

313
- Add `<HtmlPanel>` component

lib/Session.tsx

+11-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Talk from "talkjs";
33
import { SessionContext } from "./SessionContext";
44
import { SessionEvents } from "./types";
55
import { EventListeners } from "./EventListeners";
6-
import { useSetter } from "./hooks";
6+
import { useMethod } from "./hooks";
77

88
type UserProps =
99
| { userId: string; syncUser?: undefined }
@@ -22,8 +22,14 @@ export function Session(props: SessionProps) {
2222
const [ready, markReady] = useState(false);
2323
const [session, setSession] = useState<Talk.Session>();
2424

25-
const { userId, syncUser, appId, signature, sessionRef, desktopNotificationEnabled } =
26-
props;
25+
const {
26+
userId,
27+
syncUser,
28+
appId,
29+
signature,
30+
sessionRef,
31+
desktopNotificationEnabled,
32+
} = props;
2733

2834
useEffect(() => {
2935
Talk.ready.then(() => markReady(true));
@@ -55,9 +61,9 @@ export function Session(props: SessionProps) {
5561
}
5662
};
5763
}
58-
}, [ready, appId, userId, syncUser, sessionRef]);
64+
}, [ready, signature, appId, userId, syncUser, sessionRef]);
5965

60-
useSetter(
66+
useMethod(
6167
session,
6268
desktopNotificationEnabled,
6369
"setDesktopNotificationEnabled",

lib/hooks.tsx

+29-6
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,37 @@ export function usePreviousIfDeeplyEqual<T>(val: T) {
2424
}
2525

2626
/**
27-
* Calls method `setter` on `box` with `value`, iff `value` is set & deeply
27+
* Calls method `method` on `box` with `value`, iff `value` is set & deeply
2828
* different from before (and the box is alive)
2929
*/
30-
export function useSetter<
30+
export function useMethod<
3131
V,
3232
S extends string,
3333
T extends TalkObject & Record<S, (val: V) => any>,
34-
>(box: T | undefined, value: V | undefined, setter: S) {
34+
>(box: T | undefined, value: V | undefined, method: S) {
3535
value = usePreviousIfDeeplyEqual(value);
3636
useEffect(() => {
3737
if (value !== undefined && box?.isAlive) {
38-
box[setter](value);
38+
box[method](value);
3939
}
40-
}, [setter, box, value]);
40+
}, [method, box, value]);
41+
}
42+
43+
/**
44+
* Like {@link useMethod}, except `args` is an array that gets spread into
45+
* the method call
46+
*/
47+
export function useSpreadMethod<
48+
V extends any[],
49+
S extends string,
50+
T extends TalkObject & Record<S, (...args: V) => any>,
51+
>(box: T | undefined, args: V | undefined, method: S) {
52+
args = usePreviousIfDeeplyEqual(args);
53+
useEffect(() => {
54+
if (args !== undefined && box?.isAlive) {
55+
box[method](...args);
56+
}
57+
}, [method, box, args]);
4158
}
4259

4360
/**
@@ -50,14 +67,20 @@ export function useConversation<T extends Talk.UIBox>(
5067
box: T | undefined,
5168
syncConversation: ConversationProps["syncConversation"],
5269
conversationId: ConversationProps["conversationId"],
70+
asGuest: boolean | undefined,
5371
) {
5472
const conversation = useMemo(() => {
5573
if (typeof syncConversation === "function") {
5674
return session?.isAlive ? syncConversation(session) : undefined;
5775
}
5876
return syncConversation ?? conversationId;
5977
}, [session, syncConversation, conversationId]);
60-
useSetter(box, conversation, "select");
78+
79+
const args = (
80+
conversation !== undefined ? [conversation, { asGuest }] : undefined
81+
) as any;
82+
83+
useSpreadMethod(box, args, "select");
6184
}
6285

6386
// subset of Session to help TypeScript pick the right overloads

lib/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export type ConversationProps =
4343
};
4444

4545
export type UIBoxProps<T extends Talk.UIBox> = UIBoxEvents<T> &
46-
ConversationProps;
46+
ConversationProps & { asGuest?: boolean };
4747

4848
declare const testChatboxEvents: UIBoxEvents<Talk.Chatbox>;
4949
declare const testInboxEvents: UIBoxEvents<Talk.Inbox>;

lib/ui/Chatbox.tsx

+6-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
getKeyForObject,
77
splitObjectByPrefix,
88
} from "../util";
9-
import { useSetter, useConversation, useUIBox } from "../hooks";
9+
import { useMethod, useConversation, useUIBox } from "../hooks";
1010
import { FirstParameter, UIBoxProps } from "../types";
1111
import { MountedBox } from "../MountedBox";
1212

@@ -46,6 +46,7 @@ function ActiveChatbox(props: ChatboxProps & { session: Talk.Session }) {
4646
session,
4747
conversationId,
4848
syncConversation,
49+
asGuest,
4950
chatboxRef,
5051
style,
5152
className,
@@ -59,10 +60,10 @@ function ActiveChatbox(props: ChatboxProps & { session: Talk.Session }) {
5960
options;
6061

6162
const box = useUIBox(session, "createChatbox", simpleOptions, chatboxRef);
62-
useSetter(box, messageFilter, "setMessageFilter");
63-
useSetter(box, presence, "setPresence");
64-
useSetter(box, highlightedWords, "setHighlightedWords");
65-
useConversation(session, box, syncConversation, conversationId);
63+
useMethod(box, messageFilter, "setMessageFilter");
64+
useMethod(box, presence, "setPresence");
65+
useMethod(box, highlightedWords, "setHighlightedWords");
66+
useConversation(session, box, syncConversation, conversationId, asGuest);
6667

6768
return (
6869
<MountedBox

lib/ui/Inbox.tsx

+7-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
splitObjectByPrefix,
77
validateChildrenAreHtmlPanels,
88
} from "../util";
9-
import { useSetter, useConversation, useUIBox } from "../hooks";
9+
import { useMethod, useConversation, useUIBox } from "../hooks";
1010
import { FirstParameter, UIBoxProps } from "../types";
1111
import { MountedBox } from "../MountedBox";
1212

@@ -46,6 +46,7 @@ function ActiveInbox(props: InboxProps & { session: Talk.Session }) {
4646
session,
4747
conversationId,
4848
syncConversation,
49+
asGuest,
4950
inboxRef,
5051
style,
5152
className,
@@ -64,11 +65,11 @@ function ActiveInbox(props: InboxProps & { session: Talk.Session }) {
6465
} = options;
6566

6667
const box = useUIBox(session, "createInbox", simpleOptions, inboxRef);
67-
useSetter(box, messageFilter, "setMessageFilter");
68-
useSetter(box, feedFilter, "setFeedFilter");
69-
useSetter(box, presence, "setPresence");
70-
useSetter(box, highlightedWords, "setHighlightedWords");
71-
useConversation(session, box, syncConversation, conversationId);
68+
useMethod(box, messageFilter, "setMessageFilter");
69+
useMethod(box, feedFilter, "setFeedFilter");
70+
useMethod(box, presence, "setPresence");
71+
useMethod(box, highlightedWords, "setHighlightedWords");
72+
useConversation(session, box, syncConversation, conversationId, asGuest);
7273

7374
return (
7475
<MountedBox

lib/ui/Popup.tsx

+6-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
splitObjectByPrefix,
66
validateChildrenAreHtmlPanels,
77
} from "../util";
8-
import { useSetter, useConversation, useUIBox, useMountBox } from "../hooks";
8+
import { useMethod, useConversation, useUIBox, useMountBox } from "../hooks";
99
import { EventListeners } from "../EventListeners";
1010
import { UIBoxProps } from "../types";
1111
import { BoxContext } from "../MountedBox";
@@ -38,6 +38,7 @@ function ActivePopup(props: PopupProps & { session: Talk.Session }) {
3838
session,
3939
conversationId,
4040
syncConversation,
41+
asGuest,
4142
popupRef,
4243
children,
4344
...optionsAndEvents
@@ -48,10 +49,10 @@ function ActivePopup(props: PopupProps & { session: Talk.Session }) {
4849
options;
4950

5051
const box = useUIBox(session, "createPopup", simpleOptions, popupRef);
51-
useSetter(box, messageFilter, "setMessageFilter");
52-
useSetter(box, presence, "setPresence");
53-
useSetter(box, highlightedWords, "setHighlightedWords");
54-
useConversation(session, box, syncConversation, conversationId);
52+
useMethod(box, messageFilter, "setMessageFilter");
53+
useMethod(box, presence, "setPresence");
54+
useMethod(box, highlightedWords, "setHighlightedWords");
55+
useConversation(session, box, syncConversation, conversationId, asGuest);
5556
useMountBox(box, undefined);
5657

5758
return (

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.6-beta.0",
31+
"version": "0.1.7-beta.0",
3232
"type": "module",
3333
"files": [
3434
"dist"

0 commit comments

Comments
 (0)