Commit 031096d3 authored by Igor Stuev's avatar Igor Stuev Committed by GitHub

Merge pull request #335 from blockscout/hash-shorten-fix

use binary search in prod for the 1st time in my life
parents ea0a22cd c375a10a
......@@ -46,14 +46,21 @@ const HashStringShortenDynamic = ({ hash, fontWeight = '400' }: Props) => {
const parentWidth = getWidth(parent);
if (getWidth(shadowEl) > parentWidth) {
for (let i = 1; i <= hash.length - TAIL_LENGTH - HEAD_MIN_LENGTH; i++) {
const res = hash.slice(0, hash.length - i - TAIL_LENGTH) + '...' + hash.slice(-TAIL_LENGTH);
const tail = hash.slice(-TAIL_LENGTH);
let leftI = HEAD_MIN_LENGTH;
let rightI = hash.length - TAIL_LENGTH;
while (rightI - leftI > 1) {
const medI = ((rightI - leftI) % 2) ? leftI + (rightI - leftI + 1) / 2 : leftI + (rightI - leftI) / 2;
const res = hash.slice(0, medI) + '...' + tail;
shadowEl.textContent = res;
if (getWidth(shadowEl) < parentWidth || i === hash.length - TAIL_LENGTH - HEAD_MIN_LENGTH) {
setDisplayedString(res);
break;
if (getWidth(shadowEl) < parentWidth) {
leftI = medI;
} else {
rightI = medI;
}
}
setDisplayedString(hash.slice(0, rightI - 1) + '...' + tail);
} else {
setDisplayedString(hash);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment