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
1 changed files with 13 additions and 5 deletions

View File

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