From 06dcdc940b6b3b6646893859866f0631bd0f2654 Mon Sep 17 00:00:00 2001 From: Jennifer Chen Date: Wed, 11 Dec 2024 11:54:45 -0800 Subject: [PATCH 1/3] fetch citation in bulk --- src/components/ResultList/Item/Item.tsx | 4 ++- .../ResultList/Item/ItemResourceDropdowns.tsx | 25 +++----------- .../ResultList/SimpleResultList.tsx | 34 ++++++++++++++++++- 3 files changed, 41 insertions(+), 22 deletions(-) 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)} /> ))} From 51656443c35d72d86049d957687634b6e670e479 Mon Sep 17 00:00:00 2001 From: Jennifer Chen Date: Fri, 13 Dec 2024 10:43:55 -0800 Subject: [PATCH 2/3] error checking --- src/components/ResultList/SimpleResultList.tsx | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/components/ResultList/SimpleResultList.tsx b/src/components/ResultList/SimpleResultList.tsx index ba14be88e..c75a666f8 100644 --- a/src/components/ResultList/SimpleResultList.tsx +++ b/src/components/ResultList/SimpleResultList.tsx @@ -61,10 +61,14 @@ export const SimpleResultList = (props: ISimpleResultListProps): ReactElement => // 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); - }); + try { + if (!!citationData) { + citationData.export.split('\n').forEach((c, index) => { + citationSet.set(bibcodes[index], c); + }); + } + } catch { + defaultCitations.clear(); } return citationSet; }, [citationData]); From 74fe5fb7d1ae0c02131f2b93eded909772f926db Mon Sep 17 00:00:00 2001 From: Jennifer Chen Date: Fri, 13 Dec 2024 11:35:08 -0800 Subject: [PATCH 3/3] error log --- src/components/ResultList/SimpleResultList.tsx | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/components/ResultList/SimpleResultList.tsx b/src/components/ResultList/SimpleResultList.tsx index c75a666f8..951d2f253 100644 --- a/src/components/ResultList/SimpleResultList.tsx +++ b/src/components/ResultList/SimpleResultList.tsx @@ -9,6 +9,7 @@ import { useGetExportCitation } from '@/api/export/export'; import { useSettings } from '@/lib/useSettings'; import { ExportApiFormatKey } from '@/api/export/types'; import { exportFormats } from '../CitationExporter'; +import { logger } from '@/logger'; export interface ISimpleResultListProps extends HTMLAttributes { docs: IDocsEntity[]; @@ -67,11 +68,11 @@ export const SimpleResultList = (props: ISimpleResultListProps): ReactElement => citationSet.set(bibcodes[index], c); }); } - } catch { - defaultCitations.clear(); + } catch (err) { + logger.error({ err }, 'Error processing citation data'); } return citationSet; - }, [citationData]); + }, [citationData, bibcodes]); return (