Skip to content

Commit

Permalink
Open KT & Operation addresses when searching (#363)
Browse files Browse the repository at this point in the history
* Move to KT1 or TZ1

* wait until suggests are loaded

* use already written code
  • Loading branch information
ungarson authored Apr 9, 2022
1 parent d39e558 commit e8da7fb
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 9 deletions.
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@sentry/integrations": "^5.27.6",
"@taquito/beacon-wallet": "^11.0.2",
"@taquito/taquito": "^11.0.2",
"async-wait-until": "^2.0.12",
"axios": "^0.21.4",
"bignumber.js": "^9.0.1",
"bs58check": "^2.1.2",
Expand Down
31 changes: 28 additions & 3 deletions src/components/SearchBox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ import {
removeHistoryItem,
} from "@/utils/history.js";
import {SEARCH_TABS} from "../constants/searchTabs";
import { isKT1Address, isOperationHash } from "../utils/tz";
import waitUntil from "async-wait-until";
export default {
props: {
Expand All @@ -197,19 +199,23 @@ export default {
_locked: false,
_timerId: null,
isFocused: false,
isSearchCalled: false,
seqno: 0,
menuProps: {},
}),
created() {
this.suggests = this.getHistoryItems("");
},
destroyed() {
clearTimeout(this._timerId);
},
computed: {
searchBoxClassName() {
if (this.isFocused) {
return 'focused-searchbar';
}
return 'unfocused-searchbar';
}
},
},
methods: {
...mapActions(["showError"]),
Expand Down Expand Up @@ -284,14 +290,31 @@ export default {
}
return historyItem;
},
onEnter(searchText) {
async onEnter(searchText) {
this.isFocused = true;
await waitUntil(() => this.isSearchCalled === false);
if (isKT1Address(searchText)) {
const firstContractSuggest = this.suggests.find((suggest) => suggest.type === 'contract');
if (firstContractSuggest) {
const { body } = firstContractSuggest;
await this.$router.push({path: `/${body.network}/${body.address}`});
return;
}
}
if (isOperationHash(searchText)) {
const firstOperationSuggest = this.suggests.find((suggest) => suggest.type === 'operation');
if (firstOperationSuggest) {
const { body } = firstOperationSuggest;
await this.$router.push({path: `/${body.network}/opg/${body.hash}`});
return;
}
}
if (searchText !== null && searchText.length > 2) {
addHistoryItem({
value: searchText,
});
this.$router.push({ name: "search", query: { text: searchText } });
await this.$router.push({ name: "search", query: { text: searchText } });
}
},
getHistoryItems(searchText) {
Expand Down Expand Up @@ -321,6 +344,7 @@ export default {
fetchSearchDebounced(text, seqno) {
if (!text || text.length < 3) return;
this.isSearchCalled = true;
clearTimeout(this._timerId);
this._timerId = setTimeout(() => {
Expand All @@ -344,6 +368,7 @@ export default {
})
.finally(() => {
this.isSuggestionsLoading = false;
this.isSearchCalled = false;
});
}, 500);
},
Expand Down
8 changes: 8 additions & 0 deletions src/utils/tz.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,11 @@ export function round(value, decimals) {
}
return parseFloat((value / 10 ** decimals).toFixed(decimals));
}

export function isKT1Address(val) {
return /^(KT)[1-9A-HJ-NP-Za-km-z]{34}$/.test(val);
}

export function isOperationHash(val) {
return /^o[1-9A-HJ-NP-Za-km-z]{50}$/.test(val);
}
6 changes: 0 additions & 6 deletions src/views/extended_search/ExtendedSearch.vue
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,6 @@ export default {
});
}, 500);
},
isAddress() {
return /^(tz|KT)[1-9A-HJ-NP-Za-km-z]{34}$/.test(this.searchText);
},
isOpgHash() {
return /^o[1-9A-HJ-NP-Za-km-z]{50}$/.test(this.searchText);
},
clearTotal() {
this.suggests = [];
this.total = 0;
Expand Down

0 comments on commit e8da7fb

Please sign in to comment.