Skip to content

Commit 7ef8f14

Browse files
committed
refator: remove SearchContext from sidebar
1 parent f9fa28e commit 7ef8f14

File tree

6 files changed

+78
-40
lines changed

6 files changed

+78
-40
lines changed

src/library-authoring/collections/CollectionDetails.tsx

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import classNames from 'classnames';
55

66
import { getItemIcon } from '../../generic/block-type-utils';
77
import { ToastContext } from '../../generic/toast-context';
8-
import { BlockTypeLabel, type CollectionHit, useSearchContext } from '../../search-manager';
8+
import { BlockTypeLabel, type CollectionHit, useGetBlockTypes } from '../../search-manager';
99
import type { ContentLibrary } from '../data/api';
1010
import { useUpdateCollection } from '../data/apiHooks';
1111
import HistoryWidget from '../generic/history-widget';
@@ -36,10 +36,19 @@ const BlockCount = ({
3636
);
3737
};
3838

39-
const CollectionStatsWidget = () => {
40-
const {
41-
blockTypes,
42-
} = useSearchContext();
39+
interface CollectionStatsWidgetProps {
40+
collection: CollectionHit,
41+
}
42+
43+
const CollectionStatsWidget = ({ collection }: CollectionStatsWidgetProps) => {
44+
const { data: blockTypes } = useGetBlockTypes([
45+
`context_key = "${collection.contextKey}"`,
46+
`collections.key = "${collection.blockId}"`,
47+
]);
48+
49+
if (!blockTypes) {
50+
return null;
51+
}
4352

4453
const blockTypesArray = Object.entries(blockTypes)
4554
.map(([blockType, count]) => ({ blockType, count }))
@@ -51,7 +60,12 @@ const CollectionStatsWidget = () => {
5160

5261
if (totalBlocksCount === 0) {
5362
return (
54-
<div className="text-center text-muted">
63+
<div
64+
className="text-center text-muted align-content-center"
65+
style={{
66+
height: '72px', // same height as the BlockCount component
67+
}}
68+
>
5569
<FormattedMessage {...messages.detailsTabStatsNoComponents} />
5670
</div>
5771
);
@@ -135,7 +149,7 @@ const CollectionDetails = ({ library, collection }: CollectionDetailsProps) => {
135149
<h3 className="h5">
136150
{intl.formatMessage(messages.detailsTabStatsTitle)}
137151
</h3>
138-
<CollectionStatsWidget />
152+
<CollectionStatsWidget collection={collection} />
139153
</div>
140154
<hr className="w-100" />
141155
<div>

src/library-authoring/library-sidebar/LibrarySidebar.tsx

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ import {
66
} from '@openedx/paragon';
77
import { Close } from '@openedx/paragon/icons';
88
import { useIntl } from '@edx/frontend-platform/i18n';
9-
import { SearchParams } from 'meilisearch';
109

11-
import {
12-
SearchContextProvider,
13-
} from '../../search-manager';
1410
import { AddContentContainer, AddContentHeader } from '../add-content';
1511
import { CollectionInfo, CollectionInfoHeader } from '../collections';
1612
import { ContentLibrary } from '../data/api';
@@ -68,36 +64,23 @@ const LibrarySidebar = ({ library }: LibrarySidebarProps) => {
6864
const buildBody = () : React.ReactNode => bodyComponentMap[sidebarBodyComponent || 'unknown'];
6965
const buildHeader = (): React.ReactNode => headerComponentMap[sidebarBodyComponent || 'unknown'];
7066

71-
const collectionQuery: SearchParams | undefined = currentCollectionHit ? {
72-
filter: ['type = "collection"', `context_key = "${library.id}"`, `block_id = "${currentCollectionHit.blockId}"`],
73-
limit: 1,
74-
} : undefined;
75-
7667
return (
77-
<SearchContextProvider
78-
extraFilter={[
79-
`context_key = "${library.id}"`,
80-
...(currentCollectionHit ? [`collections.key = "${currentCollectionHit.blockId}"`] : []),
81-
]}
82-
overrideQueries={{ ...(collectionQuery ? { collections: collectionQuery } : {}) }}
83-
>
84-
<Stack gap={4} className="p-3 text-primary-700">
85-
<Stack direction="horizontal" className="d-flex justify-content-between">
86-
{buildHeader()}
87-
<IconButton
88-
className="mt-1"
89-
src={Close}
90-
iconAs={Icon}
91-
alt={intl.formatMessage(messages.closeButtonAlt)}
92-
onClick={closeLibrarySidebar}
93-
size="inline"
94-
/>
95-
</Stack>
96-
<div>
97-
{buildBody()}
98-
</div>
68+
<Stack gap={4} className="p-3 text-primary-700">
69+
<Stack direction="horizontal" className="d-flex justify-content-between">
70+
{buildHeader()}
71+
<IconButton
72+
className="mt-1"
73+
src={Close}
74+
iconAs={Icon}
75+
alt={intl.formatMessage(messages.closeButtonAlt)}
76+
onClick={closeLibrarySidebar}
77+
size="inline"
78+
/>
9979
</Stack>
100-
</SearchContextProvider>
80+
<div>
81+
{buildBody()}
82+
</div>
83+
</Stack>
10184
);
10285
};
10386

src/search-manager/SearchManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export const SearchContextProvider: React.FC<{
166166
searchSortOrder,
167167
setSearchSortOrder,
168168
defaultSearchSortOrder,
169-
closeSearchModal: props.closeSearchModal ?? (() => {}),
169+
closeSearchModal: props.closeSearchModal ?? (() => { }),
170170
hasError: hasConnectionError || result.isError,
171171
...result,
172172
},

src/search-manager/data/api.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,29 @@ export async function fetchSearchResults({
303303
};
304304
}
305305

306+
/**
307+
* Fetch the block types facet distribution for the search results.
308+
*/
309+
export const fetchBlockTypes = async (
310+
client: MeiliSearch,
311+
indexName: string,
312+
extraFilter?: Filter,
313+
): Promise<Record<string, number>> => {
314+
// Convert 'extraFilter' into an array
315+
const extraFilterFormatted = forceArray(extraFilter);
316+
317+
const { results } = await client.multiSearch({
318+
queries: [{
319+
indexUid: indexName,
320+
facets: ['block_type', 'content.problem_types'],
321+
filter: extraFilterFormatted,
322+
limit: 0, // We don't need any "hits" for this - just the facetDistribution
323+
}],
324+
});
325+
326+
return results[0].facetDistribution?.block_type ?? {};
327+
};
328+
306329
/** Information about a single tag in the tag tree, as returned by fetchAvailableTagOptions() */
307330
export interface TagEntry {
308331
tagName: string;

src/search-manager/data/apiHooks.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
fetchTagsThatMatchKeyword,
1111
getContentSearchConfig,
1212
fetchDocumentById,
13+
fetchBlockTypes,
1314
OverrideQueries,
1415
} from './api';
1516

@@ -243,6 +244,22 @@ export const useTagFilterOptions = (args: {
243244
return { ...mainQuery, data };
244245
};
245246

247+
export const useGetBlockTypes = (extraFilters: Filter) => {
248+
const { client, indexName } = useContentSearchConnection();
249+
return useQuery({
250+
enabled: client !== undefined && indexName !== undefined,
251+
queryKey: [
252+
'content_search',
253+
client?.config.apiKey,
254+
client?.config.host,
255+
indexName,
256+
extraFilters,
257+
'block_types',
258+
],
259+
queryFn: () => fetchBlockTypes(client!, indexName!, extraFilters),
260+
});
261+
};
262+
246263
/* istanbul ignore next */
247264
export const useGetSingleDocument = ({ client, indexName, id }: {
248265
client?: MeiliSearch;

src/search-manager/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ export { default as SearchKeywordsField } from './SearchKeywordsField';
88
export { default as SearchSortWidget } from './SearchSortWidget';
99
export { default as Stats } from './Stats';
1010
export { HIGHLIGHT_PRE_TAG, HIGHLIGHT_POST_TAG } from './data/api';
11+
export { useGetBlockTypes } from './data/apiHooks';
1112

1213
export type { CollectionHit, ContentHit, ContentHitTags } from './data/api';

0 commit comments

Comments
 (0)