Skip to content

Commit

Permalink
Merge branch 'feature/mp4-support'
Browse files Browse the repository at this point in the history
  • Loading branch information
eguajardo committed Sep 6, 2021
2 parents 70413c6 + 5770531 commit 0986434
Show file tree
Hide file tree
Showing 13 changed files with 284 additions and 70 deletions.
5 changes: 5 additions & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@fortawesome/fontawesome-svg-core": "^1.2.36",
"@fortawesome/free-brands-svg-icons": "^5.15.4",
"@fortawesome/free-regular-svg-icons": "^5.15.4",
"@fortawesome/free-solid-svg-icons": "^5.15.4",
"@fortawesome/react-fontawesome": "^0.1.15",
"@testing-library/jest-dom": "^5.12.0",
"@testing-library/react": "^11.2.7",
"@testing-library/user-event": "^12.8.3",
Expand Down
19 changes: 7 additions & 12 deletions frontend/src/components/UI/BlueprintCard.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { loadJsonFromIPFS, ipfsPathToURL } from "../../helpers/ipfs";
import { loadJsonFromIPFS } from "../../helpers/ipfs";
import { useState, useCallback, useEffect } from "react";

import Player from "./Player";

function BlueprintCard(props) {
const [metadata, setMetadata] = useState({});
const [selected, setSelected] = useState(false);

const source = ipfsPathToURL(metadata.image);

const loadMetadata = useCallback(async () => {
const json = await loadJsonFromIPFS(props.uri);

Expand All @@ -33,16 +33,11 @@ function BlueprintCard(props) {
onClick={toggleSelected}
>
{metadata.image && (
<video
key={source}
autoPlay
muted
loop
poster={source}
<Player
image={metadata.image}
animation_url={metadata.animation_url}
className="card-img-top"
>
<source src={source} />
</video>
/>
)}
<div className="card-body">
{metadata.name && (
Expand Down
19 changes: 7 additions & 12 deletions frontend/src/components/UI/CollectionCard.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { utils } from "ethers";
import { loadJsonFromIPFS, ipfsPathToURL } from "../../helpers/ipfs";
import { loadJsonFromIPFS } from "../../helpers/ipfs";

import { useState, useCallback, useEffect } from "react";
import { useContractFunction, useEthers, useBlockNumber } from "@usedapp/core";
import { useContract } from "../../hooks/useContract";

import Player from "./Player";

function CollectionCard(props) {
const [metadata, setMetadata] = useState({});
const [requestId, setRequestId] = useState();
Expand All @@ -14,8 +16,6 @@ function CollectionCard(props) {
const block = useBlockNumber();
const tokenPackContract = useContract("TokenPack");

const source = ipfsPathToURL(metadata.image);

const [buttonState, setButtonState] = useState({
class: "btn btn-primary btn-lg btn-block mt-2",
disabled: false,
Expand Down Expand Up @@ -149,16 +149,11 @@ function CollectionCard(props) {
<div className="row">
<div className="col-6">
{metadata.image && (
<video
key={source}
autoPlay
muted
loop
poster={source}
<Player
image={metadata.image}
animation_url={metadata.animation_url}
className="card-img-top"
>
<source src={source} />
</video>
/>
)}
</div>
<div className="col-6">
Expand Down
14 changes: 7 additions & 7 deletions frontend/src/components/UI/FormGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ function FormGroup(props) {
}

const [previewSrc, fileInputLabel] = useMemo(() => {
let source = props.previewSrc;
let source = props.preview?.source;
let label = "[Choose file]";
if (props.formField.enteredFiles && props.formField.enteredFiles[0]) {
source = URL.createObjectURL(props.formField.enteredFiles[0]);
label = props.formField.enteredFiles[0].name;
} else if (source) {
source = ipfsPathToURL(props.previewSrc);
source = ipfsPathToURL(props.preview.source);
label = "[Change file]";
}

return [source, label];
}, [props.formField.enteredFiles, props.previewSrc]);
}, [props.formField.enteredFiles, props.preview?.source]);

console.log("previewSrc", previewSrc);

Expand Down Expand Up @@ -67,11 +67,11 @@ function FormGroup(props) {
<div>
<video
key={previewSrc}
autoPlay
muted
loop
autoPlay={props.preview?.autoPlay}
loop={props.preview?.loop}
controls={props.preview?.controls}
poster={previewSrc}
className={props.previewClass}
className={props.preview?.className}
>
<source src={previewSrc} />
</video>
Expand Down
19 changes: 7 additions & 12 deletions frontend/src/components/UI/NftCard.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { loadJsonFromIPFS, ipfsPathToURL } from "../../helpers/ipfs";
import { loadJsonFromIPFS } from "../../helpers/ipfs";
import { useState, useCallback, useEffect } from "react";

import Player from "./Player";

function NftCard(props) {
const [metadata, setMetadata] = useState({});

const source = ipfsPathToURL(metadata.image);

const loadMetadata = useCallback(async () => {
const json = await loadJsonFromIPFS(props.uri);

Expand All @@ -19,16 +19,11 @@ function NftCard(props) {
return (
<div className="card">
{metadata.image && (
<video
key={source}
autoPlay
muted
loop
poster={source}
<Player
image={metadata.image}
animation_url={metadata.animation_url}
className="card-img-top"
>
<source src={source} />
</video>
/>
)}
<div className="card-body">
{metadata.name && (
Expand Down
76 changes: 76 additions & 0 deletions frontend/src/components/UI/Player.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { ipfsPathToURL } from "../../helpers/ipfs";

import { useEffect, useRef, useState } from "react";

import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faPlayCircle } from "@fortawesome/free-regular-svg-icons";

function Player(props) {
const [isPreview, setPreview] = useState(true);

const videoPreviewRef = useRef();
const videoRef = useRef();

const image = ipfsPathToURL(props.image);
const animation = ipfsPathToURL(props.animation_url);

const play = () => {
if (animation) {
console.log(
"videoPreviewRef.current.currentTime",
videoPreviewRef.current?.currentTime
);
setPreview(false);
}
};

useEffect(() => {
if (!isPreview) {
console.log(
"videoRef.current.currentTime",
videoRef.current?.currentTime
);

videoRef.current.play();
}
}, [isPreview]);

return (
<div>
{isPreview && (
<div>
<video
key={"image_" + image}
ref={videoPreviewRef}
autoPlay
muted
loop
poster={image}
className={props.className}
>
<source src={image} />
</video>
{animation && (
<div className="play-button" onClick={play}>
<FontAwesomeIcon icon={faPlayCircle} />
</div>
)}
</div>
)}

{!isPreview && (
<video
key={"animation_" + animation}
ref={videoRef}
controls
loop
className={props.className}
>
<source src={animation} />
</video>
)}
</div>
);
}

export default Player;
6 changes: 3 additions & 3 deletions frontend/src/helpers/contracts.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const contracts = {
"localhost": {
"TokenPack": {
"address": "0xc776449BDB5fb203c409bE3fd1BD14F3c7c97BC7",
"address": "0xCD5d8b1061Af669383a1AA6aA67e7a71B6AeC666",
"abi": [
{
"inputs": [
Expand Down Expand Up @@ -387,7 +387,7 @@ export const contracts = {
]
},
"Token": {
"address": "0x7109B31E9C9B13783faFc60622AD6592EE870E61",
"address": "0xe4D8AFb5E5858e9116d46Ac0Ed4F3638CC78848D",
"abi": [
{
"inputs": [
Expand Down Expand Up @@ -1107,7 +1107,7 @@ export const contracts = {
]
},
"Blueprint": {
"address": "0x2E3AeD067e83B7046ED8639376a1EC20eb088687",
"address": "0xf644739fC6ba5292CE6296FC24F7e32Df8e51850",
"abi": [
{
"anonymous": false,
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/helpers/ipfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const ipfsWriter = new IPFS({
});

const ipfsReader = {
host: "dweb.link",
host: "infura-ipfs.io",
protocol: "https",
};

Expand Down
28 changes: 28 additions & 0 deletions frontend/src/helpers/videoPoster.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export const getVideoPoster = (source) => {
return new Promise((resolve, reject) => {
try {
const video = document.createElement("video");

video.onloadedmetadata = () => {
video.currentTime = 0;
};

video.onseeked = () => {
const canvas = document.createElement("canvas");
canvas.height = video.videoHeight;
canvas.width = video.videoWidth;
const context = canvas.getContext("2d");
context.drawImage(video, 0, 0, canvas.width, canvas.height);
const img = new Image();
img.src = canvas.toBlob((blob) => {
const file = new File([blob], "poster.png");
resolve(file);
}, "image/png");
};

video.src = source;
} catch (error) {
reject(error);
}
});
};
4 changes: 2 additions & 2 deletions frontend/src/hooks/useFormFields.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function useFormFields(initialValues) {

const hasError = (key) => {
if (key.isTouched && key.validator) {
return key.validator(key);
return key.validator(key, formFields);
}

return null;
Expand All @@ -42,7 +42,7 @@ export function useFormFields(initialValues) {
for (const key in formFields) {
if (
formFields[key].validator &&
formFields[key].validator(formFields[key])
formFields[key].validator(formFields[key], formFields)
) {
isValid = false;
break;
Expand Down
Loading

1 comment on commit 0986434

@vercel
Copy link

@vercel vercel bot commented on 0986434 Sep 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.