Skip to content

Commit

Permalink
[Explorer] Fixes mistaken rendering of image in case of 0x2 (MystenLa…
Browse files Browse the repository at this point in the history
…bs#2872)

* fixes mistaken rendering of image in case of 0x2
  • Loading branch information
apburnie authored Jul 1, 2022
1 parent 5f79c36 commit b2dfe9b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
20 changes: 16 additions & 4 deletions explorer/client/src/utils/objectUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,28 @@ import {
type ObjectOwner,
} from '@mysten/sui.js';

import { findIPFSvalue } from './stringUtils';

import type { GetObjectDataResponse } from '@mysten/sui.js';

export function parseImageURL(data: any): string {
return (
const url =
data?.url ||
// TODO: Remove Legacy format
data?.display ||
data?.contents?.display ||
''
);
data?.contents?.display;

if (!url) return '';

if (findIPFSvalue(url)) return url;

// String respresenting true http/https URLs are valid:
try {
new URL(url);
return url;
} catch {
return '';
}
}

export function parseObjectType(data: GetObjectDataResponse): string {
Expand Down
7 changes: 5 additions & 2 deletions explorer/client/src/utils/stringUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ export const handleCoinType = (str: string): string =>
? 'SUI'
: str.match(/^([a-zA-Z0-9:]*)<([a-zA-Z0-9:]*)>$/)?.[2] || str;

export const findIPFSvalue = (url: string): string | undefined =>
url.match(/^ipfs:\/\/(.*)/)?.[1];

export function transformURL(url: string) {
const found = url.match(/^ipfs:\/\/(.*)/);
const found = findIPFSvalue(url);
if (!found) {
return url;
}
return `https://ipfs.io/ipfs/${found[1]}`;
return `https://ipfs.io/ipfs/${found}`;
}

export function truncate(fullStr: string, strLen: number, separator?: string) {
Expand Down

0 comments on commit b2dfe9b

Please sign in to comment.