1import { Component, createRef } from "react";
2import { glossary } from "../lib/glossary";
3import { withRouter } from "next/router";
4import Downshift from "downshift";
6class Search extends Component {
12 this.searchEndpoint = this.searchEndpoint.bind(this);
13 this.onInputValueChange = this.onInputValueChange.bind(this);
14 this.onSelect = this.onSelect.bind(this);
15 this.glossarySearch = this.glossarySearch.bind(this);
18 searchEndpoint(query) {
19 return `/api/search?q=${query}`;
22 glossarySearch(query) {
23 return glossary.filter((entry) => {
24 return query.toLowerCase() === entry.name || entry.symbol === query;
30 this.props.router.push(item.slug);
38 this.props.closeSearch();
41 onInputValueChange(query) {
43 fetch(this.searchEndpoint(query))
44 .then((res) => res.json())
46 // Wrap results in an object which will tell React what component to use to render results.
47 const results = res.results.map((item) => ({
52 const glossaryResults = this.glossarySearch(query).map((item) => ({
53 type: "GLOSSARY_RESULT",
57 const list = [...glossaryResults, ...results];
59 this.setState({ results: list });
62 this.setState({ results: [] });
67 const { state, props } = this;
69 if (props.showSearch) {
72 onSelect={(selection) => this.onSelect(selection)}
73 onInputValueChange={(event) => this.onInputValueChange(event)}
74 itemToString={(item) => (item ? item.slug : "")}
75 defaultHighlightedIndex={0}
88 <div className="fixed w-screen h-screen z-50 flex flex-col items-center p-4">
90 onClick={(event) => props.closeSearch(event)}
91 className="top-0 left-0 fixed w-screen h-screen bg-washedWall"
93 <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">
95 style={{ display: "inline-block" }}
96 {...getRootProps({}, { suppressRefError: true })}
100 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"
101 placeholder="Search..."
103 onClick={(e) => e.stopPropagation()}
105 onKeyDown: (event) => {
106 if (event.key === "Escape") {
107 // Prevent Downshift's default 'Escape' behavior.
108 event.nativeEvent.preventDownshiftDefault = true;
109 this.props.closeSearch(event);
115 <ul {...getMenuProps()} className="overflow-y-scroll">
117 ? state.results.map((item, index) => {
118 const selected = highlightedIndex === index;
119 if (item.type === "GLOSSARY_RESULT") {
122 className={`cursor-pointer flex text-left w-full ${
123 selected ? "bg-green-400" : ""
126 key: item.content.slug + "-" + index,
129 selected: highlightedIndex === index,
132 <div className="font-semibold p-3">
134 className={`text-base ${
135 selected ? "text-white" : "text-wall-600"
138 {item.content.symbol.length > 0 && (
140 className={`mr-1 rounded px-1 py-0.5 ${
146 {item.content.symbol}
152 className={`font-normal text-base mt-1 ${
153 selected ? "text-white" : "text-wall-600"
162 if (item.type === "RESULT") {
165 className={`cursor-pointer flex text-left w-full ${
166 selected ? "bg-green-400" : ""
169 key: item.content.link + "-" + index,
175 <div className="p-3">
177 className={`font-medium text-base ${
178 selected ? "text-white" : "text-wall-600"
181 {item.content.parent !== "Content"
182 ? `${item.content.parent} /`
187 className={`text-base font-regular text-small ${
188 selected ? "text-midWhite" : "text-wall-500"
191 {item.content.content}
212export 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