Skip to content
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

fix(fix formatting of ipfs urls on hypercert page) #1183

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading