Skip to content

Commit

Permalink
fix(ui-ux): fix NaN bug in homepage (#1939)
Browse files Browse the repository at this point in the history
* return 0% if divided by 0

* remove added ,

* remove console

* lint

* refine CalculatePercentage

---------

Co-authored-by: canonbrother <[email protected]>
  • Loading branch information
nattadex and canonbrother authored Oct 10, 2024
1 parent b618a70 commit af2b2b4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
15 changes: 7 additions & 8 deletions src/components/commons/searchbar/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,10 @@ export function SearchBar(props: SearchBarInterface): JSX.Element {
const currentReference = refs.reference.current as Element;
Object.assign(refs.floating.current?.style, {
width: `${currentReference.scrollWidth}px`,
left:
`${
currentReference.scrollLeft +
(currentReference.scrollWidth - refs.floating.current?.scrollWidth)
}px` ?? "",
left: `${
currentReference.scrollLeft +
(currentReference.scrollWidth - refs.floating.current?.scrollWidth)
}px`,
});
}

Expand Down Expand Up @@ -113,7 +112,7 @@ export function SearchBar(props: SearchBarInterface): JSX.Element {

const onChangeDebounceHandler = useMemo(
() => debounce(changeHandler, 200),
[]
[],
);

function onSelect(result: SearchResult): void {
Expand All @@ -137,7 +136,7 @@ export function SearchBar(props: SearchBarInterface): JSX.Element {
>
<div
className={classNames(
"flex w-full p-2 rounded-3xl h-10 bg-white dark:bg-gray-800 dark:border-gray-700 border focus-within:border-primary-200"
"flex w-full p-2 rounded-3xl h-10 bg-white dark:bg-gray-800 dark:border-gray-700 border focus-within:border-primary-200",
)}
data-testid="SearchBar"
ref={reference}
Expand Down Expand Up @@ -184,7 +183,7 @@ export function SearchBar(props: SearchBarInterface): JSX.Element {
async function getSearchResults(
api: WhaleApiClient,
network: NetworkName,
query: string
query: string,
): Promise<SearchResult[]> {
const searchResults: SearchResult[] = [];

Expand Down
9 changes: 7 additions & 2 deletions src/utils/index/CalculatePercentage.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
export function CalculatePercentage(
value1: number | undefined,
value2: number | undefined
value2: number | undefined,
): string {
if (value1 === undefined || value2 === undefined) {
return "";
}

return `${((value1 / value2) * 100).toFixed(2)}%`;
const result = value1 / value2;
if (isNaN(result)) {
return "0.00%";
}

return `${(result * 100).toFixed(2)}%`;
}

0 comments on commit af2b2b4

Please sign in to comment.