1import { Component } from "react";
2import { withRouter } from "next/router";
3import debounce from "lodash.debounce";
4import Downshift from "downshift";
5import ob from "urbit-ob";
6import Sigil from "../components/Sigil";
8class Search extends Component {
14 this.searchEndpoint = this.searchEndpoint.bind(this);
15 this.onInputValueChange = this.onInputValueChange.bind(this);
16 this.onSelect = this.onSelect.bind(this);
17 this.glossarySearch = this.glossarySearch.bind(this);
18 this.patpSearch = this.patpSearch.bind(this);
19 this.devSearch = this.devSearch.bind(this);
20 this.opsSearch = this.opsSearch.bind(this);
23 searchEndpoint(query) {
24 return `/api/search?q=${query}`;
28 return `/api/dev-search?q=${query}`;
31 glossarySearch(query) {
32 return `/api/glossary?q=${encodeURIComponent(query)}`;
36 return `/api/ops-search?q=${query}`;
41 (ob.isValidPatp(`~${deSig(query.toLowerCase())}`) &&
42 `~${deSig(query.toLowerCase())}`.length < 15) ||
43 (!isNaN(query) && query <= 4294967295)
49 this.props.router.push(item.slug);
57 this.props.closeSearch();
60 onInputValueChange = debounce(async (query) => {
62 const search = fetch(this.searchEndpoint(query))
63 .then((res) => res.json())
64 .then(async (res) => {
65 return res.results.map((item) => ({
71 const patp = this.patpSearch(query)
74 : ob.patp(ob.patp2dec(`~${deSig(query)}`))
77 const patpResult = this.patpSearch(query)
89 const glossarySearch = fetch(this.glossarySearch(query))
90 .then((res) => res.json())
92 return res.results.map((item) => ({
93 type: "GLOSSARY_RESULT",
98 const devSearch = fetch(this.devSearch(query))
99 .then((res) => res.json())
101 return res.results.map((item) => ({
107 const opsSearch = fetch(this.opsSearch(query))
108 .then((res) => res.json())
110 return res.results.map((item) => ({
116 const [results, glossaryResults, devResults, opsResults] =
117 await Promise.all([search, glossarySearch, devSearch, opsSearch]);
127 this.setState({ results: list });
129 this.setState({ results: [] });
134 const { state, props } = this;
136 if (props.showSearch) {
139 onSelect={(selection) => this.onSelect(selection)}
140 onInputValueChange={(event) => this.onInputValueChange(event)}
141 itemToString={(item) => (item ? item.slug : "")}
142 defaultHighlightedIndex={0}
155 <div className="fixed w-screen h-screen z-50 flex flex-col items-center p-4">
157 onClick={(event) => props.closeSearch(event)}
158 className="top-0 left-0 fixed w-screen h-screen bg-washedWall"
160 <div className="relative flex flex-col max-w-screen-lg md:my-32 w-full md:w-10/12 lg:w-8/12 xl:w-6/12 rounded-xl bg-white min-h-0 overflow-hidden">
162 style={{ display: "inline-block" }}
163 {...getRootProps({}, { suppressRefError: true })}
167 className="text-lg md:text-xl lg:text-2xl font-medium text-green-400 bg-transparent py-2 px-4 outline-none relative w-full"
168 placeholder="Search..."
170 onClick={(e) => e.stopPropagation()}
172 onKeyDown: (event) => {
173 if (event.key === "Escape") {
174 // Prevent Downshift's default 'Escape' behavior.
175 event.nativeEvent.preventDownshiftDefault = true;
176 this.props.closeSearch(event);
182 <ul {...getMenuProps()} className="overflow-y-scroll">
184 ? state.results.map((item, index) => {
185 const selected = highlightedIndex === index;
186 if (item.type === "PATP") {
189 className={`cursor-pointer p-2 flex space-x-2 items-center text-left w-full ${
190 selected ? "bg-green-400" : ""
193 key: item.content.slug + "-" + index,
196 selected: highlightedIndex === index,
199 <div className="rounded-md overflow-hidden">
201 patp={item.content.patp}
206 <p className="font-mono">{item.content.patp}</p>
210 if (item.type === "GLOSSARY_RESULT") {
213 className={`cursor-pointer flex text-left w-full ${
214 selected ? "bg-green-400" : ""
217 key: item.content.slug + "-" + index,
220 selected: highlightedIndex === index,
223 <div className="font-semibold p-3">
225 className={`text-base ${
226 selected ? "text-white" : "text-wall-600"
229 {item.content.symbol.length > 0 && (
231 className={`mr-1 rounded px-1 py-0.5 ${
237 {item.content.symbol}
243 className={`font-normal text-base mt-1 ${
244 selected ? "text-white" : "text-wall-600"
246 dangerouslySetInnerHTML={{
247 __html: item.content.desc,
254 if (item.type === "RESULT") {
257 className={`cursor-pointer flex text-left w-full ${
258 selected ? "bg-green-400" : ""
261 key: item.content.link + "-" + index,
267 <div className="p-3">
269 className={`font-medium text-base ${
270 selected ? "text-white" : "text-wall-600"
273 {item.content.parent !== "Content"
274 ? `${item.content.parent} /`
279 className={`text-base font-regular text-small ${
280 selected ? "text-midWhite" : "text-wall-500"
283 {item.content.content}
284 {item?.content?.foundOnPage && (
285 <span className="italic block">
286 Found in page content
294 if (item.type === "DEV_RESULT") {
295 const devItem = Object.assign({}, item.content);
298 ] = `https://developers.urbit.org${item.content.slug}`;
301 className={`cursor-pointer flex text-left w-full ${
302 selected ? "bg-green-400" : ""
305 key: item.content.link + "-" + index,
311 <div className="p-3">
313 className={`font-medium text-base ${
314 selected ? "text-white" : "text-wall-600"
317 <span className="text-wall-400">
318 {item.content.parent !== "Content"
319 ? `developers.urbit.org / ${item.content.parent} /`
320 : "developers.urbit.org /"}{" "}
325 className={`text-base font-regular text-small ${
326 selected ? "text-midWhite" : "text-wall-500"
329 {item.content.content}
330 {item?.content?.foundOnPage && (
331 <span className="italic block">
332 Found in page content
340 if (item.type === "OPS_RESULT") {
341 const opsItem = Object.assign({}, item.content);
344 ] = `https://operators.urbit.org${item.content.slug}`;
347 className={`cursor-pointer flex text-left w-full ${
348 selected ? "bg-green-400" : ""
351 key: item.content.link + "-" + index,
357 <div className="p-3">
359 className={`font-medium text-base ${
360 selected ? "text-white" : "text-wall-600"
363 <span className="text-wall-400">
364 {item.content.parent !== "Content"
365 ? `operators.urbit.org / ${item.content.parent} /`
366 : "operators.urbit.org /"}{" "}
371 className={`text-base font-regular text-small ${
372 selected ? "text-midWhite" : "text-wall-500"
375 {item.content.content}
376 {item?.content?.foundOnPage && (
377 <span className="italic block">
378 Found in page content
401function deSig(string) {
402 return string.startsWith("~") ? string.substring(1) : string;
405export default withRouter(Search);
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list