forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
356 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,147 @@ | ||
/* eslint-disable react/prop-types */ | ||
// @ts-check | ||
import React from 'react'; | ||
import { Highlight } from 'react-instantsearch'; | ||
import BlockTypeLabel from './BlockTypeLabel'; | ||
import { getConfig, getPath } from '@edx/frontend-platform'; | ||
import { | ||
Icon, | ||
IconButton, | ||
Stack, | ||
} from '@openedx/paragon'; | ||
import { | ||
Article, | ||
Folder, | ||
OpenInNew, | ||
Question, | ||
TextFields, | ||
Videocam, | ||
} from '@openedx/paragon/icons'; | ||
import { | ||
Highlight, | ||
Snippet, | ||
} from 'react-instantsearch'; | ||
import { useSelector } from 'react-redux'; | ||
import { useNavigate } from 'react-router-dom'; | ||
|
||
import { getStudioHomeData } from '../studio-home/data/selectors'; | ||
|
||
/** | ||
* A single search result (row), usually represents an XBlock/Component | ||
* @type {React.FC<{hit: import('instantsearch.js').Hit<{ | ||
* id: string, | ||
* display_name: string, | ||
* block_type: string, | ||
* 'content.html_content'?: string, | ||
* 'content.capa_content'?: string, | ||
* breadcrumbs: {display_name: string}[]}>, | ||
* @typedef {import('instantsearch.js').Hit<{ | ||
* id: string, | ||
* usage_key: string, | ||
* context_key: string, | ||
* display_name: string, | ||
* block_type: string, | ||
* 'content.html_content'?: string, | ||
* 'content.capa_content'?: string, | ||
* breadcrumbs: {display_name: string}[] | ||
* breadcrumbsNames: string[], | ||
* }>} CustomHit | ||
*/ | ||
|
||
/** | ||
* Custom Highlight component that uses the <b> tag for highlighting | ||
* @type {React.FC<{ | ||
* attribute: keyof CustomHit | string[], | ||
* hit: CustomHit, | ||
* separator?: string, | ||
* }>} | ||
*/ | ||
const SearchResult = ({ hit }) => ( | ||
<div className="my-2 pb-2 border-bottom"> | ||
<div className="hit-name small"> | ||
<strong><Highlight attribute="display_name" hit={hit} /></strong>{' '} | ||
(<BlockTypeLabel type={hit.block_type} />) | ||
</div> | ||
<div className="hit-description x-small text-truncate"> | ||
<Highlight attribute="content.html_content" hit={hit} /> | ||
<Highlight attribute="content.capa_content" hit={hit} /> | ||
</div> | ||
<div className="text-muted x-small"> | ||
{hit.breadcrumbs.map((bc, i) => ( | ||
// eslint-disable-next-line react/no-array-index-key | ||
<span key={i}>{bc.display_name} {i !== hit.breadcrumbs.length - 1 ? '/' : ''} </span> | ||
))} | ||
</div> | ||
</div> | ||
const CustomHighlight = ({ attribute, hit, separator }) => ( | ||
<Highlight | ||
attribute={attribute} | ||
hit={hit} | ||
separator={separator} | ||
highlightedTagName="b" | ||
/> | ||
); | ||
|
||
const ItemIcon = { | ||
vertical: Folder, | ||
sequential: Folder, | ||
chapter: Folder, | ||
problem: Question, | ||
video: Videocam, | ||
html: TextFields, | ||
}; | ||
|
||
/** | ||
* Returns the URL for the context of the hit | ||
* @param {CustomHit} hit | ||
* @param {boolean} newWindow | ||
* @param {string} libraryAuthoringMfeUrl | ||
* @returns {string} | ||
*/ | ||
const getContextUrl = (hit, newWindow, libraryAuthoringMfeUrl) => { | ||
const { context_key: contextKey, usage_key: usageKey } = hit; | ||
if (contextKey.startsWith('course-v1:')) { | ||
if (newWindow) { | ||
return `${getPath(getConfig().PUBLIC_PATH)}/course/${contextKey}?show=${encodeURIComponent(usageKey)}`; | ||
} | ||
return `/course/${contextKey}?show=${encodeURIComponent(usageKey)}`; | ||
} | ||
if (usageKey.includes('lb:')) { | ||
return `${libraryAuthoringMfeUrl}library/${contextKey}`; | ||
} | ||
return '#'; | ||
}; | ||
|
||
/** | ||
* A single search result (row), usually represents an XBlock/Component | ||
* @type {React.FC<{ hit: CustomHit, closeSearch: () => void}>} | ||
*/ | ||
const SearchResult = ({ hit, closeSearch }) => { | ||
const navigate = useNavigate(); | ||
const { libraryAuthoringMfeUrl } = useSelector(getStudioHomeData); | ||
|
||
/** | ||
* Navigates to the context of the hit | ||
* @param {React.MouseEvent} e | ||
* @param {boolean} newWindow | ||
* @returns {void} | ||
* */ | ||
const navigateToContext = (e, newWindow) => { | ||
e.stopPropagation(); | ||
const url = getContextUrl(hit, newWindow, libraryAuthoringMfeUrl); | ||
if (newWindow) { | ||
window.open(url, '_blank'); | ||
return; | ||
} | ||
|
||
if (url.startsWith('http')) { | ||
window.location.href = url; | ||
return; | ||
} | ||
|
||
navigate(url); | ||
closeSearch(); | ||
}; | ||
|
||
return ( | ||
<Stack | ||
className="border-bottom search-result p-2" | ||
role="button" | ||
direction="horizontal" | ||
gap={2} | ||
onClick={navigateToContext} | ||
> | ||
<div className="align-top"> | ||
<Icon className="align-top" src={ItemIcon[hit.block_type] || Article} /> | ||
</div> | ||
<Stack> | ||
<div className="hit-name small"> | ||
<CustomHighlight attribute="display_name" hit={hit} /> | ||
</div> | ||
<div className="hit-description x-small text-truncate"> | ||
<Snippet attribute="content.html_content" hit={hit} highlightedTagName="b" /> | ||
<Snippet attribute="content.capa_content" hit={hit} highlightedTagName="b" /> | ||
</div> | ||
<div className="text-muted x-small"> | ||
<CustomHighlight attribute="breadcrumbsNames" separator=" / " hit={hit} /> | ||
</div> | ||
</Stack> | ||
<IconButton src={OpenInNew} iconAs={Icon} onClick={(e) => navigateToContext(e, true)} /> | ||
</Stack> | ||
); | ||
}; | ||
|
||
export default SearchResult; |
Oops, something went wrong.