Skip to content

Commit bd41304

Browse files
committed
Make useXXXContext return all states & actions for backward compatibility
1 parent e175dd2 commit bd41304

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

src/modules/GroupChannel/context/GroupChannelProvider.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useMemo, useEffect, useRef, createContext, useContext } from 'react';
1+
import React, { useMemo, useEffect, useRef, createContext } from 'react';
22
import {
33
ReplyType as ChatReplyType,
44
} from '@sendbird/chat/message';
@@ -336,12 +336,10 @@ const useGroupChannelStore = () => {
336336
};
337337
/**
338338
* Keep this function for backward compatibility.
339-
* @returns {ReturnType<typeof createStore<GroupChannelState>>}
340339
*/
341340
const useGroupChannelContext = () => {
342-
const store = useContext(GroupChannelContext);
343-
if (!store) throw new Error('GroupChannelContext not found. Use within the GroupChannel module.');
344-
return store.getState();
341+
const { state, actions } = useGroupChannel();
342+
return { ...state, ...actions };
345343
};
346344

347345
export {

src/modules/MessageSearch/context/MessageSearchProvider.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { createContext, useRef, useContext, useCallback, useEffect } from 'react';
1+
import React, { createContext, useRef, useCallback, useEffect } from 'react';
22
import type { GroupChannel } from '@sendbird/chat/groupChannel';
33
import { MessageSearchQuery } from '@sendbird/chat/message';
44
import { ClientSentMessages } from '../../../types';
@@ -14,6 +14,7 @@ import useSearchStringEffect from './hooks/useSearchStringEffect';
1414
import { CoreMessageType } from '../../../utils';
1515
import { createStore } from '../../../utils/storeManager';
1616
import { useStore } from '../../../hooks/useStore';
17+
import useMessageSearch from './hooks/useMessageSearch';
1718

1819
export interface MessageSearchProviderProps {
1920
channelUrl: string;
@@ -164,12 +165,10 @@ const MessageSearchProvider: React.FC<MessageSearchProviderProps> = ({
164165

165166
/**
166167
* Keep this function for backward compatibility.
167-
* @returns {ReturnType<typeof createStore<MessageSearchState>>}
168168
*/
169169
const useMessageSearchContext = () => {
170-
const store = useContext(MessageSearchContext);
171-
if (!store) throw new Error('MessageSearchContext not found. Use within the MessageSearch module.');
172-
return store.getState();
170+
const { state, actions } = useMessageSearch();
171+
return { ...state, ...actions };
173172
};
174173

175174
export {

src/modules/MessageSearch/context/__test__/MessageSearchProvider.spec.tsx

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { waitFor, act } from '@testing-library/react';
33
import { renderHook } from '@testing-library/react-hooks';
44
import { MessageSearchQuery } from '@sendbird/chat/message';
55

6-
import { MessageSearchProvider, useMessageSearchContext } from '../MessageSearchProvider';
6+
import { MessageSearchProvider } from '../MessageSearchProvider';
77
import useMessageSearch from '../hooks/useMessageSearch';
88

99
jest.mock('../../../../hooks/useSendbirdStateContext', () => ({
@@ -71,28 +71,38 @@ describe('MessageSearchProvider', () => {
7171
</MessageSearchProvider>
7272
);
7373

74-
const { result } = renderHook(() => useMessageSearchContext(), { wrapper });
74+
const { result } = renderHook(() => useMessageSearch(), { wrapper });
7575

76-
expect(result.current.getState()).toEqual(expect.objectContaining(initialState));
76+
expect(result.current.state).toEqual(expect.objectContaining(initialState));
7777
});
7878

7979
it('updates state correctly when props change', async () => {
80-
const wrapper = ({ children }) => (
81-
<MessageSearchProvider channelUrl="test-channel">
80+
const initialUrl = 'test-channel';
81+
const newUrl = 'new-channel';
82+
83+
const wrapper = ({ children, channelUrl }) => (
84+
<MessageSearchProvider channelUrl={channelUrl}>
8285
{children}
8386
</MessageSearchProvider>
8487
);
8588

86-
const { result } = renderHook(() => useMessageSearchContext(), { wrapper });
89+
const { result, rerender } = renderHook(
90+
() => useMessageSearch(),
91+
{
92+
wrapper,
93+
initialProps: { channelUrl: initialUrl, children: null },
94+
},
95+
);
8796

88-
expect(result.current.getState().channelUrl).toBe('test-channel');
97+
expect(result.current.state.channelUrl).toBe(initialUrl);
8998

9099
await act(async () => {
91-
result.current.setState({ channelUrl: 'new-channel' });
100+
rerender({ channelUrl: newUrl, children: null });
101+
92102
await waitFor(() => {
93-
const newState = result.current.getState();
94-
expect(newState.channelUrl).toBe('new-channel');
95103
// Verify other states remain unchanged
104+
const newState = result.current.state;
105+
expect(newState.channelUrl).toBe(newUrl);
96106
expect(newState.allMessages).toEqual(initialState.allMessages);
97107
expect(newState.loading).toBe(initialState.loading);
98108
expect(newState.isQueryInvalid).toBe(initialState.isQueryInvalid);

0 commit comments

Comments
 (0)