Skip to content

Commit 015a4b9

Browse files
authored
Merge pull request #9 from talkjs/feat/asguest_prop
Add `asGuest` prop
2 parents 3111bdb + 4bfcecf commit 015a4b9

File tree

6 files changed

+60
-28
lines changed

6 files changed

+60
-28
lines changed

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
@@ -2,7 +2,7 @@ import { CSSProperties, ReactNode } from "react";
22
import type Talk from "talkjs";
33
import { useSession } from "../SessionContext";
44
import { getKeyForObject, splitObjectByPrefix } from "../util";
5-
import { useSetter, useConversation, useUIBox } from "../hooks";
5+
import { useMethod, useConversation, useUIBox } from "../hooks";
66
import { FirstParameter, UIBoxProps } from "../types";
77
import { MountedBox } from "../MountedBox";
88

@@ -35,6 +35,7 @@ function ActiveChatbox(props: ChatboxProps & { session: Talk.Session }) {
3535
session,
3636
conversationId,
3737
syncConversation,
38+
asGuest,
3839
chatboxRef,
3940
style,
4041
className,
@@ -47,10 +48,10 @@ function ActiveChatbox(props: ChatboxProps & { session: Talk.Session }) {
4748
options;
4849

4950
const box = useUIBox(session, "createChatbox", simpleOptions, chatboxRef);
50-
useSetter(box, messageFilter, "setMessageFilter");
51-
useSetter(box, presence, "setPresence");
52-
useSetter(box, highlightedWords, "setHighlightedWords");
53-
useConversation(session, box, syncConversation, conversationId);
51+
useMethod(box, messageFilter, "setMessageFilter");
52+
useMethod(box, presence, "setPresence");
53+
useMethod(box, highlightedWords, "setHighlightedWords");
54+
useConversation(session, box, syncConversation, conversationId, asGuest);
5455

5556
return (
5657
<MountedBox

lib/ui/Inbox.tsx

+7-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { CSSProperties, ReactNode } from "react";
22
import type Talk from "talkjs";
33
import { useSession } from "../SessionContext";
44
import { getKeyForObject, splitObjectByPrefix } from "../util";
5-
import { useSetter, useConversation, useUIBox } from "../hooks";
5+
import { useMethod, useConversation, useUIBox } from "../hooks";
66
import { FirstParameter, UIBoxProps } from "../types";
77
import { MountedBox } from "../MountedBox";
88

@@ -35,6 +35,7 @@ function ActiveInbox(props: InboxProps & { session: Talk.Session }) {
3535
session,
3636
conversationId,
3737
syncConversation,
38+
asGuest,
3839
inboxRef,
3940
style,
4041
className,
@@ -52,11 +53,11 @@ function ActiveInbox(props: InboxProps & { session: Talk.Session }) {
5253
} = options;
5354

5455
const box = useUIBox(session, "createInbox", simpleOptions, inboxRef);
55-
useSetter(box, messageFilter, "setMessageFilter");
56-
useSetter(box, feedFilter, "setFeedFilter");
57-
useSetter(box, presence, "setPresence");
58-
useSetter(box, highlightedWords, "setHighlightedWords");
59-
useConversation(session, box, syncConversation, conversationId);
56+
useMethod(box, messageFilter, "setMessageFilter");
57+
useMethod(box, feedFilter, "setFeedFilter");
58+
useMethod(box, presence, "setPresence");
59+
useMethod(box, highlightedWords, "setHighlightedWords");
60+
useConversation(session, box, syncConversation, conversationId, asGuest);
6061

6162
return (
6263
<MountedBox

lib/ui/Popup.tsx

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Talk from "talkjs";
22
import { useSession } from "../SessionContext";
33
import { getKeyForObject, splitObjectByPrefix } from "../util";
4-
import { useSetter, useConversation, useUIBox, useMountBox } from "../hooks";
4+
import { useMethod, useConversation, useUIBox, useMountBox } from "../hooks";
55
import { EventListeners } from "../EventListeners";
66
import { UIBoxProps } from "../types";
77

@@ -26,6 +26,7 @@ function ActivePopup(props: PopupProps & { session: Talk.Session }) {
2626
session,
2727
conversationId,
2828
syncConversation,
29+
asGuest,
2930
popupRef,
3031
...optionsAndEvents
3132
} = props;
@@ -35,10 +36,10 @@ function ActivePopup(props: PopupProps & { session: Talk.Session }) {
3536
options;
3637

3738
const box = useUIBox(session, "createPopup", simpleOptions, popupRef);
38-
useSetter(box, messageFilter, "setMessageFilter");
39-
useSetter(box, presence, "setPresence");
40-
useSetter(box, highlightedWords, "setHighlightedWords");
41-
useConversation(session, box, syncConversation, conversationId);
39+
useMethod(box, messageFilter, "setMessageFilter");
40+
useMethod(box, presence, "setPresence");
41+
useMethod(box, highlightedWords, "setHighlightedWords");
42+
useConversation(session, box, syncConversation, conversationId, asGuest);
4243
useMountBox(box, undefined);
4344

4445
return <EventListeners target={box} handlers={events} />;

0 commit comments

Comments
 (0)