Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/NavBar/useTour.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ export const getResultsSteps = () => {
title: 'View highlights',
text: 'Turn on highlights to see the context of your search terms. Note that the length of returned highlights is limited by publishers.',
attachTo: {
element: '#menu-button-tour-view-highlights',
element: '#tour-view-highlights',
on: 'bottom',
},
classes: 'example-step-extra-class',
Expand Down
42 changes: 26 additions & 16 deletions src/components/ResultList/Item/Item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { useColorModeColors } from '@/lib/useColorModeColors';

import { getFormattedNumericPubdate, unwrapStringValue } from '@/utils/common/formatters';
import { IDocsEntity } from '@/api/search/types';
import { keys, toPairs } from 'ramda';

const AbstractPreview = dynamic<IAbstractPreviewProps>(
() =>
Expand All @@ -48,7 +49,7 @@ export interface IItemProps {
useNormCite?: boolean;
showHighlights?: boolean;
isFetchingHighlights?: boolean;
highlights?: string[];
highlights?: Record<string, string[]>;
extraInfo?: string;
linkNewTab?: boolean;
defaultCitation: string;
Expand Down Expand Up @@ -209,21 +210,30 @@ const Highlights = ({
showIndicator && <CircularProgress mt={5} isIndeterminate size="20px" />
) : (
<Fade in={!!highlights}>
{highlights.length > 0 ? (
highlights.map((hl) => (
<Text
sx={{
// Apply a style to the <em> tag, which is included in the highlight string
'& em': {
backgroundColor: 'blue.100',
color: 'gray.800',
padding: 'var(--chakra-space-1)',
fontWeight: 'bold',
},
}}
key={hl}
dangerouslySetInnerHTML={{ __html: hl }}
></Text>
{keys(highlights).length > 0 ? (
toPairs(highlights).map(([key, hls]) => (
<>
<Text fontSize="sm" fontWeight="semibold">
{key.toLocaleUpperCase()}
</Text>
{hls.map((hl) => (
<Text
ml={2}
my={1}
sx={{
// Apply a style to the <em> tag, which is included in the highlight string
'& em': {
backgroundColor: 'blue.100',
color: 'gray.800',
padding: 'var(--chakra-space-1)',
fontWeight: 'bold',
},
}}
key={hl}
dangerouslySetInnerHTML={{ __html: hl }}
></Text>
))}
</>
))
) : (
<Text>No Highlights</Text>
Expand Down
37 changes: 12 additions & 25 deletions src/components/ResultList/ListActions.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { BellIcon, ChevronDownIcon, SettingsIcon } from '@chakra-ui/icons';
import { BellIcon, ChevronDownIcon } from '@chakra-ui/icons';
import {
Box,
Button,
Checkbox,
Flex,
Icon,
IconButton,
Menu,
MenuButton,
Expand All @@ -17,7 +16,6 @@ import {
MenuOptionGroup,
Portal,
Stack,
Switch,
Text,
Tooltip,
useDisclosure,
Expand Down Expand Up @@ -48,8 +46,9 @@ import { SolrSort, SolrSortField } from '@/api/models';
import { useVaultBigQuerySearch } from '@/api/vault/vault';
import { Bibcode } from '@/api/search/types';
import { ExportApiFormatKey } from '@/api/export/types';
import { DocumentTextIcon } from '@heroicons/react/24/outline';
import { useExportFormats } from '@/lib/useExportFormats';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faHighlighter } from '@fortawesome/free-solid-svg-icons';

export interface IListActionsProps {
onSortChange?: ISortProps<SolrSort, SolrSortField>['onChange'];
Expand Down Expand Up @@ -187,7 +186,7 @@ export const ListActions = (props: IListActionsProps): ReactElement => {
id="tour-email-notification"
/>
</Tooltip>
<SettingsMenu />
<HighlightsToggle />
</Flex>
)}
</Flex>
Expand Down Expand Up @@ -319,31 +318,19 @@ const SortWrapper = ({ onChange }: { onChange: ISortProps<SolrSort, SolrSortFiel
);
};

const SettingsMenu = () => {
return (
<Menu isLazy autoSelect={false} id="tour-view-highlights">
<Tooltip label="Turn on/off highlights">
<MenuButton as={IconButton} aria-label="Result list settings" variant="outline" icon={<SettingsIcon />} />
</Tooltip>
<MenuList>
<HighlightsToggle />
</MenuList>
</Menu>
);
};

const HighlightsToggle = () => {
const showHighlights = useStore((state) => state.showHighlights);
const toggleShowHighlights = useStore((state) => state.toggleShowHighlights);

return (
<Tooltip label="Show or hide keyword highlights in the results.">
<MenuItem onClick={toggleShowHighlights} icon={<Icon as={DocumentTextIcon} fontSize={20} />} iconSpacing={4}>
<Flex justifyContent="space-between">
Highlights
<Switch isChecked={showHighlights} id="show-highlights" onClick={toggleShowHighlights} />
</Flex>
</MenuItem>
<Tooltip label={`${showHighlights ? 'Hide' : 'Show'} keyword highlights in the results.`}>
<IconButton
id="tour-view-highlights"
icon={<FontAwesomeIcon icon={faHighlighter} />}
aria-label={`${showHighlights ? 'Hide' : 'Show'} keyword highlights in the results.`}
variant={showHighlights ? 'solid' : 'outline'}
onClick={toggleShowHighlights}
/>
</Tooltip>
);
};
Expand Down
2 changes: 1 addition & 1 deletion src/components/ResultList/SimpleResultList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export const SimpleResultList = (props: ISimpleResultListProps): ReactElement =>
hideCheckbox={!isClient ? true : hideCheckboxes}
hideActions={hideActions}
showHighlights={allowHighlight && showHighlights}
highlights={highlights?.[index] ?? []}
highlights={highlights?.[index] ?? {}}
isFetchingHighlights={allowHighlight && isFetchingHighlights}
useNormCite={useNormCite}
defaultCitation={defaultCitations?.get(doc.bibcode)}
Expand Down
20 changes: 1 addition & 19 deletions src/components/ResultList/useHighlights.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
import { AppState, useStore } from '@/store';
import { decode } from 'he';
import { flatten, map, pipe, reduce, values } from 'ramda';
import { useGetHighlights } from '@/api/search/search';

/**
* Transform incoming highlights data into 2d array
*
* This also performs decoding of HTML entities on the strings
*
* {
* 1111: { abstract: [ "foo" ] },
* 2222: { abstract: [ "bar" ], title: [ "baz &copy;" ] }
* }
* ---> [["foo"], ["bar", "baz ©"]]
*/
const decoder = pipe<[Record<string, string[]>], string[][], string[], string[]>(values, flatten, map(decode));
const transformHighlights = pipe<[Record<string, string[]>[]], string[][]>(
reduce((acc, value) => [...acc, [...decoder(value)]], [] as string[][]),
);

const selectors = {
latestQuery: (state: AppState) => state.latestQuery,
showHighlights: (state: AppState) => state.showHighlights,
Expand All @@ -43,5 +25,5 @@ export const useHighlights = () => {
// Do this first to maintain results ordering
const highlights = data?.docs.map(({ id }) => data.highlighting[id]) ?? [];

return { showHighlights, highlights: transformHighlights(highlights), isFetchingHighlights: isFetching };
return { showHighlights, highlights, isFetchingHighlights: isFetching };
};
Loading