Skip to content
Open
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
6 changes: 4 additions & 2 deletions src/api/resolver/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ export interface IADSApiResolverParams {
}

export interface IADSApiResolverResponse {
action: string;
links: {
action: string; // display, redirect
link?: string;
link_type?: string; // TODO: https://ui.adsabs.harvard.edu/help/api/api-docs.html#tag--resolver
links?: {
count: number;
link_type: string;
records: [
Expand Down
24 changes: 16 additions & 8 deletions src/components/FeedbackForms/MissingRecord/RecordPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,22 @@ import { getDiffSections, getDiffString, processFormValues } from './DiffUtil';
import { KeywordsField } from './KeywordsField';
import { PubDateField } from './PubDateField';
import { ReferencesField } from './ReferencesField';
import { DiffSection, FormValues, IAuthor, IKeyword, IReference } from './types';
import { DiffSection, FormValues, IAuthor, IKeyword, IReference, IResourceUrl } from './types';
import { UrlsField } from './UrlsField';
import { DiffSectionPanel } from './DiffSectionPanel';
import { AxiosError } from 'axios';
import { z } from 'zod';
import { zodResolver } from '@hookform/resolvers/zod';
import { SimpleLink } from '@/components/SimpleLink';
import { PreviewModal } from '@/components/FeedbackForms';
import { IResourceUrl, useGetResourceLinks } from '@/lib/useGetResourceLinks';
import { useGetUserEmail } from '@/lib/useGetUserEmail';
import { parsePublicationDate } from '@/utils/common/parsePublicationDate';
import type { Database, IDocsEntity } from '@/api/search/types';
import type { IFeedbackParams } from '@/api/feedback/types';
import { useGetSingleRecord } from '@/api/search/search';
import { useResolverQuery } from '@/api/resolver/resolver';
import { IADSApiResolverResponse } from '@/api/resolver/types';
import { transformUrl } from './UrlUtil';

const collections: { value: Database; label: string }[] = [
{ value: 'astronomy', label: 'Astronomy and Astrophysics' },
Expand Down Expand Up @@ -182,10 +184,7 @@ export const RecordPanel = ({
isSuccess: urlsIsSuccess,
isFetching: urlsIsFetching,
refetch: urlsRefetch,
} = useGetResourceLinks({
identifier: getValues('bibcode'),
options: { enabled: false },
});
} = useResolverQuery({ bibcode: getValues('bibcode'), link_type: 'ESOURCE' }, { enabled: false });

// when this tab is focused, set focus on name field
useEffect(() => {
Expand Down Expand Up @@ -358,9 +357,18 @@ export const RecordPanel = ({
};

// when url data is fetch, set then in form values
const handleUrlsLoaded = (urlsData: IResourceUrl[]) => {
const handleUrlsLoaded = (urlsData: IADSApiResolverResponse) => {
const urls =
urlsData.action === 'display' && urlsData.links?.records
? urlsData.links.records.map((r) => decodeURIComponent(r.url))
: urlsData.action === 'redirect' && urlsData.link
? [decodeURIComponent(urlsData.link)]
: [];

// tranform urls to IResourceUrl, and remove any nulls (invalid urls)
const transformedUrls = urls.map((url) => transformUrl(url)).filter((tu) => tu !== null);
if (!isNew) {
setRecordOriginalFormValues((prev) => ({ ...prev, urls: urlsData }));
setRecordOriginalFormValues((prev) => ({ ...prev, urls: transformedUrls }));
}
};

Expand Down
34 changes: 34 additions & 0 deletions src/components/FeedbackForms/MissingRecord/UrlUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { ResourceUrlType, IResourceUrl } from './types';

const URL_TYPE_MAP: Record<string, ResourceUrlType> = {
arxiv: 'arXiv',
pdf: 'PDF',
doi: 'DOI',
html: 'HTML',
};

const RESOURCE_EXT_REGEX = /\.(jpg|jpeg|png|gif|webp|svg|css|js|ico|woff2?|ttf|otf|eot|map|mp4|webm)(\?|$)/i;

const isValidUrl = (url: string) => {
try {
const tempUrl = new URL(url);
return ['http:', 'https:'].includes(tempUrl.protocol);
} catch {
return false;
}
};

/**
* Transforms a URL into a structured resource link object.
* @param url
*/
export const transformUrl = (url: string) => {
if (!url || typeof url !== 'string' || !isValidUrl(url) || RESOURCE_EXT_REGEX.test(url)) {
return null;
}

const normalizedUrl = url.toLowerCase().replace(/\/$/, '');
const urlType = Object.keys(URL_TYPE_MAP).find((key) => normalizedUrl.includes(key));
const type = urlType ? URL_TYPE_MAP[urlType] : 'HTML';
return { type, url: normalizedUrl } as IResourceUrl;
};
3 changes: 1 addition & 2 deletions src/components/FeedbackForms/MissingRecord/UrlsField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { Select, SelectOption } from '@/components/Select';
import { ChangeEvent, KeyboardEvent, MouseEvent, useRef, useState } from 'react';
import { useFieldArray } from 'react-hook-form';
import { SelectInstance } from 'react-select';
import { FormValues } from './types';
import { IResourceUrl, ResourceUrlType, resourceUrlTypes } from '@/lib/useGetResourceLinks';
import { FormValues, IResourceUrl, ResourceUrlType, resourceUrlTypes } from './types';
import { useIsClient } from '@/lib/useIsClient';

export const UrlsField = () => {
Expand Down
12 changes: 11 additions & 1 deletion src/components/FeedbackForms/MissingRecord/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface IAuthor {

export const referenceTypes = ['Raw Text', 'DOI', 'Bibcode'] as const;

export type ReferenceType = typeof referenceTypes[number];
export type ReferenceType = (typeof referenceTypes)[number];

export interface IReference {
type: ReferenceType;
Expand Down Expand Up @@ -46,3 +46,13 @@ export type DiffSection = {
type: 'array' | 'text';
newValue: string;
};

// URLs (ESOURCE) types and interfaces

export const resourceUrlTypes = ['arXiv', 'PDF', 'DOI', 'HTML', 'Other'] as const;

export type ResourceUrlType = (typeof resourceUrlTypes)[number];
export interface IResourceUrl {
type: ResourceUrlType;
url: string;
}
48 changes: 48 additions & 0 deletions src/components/__tests__/UrlUtil.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { beforeEach, describe, expect, test, vi } from 'vitest';
import { transformUrl } from '@/components/FeedbackForms/MissingRecord/UrlUtil';

describe('resourceLinks', () => {
beforeEach(() => {
vi.resetAllMocks();
global.fetch = vi.fn();
});

test('transformUrl filters known static/resource files', () => {
expect(transformUrl('https://example.com/image.jpg')).toBeNull();
expect(transformUrl('https://example.com/script.js')).toBeNull();
});

test('transformUrl assigns correct type', () => {
expect(transformUrl('https://arxiv.org/pdf/foo.pdf')).toEqual({
type: 'arXiv',
url: 'https://arxiv.org/pdf/foo.pdf',
});

expect(transformUrl('https://doi.org/10.1234')).toEqual({
type: 'DOI',
url: 'https://doi.org/10.1234',
});

expect(transformUrl('https://example.com/anything')).toEqual({
type: 'HTML',
url: 'https://example.com/anything',
});
});

test('transformUrl returns null for empty or invalid URLs', () => {
expect(transformUrl('')).toBeNull();
expect(transformUrl('invalid-url')).toBeNull();
expect(transformUrl('https://example.com/image.png')).toBeNull();
});

test('transformUrl normalizes URLs', () => {
expect(transformUrl('https://example.com/')).toEqual({
type: 'HTML',
url: 'https://example.com',
});
expect(transformUrl('https://example.com/page.HTML')).toEqual({
type: 'HTML',
url: 'https://example.com/page.html',
});
});
});
195 changes: 0 additions & 195 deletions src/lib/__tests__/useGetResourceLinks.test.ts

This file was deleted.

Loading
Loading