Skip to content

Commit 2e72d51

Browse files
committed
feat: Add asGuest prop
1 parent 3111bdb commit 2e72d51

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed

lib/hooks.tsx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,23 @@ export function useSetter<
4040
}, [setter, box, value]);
4141
}
4242

43+
/**
44+
* Calls method `setter` on `box` with the arguments `args`, iff `args` is set &
45+
* deeply different from before (and the box is alive)
46+
*/
47+
export function useSpreadSetter<
48+
V extends any[],
49+
S extends string,
50+
T extends TalkObject & Record<S, (...args: V) => any>,
51+
>(box: T | undefined, args: V | undefined, setter: S) {
52+
args = usePreviousIfDeeplyEqual(args);
53+
useEffect(() => {
54+
if (args !== undefined && box?.isAlive) {
55+
box[setter](...args);
56+
}
57+
}, [setter, box, args]);
58+
}
59+
4360
/**
4461
* Calls `box.select` with either `syncConversation` or `conversationId`
4562
* depending on which is set. If neither is set, which is valid for the Inbox,
@@ -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 }] : []
81+
) as any;
82+
83+
useSpreadSetter(box, args, "select");
6184
}
6285

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

lib/types.ts

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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,
@@ -50,7 +51,7 @@ function ActiveChatbox(props: ChatboxProps & { session: Talk.Session }) {
5051
useSetter(box, messageFilter, "setMessageFilter");
5152
useSetter(box, presence, "setPresence");
5253
useSetter(box, highlightedWords, "setHighlightedWords");
53-
useConversation(session, box, syncConversation, conversationId);
54+
useConversation(session, box, syncConversation, conversationId, asGuest);
5455

5556
return (
5657
<MountedBox

lib/ui/Inbox.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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,
@@ -56,7 +57,7 @@ function ActiveInbox(props: InboxProps & { session: Talk.Session }) {
5657
useSetter(box, feedFilter, "setFeedFilter");
5758
useSetter(box, presence, "setPresence");
5859
useSetter(box, highlightedWords, "setHighlightedWords");
59-
useConversation(session, box, syncConversation, conversationId);
60+
useConversation(session, box, syncConversation, conversationId, asGuest);
6061

6162
return (
6263
<MountedBox

lib/ui/Popup.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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;
@@ -38,7 +39,7 @@ function ActivePopup(props: PopupProps & { session: Talk.Session }) {
3839
useSetter(box, messageFilter, "setMessageFilter");
3940
useSetter(box, presence, "setPresence");
4041
useSetter(box, highlightedWords, "setHighlightedWords");
41-
useConversation(session, box, syncConversation, conversationId);
42+
useConversation(session, box, syncConversation, conversationId, asGuest);
4243
useMountBox(box, undefined);
4344

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

0 commit comments

Comments
 (0)