fix: og image load error

This commit is contained in:
Tristan Yang
2023-03-25 11:22:20 +08:00
parent 1d9eebd8d5
commit 34dcd6fbb7
2 changed files with 8 additions and 10 deletions
+7 -7
View File
@@ -1,9 +1,12 @@
import { useState, useEffect } from "react"; import { useState, useEffect } from "react";
import { useDispatch } from "react-redux";
import { useLazyGetOGInfoQuery } from "../../../app/services/message"; import { useLazyGetOGInfoQuery } from "../../../app/services/message";
import { upsertOG } from "../../../app/slices/footprint";
import { useAppSelector } from "../../../app/store"; import { useAppSelector } from "../../../app/store";
export default function URLPreview({ url = "" }) { export default function URLPreview({ url = "" }) {
const dispatch = useDispatch();
const [favicon, setFavicon] = useState(""); const [favicon, setFavicon] = useState("");
const [getInfo, { isLoading }] = useLazyGetOGInfoQuery(); const [getInfo, { isLoading }] = useLazyGetOGInfoQuery();
const ogData = useAppSelector(store => store.footprint.og[url]); const ogData = useAppSelector(store => store.footprint.og[url]);
@@ -32,12 +35,9 @@ export default function URLPreview({ url = "" }) {
// const handleFavError = () => { // const handleFavError = () => {
// setFavicon(""); // setFavicon("");
// }; // };
// const handleOGImageError = () => { const handleOGImageError = () => {
// setData(prev => { dispatch(upsertOG({ key: url, value: { ...ogData, images: [] } }));
// if (!prev) return prev; };
// return { ...prev, ogImage: "" };
// });
// };
if (isLoading) return <div className="h-28"></div>; if (isLoading) return <div className="h-28"></div>;
if (!url || !data || !data.title) return null; if (!url || !data || !data.title) return null;
const { title, description, ogImage } = data; const { title, description, ogImage } = data;
@@ -50,7 +50,7 @@ export default function URLPreview({ url = "" }) {
<h3 className={`text-primary-500 w-full truncate`}>{title}</h3> <h3 className={`text-primary-500 w-full truncate`}>{title}</h3>
<p className={`text-xs text-gray-400 mb-2 w-full truncate`}>{description}</p> <p className={`text-xs text-gray-400 mb-2 w-full truncate`}>{description}</p>
<div className="w-full h-[180px]"> <div className="w-full h-[180px]">
<img className="w-full h-full object-cover" src={ogImage} alt="og image" /> <img className="w-full h-full object-cover" onError={handleOGImageError} src={ogImage} alt="og image" />
</div> </div>
</a> </a>
) : ( ) : (
+1 -3
View File
@@ -44,9 +44,7 @@ export interface OG {
width?: number; width?: number;
height?: number; height?: number;
alt?: string; alt?: string;
}[ }[];
];
audios: { audios: {
type?: string; type?: string;
url: string; url: string;