Skip to content

Commit b978c59

Browse files
authored
Merge pull request #572 from shinyichen/citation-bulk-fetch
Citation bulk fetch
2 parents e4d1e78 + 74fe5fb commit b978c59

File tree

3 files changed

+46
-22
lines changed

3 files changed

+46
-22
lines changed

src/components/ResultList/Item/Item.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export interface IItemProps {
5050
highlights?: string[];
5151
extraInfo?: string;
5252
linkNewTab?: boolean;
53+
defaultCitation: string;
5354
}
5455

5556
export const Item = (props: IItemProps): ReactElement => {
@@ -64,6 +65,7 @@ export const Item = (props: IItemProps): ReactElement => {
6465
highlights,
6566
extraInfo,
6667
linkNewTab = false,
68+
defaultCitation = '',
6769
} = props;
6870
const { bibcode, pubdate, title = ['Untitled'], author = [], author_count, pub } = doc;
6971
const formattedPubDate = getFormattedNumericPubdate(pubdate);
@@ -118,7 +120,7 @@ export const Item = (props: IItemProps): ReactElement => {
118120
<Text as={MathJax} dangerouslySetInnerHTML={{ __html: unwrapStringValue(title) }} />
119121
</SimpleLink>
120122
<Flex alignItems="start" ml={1}>
121-
{!isClient || hideActions ? null : <ItemResourceDropdowns doc={doc} />}
123+
{!isClient || hideActions ? null : <ItemResourceDropdowns doc={doc} defaultCitation={defaultCitation} />}
122124
</Flex>
123125
</Flex>
124126
<Flex direction="column">

src/components/ResultList/Item/ItemResourceDropdowns.tsx

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,11 @@ import { MouseEventHandler, ReactElement, useEffect } from 'react';
99
import { SimpleLinkDropdown } from '@/components/Dropdown';
1010
import { isBrowser } from '@/utils/common/guards';
1111
import { IDocsEntity } from '@/api/search/types';
12-
import { useGetExportCitation } from '@/api/export/export';
13-
import { useSettings } from '@/lib/useSettings';
14-
import { exportFormats } from '@/components/CitationExporter/models';
15-
import { ExportApiFormatKey } from '@/api/export/types';
1612
import CopyToClipboard from 'react-copy-html-to-clipboard';
1713

1814
export interface IItemResourceDropdownsProps {
1915
doc: IDocsEntity;
16+
defaultCitation: string;
2017
}
2118

2219
export interface IItem {
@@ -25,22 +22,10 @@ export interface IItem {
2522
path?: string;
2623
}
2724

28-
export const ItemResourceDropdowns = ({ doc }: IItemResourceDropdownsProps): ReactElement => {
25+
export const ItemResourceDropdowns = ({ doc, defaultCitation }: IItemResourceDropdownsProps): ReactElement => {
2926
const router = useRouter();
3027
const isClient = useIsClient();
3128
const toast = useToast({ duration: 2000 });
32-
const { settings } = useSettings();
33-
const { defaultExportFormat, customFormats } = settings;
34-
35-
const { data: citationData } = useGetExportCitation(
36-
{
37-
// format: values(exportFormats).find((f) => f.label === defaultExportFormat).id,
38-
format: ExportApiFormatKey.agu,
39-
customFormat: defaultExportFormat === exportFormats.custom.label ? customFormats[0].code : undefined,
40-
bibcode: [doc.bibcode],
41-
},
42-
{ enabled: !!settings?.defaultExportFormat },
43-
);
4429

4530
const { hasCopied, onCopy, setValue, value } = useClipboard('');
4631

@@ -172,10 +157,10 @@ export const ItemResourceDropdowns = ({ doc }: IItemResourceDropdownsProps): Rea
172157
};
173158

174159
const handleCitationCopied = () => {
175-
if (citationData?.export) {
160+
if (defaultCitation !== '') {
176161
toast({ status: 'info', title: 'Copied to Clipboard' });
177162
} else {
178-
toast({ status: 'error', title: 'There was a problem fetching citation' });
163+
toast({ status: 'error', title: 'There was a problem fetching citation. Try reloading the page.' });
179164
}
180165
};
181166

@@ -300,7 +285,7 @@ export const ItemResourceDropdowns = ({ doc }: IItemResourceDropdownsProps): Rea
300285
<MenuList>
301286
<MenuItem onClick={handleCopyAbstractUrl}>Copy URL</MenuItem>
302287

303-
<CopyToClipboard text={citationData?.export} onCopy={handleCitationCopied} options={{ asHtml: true }}>
288+
<CopyToClipboard text={defaultCitation} onCopy={handleCitationCopied} options={{ asHtml: true }}>
304289
<MenuItem>Copy Citation</MenuItem>
305290
</CopyToClipboard>
306291
</MenuList>

src/components/ResultList/SimpleResultList.tsx

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import { Flex, VisuallyHidden } from '@chakra-ui/react';
22
import { useIsClient } from '@/lib/useIsClient';
33
import PT from 'prop-types';
4-
import { HTMLAttributes, ReactElement } from 'react';
4+
import { HTMLAttributes, ReactElement, useMemo } from 'react';
55
import { Item } from './Item';
66
import { useHighlights } from './useHighlights';
77
import { IDocsEntity } from '@/api/search/types';
8+
import { useGetExportCitation } from '@/api/export/export';
9+
import { useSettings } from '@/lib/useSettings';
10+
import { ExportApiFormatKey } from '@/api/export/types';
11+
import { exportFormats } from '../CitationExporter';
12+
import { logger } from '@/logger';
813

914
export interface ISimpleResultListProps extends HTMLAttributes<HTMLDivElement> {
1015
docs: IDocsEntity[];
@@ -38,6 +43,37 @@ export const SimpleResultList = (props: ISimpleResultListProps): ReactElement =>
3843

3944
const { highlights, showHighlights, isFetchingHighlights } = useHighlights();
4045

46+
const { settings } = useSettings();
47+
const { defaultExportFormat, customFormats } = settings;
48+
49+
const bibcodes = docs.map((d) => d.bibcode).sort();
50+
51+
const { data: citationData } = useGetExportCitation(
52+
{
53+
// format: values(exportFormats).find((f) => f.label === defaultExportFormat).id,
54+
format: ExportApiFormatKey.agu,
55+
customFormat: defaultExportFormat === exportFormats.custom.label ? customFormats[0].code : undefined,
56+
bibcode: bibcodes,
57+
sort: ['bibcode asc'],
58+
},
59+
{ enabled: !!settings?.defaultExportFormat },
60+
);
61+
62+
// a map from bibcode to citation
63+
const defaultCitations = useMemo(() => {
64+
const citationSet = new Map<string, string>();
65+
try {
66+
if (!!citationData) {
67+
citationData.export.split('\n').forEach((c, index) => {
68+
citationSet.set(bibcodes[index], c);
69+
});
70+
}
71+
} catch (err) {
72+
logger.error({ err }, 'Error processing citation data');
73+
}
74+
return citationSet;
75+
}, [citationData, bibcodes]);
76+
4177
return (
4278
<Flex
4379
as="section"
@@ -61,6 +97,7 @@ export const SimpleResultList = (props: ISimpleResultListProps): ReactElement =>
6197
highlights={highlights?.[index] ?? []}
6298
isFetchingHighlights={allowHighlight && isFetchingHighlights}
6399
useNormCite={useNormCite}
100+
defaultCitation={defaultCitations?.get(doc.bibcode)}
64101
/>
65102
))}
66103
</Flex>

0 commit comments

Comments
 (0)