Skip to content

Commit

Permalink
feat: direct link to single block in library
Browse files Browse the repository at this point in the history
Adds support for displaying single xblock in a library when passed a
query param: usageKey. This is required for directing users to a
specific block from course.
  • Loading branch information
navinkarkera committed Oct 23, 2024
1 parent 11470f2 commit 7394dea
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/library-authoring/components/LibraryComponents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ const LibraryComponents = ({ variant }: LibraryComponentsProps) => {
fetchNextPage,
isLoading,
isFiltered,
usageKey,
} = useSearchContext();
const { openAddContentSidebar } = useLibraryContext();
const { openAddContentSidebar, openComponentInfoSidebar } = useLibraryContext();

if (usageKey) {
openComponentInfoSidebar(usageKey);
}

const componentList = variant === 'preview' ? hits.slice(0, LIBRARY_SECTION_PREVIEW_LIMIT) : hits;

Expand Down
9 changes: 5 additions & 4 deletions src/search-manager/SearchKeywordsField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import { useSearchContext } from './SearchManager';
*/
const SearchKeywordsField: React.FC<{ className?: string, placeholder?: string }> = (props) => {
const intl = useIntl();
const { searchKeywords, setSearchKeywords } = useSearchContext();
const { searchKeywords, setSearchKeywords, usageKey } = useSearchContext();
const defaultPlaceholder = usageKey ? messages.clearUsageKeyToSearch : messages.inputPlaceholder;
const { placeholder = intl.formatMessage(defaultPlaceholder) } = props;

return (
<SearchField.Advanced
Expand All @@ -18,13 +20,12 @@ const SearchKeywordsField: React.FC<{ className?: string, placeholder?: string }
onClear={() => setSearchKeywords('')}
value={searchKeywords}
className={props.className}
disabled={!!usageKey}
>
<SearchField.Label />
<SearchField.Input
autoFocus
placeholder={props.placeholder ? props.placeholder : intl.formatMessage(
messages.inputPlaceholder,
)}
placeholder={placeholder}
/>
<SearchField.ClearButton />
<SearchField.SubmitButton />
Expand Down
14 changes: 14 additions & 0 deletions src/search-manager/SearchManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export interface SearchContextData {
hasError: boolean;
collectionHits: CollectionHit[];
totalCollectionHits: number;
usageKey: string;
}

const SearchContext = React.createContext<SearchContextData | undefined>(undefined);
Expand Down Expand Up @@ -101,7 +102,17 @@ export const SearchContextProvider: React.FC<{
const [blockTypesFilter, setBlockTypesFilter] = React.useState<string[]>([]);
const [problemTypesFilter, setProblemTypesFilter] = React.useState<string[]>([]);
const [tagsFilter, setTagsFilter] = React.useState<string[]>([]);
const [usageKey, setUsageKey] = useStateWithUrlSearchParam(
'',
'usageKey',
(value: string) => value,
(value: string) => value,
);

let extraFilter: string[] = forceArray(props.extraFilter);
if (usageKey) {
extraFilter.push(`usage_key = "${usageKey}"`);
}

// The search sort order can be set via the query string
// E.g. ?sort=display_name:desc maps to SearchSortOption.TITLE_ZA.
Expand Down Expand Up @@ -131,12 +142,14 @@ export const SearchContextProvider: React.FC<{
blockTypesFilter.length > 0
|| problemTypesFilter.length > 0
|| tagsFilter.length > 0
|| !!usageKey
);
const isFiltered = canClearFilters || (searchKeywords !== '');
const clearFilters = React.useCallback(() => {
setBlockTypesFilter([]);
setTagsFilter([]);
setProblemTypesFilter([]);
setUsageKey('');
}, []);

// Initialize a connection to Meilisearch:
Expand Down Expand Up @@ -176,6 +189,7 @@ export const SearchContextProvider: React.FC<{
defaultSearchSortOrder,
closeSearchModal: props.closeSearchModal ?? (() => { }),
hasError: hasConnectionError || result.isError,
usageKey,
...result,
},
}, props.children);
Expand Down
5 changes: 5 additions & 0 deletions src/search-manager/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ const messages = defineMessages({
defaultMessage: 'Search',
description: 'Placeholder text shown in the keyword input field when the user has not yet entered a keyword',
},
clearUsageKeyToSearch: {
id: 'course-authoring.search-manager.clearUsageKeyToSearch',
defaultMessage: 'Displaying single block, clear filters to search',
description: 'Placeholder text shown in the keyword input field when a single block filtered by usage key is shown',
},
blockTypeFilter: {
id: 'course-authoring.search-manager.blockTypeFilter',
defaultMessage: 'Type',
Expand Down

0 comments on commit 7394dea

Please sign in to comment.