|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { StyledSyntaxHighlighter } from "@/markdown/styledSyntaxHighlighter"; |
| 4 | +import { langConstants } from "@my-code/runtime/languages"; |
| 5 | +import { useState } from "react"; |
| 6 | + |
| 7 | +export interface LicenseEntry { |
| 8 | + name: string; |
| 9 | + version: string; |
| 10 | + author?: string; |
| 11 | + repository?: string; |
| 12 | + source?: string; |
| 13 | + license: string; |
| 14 | + licenseText?: string; |
| 15 | +} |
| 16 | + |
| 17 | +export function ThirdPartyLicenses({ licenses }: { licenses: LicenseEntry[] }) { |
| 18 | + const [expanded, setExpanded] = useState<string | null>(null); |
| 19 | + |
| 20 | + return ( |
| 21 | + <div className="flex flex-col gap-2"> |
| 22 | + {licenses.map((pkg) => { |
| 23 | + const key = `${pkg.name}@${pkg.version}`; |
| 24 | + const isOpen = expanded === key; |
| 25 | + let repositoryURL: string | undefined = undefined; |
| 26 | + if (pkg.repository?.startsWith("http")) { |
| 27 | + repositoryURL = pkg.repository; |
| 28 | + } else if (pkg.repository?.startsWith("git+http")) { |
| 29 | + repositoryURL = pkg.repository?.slice(4); |
| 30 | + } else if (pkg.repository?.startsWith("git@")) { |
| 31 | + repositoryURL = |
| 32 | + "https://" + |
| 33 | + pkg.repository |
| 34 | + .slice(4) |
| 35 | + .replace(":", "/") |
| 36 | + .replace(/\.git$/, ""); |
| 37 | + } else if ( |
| 38 | + pkg.repository && |
| 39 | + /github:[\w-]+\/[\w-]+/.test(pkg.repository) |
| 40 | + ) { |
| 41 | + repositoryURL = `https://github.com/${pkg.repository.slice(7)}`; |
| 42 | + } else if (pkg.repository && /[\w-]+\/[\w-]+/.test(pkg.repository)) { |
| 43 | + // assume github username/repository |
| 44 | + repositoryURL = `https://github.com/${pkg.repository}`; |
| 45 | + } else { |
| 46 | + // fallback to source url |
| 47 | + // repositoryURL = pkg.source ?? ""; |
| 48 | + } |
| 49 | + return ( |
| 50 | + <div key={key} className="collapse collapse-arrow bg-base-200"> |
| 51 | + <input |
| 52 | + type="checkbox" |
| 53 | + checked={isOpen} |
| 54 | + onChange={() => setExpanded(isOpen ? null : key)} |
| 55 | + /> |
| 56 | + <div className="collapse-title font-mono"> |
| 57 | + <span className="font-bold">{pkg.name}</span> |
| 58 | + <span className="opacity-60 ml-2">v{pkg.version}</span> |
| 59 | + <span className="badge badge-primary badge-soft badge-sm ml-3"> |
| 60 | + {pkg.license} |
| 61 | + </span> |
| 62 | + </div> |
| 63 | + <div className="collapse-content"> |
| 64 | + {pkg.author && ( |
| 65 | + <p className="mb-1"> |
| 66 | + <span className="opacity-60">Author: </span> |
| 67 | + {pkg.author} |
| 68 | + </p> |
| 69 | + )} |
| 70 | + {repositoryURL && ( |
| 71 | + <p className="mb-1"> |
| 72 | + <span className="opacity-60">Repository: </span> |
| 73 | + <a |
| 74 | + className="link link-info break-all" |
| 75 | + href={repositoryURL} |
| 76 | + target="_blank" |
| 77 | + > |
| 78 | + {repositoryURL} |
| 79 | + </a> |
| 80 | + </p> |
| 81 | + )} |
| 82 | + {pkg.licenseText && ( |
| 83 | + <StyledSyntaxHighlighter |
| 84 | + className="text-sm" |
| 85 | + language={langConstants(undefined)} |
| 86 | + > |
| 87 | + {pkg.licenseText} |
| 88 | + </StyledSyntaxHighlighter> |
| 89 | + )} |
| 90 | + </div> |
| 91 | + </div> |
| 92 | + ); |
| 93 | + })} |
| 94 | + </div> |
| 95 | + ); |
| 96 | +} |
0 commit comments