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
14 changes: 10 additions & 4 deletions src/components/FeedbackForms/MissingRecord/UrlsField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { SelectInstance } from 'react-select';
import { FormValues } from './types';
import { IResourceUrl, ResourceUrlType, resourceUrlTypes } from '@/lib/useGetResourceLinks';
import { useIsClient } from '@/lib/useIsClient';
import { EXTERNAL_URLS } from '@/config';

export const UrlsField = () => {
return (
Expand All @@ -24,6 +25,8 @@ const typeOptions: SelectOption<ResourceUrlType>[] = resourceUrlTypes.map((t) =>
value: t as string,
}));

const { DOI_ORIGIN, ARXIV_ORIGIN } = EXTERNAL_URLS;

export const UrlsTable = ({ editable }: { editable: boolean }) => {
const isClient = useIsClient();

Expand All @@ -37,7 +40,7 @@ export const UrlsTable = ({ editable }: { editable: boolean }) => {
});

// New row being added
const [newUrl, setNewUrl] = useState<IResourceUrl>({ type: 'arXiv', url: '' });
const [newUrl, setNewUrl] = useState<IResourceUrl>({ type: 'arXiv', url: `${ARXIV_ORIGIN}/` });

// Existing row being edited
const [editUrl, setEditUrl] = useState<{ index: number; url: IResourceUrl }>({
Expand Down Expand Up @@ -67,7 +70,9 @@ export const UrlsTable = ({ editable }: { editable: boolean }) => {

try {
const testUrl = new URL(url);
return VALID_PROTOCOLS.includes(testUrl.protocol);
return testUrl.origin === DOI_ORIGIN || testUrl.origin === ARXIV_ORIGIN
? testUrl.pathname.length > 1
: VALID_PROTOCOLS.includes(testUrl.protocol);
} catch {
return false;
}
Expand All @@ -80,7 +85,8 @@ export const UrlsTable = ({ editable }: { editable: boolean }) => {
// Changes to fields for adding new url

const handleNewTypeChange = (option: SelectOption<ResourceUrlType>) => {
setNewUrl((prev) => ({ ...prev, type: option.id }));
const origin = option.id === 'DOI' ? `${DOI_ORIGIN}/` : option.id === 'arXiv' ? `${ARXIV_ORIGIN}/` : '';
setNewUrl({ url: origin, type: option.id });
};

const handleNewUrlChange = (e: ChangeEvent<HTMLInputElement>) => {
Expand All @@ -90,7 +96,7 @@ export const UrlsTable = ({ editable }: { editable: boolean }) => {
const handleAddUrl = () => {
append(newUrl);
// clear input fields
setNewUrl({ type: 'arXiv', url: '' });
setNewUrl({ type: 'arXiv', url: `${ARXIV_ORIGIN}/` });
(newURLTypeInputRef.current as SelectInstance).focus();
};

Expand Down
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ export const EXTERNAL_URLS = {
CFA_SAO_HOME_PAGE: 'https://www.cfa.harvard.edu/sao' as const,
UAT: 'http://astrothesaurus.org',
ORCID: 'https://orcid.org',
DOI_ORIGIN: 'https://doi.org',
ARXIV_ORIGIN: 'https://arxiv.org',
};

export const TRACING_HEADERS = ['X-Original-Uri', 'X-Original-Forwarded-For', 'X-Forwarded-For', 'X-Amzn-Trace-Id'];
Expand Down
Loading