diff --git a/src/components/ResultList/Item/Item.tsx b/src/components/ResultList/Item/Item.tsx index 574f2cccf..e135ffe66 100644 --- a/src/components/ResultList/Item/Item.tsx +++ b/src/components/ResultList/Item/Item.tsx @@ -50,6 +50,7 @@ export interface IItemProps { highlights?: string[]; extraInfo?: string; linkNewTab?: boolean; + defaultCitation: string; } export const Item = (props: IItemProps): ReactElement => { @@ -64,6 +65,7 @@ export const Item = (props: IItemProps): ReactElement => { highlights, extraInfo, linkNewTab = false, + defaultCitation = '', } = props; const { bibcode, pubdate, title = ['Untitled'], author = [], author_count, pub } = doc; const formattedPubDate = getFormattedNumericPubdate(pubdate); @@ -118,7 +120,7 @@ export const Item = (props: IItemProps): ReactElement => { - {!isClient || hideActions ? null : } + {!isClient || hideActions ? null : } diff --git a/src/components/ResultList/Item/ItemResourceDropdowns.tsx b/src/components/ResultList/Item/ItemResourceDropdowns.tsx index 2b250e531..dcd638743 100644 --- a/src/components/ResultList/Item/ItemResourceDropdowns.tsx +++ b/src/components/ResultList/Item/ItemResourceDropdowns.tsx @@ -9,14 +9,11 @@ import { MouseEventHandler, ReactElement, useEffect } from 'react'; import { SimpleLinkDropdown } from '@/components/Dropdown'; import { isBrowser } from '@/utils/common/guards'; import { IDocsEntity } from '@/api/search/types'; -import { useGetExportCitation } from '@/api/export/export'; -import { useSettings } from '@/lib/useSettings'; -import { exportFormats } from '@/components/CitationExporter/models'; -import { ExportApiFormatKey } from '@/api/export/types'; import CopyToClipboard from 'react-copy-html-to-clipboard'; export interface IItemResourceDropdownsProps { doc: IDocsEntity; + defaultCitation: string; } export interface IItem { @@ -25,22 +22,10 @@ export interface IItem { path?: string; } -export const ItemResourceDropdowns = ({ doc }: IItemResourceDropdownsProps): ReactElement => { +export const ItemResourceDropdowns = ({ doc, defaultCitation }: IItemResourceDropdownsProps): ReactElement => { const router = useRouter(); const isClient = useIsClient(); const toast = useToast({ duration: 2000 }); - const { settings } = useSettings(); - const { defaultExportFormat, customFormats } = settings; - - const { data: citationData } = useGetExportCitation( - { - // format: values(exportFormats).find((f) => f.label === defaultExportFormat).id, - format: ExportApiFormatKey.agu, - customFormat: defaultExportFormat === exportFormats.custom.label ? customFormats[0].code : undefined, - bibcode: [doc.bibcode], - }, - { enabled: !!settings?.defaultExportFormat }, - ); const { hasCopied, onCopy, setValue, value } = useClipboard(''); @@ -172,10 +157,10 @@ export const ItemResourceDropdowns = ({ doc }: IItemResourceDropdownsProps): Rea }; const handleCitationCopied = () => { - if (citationData?.export) { + if (defaultCitation !== '') { toast({ status: 'info', title: 'Copied to Clipboard' }); } else { - toast({ status: 'error', title: 'There was a problem fetching citation' }); + toast({ status: 'error', title: 'There was a problem fetching citation. Try reloading the page.' }); } }; @@ -300,7 +285,7 @@ export const ItemResourceDropdowns = ({ doc }: IItemResourceDropdownsProps): Rea Copy URL - + Copy Citation diff --git a/src/components/ResultList/SimpleResultList.tsx b/src/components/ResultList/SimpleResultList.tsx index c95d4b66e..ba14be88e 100644 --- a/src/components/ResultList/SimpleResultList.tsx +++ b/src/components/ResultList/SimpleResultList.tsx @@ -1,10 +1,14 @@ import { Flex, VisuallyHidden } from '@chakra-ui/react'; import { useIsClient } from '@/lib/useIsClient'; import PT from 'prop-types'; -import { HTMLAttributes, ReactElement } from 'react'; +import { HTMLAttributes, ReactElement, useMemo } from 'react'; import { Item } from './Item'; import { useHighlights } from './useHighlights'; import { IDocsEntity } from '@/api/search/types'; +import { useGetExportCitation } from '@/api/export/export'; +import { useSettings } from '@/lib/useSettings'; +import { ExportApiFormatKey } from '@/api/export/types'; +import { exportFormats } from '../CitationExporter'; export interface ISimpleResultListProps extends HTMLAttributes { docs: IDocsEntity[]; @@ -38,6 +42,33 @@ export const SimpleResultList = (props: ISimpleResultListProps): ReactElement => const { highlights, showHighlights, isFetchingHighlights } = useHighlights(); + const { settings } = useSettings(); + const { defaultExportFormat, customFormats } = settings; + + const bibcodes = docs.map((d) => d.bibcode).sort(); + + const { data: citationData } = useGetExportCitation( + { + // format: values(exportFormats).find((f) => f.label === defaultExportFormat).id, + format: ExportApiFormatKey.agu, + customFormat: defaultExportFormat === exportFormats.custom.label ? customFormats[0].code : undefined, + bibcode: bibcodes, + sort: ['bibcode asc'], + }, + { enabled: !!settings?.defaultExportFormat }, + ); + + // a map from bibcode to citation + const defaultCitations = useMemo(() => { + const citationSet = new Map(); + if (!!citationData) { + citationData.export.split('\n').forEach((c, index) => { + citationSet.set(bibcodes[index], c); + }); + } + return citationSet; + }, [citationData]); + return ( highlights={highlights?.[index] ?? []} isFetchingHighlights={allowHighlight && isFetchingHighlights} useNormCite={useNormCite} + defaultCitation={defaultCitations?.get(doc.bibcode)} /> ))}