-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(lld) nfts gallery in newArch
- Loading branch information
1 parent
f17a3cb
commit c107ebc
Showing
31 changed files
with
359 additions
and
395 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"ledger-live-desktop": patch | ||
--- | ||
|
||
Moove and refactor medias from nft gallery to newArch |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
98 changes: 98 additions & 0 deletions
98
apps/ledger-live-desktop/src/newArch/Collectibles/components/Media/Image.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import React, { useState } from "react"; | ||
import { ImageProps } from "LLD/Collectibles/types/Media"; | ||
import styled from "styled-components"; | ||
import { Skeleton } from "../index"; | ||
import Placeholder from "./Placeholder"; | ||
/** | ||
* Nb: This image component can be used for small listings, large gallery rendering, | ||
* and even tokens without an image where it will fallback to a generative image | ||
* based on the token metadata and some hue changes. | ||
* | ||
* The text in the fallback image is only visible if we are in `full` mode, since list | ||
* mode is not large enough for the text to be readable. | ||
*/ | ||
|
||
const Wrapper = styled.div<{ | ||
full?: ImageProps["full"]; | ||
size?: ImageProps["size"]; | ||
loaded: boolean; | ||
square: ImageProps["square"]; | ||
maxHeight?: ImageProps["maxHeight"]; | ||
objectFit?: ImageProps["objectFit"]; | ||
error?: boolean; | ||
}>` | ||
width: ${({ full, size }) => (full ? "100%" : `${size}px`)}; | ||
height: ${({ full }) => full && "100%"}; | ||
aspect-ratio: ${({ square }) => (square ? "1 / 1" : "initial")}; | ||
max-height: ${({ maxHeight }) => maxHeight && `${maxHeight}px`}; | ||
border-radius: 4px; | ||
overflow: hidden; | ||
background-size: contain; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
& > *:nth-child(1) { | ||
display: ${({ loaded, error }) => (loaded || error ? "none" : "block")}; | ||
} | ||
& > img { | ||
display: ${({ loaded, error }) => (loaded || error ? "block" : "none")}; | ||
${({ objectFit }) => | ||
objectFit === "cover" | ||
? `width: 100%; | ||
height: 100%;` | ||
: `max-width: 100%; | ||
max-height: 100%;`} | ||
object-fit: ${p => p.objectFit ?? "cover"}; | ||
border-radius: 4px; | ||
user-select: none; | ||
} | ||
`; | ||
|
||
const Image: React.FC<ImageProps> = ({ | ||
uri, | ||
metadata, | ||
full = false, | ||
size = 32, | ||
tokenId, | ||
maxHeight, | ||
onClick, | ||
square = true, | ||
objectFit = "cover", | ||
setUseFallback, | ||
isFallback, | ||
}) => { | ||
const [loaded, setLoaded] = useState(false); | ||
const [error, setError] = useState(false); | ||
const isImageReady = uri && !error; | ||
|
||
return ( | ||
<Wrapper | ||
full={full} | ||
size={size} | ||
loaded={loaded} | ||
error={error || !uri} | ||
square={square} | ||
maxHeight={maxHeight} | ||
objectFit={objectFit} | ||
> | ||
<Skeleton full /> | ||
{isImageReady ? ( | ||
<img | ||
// This prevent a bug where the change of props from isFallback | ||
// would not unbind onError and would not trigger it again in case of error | ||
key={isFallback?.toString()} | ||
onClick={onClick} | ||
onLoad={() => setLoaded(true)} | ||
onError={() => (isFallback ? setError(true) : setUseFallback(true))} | ||
src={uri} | ||
/> | ||
) : ( | ||
<Placeholder tokenId={tokenId} metadata={metadata} full={full} /> | ||
)} | ||
</Wrapper> | ||
); | ||
}; | ||
|
||
export default Image; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
apps/ledger-live-desktop/src/newArch/Collectibles/components/Media/Video.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import React, { useState } from "react"; | ||
import { VideoProps } from "LLD/Collectibles/types/Media"; | ||
import styled from "styled-components"; | ||
import { Skeleton } from "../index"; | ||
|
||
const Wrapper = styled.div<{ | ||
full?: VideoProps["full"]; | ||
size?: VideoProps["size"]; | ||
loaded: boolean; | ||
square: VideoProps["square"]; | ||
maxHeight?: VideoProps["maxHeight"]; | ||
objectFit?: VideoProps["objectFit"]; | ||
error?: boolean; | ||
}>` | ||
width: ${({ full, size }) => (full ? "100%" : `${size}px`)}; | ||
height: ${({ full }) => full && "100%"}; | ||
aspect-ratio: ${({ square, error }) => (square || error ? "1 / 1" : "initial")}; | ||
max-height: ${({ maxHeight }) => maxHeight && `${maxHeight}px`}; | ||
border-radius: 4px; | ||
overflow: hidden; | ||
background-size: contain; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
& > *:nth-child(1) { | ||
display: ${({ loaded, error }) => (loaded || error ? "none" : "block")}; | ||
} | ||
& > video { | ||
display: ${({ loaded, error }) => (loaded || error ? "block" : "none")}; | ||
${({ objectFit }) => | ||
objectFit === "cover" | ||
? `width: 100%; | ||
height: 100%;` | ||
: `max-width: 100%; | ||
max-height: 100%;`} | ||
object-fit: ${p => p.objectFit ?? "contain"}; | ||
border-radius: 4px; | ||
user-select: none; | ||
position: relative; | ||
z-index: 10; | ||
} | ||
`; | ||
|
||
const Video: React.FC<VideoProps> = ({ | ||
uri, | ||
mediaType, | ||
full = false, | ||
size = 32, | ||
maxHeight, | ||
square = true, | ||
objectFit = "contain", | ||
setUseFallback, | ||
}) => { | ||
const [loaded, setLoaded] = useState(false); | ||
|
||
return ( | ||
<Wrapper | ||
full={full} | ||
size={size} | ||
loaded={loaded} | ||
square={square} | ||
maxHeight={maxHeight} | ||
objectFit={objectFit} | ||
> | ||
<Skeleton full /> | ||
<video | ||
onError={() => { | ||
setUseFallback(true); | ||
}} | ||
onLoadedData={() => setLoaded(true)} | ||
autoPlay | ||
loop | ||
controls | ||
disablePictureInPicture | ||
> | ||
<source src={uri} type={mediaType} /> | ||
</video> | ||
</Wrapper> | ||
); | ||
}; | ||
|
||
export default Video; |
27 changes: 27 additions & 0 deletions
27
apps/ledger-live-desktop/src/newArch/Collectibles/components/Media/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React from "react"; | ||
import { MediaProps } from "LLD/Collectibles/types/Media"; | ||
import useMedia from "LLD/Collectibles/hooks/useMedia"; | ||
import Placeholder from "./Placeholder"; | ||
import Image from "./Image"; | ||
import Video from "./Video"; | ||
|
||
const Media: React.FC<MediaProps> = props => { | ||
const { useFallback, contentType, uri, mediaType, squareWithDefault, setUseFallback } = | ||
useMedia(props); | ||
const Component = contentType === "video" && !useFallback ? Video : Image; | ||
|
||
return uri ? ( | ||
<Component | ||
{...props} | ||
uri={uri} | ||
mediaType={mediaType} | ||
square={squareWithDefault} | ||
isFallback={useFallback} | ||
setUseFallback={setUseFallback} | ||
/> | ||
) : ( | ||
<Placeholder metadata={props.metadata} tokenId={props.tokenId} /> | ||
); | ||
}; | ||
|
||
export default Media; |
Oops, something went wrong.