1import { Component, createRef } from "react";
2import { glossary } from "../lib/glossary";
3import { withRouter } from "next/router";
4import debounce from "lodash.debounce";
5import Downshift from "downshift";
6import ob from "urbit-ob";
7import Sigil from "../components/Sigil";
9class Search extends Component {
15 this.searchEndpoint = this.searchEndpoint.bind(this);
16 this.onInputValueChange = this.onInputValueChange.bind(this);
17 this.onSelect = this.onSelect.bind(this);
18 this.glossarySearch = this.glossarySearch.bind(this);
19 this.patpSearch = this.patpSearch.bind(this);
22 searchEndpoint(query) {
23 return `/api/search?q=${query}`;
26 glossarySearch(query) {
27 return glossary.filter((entry) => {
29 entry.name.includes(query.toLowerCase()) || entry.symbol.includes(query)
36 (ob.isValidPatp(`~${deSig(query.toLowerCase())}`) &&
37 `~${deSig(query.toLowerCase())}`.length < 15) ||
38 (!isNaN(query) && query <= 4294967295)
44 this.props.router.push(item.slug);
52 this.props.closeSearch();
55 onInputValueChange = debounce((query) => {
57 fetch(this.searchEndpoint(query))
58 .then((res) => res.json())
60 // Wrap results in an object which will tell React what component to use to render results.
61 const results = res.results.map((item) => ({
66 const patp = this.patpSearch(query)
69 : ob.patp(ob.patp2dec(`~${deSig(query)}`))
72 const patpResult = this.patpSearch(query)
84 const glossaryResults = this.glossarySearch(query).map((item) => ({
85 type: "GLOSSARY_RESULT",
89 const list = [...glossaryResults, ...patpResult, ...results];
91 this.setState({ results: list });
94 this.setState({ results: [] });
99 const { state, props } = this;
101 if (props.showSearch) {
104 onSelect={(selection) => this.onSelect(selection)}
105 onInputValueChange={(event) => this.onInputValueChange(event)}
106 itemToString={(item) => (item ? item.slug : "")}
107 defaultHighlightedIndex={0}
120 <div className="fixed w-screen h-screen z-50 flex flex-col items-center p-4">
122 onClick={(event) => props.closeSearch(event)}
123 className="top-0 left-0 fixed w-screen h-screen bg-washedWall"
125 <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">
127 style={{ display: "inline-block" }}
128 {...getRootProps({}, { suppressRefError: true })}
132 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"
133 placeholder="Search..."
135 onClick={(e) => e.stopPropagation()}
137 onKeyDown: (event) => {
138 if (event.key === "Escape") {
139 // Prevent Downshift's default 'Escape' behavior.
140 event.nativeEvent.preventDownshiftDefault = true;
141 this.props.closeSearch(event);
147 <ul {...getMenuProps()} className="overflow-y-scroll">
149 ? state.results.map((item, index) => {
150 const selected = highlightedIndex === index;
151 if (item.type === "PATP") {
154 className={`cursor-pointer p-2 flex space-x-2 items-center text-left w-full ${
155 selected ? "bg-green-400" : ""
158 key: item.content.slug + "-" + index,
161 selected: highlightedIndex === index,
164 <div className="rounded-md overflow-hidden">
166 patp={item.content.patp}
171 <p className="font-mono">{item.content.patp}</p>
175 if (item.type === "GLOSSARY_RESULT") {
178 className={`cursor-pointer flex text-left w-full ${
179 selected ? "bg-green-400" : ""
182 key: item.content.slug + "-" + index,
185 selected: highlightedIndex === index,
188 <div className="font-semibold p-3">
190 className={`text-base ${
191 selected ? "text-white" : "text-wall-600"
194 {item.content.symbol.length > 0 && (
196 className={`mr-1 rounded px-1 py-0.5 ${
202 {item.content.symbol}
208 className={`font-normal text-base mt-1 ${
209 selected ? "text-white" : "text-wall-600"
211 dangerouslySetInnerHTML={{
212 __html: item.content.desc,
219 if (item.type === "RESULT") {
222 className={`cursor-pointer flex text-left w-full ${
223 selected ? "bg-green-400" : ""
226 key: item.content.link + "-" + index,
232 <div className="p-3">
234 className={`font-medium text-base ${
235 selected ? "text-white" : "text-wall-600"
238 {item.content.parent !== "Content"
239 ? `${item.content.parent} /`
244 className={`text-base font-regular text-small ${
245 selected ? "text-midWhite" : "text-wall-500"
248 {item.content.content}
269function deSig(string) {
270 return string.startsWith("~") ? string.substring(1) : string;
273export 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