Skip to content

Commit

Permalink
make code modular
Browse files Browse the repository at this point in the history
  • Loading branch information
rajku-dev committed Feb 15, 2025
1 parent 0e14300 commit 0e40a64
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
20 changes: 2 additions & 18 deletions src/pages/Licenses/Licenses.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,14 @@ import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs";
import Loading from "@/components/Common/Loading";

import licenseUrls from "@/pages/Licenses/components/license-urls.json";
import { LicensesSbom, PackageType } from "@/types/license";
import { getPackageUrl } from "@/pages/Licenses/utils";
import { LicensesSbom } from "@/types/license";

const sbomUrlMap = {
frontend: "/sbom/care_fe-sbom.json",
backend: "/sbom/care-sbom.json",
};

function getPackageUrl(pkgName: string, purl: string): string {
if (!purl || !pkgName) return "";

const urlMap: Record<PackageType, string> = {
pypi: `https://pypi.org/project/${pkgName}`,
npm: `https://www.npmjs.com/package/${pkgName}`,
github: `https://github.com/${pkgName}`,
githubactions: `https://github.com/actions/${pkgName}`,
};

const pkgType = Object.keys(urlMap).find((key) =>
purl.startsWith(`pkg:${key}/`),
);

return pkgType ? urlMap[pkgType as PackageType] : purl;
}

export const LicensesPage = () => {
const { t } = useTranslation();
const [tab, setTab] = useState<"frontend" | "backend">("frontend");
Expand Down
18 changes: 18 additions & 0 deletions src/pages/Licenses/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { PackageType } from "@/types/license";

export const getPackageUrl = (pkgName: string, purl: string): string => {
if (!purl || !pkgName) return "";

const urlMap: Record<PackageType, string> = {
pypi: `https://pypi.org/project/${pkgName}`,
npm: `https://www.npmjs.com/package/${pkgName}`,
github: `https://github.com/${pkgName}`,
githubactions: `https://github.com/actions/${pkgName}`,
};

const pkgType = Object.keys(urlMap).find((key) =>
purl.startsWith(`pkg:${key}/`),
);

return pkgType ? urlMap[pkgType as PackageType] : purl;
};

0 comments on commit 0e40a64

Please sign in to comment.