Skip to content

Commit

Permalink
refactor: url redirect for libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
rpenido committed Apr 12, 2024
1 parent 53d70e3 commit 6cb2d96
Showing 1 changed file with 52 additions and 36 deletions.
88 changes: 52 additions & 36 deletions src/search-modal/SearchResult.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable react/prop-types */
// @ts-check
import React from 'react';
import React, { useCallback, useMemo } from 'react';
import { getConfig, getPath } from '@edx/frontend-platform';
import {
Button,
Expand Down Expand Up @@ -65,63 +65,74 @@ const ItemIcon = {
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:')) {
const courseSufix = `course/${contextKey}?show=${encodeURIComponent(usageKey)}`;
if (newWindow) {
return `${getPath(getConfig().PUBLIC_PATH)}${courseSufix}`;
}
return `/${courseSufix}`;
}
if (usageKey.startsWith('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);
const { libraryAuthoringMfeUrl, redirectToLibraryAuthoringMfe } = useSelector(getStudioHomeData);

/**
* Navigates to the context of the hit
* Returns the URL for the context of the hit
* @param {CustomHit} hit
* @param {boolean?} newWindow
* @param {string} libraryAuthoringMfeUrl
* @returns {string?}
*/
const getContextUrl = useCallback((newWindow) => {
const { context_key: contextKey, usage_key: usageKey } = hit;
if (contextKey.startsWith('course-v1:')) {
const courseSufix = `course/${contextKey}?show=${encodeURIComponent(usageKey)}`;
if (newWindow) {
return `${getPath(getConfig().PUBLIC_PATH)}${courseSufix}`;
}
return `/${courseSufix}`;
}
if (usageKey.startsWith('lb:')) {
if (redirectToLibraryAuthoringMfe) {
return `${libraryAuthoringMfeUrl}library/${contextKey}`;
}
}

// No context URL for this hit
return undefined;
}, [libraryAuthoringMfeUrl, redirectToLibraryAuthoringMfe]);

const redirectUrl = useMemo(() => getContextUrl(), [libraryAuthoringMfeUrl, redirectToLibraryAuthoringMfe]);
const newWindowUrl = useMemo(() => getContextUrl(true), [libraryAuthoringMfeUrl, redirectToLibraryAuthoringMfe]);

/**
* Opens the context of the hit in a new window
* @param {React.MouseEvent} e
* @param {boolean} newWindow
* @returns {void}
* */
const navigateToContext = (e, newWindow) => {
const openContextInNewWindow = (e) => {
e.stopPropagation();
const url = getContextUrl(hit, newWindow, libraryAuthoringMfeUrl);
if (newWindow) {
window.open(url, '_blank');
return;
}
window.open(newWindowUrl, '_blank');
};

if (url.startsWith('http')) {
window.location.href = url;
/**
* Navigates to the context of the hit
* @param {React.MouseEvent} e
* @returns {void}
* */
const navigateToContext = (e) => {
e.stopPropagation();
if (redirectUrl.startsWith('http')) {

Check failure on line 122 in src/search-modal/SearchResult.jsx

View workflow job for this annotation

GitHub Actions / tests

'redirectUrl' is possibly 'undefined'.
window.location.href = redirectUrl;

Check failure on line 123 in src/search-modal/SearchResult.jsx

View workflow job for this annotation

GitHub Actions / tests

Type 'string | undefined' is not assignable to type 'string'.
return;
}

navigate(url);
navigate(redirectUrl);

Check failure on line 127 in src/search-modal/SearchResult.jsx

View workflow job for this annotation

GitHub Actions / tests

No overload matches this call.
closeSearch?.();
};

return (
<Button
variant="tertiary w-100 text-left"
onClick={navigateToContext}
disabled={!redirectUrl}
>
<Stack
className="border-bottom search-result p-2 w-100"
Expand All @@ -143,7 +154,12 @@ const SearchResult = ({ hit, closeSearch }) => {
<CustomHighlight attribute="breadcrumbsNames" separator=" / " hit={hit} />
</div>
</Stack>
<IconButton src={OpenInNew} iconAs={Icon} onClick={(e) => navigateToContext(e, true)} />
<IconButton
src={OpenInNew}
iconAs={Icon}
disabled={!newWindowUrl}
onClick={openContextInNewWindow}
/>
</Stack>
</Button>
);
Expand Down

0 comments on commit 6cb2d96

Please sign in to comment.