Skip to content

Commit 06bf1eb

Browse files
authored
chore: chat configuration query params (#1957)
Enables the following chat configuration parameters: - `?disable_chat=true` to completely disable chat in Pronto - `?channel_type=messaging` to switch to a particular channel type (`videocall` remains the default) - `?channel_id=my-channel` to switch to a particular channel id (the default is the current `call_id`)
1 parent 0b48a2a commit 06bf1eb

File tree

6 files changed

+67
-42
lines changed

6 files changed

+67
-42
lines changed

sample-apps/react/react-dogfood/components/ActiveCall.tsx

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
import clsx from 'clsx';
2121
import { useEffect, useState } from 'react';
2222
import { StreamChat } from 'stream-chat';
23+
import { useRouter } from 'next/router';
2324

2425
import { ActiveCallHeader } from './ActiveCallHeader';
2526
import { CallStatsSidebar, ToggleStatsButton } from './CallStatsWrapper';
@@ -107,6 +108,10 @@ export const ActiveCall = (props: ActiveCallProps) => {
107108
const showChat = sidebarContent === 'chat';
108109
const showStats = sidebarContent === 'stats';
109110
const showClosedCaptions = sidebarContent === 'closed-captions';
111+
const router = useRouter();
112+
const chatDisabled =
113+
router.query['disable_chat'] === 'true' ||
114+
process.env.NEXT_PUBLIC_DISABLE_CHAT === 'true';
110115

111116
// FIXME: could be replaced with "notification.message_new" but users would have to be at least members
112117
// possible fix with "allow to join" permissions in place (expensive?)
@@ -339,35 +344,40 @@ export const ActiveCall = (props: ActiveCallProps) => {
339344
setSidebarContent(showParticipants ? null : 'participants');
340345
}}
341346
/>
342-
<NewMessageNotification
343-
chatClient={chatClient}
344-
channelWatched={channelWatched}
345-
disableOnChatOpen={showChat}
346-
>
347-
<div className="str-chat__chat-button__wrapper">
348-
<WithTooltip title={t('Chat')}>
349-
<CompositeButton
350-
active={showChat}
351-
disabled={!chatClient}
352-
onClick={() => {
353-
if (isTourActive && currentTourStep === StepNames.Chat) {
354-
nextTourStep();
355-
}
356-
setSidebarContent(showChat ? null : 'chat');
357-
}}
358-
>
359-
<Icon icon="chat" />
360-
</CompositeButton>
361-
</WithTooltip>
362-
{!showChat && (
363-
<UnreadCountBadge
364-
channelWatched={channelWatched}
365-
chatClient={chatClient}
366-
channelId={activeCall.id}
367-
/>
368-
)}
369-
</div>
370-
</NewMessageNotification>
347+
{!chatDisabled && (
348+
<NewMessageNotification
349+
chatClient={chatClient}
350+
channelWatched={channelWatched}
351+
disableOnChatOpen={showChat}
352+
>
353+
<div className="str-chat__chat-button__wrapper">
354+
<WithTooltip title={t('Chat')}>
355+
<CompositeButton
356+
active={showChat}
357+
disabled={!chatClient}
358+
onClick={() => {
359+
if (
360+
isTourActive &&
361+
currentTourStep === StepNames.Chat
362+
) {
363+
nextTourStep();
364+
}
365+
setSidebarContent(showChat ? null : 'chat');
366+
}}
367+
>
368+
<Icon icon="chat" />
369+
</CompositeButton>
370+
</WithTooltip>
371+
{!showChat && (
372+
<UnreadCountBadge
373+
channelWatched={channelWatched}
374+
chatClient={chatClient}
375+
channelId={activeCall.id}
376+
/>
377+
)}
378+
</div>
379+
</NewMessageNotification>
380+
)}
371381
</div>
372382
</div>
373383
</div>

sample-apps/react/react-dogfood/components/ChatUI.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useEffect } from 'react';
2+
import { useRouter } from 'next/router';
23
import {
34
Channel,
45
MESSAGE_ACTIONS,
@@ -74,18 +75,22 @@ export const ChatSendButton = ({ sendMessage, ...rest }: SendButtonProps) => {
7475

7576
export const ChatUI = ({
7677
onClose,
78+
channelType = CHANNEL_TYPE,
7779
channelId,
7880
}: {
7981
onClose: () => void;
82+
channelType?: string;
8083
channelId: string;
8184
}) => {
8285
const { client, setActiveChannel } = useChatContext();
8386

87+
const router = useRouter();
8488
useEffect(() => {
85-
const channel = client.channel(CHANNEL_TYPE, channelId);
89+
const type = (router.query['channel_type'] as string) || channelType;
90+
const channel = client.channel(type, channelId);
8691

8792
setActiveChannel(channel);
88-
}, [channelId, client, setActiveChannel]);
93+
}, [channelId, channelType, client, router.query, setActiveChannel]);
8994

9095
return (
9196
<Channel

sample-apps/react/react-dogfood/components/UnreadCountBadge.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { useState, useEffect } from 'react';
2-
import { StreamChat, Event } from 'stream-chat';
1+
import { useEffect, useState } from 'react';
2+
import { useRouter } from 'next/router';
3+
import { Event, StreamChat } from 'stream-chat';
34
import { CHANNEL_TYPE } from '.';
45

56
// FIXME: unread count 0 on load (even if before refresh was >0)
@@ -13,8 +14,9 @@ export const UnreadCountBadge = ({
1314
channelWatched: boolean;
1415
}) => {
1516
const [unread, setUnread] = useState(0);
16-
17-
const cid = `${CHANNEL_TYPE}:${channelId}`;
17+
const router = useRouter();
18+
const channelType = (router.query['channel_type'] as string) || CHANNEL_TYPE;
19+
const cid = `${channelType}:${channelId}`;
1820

1921
useEffect(() => {
2022
if (!client) return;
@@ -32,7 +34,6 @@ export const UnreadCountBadge = ({
3234

3335
const handleEvent = () => {
3436
const channel = client.activeChannels[cid];
35-
3637
setUnread(channel?.countUnread() ?? 0);
3738
};
3839

sample-apps/react/react-dogfood/components/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ export * from './MeetingUI';
44
export * from './NewMessageNotification';
55
export * from './UnreadCountBadge';
66

7+
/**
8+
* Defaults to `videocall`. Make sure to respect the `channel_type`
9+
* query parameter if provided.
10+
*/
711
export const CHANNEL_TYPE = 'videocall';

sample-apps/react/react-dogfood/hooks/useChatClient.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useEffect, useState } from 'react';
2+
import { useRouter } from 'next/router';
23
import {
34
OwnUserResponse,
45
StreamChat,
@@ -16,9 +17,11 @@ export const useCreateStreamChatClient = ({
1617
tokenOrProvider: TokenOrProvider;
1718
}) => {
1819
const [chatClient, setChatClient] = useState<StreamChat | null>(null);
19-
20+
const router = useRouter();
2021
useEffect(() => {
21-
const disableChat = process.env.NEXT_PUBLIC_DISABLE_CHAT === 'true';
22+
const disableChat =
23+
router.query['disable_chat'] === 'true' ||
24+
process.env.NEXT_PUBLIC_DISABLE_CHAT === 'true';
2225
if (disableChat || !apiKey) return;
2326

2427
const client = new StreamChat(apiKey, {
@@ -42,7 +45,7 @@ export const useCreateStreamChatClient = ({
4245
});
4346
};
4447
// eslint-disable-next-line react-hooks/exhaustive-deps
45-
}, [apiKey, userData.id, tokenOrProvider]);
48+
}, [apiKey, router.query, userData.id, tokenOrProvider]);
4649

4750
return chatClient;
4851
};

sample-apps/react/react-dogfood/hooks/useWatchChannel.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useEffect, useState } from 'react';
2+
import { useRouter } from 'next/router';
23
import type { Event, StreamChat } from 'stream-chat';
34

45
import { CHANNEL_TYPE } from '../components';
@@ -13,11 +14,12 @@ export const useWatchChannel = ({
1314
channelType?: string;
1415
}) => {
1516
const [channelWatched, setChannelWatched] = useState(false);
17+
const router = useRouter();
1618

1719
useEffect(() => {
1820
if (!client) return;
19-
20-
const channel = client.channel(channelType, channelId);
21+
const type = (router.query['channel_type'] as string) || channelType;
22+
const channel = client.channel(type, channelId);
2123
// initiate watching now so we can receive message events
2224
const watchingPromise = channel.watch();
2325

@@ -26,7 +28,7 @@ export const useWatchChannel = ({
2628
// channel.stopWatching();
2729
});
2830
};
29-
}, [client, channelId, channelType]);
31+
}, [client, channelId, channelType, router.query]);
3032

3133
useEffect(() => {
3234
if (!client) return;

0 commit comments

Comments
 (0)