Skip to content

Commit

Permalink
feat: add address dynamic shortening based on window size
Browse files Browse the repository at this point in the history
  • Loading branch information
zugdev committed Oct 15, 2024
1 parent b49974c commit 6ff3225
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
56 changes: 43 additions & 13 deletions static/scripts/rewards/render-transaction/insert-table-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,49 @@ import { ERC20Permit, ERC721Permit } from "@ubiquibot/permit-generation/types";
import { BigNumber, ethers } from "ethers";
import { app, AppState } from "../app-state";

// dinamically shortens a string by slicing and elipsing it's middle
function shortenAddress(address: string): string {
return `${address.slice(0, 10)}...${address.slice(-8)}`;
const initialLength = 42; // address has 42 chars
const maxWidth = 520; // with to trigger shortning

if (window.innerWidth >= maxWidth) {
return address;
}

// remove 1 letter for every 6px below 520px
const charsToRemove = Math.floor((maxWidth - window.innerWidth) / 6);

// limit shortening
const newLength = Math.max(initialLength - charsToRemove, 10);

const frontChars = Math.ceil(newLength / 2);
const backChars = newLength - frontChars;

return `${address.slice(0, frontChars)}...${address.slice(-backChars)}`;
}

// function to update addresses based on the current window size
function updateAddresses() {
const addressElements = document.getElementsByClassName("address");

Array.from(addressElements).forEach((element) => {
// get or store the original address as an attribute
let fullAddress = element.getAttribute("data-full-address");
if (!fullAddress) {
fullAddress = element.innerHTML;
element.setAttribute("data-full-address", fullAddress);
}

element.innerHTML = shortenAddress(fullAddress);
});
}

// shortens address on load
updateAddresses();

// triggers shortening on resize
window.addEventListener("resize", updateAddresses);

function formatLargeNumber(value: BigNumber, decimals: number): string {
const num = parseFloat(ethers.utils.formatUnits(value, decimals));

Expand Down Expand Up @@ -36,7 +75,7 @@ export function insertErc20PermitTableData(
renderDetailsFields([
{
name: "From",
value: `<a target="_blank" rel="noopener noreferrer" href="${app.currentExplorerUrl}/address/${reward.owner}">${shortenAddress(reward.owner)}</a>`,
value: `<a class="address" target="_blank" rel="noopener noreferrer" href="${app.currentExplorerUrl}/address/${reward.owner}">${reward.owner}</a>`,
},
{
name: "Expiry",
Expand Down Expand Up @@ -120,15 +159,6 @@ function renderTokenFields(tokenAddress: string, explorerUrl: string) {
}

function renderToFields(receiverAddress: string, explorerUrl: string) {
const toFull = document.querySelector("#rewardRecipient .full") as Element;
const toShort = document.querySelector("#rewardRecipient .short") as Element;

// if the for address is an ENS name neither will be found
if (!toFull || !toShort) return;

toFull.innerHTML = `<div>${receiverAddress}</div>`;
toShort.innerHTML = `<div>${shortenAddress(receiverAddress)}</div>`;

const toBoth = document.getElementById(`rewardRecipient`) as Element;
toBoth.innerHTML = `<a target="_blank" rel="noopener noreferrer" href="${explorerUrl}/address/${receiverAddress}">${toBoth.innerHTML}</a>`;
const rewardRecipient = document.getElementById(`rewardRecipient`) as Element;
rewardRecipient.innerHTML = `<a class="address" target="_blank" rel="noopener noreferrer" href="${explorerUrl}/address/${receiverAddress}">${receiverAddress}</a>`;
}
8 changes: 0 additions & 8 deletions static/styles/rewards/claim-table.css
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,6 @@ table[data-details-visible="false"] #rewardToken .short,
table[data-details-visible="true"] #rewardToken .full {
display: initial;
}
table[data-details-visible="false"] #rewardRecipient .full,
table[data-details-visible="true"] #rewardRecipient .short {
display: none;
}
table[data-details-visible="false"] #rewardRecipient .short,
table[data-details-visible="true"] #rewardRecipient .full {
display: initial;
}
#To > td,
#To > th {
padding-bottom: 24px;
Expand Down

0 comments on commit 6ff3225

Please sign in to comment.