-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: shorten "From" in details #336
Draft
zugdev
wants to merge
8
commits into
ubiquity:development
Choose a base branch
from
zugdev:development
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b49974c
feat: shorten "From" in details
zugdev 6ff3225
feat: add address dynamic shortening based on window size
zugdev f4ca60c
feat: update shortening numbers
zugdev 400c97d
feat: handle ens shortening
zugdev 96400e8
chore: yarn
zugdev 7dfa73b
chore: lint
zugdev 71f126c
chore: cspell
zugdev 9d77f84
feat: use css to trim addresses
zugdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,15 +2,51 @@ 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 = 570; // 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, 20); | ||||||
|
||||||
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); | ||||||
}); | ||||||
} | ||||||
|
||||||
// triggers shortening on resize | ||||||
window.addEventListener("resize", updateAddresses); | ||||||
|
||||||
function formatLargeNumber(value: BigNumber, decimals: number): string { | ||||||
const num = parseFloat(ethers.utils.formatUnits(value, decimals)); | ||||||
|
||||||
if (num >= 1_000_000_000_000_000) { | ||||||
return "Unlimited"; // we can consider quintillion and above basically unlimited | ||||||
return "Unlimited"; // we can consider quintillion and above basically unlimited | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} else if (num >= 1_000_000_000_000) { | ||||||
return `${(num / 1_000_000_000_000).toFixed(1)}T`; // i.e: 1.2T | ||||||
} else if (num >= 1_000_000_000) { | ||||||
|
@@ -34,7 +70,10 @@ export function insertErc20PermitTableData( | |||||
renderToFields(reward.beneficiary, app.currentExplorerUrl); | ||||||
renderTokenFields(reward.tokenAddress, app.currentExplorerUrl); | ||||||
renderDetailsFields([ | ||||||
{ name: "From", value: `<a target="_blank" rel="noopener noreferrer" href="${app.currentExplorerUrl}/address/${reward.owner}">${reward.owner}</a>` }, | ||||||
{ | ||||||
name: "From", | ||||||
value: `<a class="address" target="_blank" rel="noopener noreferrer" href="${app.currentExplorerUrl}/address/${reward.owner}">${reward.owner}</a>`, | ||||||
}, | ||||||
{ | ||||||
name: "Expiry", | ||||||
value: (() => { | ||||||
|
@@ -52,6 +91,8 @@ export function insertErc20PermitTableData( | |||||
}, | ||||||
]); | ||||||
table.setAttribute(`data-make-claim-rendered`, "true"); | ||||||
// shortens addresses if necessary | ||||||
updateAddresses(); | ||||||
return requestedAmountElement; | ||||||
} | ||||||
|
||||||
|
@@ -117,15 +158,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>`; | ||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use pure CSS. I implemented this logic in work.ubq.fi for the issue titles and repository names. You can probably use the same logic.