mirror of
https://github.com/filecoin-project/slate.git
synced 2024-11-23 05:54:49 +03:00
feat(LinkPreview): add fallback support to more edge cases
This commit is contained in:
parent
c54d4b34cc
commit
92a4191882
@ -1,6 +1,7 @@
|
|||||||
import * as React from "react";
|
import * as React from "react";
|
||||||
import * as Styles from "~/common/styles";
|
import * as Styles from "~/common/styles";
|
||||||
import * as SVG from "~/common/svg";
|
import * as SVG from "~/common/svg";
|
||||||
|
import * as Constants from "~/common/constants";
|
||||||
|
|
||||||
import { P3 } from "~/components/system/components/Typography";
|
import { P3 } from "~/components/system/components/Typography";
|
||||||
import { css } from "@emotion/react";
|
import { css } from "@emotion/react";
|
||||||
@ -36,6 +37,28 @@ export default function LinkObjectPreview({ file, ratio, ...props }) {
|
|||||||
data: { link },
|
data: { link },
|
||||||
} = file;
|
} = file;
|
||||||
|
|
||||||
|
const [imgState, setImgState] = React.useState({
|
||||||
|
isLoaded: false,
|
||||||
|
show: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
if (!link.image) setImgState({ show: false, isLoaded: true });
|
||||||
|
|
||||||
|
const img = new Image();
|
||||||
|
img.src = link.image;
|
||||||
|
img.onload = () => {
|
||||||
|
if (img.naturalWidth < Constants.grids.object.desktop.width) {
|
||||||
|
setImgState({ isLoaded: true, show: false });
|
||||||
|
} else {
|
||||||
|
setImgState({ isLoaded: true, show: true });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
img.onerror = () => {
|
||||||
|
setImgState({ isLoaded: true, show: true });
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
const tag = (
|
const tag = (
|
||||||
<a
|
<a
|
||||||
css={Styles.LINK}
|
css={Styles.LINK}
|
||||||
@ -64,13 +87,14 @@ export default function LinkObjectPreview({ file, ratio, ...props }) {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<ObjectPreviewPrimitive file={file} tag={tag} {...props}>
|
<ObjectPreviewPrimitive file={file} tag={tag} {...props}>
|
||||||
{link.image ? (
|
{imgState.isLoaded &&
|
||||||
<img src={link.image} alt="Link preview" css={Styles.IMAGE_FILL} />
|
(imgState.show ? (
|
||||||
) : (
|
<img src={link.image} alt="Link preview" css={Styles.IMAGE_FILL} />
|
||||||
<div css={STYLES_PLACEHOLDER_CONTAINER}>
|
) : (
|
||||||
<LinkPlaceholder ratio={ratio} />
|
<div css={STYLES_PLACEHOLDER_CONTAINER}>
|
||||||
</div>
|
<LinkPlaceholder ratio={ratio} />
|
||||||
)}
|
</div>
|
||||||
|
))}
|
||||||
</ObjectPreviewPrimitive>
|
</ObjectPreviewPrimitive>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user