on master help sign in
search: add dev.urbit.org searches
commit38e263fdfcb3a0f5ba0817a20f1bdc4afd67fb44 authorMatilde Park <matilde.park@urbit.org> dateTue 28 Jun 2022, 15:09 (4 years ago) parent0af782d
2 files changed+71−4
components/Search.js+63−4 pages/api/dev-search.js+8
components/Search.js+63−4
@@ -18,12 +18,17 @@ class Search extends Component {
1818 this.onSelect = this.onSelect.bind(this);
1919 this.glossarySearch = this.glossarySearch.bind(this);
2020 this.patpSearch = this.patpSearch.bind(this);
21+ this.devSearch = this.devSearch.bind(this);
2122 }
2223
2324 searchEndpoint(query) {
2425 return `/api/search?q=${query}`;
2526 }
2627
28+ devSearch(query) {
29+ return `/api/dev-search?q=${query}`;
30+ }
31+
2732 glossarySearch(query) {
2833 const entries = glossary.filter((entry) => {
2934 return (
@@ -54,12 +59,11 @@ class Search extends Component {
5459 this.props.closeSearch();
5560 }
5661
57− onInputValueChange = debounce((query) => {
62+ onInputValueChange = debounce(async (query) => {
5863 if (query.length) {
5964 fetch(this.searchEndpoint(query))
6065 .then((res) => res.json())
61− .then((res) => {
62− console.log(res);
66+ .then(async (res) => {
6367 // Wrap results in an object which will tell React what component to use to render results.
6468 const results = res.results.map((item) => ({
6569 type: "RESULT",
@@ -89,7 +93,21 @@ class Search extends Component {
8993 content: item,
9094 }));
9195
92− const list = [...glossaryResults, ...patpResult, ...results];
96+ const devResults = await fetch(this.devSearch(query))
97+ .then((res) => res.json())
98+ .then((res) => {
99+ return res.results.map((item) => ({
100+ type: "DEV_RESULT",
101+ content: item,
102+ }));
103+ });
104+
105+ const list = [
106+ ...glossaryResults,
107+ ...patpResult,
108+ ...results,
109+ ...devResults,
110+ ];
93111
94112 this.setState({ results: list });
95113 });
@@ -254,6 +272,47 @@ class Search extends Component {
254272 </li>
255273 );
256274 }
275+ if (item.type === "DEV_RESULT") {
276+ const devItem = Object.assign({}, item.content);
277+ devItem[
278+ "slug"
279+ ] = `https://developers.urbit.org${item.content.slug}`;
280+ return (
281+ <li
282+ className={`cursor-pointer flex text-left w-full ${
283+ selected ? "bg-green-400" : ""
284+ }`}
285+ {...getItemProps({
286+ key: item.content.link + "-" + index,
287+ index,
288+ item: devItem,
289+ selected,
290+ })}
291+ >
292+ <div className="p-3">
293+ <p
294+ className={`font-medium text-base ${
295+ selected ? "text-white" : "text-wall-600"
296+ }`}
297+ >
298+ <span className="text-wall-400">
299+ {item.content.parent !== "Content"
300+ ? `developers.urbit.org / ${item.content.parent} /`
301+ : "developers.urbit.org /"}{" "}
302+ </span>
303+ {item.content.title}
304+ </p>
305+ <p
306+ className={`text-base font-regular text-small ${
307+ selected ? "text-midWhite" : "text-wall-500"
308+ }`}
309+ >
310+ {item.content.content}
311+ </p>
312+ </div>
313+ </li>
314+ );
315+ }
257316 return null;
258317 })
259318 : null}
pages/api/dev-search.jsnew file+8
@@ -0,0 +1,8 @@
1+export default async (req, res) => {
2+ const results = await fetch(
3+ `https://developers.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+};