Skip to content

Commit

Permalink
fetch citation in bulk
Browse files Browse the repository at this point in the history
  • Loading branch information
shinyichen committed Dec 11, 2024
1 parent e4d1e78 commit 06dcdc9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
4 changes: 3 additions & 1 deletion src/components/ResultList/Item/Item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export interface IItemProps {
highlights?: string[];
extraInfo?: string;
linkNewTab?: boolean;
defaultCitation: string;
}

export const Item = (props: IItemProps): ReactElement => {
Expand All @@ -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);
Expand Down Expand Up @@ -118,7 +120,7 @@ export const Item = (props: IItemProps): ReactElement => {
<Text as={MathJax} dangerouslySetInnerHTML={{ __html: unwrapStringValue(title) }} />
</SimpleLink>
<Flex alignItems="start" ml={1}>
{!isClient || hideActions ? null : <ItemResourceDropdowns doc={doc} />}
{!isClient || hideActions ? null : <ItemResourceDropdowns doc={doc} defaultCitation={defaultCitation} />}
</Flex>
</Flex>
<Flex direction="column">
Expand Down
25 changes: 5 additions & 20 deletions src/components/ResultList/Item/ItemResourceDropdowns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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('');

Expand Down Expand Up @@ -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.' });
}
};

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

<CopyToClipboard text={citationData?.export} onCopy={handleCitationCopied} options={{ asHtml: true }}>
<CopyToClipboard text={defaultCitation} onCopy={handleCitationCopied} options={{ asHtml: true }}>
<MenuItem>Copy Citation</MenuItem>
</CopyToClipboard>
</MenuList>
Expand Down
34 changes: 33 additions & 1 deletion src/components/ResultList/SimpleResultList.tsx
Original file line number Diff line number Diff line change
@@ -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<HTMLDivElement> {
docs: IDocsEntity[];
Expand Down Expand Up @@ -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<string, string>();
if (!!citationData) {
citationData.export.split('\n').forEach((c, index) => {
citationSet.set(bibcodes[index], c);
});
}
return citationSet;
}, [citationData]);

return (
<Flex
as="section"
Expand All @@ -61,6 +92,7 @@ export const SimpleResultList = (props: ISimpleResultListProps): ReactElement =>
highlights={highlights?.[index] ?? []}
isFetchingHighlights={allowHighlight && isFetchingHighlights}
useNormCite={useNormCite}
defaultCitation={defaultCitations?.get(doc.bibcode)}
/>
))}
</Flex>
Expand Down

0 comments on commit 06dcdc9

Please sign in to comment.