on master help sign in
search: add operators.urbit.org searches
50ab536Matilde Park
components/Search.js+56
@@ -18,6 +18,7 @@ class Search extends Component {
18 this.glossarySearch = this.glossarySearch.bind(this);
18 this.glossarySearch = this.glossarySearch.bind(this);
19 this.patpSearch = this.patpSearch.bind(this);
19 this.patpSearch = this.patpSearch.bind(this);
20 this.devSearch = this.devSearch.bind(this);
20 this.devSearch = this.devSearch.bind(this);
21+ this.opsSearch = this.opsSearch.bind(this);
21 }
22 }
22
23
23 searchEndpoint(query) {
24 searchEndpoint(query) {
@@ -32,6 +33,10 @@ class Search extends Component {
32 return `/api/glossary?q=${encodeURIComponent(query)}`;
33 return `/api/glossary?q=${encodeURIComponent(query)}`;
33 }
34 }
34
35
36+ opsSearch(query) {
37+ return `/api/ops-search?q=${query}`;
38+ }
39+
35 patpSearch(query) {
40 patpSearch(query) {
36 return (
41 return (
37 (ob.isValidPatp(`~${deSig(query.toLowerCase())}`) &&
42 (ob.isValidPatp(`~${deSig(query.toLowerCase())}`) &&
@@ -100,11 +105,21 @@ class Search extends Component {
100 }));
105 }));
101 });
106 });
102
107
108+ const opsResults = await fetch(this.opsSearch(query))
109+ .then((res) => res.json())
110+ .then((res) => {
111+ return res.results.map((item) => ({
112+ type: "OPS_RESULT",
113+ content: item,
114+ }));
115+ });
116+
103 const list = [
117 const list = [
104 ...glossaryResults,
118 ...glossaryResults,
105 ...patpResult,
119 ...patpResult,
106 ...results,
120 ...results,
107 ...devResults,
121 ...devResults,
122+ ...opsResults,
108 ];
123 ];
109
124
110 this.setState({ results: list });
125 this.setState({ results: list });
@@ -311,6 +326,47 @@ class Search extends Component {
311 </li>
326 </li>
312 );
327 );
313 }
328 }
329+ if (item.type === "OPS_RESULT") {
330+ const opsItem = Object.assign({}, item.content);
331+ opsItem[
332+ "slug"
333+ ] = `https://operators.urbit.org${item.content.slug}`;
334+ return (
335+ <li
336+ className={`cursor-pointer flex text-left w-full ${
337+ selected ? "bg-green-400" : ""
338+ }`}
339+ {...getItemProps({
340+ key: item.content.link + "-" + index,
341+ index,
342+ item: opsItem,
343+ selected,
344+ })}
345+ >
346+ <div className="p-3">
347+ <p
348+ className={`font-medium text-base ${
349+ selected ? "text-white" : "text-wall-600"
350+ }`}
351+ >
352+ <span className="text-wall-400">
353+ {item.content.parent !== "Content"
354+ ? `operators.urbit.org / ${item.content.parent} /`
355+ : "operators.urbit.org /"}{" "}
356+ </span>
357+ {item.content.title}
358+ </p>
359+ <p
360+ className={`text-base font-regular text-small ${
361+ selected ? "text-midWhite" : "text-wall-500"
362+ }`}
363+ >
364+ {item.content.content}
365+ </p>
366+ </div>
367+ </li>
368+ );
369+ }
314 return null;
370 return null;
315 })
371 })
316 : null}
372 : null}
pages/api/ops-search.jsnew file+8
@@ -0,0 +1,8 @@
1+export default async (req, res) => {
2+ const results = await fetch(
3+ `https://operators.urbit.org/api/search?q=${req.query.q}`
4+ ).then((res) => res.json());
5+ res.statusCode = 200;
6+ res.setHeader("Content-Type", "application/json");
7+ res.end(JSON.stringify(results));
8+};