hide link preview when error

This commit is contained in:
Kieran 2023-04-04 13:55:29 +01:00
parent 44b45bf0b7
commit 2a7ca01118
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941

View File

@ -11,24 +11,32 @@ interface LinkPreviewData {
} }
async function fetchUrlPreviewInfo(url: string) { async function fetchUrlPreviewInfo(url: string) {
try {
const res = await fetch(`${ApiHost}/api/v1/preview?url=${encodeURIComponent(url)}`); const res = await fetch(`${ApiHost}/api/v1/preview?url=${encodeURIComponent(url)}`);
if (res.ok) { if (res.ok) {
return (await res.json()) as LinkPreviewData; return (await res.json()) as LinkPreviewData;
} }
} catch (e) {
console.warn(`Failed to load link preview`, url);
}
} }
const LinkPreview = ({ url }: { url: string }) => { const LinkPreview = ({ url }: { url: string }) => {
const [preview, setPreview] = useState<LinkPreviewData>(); const [preview, setPreview] = useState<LinkPreviewData | null>();
useEffect(() => { useEffect(() => {
(async () => { (async () => {
const data = await fetchUrlPreviewInfo(url); const data = await fetchUrlPreviewInfo(url);
if (data) { if (data) {
setPreview(data); setPreview(data);
} else {
setPreview(null);
} }
})().catch(console.error); })();
}, [url]); }, [url]);
if (preview === null) return null;
return ( return (
<div className="link-preview-container"> <div className="link-preview-container">
{preview && ( {preview && (