Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Commit

Permalink
fix(v0.1.2): Resolve IPFS CIDs from localhost in sandbox mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip Diaz committed Feb 16, 2021
1 parent 6d1d877 commit d06cf5c
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
8 changes: 7 additions & 1 deletion client/src/components/Collections/Catalog/TokenGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { AspectRatio, Box, Flex, Grid, Image, Text } from '@chakra-ui/react';
import { Wind, HelpCircle } from 'react-feather';
import { Token, CollectionsState } from '../../../reducer/slices/collections';
import { ipfsUriToGatewayUrl } from '../../../lib/util/ipfs';
import { useSelector } from '../../../reducer';

interface TokenTileProps extends Token {
network: string;
selectedCollection: string;
}

Expand Down Expand Up @@ -61,7 +63,9 @@ function TokenTile(props: TokenTileProps) {
>
<AspectRatio ratio={3 / 2}>
<Box p={4}>
<TokenImage src={ipfsUriToGatewayUrl(props.artifactUri)} />
<TokenImage
src={ipfsUriToGatewayUrl(props.network, props.artifactUri)}
/>
</Box>
</AspectRatio>
<Flex
Expand All @@ -84,6 +88,7 @@ interface TokenGridProps {
}

export default function TokenGrid({ state, walletAddress }: TokenGridProps) {
const network = useSelector(s => s.system.config.network);
const selectedCollection = state.selectedCollection;

if (selectedCollection === null) {
Expand Down Expand Up @@ -131,6 +136,7 @@ export default function TokenGrid({ state, walletAddress }: TokenGridProps) {
<TokenTile
key={token.id}
selectedCollection={selectedCollection}
network={network}
{...token}
/>
);
Expand Down
4 changes: 3 additions & 1 deletion client/src/components/Collections/TokenDetail/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ function TokenDetail({ contractAddress, tokenId }: TokenDetailProps) {
</MinterButton>
</Flex>
<Flex align="center" justify="center" flex="1" px={16}>
<TokenImage src={ipfsUriToGatewayUrl(token.artifactUri)} />
<TokenImage
src={ipfsUriToGatewayUrl(system.config.network, token.artifactUri)}
/>
</Flex>
</Flex>
<Flex w="50%" h="100%" flexDir="column">
Expand Down
3 changes: 2 additions & 1 deletion client/src/components/CreateNonFungiblePage/FileUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { uploadFiletoIpfs } from '../../lib/util/ipfs';

export default function FileUpload() {
const state = useSelector(s => s.createNft);
const network = useSelector(s => s.system.config.network);
const dispatch = useDispatch();

const onDrop = useCallback(
Expand Down Expand Up @@ -59,7 +60,7 @@ export default function FileUpload() {
p={4}
maxWidth="400px"
maxHeight="400px"
src={ipfsUriToGatewayUrl(state.artifactUri)}
src={ipfsUriToGatewayUrl(network, state.artifactUri)}
/>
) : (
<Flex
Expand Down
5 changes: 4 additions & 1 deletion client/src/components/CreateNonFungiblePage/Preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ipfsUriToGatewayUrl, uriToCid } from '../../lib/util/ipfs';
import { useSelector } from '../../reducer';

export default function Preview() {
const network = useSelector(s => s.system.config.network);
const state = useSelector(s => s.createNft);
const { name, description } = state.fields;
return (
Expand All @@ -25,7 +26,9 @@ export default function Preview() {
>
<Image
src={
(state.artifactUri && ipfsUriToGatewayUrl(state.artifactUri)) || ''
(state.artifactUri &&
ipfsUriToGatewayUrl(network, state.artifactUri)) ||
''
}
overflow="hidden"
objectFit="contain"
Expand Down
8 changes: 6 additions & 2 deletions client/src/lib/util/ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ export function ipfsUriToCid(uri: string) {
return null;
}

export function ipfsUriToGatewayUrl(uri: string) {
export function ipfsUriToGatewayUrl(network: string, uri: string) {
const ipfsHost =
network === 'sandbox'
? 'http://localhost:5001'
: 'https://cloudflare-ipfs.com';
const cid = ipfsUriToCid(uri);
return cid ? `https://cloudflare-ipfs.com/ipfs/${cid}` : uri;
return cid ? `${ipfsHost}/ipfs/${cid}` : uri;
}

export function uriToCid(uri: string) {
Expand Down

0 comments on commit d06cf5c

Please sign in to comment.