Skip to content

Commit fbb494c

Browse files
authored
Merge pull request #2035 from GetStream/develop
v10.8.5
2 parents de3aee4 + d2ad5eb commit fbb494c

File tree

2 files changed

+74
-15
lines changed

2 files changed

+74
-15
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import React from 'react';
2+
import { renderHook } from '@testing-library/react-hooks';
3+
4+
import { useCooldownTimer } from '../useCooldownTimer';
5+
6+
import { ChannelStateProvider, ChatProvider } from '../../../../context';
7+
import { getTestClient } from '../../../../mock-builders';
8+
9+
async function renderUseCooldownTimerHook({ channel, chatContext }) {
10+
const client = await getTestClient();
11+
12+
const wrapper = ({ children }) => (
13+
<ChatProvider value={{ client, ...chatContext }}>
14+
<ChannelStateProvider value={{ channel }}>{children}</ChannelStateProvider>
15+
</ChatProvider>
16+
);
17+
return renderHook(useCooldownTimer, { wrapper });
18+
}
19+
20+
const cid = 'cid';
21+
const cooldown = 30;
22+
describe('useCooldownTimer', () => {
23+
it('should set remaining cooldown time to if no channel.cooldown', async () => {
24+
const channel = { cid };
25+
const chatContext = { latestMessageDatesByChannels: {} };
26+
const { result } = await renderUseCooldownTimerHook({ channel, chatContext });
27+
expect(result.current.cooldownRemaining).toBe(0);
28+
});
29+
it('should set remaining cooldown time to 0 if skip-slow-mode is among own_capabilities', async () => {
30+
const channel = { cid, data: { cooldown, own_capabilities: ['skip-slow-mode'] } };
31+
const chatContext = { latestMessageDatesByChannels: { cid: new Date() } };
32+
const { result } = await renderUseCooldownTimerHook({ channel, chatContext });
33+
expect(result.current.cooldownRemaining).toBe(0);
34+
});
35+
it('should set remaining cooldown time to 0 if no previous messages sent', async () => {
36+
const channel = { cid, data: { cooldown } };
37+
const chatContext = { latestMessageDatesByChannels: {} };
38+
const { result } = await renderUseCooldownTimerHook({ channel, chatContext });
39+
expect(result.current.cooldownRemaining).toBe(0);
40+
});
41+
it('should set remaining cooldown time to 0 if previous messages sent earlier than channel.cooldown', async () => {
42+
const channel = { cid, data: { cooldown } };
43+
const chatContext = { latestMessageDatesByChannels: { cid: new Date('1970-1-1') } };
44+
const { result } = await renderUseCooldownTimerHook({ channel, chatContext });
45+
expect(result.current.cooldownRemaining).toBe(0);
46+
});
47+
it('should set remaining cooldown time to time left from previous messages sent', async () => {
48+
const channel = { cid, data: { cooldown } };
49+
const lastSentSecondsAgo = 5;
50+
const chatContext = {
51+
latestMessageDatesByChannels: {
52+
cid: new Date(new Date().getTime() - lastSentSecondsAgo * 1000),
53+
},
54+
};
55+
const { result } = await renderUseCooldownTimerHook({ channel, chatContext });
56+
expect(result.current.cooldownRemaining).toBe(cooldown - lastSentSecondsAgo);
57+
});
58+
});

src/components/MessageInput/hooks/useCooldownTimer.tsx

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import React, { useEffect, useMemo, useState } from 'react';
2-
3-
import { useChatContext } from '../../../context/ChatContext';
4-
import { useChannelStateContext } from '../../../context/ChannelStateContext';
5-
62
import type { ChannelResponse } from 'stream-chat';
73

4+
import { useChannelStateContext, useChatContext } from '../../../context';
5+
86
import type { DefaultStreamChatGenerics } from '../../../types/types';
97

108
export type CooldownTimerState = {
@@ -16,15 +14,16 @@ export type CooldownTimerState = {
1614
export const useCooldownTimer = <
1715
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
1816
>(): CooldownTimerState => {
19-
const { latestMessageDatesByChannels } = useChatContext<StreamChatGenerics>('useCooldownTimer');
17+
const { client, latestMessageDatesByChannels } = useChatContext<StreamChatGenerics>(
18+
'useCooldownTimer',
19+
);
2020
const { channel, messages = [] } = useChannelStateContext<StreamChatGenerics>('useCooldownTimer');
21-
const { client } = useChatContext<StreamChatGenerics>('useCooldownTimer');
2221
const [cooldownRemaining, setCooldownRemaining] = useState<number>();
2322

24-
const { cooldown: cooldownInterval, own_capabilities } = (channel.data ||
23+
const { cooldown: cooldownInterval = 0, own_capabilities } = (channel.data ||
2524
{}) as ChannelResponse<StreamChatGenerics>;
2625

27-
const skipCooldown = !own_capabilities?.includes('slow-mode');
26+
const skipCooldown = own_capabilities?.includes('skip-slow-mode');
2827

2928
const ownLatestMessageDate = useMemo(
3029
() =>
@@ -36,17 +35,19 @@ export const useCooldownTimer = <
3635
) as Date;
3736

3837
useEffect(() => {
39-
if (skipCooldown || !cooldownInterval || !ownLatestMessageDate) return;
40-
41-
const remainingCooldown = Math.round(
42-
cooldownInterval - (new Date().getTime() - ownLatestMessageDate.getTime()) / 1000,
38+
const timeSinceOwnLastMessage = ownLatestMessageDate
39+
? (new Date().getTime() - ownLatestMessageDate.getTime()) / 1000
40+
: undefined;
41+
42+
setCooldownRemaining(
43+
!skipCooldown && timeSinceOwnLastMessage && cooldownInterval > timeSinceOwnLastMessage
44+
? Math.round(cooldownInterval - timeSinceOwnLastMessage)
45+
: 0,
4346
);
44-
45-
if (remainingCooldown > 0) setCooldownRemaining(remainingCooldown);
4647
}, [cooldownInterval, ownLatestMessageDate, skipCooldown]);
4748

4849
return {
49-
cooldownInterval: cooldownInterval ?? 0,
50+
cooldownInterval,
5051
cooldownRemaining,
5152
setCooldownRemaining,
5253
};

0 commit comments

Comments
 (0)