-
-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathMatchLicenseExpression.tsx
More file actions
108 lines (99 loc) · 2.81 KB
/
Copy pathMatchLicenseExpression.tsx
File metadata and controls
108 lines (99 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import React, { useMemo } from "react";
import CoreLink from "../../CoreLink/CoreLink";
import {
LICENSE_EXPRESSIONS_CONJUNCTIONS,
parseTokensFromExpression,
} from "../../../utils/expressions";
import {
LicenseClueMatch,
LicenseDetectionMatch,
} from "../../../services/importedJsonTypes";
const DEBUG_URLS = false;
interface MatchLicenseExpressionRendererProps {
matchInfo:
| {
spdxLicense: false;
match: LicenseClueMatch;
}
| {
spdxLicense: true;
match: LicenseDetectionMatch;
};
}
interface ParsedTokens {
value: string;
href?: string;
}
const MatchLicenseExpressionRenderer = (
props: MatchLicenseExpressionRendererProps
) => {
const { matchInfo } = props;
const { license_expression, license_expression_keys } = matchInfo.match;
const parsedComponents = useMemo<ParsedTokens[]>(() => {
if (!license_expression) return [];
let newParsedComponents: ParsedTokens[];
if (matchInfo.spdxLicense) {
const licenseExpressionSpdxKeysMap = new Map(
// Handle deferred state update to ag data grid
(matchInfo.match.license_expression_spdx_keys || []).map((expKey) => [
expKey.key,
expKey,
])
);
newParsedComponents = parseTokensFromExpression(
matchInfo.match.spdx_license_expression
).map((token) => {
const tokenInfo = licenseExpressionSpdxKeysMap.get(token);
if (tokenInfo) {
return {
value: tokenInfo.key,
href: tokenInfo.spdx_url,
};
}
return { value: token };
});
} else {
const licenseExpressionKeysMap = new Map(
license_expression_keys.map((expKey) => [expKey.key, expKey])
);
newParsedComponents = parseTokensFromExpression(license_expression).map(
(token) => {
const tokenInfo = licenseExpressionKeysMap.get(token);
if (tokenInfo) {
return {
value: tokenInfo.key,
href: tokenInfo?.licensedb_url || tokenInfo?.scancode_url || "",
};
}
return { value: token };
}
);
}
return newParsedComponents;
}, [matchInfo]);
return (
<>
{parsedComponents.map(({ value, href }, idx) => {
if (href) {
return (
<CoreLink href={href} key={href + value}>
{value}
{DEBUG_URLS && `(${href})`}
</CoreLink>
);
}
return (
<React.Fragment key={value + idx}>
{value}
{DEBUG_URLS
? value.trim().length > 0 &&
!LICENSE_EXPRESSIONS_CONJUNCTIONS.includes(value) &&
"(NoURL)"
: ""}
</React.Fragment>
);
})}
</>
);
};
export default MatchLicenseExpressionRenderer;