This repository has been archived by the owner on Sep 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Results.js
126 lines (117 loc) · 3.82 KB
/
Results.js
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import React from "react";
import FilterLink from "./FilterLink";
function LineItem({ label, value, children }) {
if (!value) return null;
if (Array.isArray(value) && !value.length) return null;
return (
<li>
<strong>{label}</strong>: {children(value)}
</li>
);
}
function Lisence({ value, children }) {
if (!value) return null;
if (Array.isArray(value) && !value.length) return null;
return <div className="result__lisence">{value}</div>;
}
function Description({ value, children }) {
if (!value) return null;
if (Array.isArray(value) && !value.length) return null;
return (
<p
className="result__description"
dangerouslySetInnerHTML={createMarkup(value)}
/>
);
}
function createMarkup(html) {
return { __html: html };
}
function getRaw(result, field) {
return (result.data[field] || {}).raw;
}
function getSnippet(result, field) {
return (result.data[field] || {}).snippet;
}
export default function Results({ results, queryState, trackClick }) {
return (
<ul>
{results.map(result => (
<li className="result" key={getRaw(result, "name")}>
<div className="result__header">
<a
className="result__title"
href={`https://www.npmjs.com/package/${getRaw(result, "name")}`}
target="_blank"
rel="noopener noreferrer"
onClick={e => trackClick(getRaw(result, "id"))}
dangerouslySetInnerHTML={createMarkup(getSnippet(result, "name"))}
/>
<Lisence value={getRaw(result, "license")} />
</div>
<div className="result__body">
<Description value={getSnippet(result, "description")} />
<ul className="result__details">
<LineItem label="Version" value={getRaw(result, "version")}>
{value => <span className="result__version">{value}</span>}
</LineItem>
<LineItem label="Home Page" value={getRaw(result, "homepage")}>
{value => (
<a
target="_blank subtle-link"
href={getRaw(result, "homepage")}
>
{value}
</a>
)}
</LineItem>
<LineItem
label="Dependencies"
value={getRaw(result, "dependencies")}
>
{values =>
values.map(value => (
<span key={value}>
<FilterLink
name="dependencies"
value={value}
queryState={queryState}
>
{value}
</FilterLink>
</span>
))
}
</LineItem>
<LineItem label="License" value={getRaw(result, "license")}>
{value => value.join(", ")}
</LineItem>
<LineItem label="Keywords" value={getRaw(result, "keywords")}>
{values =>
values.map((value, index) => (
<span key={`${value}-${index}`}>
<FilterLink
name="keywords"
value={value}
queryState={queryState}
>
{value}
</FilterLink>
</span>
))
}
</LineItem>
</ul>
</div>
<div className="result__footer">
<div className="result__owner">
<LineItem label="Owners" value={getRaw(result, "owners")}>
{value => value.join(", ")}
</LineItem>
</div>
</div>
</li>
))}
</ul>
);
}