2019-08-25 19:34:27 +07:00
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
|
|
|
|
import { callDetailPage, callReadme } from '../../utils/calls';
|
|
|
|
import { buildScopePackage } from '../../utils/package';
|
|
|
|
import Loading from '../../components/Loading/Loading';
|
|
|
|
import NotFound from '../../components/NotFound';
|
|
|
|
|
|
|
|
import { Layout } from './Layout';
|
|
|
|
import { DetailContextProvider } from './context';
|
|
|
|
import { StateInterface } from './types';
|
|
|
|
|
|
|
|
export function getRouterPackageName(params): string {
|
|
|
|
const packageName = params.package;
|
|
|
|
const { scope } = params;
|
|
|
|
if (scope) {
|
|
|
|
return buildScopePackage(scope, packageName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return packageName;
|
|
|
|
}
|
|
|
|
|
2019-09-01 18:09:23 +07:00
|
|
|
function fillTitle(text) {
|
|
|
|
return `Verdaccio - ${text}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isVersionValid(packageMeta, packageVersion): boolean {
|
|
|
|
const hasVersion = typeof packageVersion !== 'undefined';
|
|
|
|
if (!hasVersion) {
|
|
|
|
// if is undefined, that means versions does not exist, we continue
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const hasMatchVersion = Object.keys(packageMeta.versions).includes(packageVersion);
|
|
|
|
return hasMatchVersion;
|
|
|
|
}
|
|
|
|
|
2019-08-25 19:34:27 +07:00
|
|
|
const Version = ({ match: { params } }) => {
|
|
|
|
const pkgName = getRouterPackageName(params);
|
|
|
|
const [readMe, setReadme] = useState();
|
|
|
|
const [packageName, setPackageName] = useState(pkgName);
|
2019-09-01 18:09:23 +07:00
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
const [packageVersion, setPackageVersion] = useState(params.version);
|
2019-08-25 19:34:27 +07:00
|
|
|
const [packageMeta, setPackageMeta] = useState();
|
|
|
|
const [isLoading, setIsLoading] = useState(true);
|
|
|
|
const [notFound, setNotFound] = useState(false);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
(async () => {
|
|
|
|
try {
|
2019-09-01 18:09:23 +07:00
|
|
|
const packageMeta = (await callDetailPage(packageName, packageVersion)) as Partial<StateInterface>;
|
|
|
|
const readMe = (await callReadme(packageName, packageVersion)) as Partial<StateInterface>;
|
|
|
|
if (isVersionValid(packageMeta, packageVersion)) {
|
|
|
|
setReadme(readMe);
|
|
|
|
setPackageMeta(packageMeta);
|
|
|
|
setIsLoading(false);
|
|
|
|
} else {
|
|
|
|
setIsLoading(false);
|
|
|
|
setNotFound(true);
|
|
|
|
}
|
2019-08-25 19:34:27 +07:00
|
|
|
} catch (error) {
|
|
|
|
setNotFound(true);
|
|
|
|
setIsLoading(false);
|
|
|
|
}
|
|
|
|
})();
|
2019-09-01 18:09:23 +07:00
|
|
|
}, [packageName, packageVersion]);
|
2019-08-25 19:34:27 +07:00
|
|
|
|
|
|
|
useEffect(() => {
|
2019-09-01 18:09:23 +07:00
|
|
|
if (!packageVersion) {
|
|
|
|
document.title = fillTitle(packageName);
|
|
|
|
} else {
|
|
|
|
document.title = fillTitle(`${packageName}@${packageVersion}`);
|
|
|
|
}
|
|
|
|
}, [packageName, packageVersion]);
|
2019-08-25 19:34:27 +07:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const pkgName = getRouterPackageName(params);
|
|
|
|
|
|
|
|
setPackageName(pkgName);
|
2019-09-01 18:09:23 +07:00
|
|
|
setPackageVersion(params.version);
|
|
|
|
}, [params, setPackageName, setPackageVersion]);
|
2019-08-25 19:34:27 +07:00
|
|
|
|
|
|
|
const isNotFound = notFound || typeof packageMeta === 'undefined' || typeof packageName === 'undefined' || typeof readMe === 'undefined';
|
|
|
|
const renderContent = (): React.ReactElement<HTMLElement> => {
|
|
|
|
if (isLoading) {
|
|
|
|
return <Loading />;
|
|
|
|
} else if (isNotFound) {
|
|
|
|
return <NotFound />;
|
|
|
|
} else {
|
|
|
|
return <Layout />;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-01 18:09:23 +07:00
|
|
|
return (
|
|
|
|
<DetailContextProvider value={{ packageMeta, packageVersion, readMe, packageName, enableLoading: setIsLoading }}>{renderContent()}</DetailContextProvider>
|
|
|
|
);
|
2019-08-25 19:34:27 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
export default Version;
|