Skip to content

Commit

Permalink
fix: console warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
rpenido committed Apr 16, 2024
1 parent afa8fdd commit 8e468ef
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 30 deletions.
12 changes: 12 additions & 0 deletions src/search-modal/SearchModal.scss
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,16 @@
.fs-large {
font-size: $font-size-base * 1.25;
}

.search-result {
&:hover {
background-color: $gray-100;
cursor: pointer;
}

&:hover.text-muted {
cursor: unset;
background-color: unset;
}
}
}
66 changes: 36 additions & 30 deletions src/search-modal/SearchResult.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// @ts-check
import React, { useCallback, useMemo } from 'react';
import { getConfig, getPath } from '@edx/frontend-platform';
import { useIntl } from '@edx/frontend-platform/i18n';
import {
Button,
Icon,
IconButton,
Stack,
Expand All @@ -24,6 +24,7 @@ import { useSelector } from 'react-redux';
import { useNavigate } from 'react-router-dom';

import { getStudioHomeData } from '../studio-home/data/selectors';
import messages from './messages';

/**
* @typedef {import('instantsearch.js').Hit<{
Expand Down Expand Up @@ -70,6 +71,7 @@ const ItemIcon = {
* @type {React.FC<{ hit: CustomHit, closeSearch?: () => void}>}
*/
const SearchResult = ({ hit, closeSearch }) => {
const intl = useIntl();
const navigate = useNavigate();
const { libraryAuthoringMfeUrl, redirectToLibraryAuthoringMfe } = useSelector(getStudioHomeData);

Expand Down Expand Up @@ -119,7 +121,7 @@ const SearchResult = ({ hit, closeSearch }) => {

/**
* Navigates to the context of the hit
* @param {React.MouseEvent} e
* @param {(React.MouseEvent | React.KeyboardEvent)} e
* @returns {void}
* */
const navigateToContext = (e) => {
Expand All @@ -129,6 +131,11 @@ const SearchResult = ({ hit, closeSearch }) => {
return;
}

// @ts-ignore Cannot use 'instanceof' with React.KeyboardEvent to narrow down the type
if (e.nativeEvent instanceof KeyboardEvent && e.key !== 'Enter' && e.key !== ' ') {
return;
}

if (redirectUrl.startsWith('http')) {
window.location.href = redirectUrl;
return;
Expand All @@ -139,37 +146,36 @@ const SearchResult = ({ hit, closeSearch }) => {
};

return (
<Button
variant="tertiary w-100 text-left"
<Stack
className={`border-bottom search-result p-2 align-items-start ${!redirectUrl ? 'text-muted' : ''}`}
direction="horizontal"
gap={3}
onClick={navigateToContext}
disabled={!redirectUrl}
onKeyDown={navigateToContext}
tabIndex={redirectUrl ? 0 : undefined}
role="button"
>
<Stack
className="border-bottom search-result p-2 w-100 align-items-start"
direction="horizontal"
gap={3}
>
<Icon className="text-muted" src={ItemIcon[hit.block_type] || Article} />
<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}
disabled={!newWindowUrl}
onClick={openContextInNewWindow}
/>
<Icon className="text-muted" src={ItemIcon[hit.block_type] || Article} />
<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>
</Button>
<IconButton
src={OpenInNew}
iconAs={Icon}
disabled={!newWindowUrl}
onClick={openContextInNewWindow}
alt={intl.formatMessage(messages.openInNewWindow)}
/>
</Stack>
);
};

Expand Down
5 changes: 5 additions & 0 deletions src/search-modal/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ const messages = defineMessages({
defaultMessage: 'Show more results',
description: 'Show more results button label',
},
openInNewWindow: {
id: 'course-authoring.course-search.openInNewWindow',
defaultMessage: 'Open in new window',
description: 'Alt text for the button that opens the search result in a new window',
},
});

export default messages;

0 comments on commit 8e468ef

Please sign in to comment.