on master help sign in
search: add id searching
commit96043e35a3eafda858f8efba7065e051a9707835 authorMatilde Park <matilde.park@urbit.org> dateSun 24 Apr 2022, 16:13 (5 years ago) parent6f2c202
1 file changed+54−1
components/Search.js+54−1
components/Search.js+54−1
@@ -3,6 +3,8 @@ import { glossary } from "../lib/glossary";
33import { withRouter } from "next/router";
44import debounce from "lodash.debounce";
55import Downshift from "downshift";
6+import ob from "urbit-ob";
7+import Sigil from "../components/Sigil";
68
79class Search extends Component {
810 constructor(props) {
@@ -14,6 +16,7 @@ class Search extends Component {
1416 this.onInputValueChange = this.onInputValueChange.bind(this);
1517 this.onSelect = this.onSelect.bind(this);
1618 this.glossarySearch = this.glossarySearch.bind(this);
19+ this.patpSearch = this.patpSearch.bind(this);
1720 }
1821
1922 searchEndpoint(query) {
@@ -26,6 +29,10 @@ class Search extends Component {
2629 });
2730 }
2831
32+ patpSearch(query) {
33+ return ob.isValidPatp(`~${deSig(query.toLowerCase())}`) || !isNaN(query);
34+ }
35+
2936 onSelect(item) {
3037 if (item.slug) {
3138 this.props.router.push(item.slug);
@@ -50,12 +57,30 @@ class Search extends Component {
5057 content: item,
5158 }));
5259
60+ const patp = this.patpSearch(query)
61+ ? !isNaN(query)
62+ ? ob.patp(query)
63+ : ob.patp(ob.patp2dec(`~${deSig(query)}`))
64+ : null;
65+
66+ const patpResult = this.patpSearch(query)
67+ ? [
68+ {
69+ type: "PATP",
70+ content: {
71+ patp: patp,
72+ slug: `/id/${patp}`,
73+ },
74+ },
75+ ]
76+ : [];
77+
5378 const glossaryResults = this.glossarySearch(query).map((item) => ({
5479 type: "GLOSSARY_RESULT",
5580 content: item,
5681 }));
5782
58− const list = [...glossaryResults, ...results];
83+ const list = [...patpResult, ...glossaryResults, ...results];
5984
6085 this.setState({ results: list });
6186 });
@@ -117,6 +142,30 @@ class Search extends Component {
117142 {isOpen
118143 ? state.results.map((item, index) => {
119144 const selected = highlightedIndex === index;
145+ if (item.type === "PATP") {
146+ return (
147+ <li
148+ className={`cursor-pointer p-2 flex space-x-2 items-center text-left w-full ${
149+ selected ? "bg-green-400" : ""
150+ }`}
151+ {...getItemProps({
152+ key: item.content.slug + "-" + index,
153+ index,
154+ item: item.content,
155+ selected: highlightedIndex === index,
156+ })}
157+ >
158+ <div className="rounded-md overflow-hidden">
159+ <Sigil
160+ patp={item.content.patp}
161+ size={25}
162+ icon
163+ />
164+ </div>
165+ <p className="font-mono">{item.content.patp}</p>
166+ </li>
167+ );
168+ }
120169 if (item.type === "GLOSSARY_RESULT") {
121170 return (
122171 <li
@@ -210,4 +259,8 @@ class Search extends Component {
210259 }
211260}
212261
262+function deSig(string) {
263+ return string.startsWith("~") ? string.substring(1) : string;
264+}
265+
213266export default withRouter(Search);