Skip to content

Commit dddf226

Browse files
committed
refactor: subscribe to search controller's internal state to watch focusedMessage value
1 parent 7f06fd5 commit dddf226

File tree

9 files changed

+38
-39
lines changed

9 files changed

+38
-39
lines changed

src/components/Channel/Channel.tsx

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ import {
6363
import { findInMsgSetByDate, findInMsgSetById, makeAddNotifications } from './utils';
6464
import { useThreadContext } from '../Threads';
6565
import { getChannel } from '../../utils';
66-
import { useStateStore } from '../../store';
6766

6867
import type {
6968
APIErrorResponse,
@@ -76,7 +75,6 @@ import type {
7675
EventAPIResponse,
7776
Message,
7877
MessageResponse,
79-
SearchControllerState,
8078
SendMessageAPIResponse,
8179
Channel as StreamChannel,
8280
StreamChat,
@@ -99,12 +97,7 @@ import {
9997
getVideoAttachmentConfiguration,
10098
} from '../Attachment/attachment-sizing';
10199
import type { URLEnrichmentConfig } from '../MessageInput/hooks/useLinkPreviews';
102-
103-
const searchControllerStateSelector = <
104-
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
105-
>(
106-
nextValue: SearchControllerState<StreamChatGenerics>,
107-
) => ({ jumpToMessageFromSearch: nextValue.focusedMessage });
100+
import { useSearchFocusedMessage } from '../../experimental/Search/hooks';
108101

109102
export type ChannelPropsForwardedToComponentContext<
110103
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
@@ -396,12 +389,7 @@ const ChannelInner = <
396389
loading: !channel.initialized,
397390
},
398391
);
399-
400-
const { jumpToMessageFromSearch } = useStateStore(
401-
searchController.state,
402-
searchControllerStateSelector,
403-
);
404-
392+
const jumpToMessageFromSearch = useSearchFocusedMessage();
405393
const isMounted = useIsMounted();
406394

407395
const originalTitle = useRef('');
@@ -666,8 +654,8 @@ const ChannelInner = <
666654
clearTimeout(clearHighlightedMessageTimeoutId.current);
667655
}
668656
clearHighlightedMessageTimeoutId.current = setTimeout(() => {
669-
if (searchController.state.getLatestValue().focusedMessage) {
670-
searchController.state.partialNext({ focusedMessage: undefined });
657+
if (searchController.internalState.getLatestValue().focusedMessage) {
658+
searchController.internalState.partialNext({ focusedMessage: undefined });
671659
}
672660
clearHighlightedMessageTimeoutId.current = null;
673661
dispatch({ type: 'clearHighlightedMessage' });

src/components/ChannelList/ChannelList.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,9 @@ const DEFAULT_FILTERS = {};
4141
const DEFAULT_OPTIONS = {};
4242
const DEFAULT_SORT = {};
4343

44-
const searchControllerStateSelector = <
45-
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
46-
>(
47-
nextValue: SearchControllerState<StreamChatGenerics>,
48-
) => ({ searchIsActive: nextValue.isActive });
44+
const searchControllerStateSelector = (nextValue: SearchControllerState) => ({
45+
searchIsActive: nextValue.isActive,
46+
});
4947

5048
export type ChannelListProps<
5149
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics

src/experimental/Search/Search.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ type SearchControllerStateSelectorReturnValue = {
1414
isActive: boolean;
1515
};
1616

17-
const searchControllerStateSelector = <
18-
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
19-
>(
20-
nextValue: SearchControllerState<StreamChatGenerics>,
17+
const searchControllerStateSelector = (
18+
nextValue: SearchControllerState,
2119
): SearchControllerStateSelectorReturnValue => ({ isActive: nextValue.isActive });
2220

2321
export type SearchProps = {
@@ -46,7 +44,7 @@ export const Search = <
4644
const { searchController } = useChatContext<StreamChatGenerics>();
4745

4846
const { isActive } = useStateStore<
49-
SearchControllerState<StreamChatGenerics>,
47+
SearchControllerState,
5048
SearchControllerStateSelectorReturnValue
5149
>(searchController.state, searchControllerStateSelector);
5250

src/experimental/Search/SearchResults/SearchResultItem.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export const MessageSearchResultItem = <
6363
if (!channel) return;
6464
await channel.state.loadMessageIntoState(item.id, undefined, DEFAULT_JUMP_TO_PAGE_SIZE);
6565
// FIXME: message focus should be handled by yet non-existent msg list controller in client packaged
66-
searchController.state.partialNext({ focusedMessage: item });
66+
searchController.internalState.partialNext({ focusedMessage: item });
6767
setActiveChannel(channel);
6868
setChannels?.((channels) => uniqBy([channel, ...channels], 'cid'));
6969
}, [channel, item, searchController, setActiveChannel, setChannels]);
@@ -77,7 +77,7 @@ export const MessageSearchResultItem = <
7777
<ChannelPreview
7878
active={
7979
channel.cid === activeChannel?.cid &&
80-
item.id === searchController.state.getLatestValue().focusedMessage?.id
80+
item.id === searchController.internalState.getLatestValue().focusedMessage?.id
8181
}
8282
channel={channel}
8383
className='str-chat__search-result'

src/experimental/Search/SearchResults/SearchResults.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ import { useStateStore } from '../../../store';
1010
import type { SearchControllerState } from 'stream-chat';
1111
import type { DefaultStreamChatGenerics } from '../../../types';
1212

13-
const searchControllerStateSelector = <
14-
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
15-
>(
16-
nextValue: SearchControllerState<StreamChatGenerics>,
17-
) => ({
13+
const searchControllerStateSelector = (nextValue: SearchControllerState) => ({
1814
activeSources: nextValue.sources.filter((s) => s.isActive),
1915
isActive: nextValue.isActive,
2016
searchQuery: nextValue.searchQuery,

src/experimental/Search/__tests__/SearchResultItem.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ describe('SearchResultItem Components', () => {
119119
fireEvent.click(screen.getByTestId(CHANNEL_PREVIEW_BUTTON_TEST_ID));
120120
});
121121

122-
expect(searchController.state.getLatestValue().focusedMessage).toStrictEqual(
122+
expect(searchController.internalState.getLatestValue().focusedMessage).toStrictEqual(
123123
messageResponseData,
124124
);
125125
expect(mockSetActiveChannel.mock.calls[0][0].id).toBe(messageResponseData.channel.id);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
export * from './useSearchFocusedMessage';
12
export * from './useSearchQueriesInProgress';
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { DefaultStreamChatGenerics } from '../../../types';
2+
import type { InternalSearchControllerState } from 'stream-chat';
3+
import { useChatContext } from '../../../context';
4+
import { useStateStore } from '../../../store';
5+
6+
const searchControllerStateSelector = <
7+
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
8+
>(
9+
nextValue: InternalSearchControllerState<StreamChatGenerics>,
10+
) => ({ focusedMessage: nextValue.focusedMessage });
11+
12+
export const useSearchFocusedMessage = <
13+
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
14+
>() => {
15+
const { searchController } = useChatContext<StreamChatGenerics>('Channel');
16+
const { focusedMessage } = useStateStore(
17+
searchController.internalState,
18+
searchControllerStateSelector,
19+
);
20+
21+
return focusedMessage;
22+
};

src/experimental/Search/hooks/useSearchQueriesInProgress.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ import { useStateStore } from '../../../store';
55
import type { SearchController, SearchControllerState, SearchSource } from 'stream-chat';
66
import type { DefaultStreamChatGenerics } from '../../../types';
77

8-
const searchControllerStateSelector = <
9-
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
10-
>(
11-
value: SearchControllerState<StreamChatGenerics>,
12-
) => ({
8+
const searchControllerStateSelector = (value: SearchControllerState) => ({
139
sources: value.sources,
1410
});
1511

0 commit comments

Comments
 (0)