Skip to content

Commit

Permalink
fix(fix formatting of ipfs urls on hypercert page) (#1183)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jipperism authored Nov 15, 2023
1 parent c93f391 commit dbdb083
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
16 changes: 8 additions & 8 deletions frontend/lib/formatting.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import _ from "lodash";
import { HypercertClient } from "@hypercerts-org/sdk";

export const formatScope = (scopeLabel: string) =>
scopeLabel.toLowerCase().replaceAll(/\s+/g, "-").trim();
Expand Down Expand Up @@ -74,18 +73,19 @@ export const formatAddress = (address: string) =>
export const cidToIpfsUri = (cid: string) =>
cid.startsWith("ipfs://") ? cid : `ipfs://${cid}`;

export const formatExternalUrl = (
client: HypercertClient,
externalUrl?: string,
) => {
const getIpfsGatewayUri = (cidOrIpfsUri: string) => {
const NFT_STORAGE_IPFS_GATEWAY = "https://nftstorage.link/ipfs/{cid}";
const cid = cidOrIpfsUri.replace("ipfs://", "");
return NFT_STORAGE_IPFS_GATEWAY.replace("{cid}", cid);
};

export const formatExternalUrl = (externalUrl?: string) => {
if (!externalUrl) {
return "";
}
if (!externalUrl.startsWith("ipfs://")) {
return externalUrl;
}

return client.storage.getNftStorageGatewayUri(
externalUrl.replace("ipfs://", ""),
);
return getIpfsGatewayUri(externalUrl);
};
10 changes: 2 additions & 8 deletions frontend/lib/hypercert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ export async function loadHypercert(
const metadata = await client.storage.getMetadata(metadataUri);
hypercert.metadata = {
...metadata,
external_url_formatted: formatExternalUrl(
client,
metadata.external_url,
),
external_url_formatted: formatExternalUrl(metadata.external_url),
};
}
return hypercert;
Expand All @@ -54,10 +51,7 @@ export async function loadHypercert(
const metadata = await client.storage.getMetadata(options.metadataUri);
return new MetadataOnlyHypercert(options.metadataUri, {
...metadata,
external_url_formatted: formatExternalUrl(
client,
metadata.external_url,
),
external_url_formatted: formatExternalUrl(metadata.external_url),
});
}
throw new Error(
Expand Down

0 comments on commit dbdb083

Please sign in to comment.