diff --git a/packages/app/src/Element/LinkPreview.tsx b/packages/app/src/Element/LinkPreview.tsx index 384357f..d07eb59 100644 --- a/packages/app/src/Element/LinkPreview.tsx +++ b/packages/app/src/Element/LinkPreview.tsx @@ -1,45 +1,51 @@ import { useEffect, useState } from "react"; +import { ApiHost } from "Const"; +import Spinner from "Icons/Spinner"; +import { ProxyImg } from "Element/ProxyImg"; + +interface LinkPreviewData { + title?: string; + description?: string; + image?: string; +} + async function fetchUrlPreviewInfo(url: string) { - // Hardcoded the dufflepud url here only for initial testing by Snort devs, - // will be more ideal for Snort to deploy its own instance of Dufflepud - // and link to it in an .env to not bombard dufflepud.onrender.com , which is - // Coracle's instance. Repo: https://github.com/staab/dufflepud - const res = await fetch("http://dufflepud.onrender.com/link/preview", { - method: "POST", - body: JSON.stringify({ url }), - headers: { - "Content-Type": "application/json", - }, - }); - const json = await res.json(); - if (json.title || json.image) { - const preview = json; - return preview; - } else { - return null; + const res = await fetch(`${ApiHost}/api/v1/preview?url=${encodeURIComponent(url)}`); + if (res.ok) { + return (await res.json()) as LinkPreviewData; } } const LinkPreview = ({ url }: { url: string }) => { - const [previewImage, setImage] = useState(); - const [previewtitle, setTitle] = useState(); + const [preview, setPreview] = useState(); + useEffect(() => { - fetchUrlPreviewInfo(url) - .then(data => { - if (data) { - setImage(data.image || undefined); - setTitle(data.title || undefined); - } - }) - .catch(console.error); + (async () => { + const data = await fetchUrlPreviewInfo(url); + if (data) { + setPreview(data); + } + })().catch(console.error); }, [url]); + return (
- - -

{previewtitle}

-
+ {preview && ( + e.stopPropagation()} target="_blank" rel="noreferrer" className="ext"> + {preview?.image && } +

+ {preview?.title} + {preview?.description && ( + <> +
+ {preview?.description} + + )} +

+
+ )} + {!preview && }
); }; diff --git a/packages/app/src/Element/Note.css b/packages/app/src/Element/Note.css index 27b0017..597fb13 100644 --- a/packages/app/src/Element/Note.css +++ b/packages/app/src/Element/Note.css @@ -233,14 +233,22 @@ } .link-preview-container { - border: 1px solid #282828; + border: 1px solid var(--gray); border-radius: 10px; + text-align: center; +} + +.link-preview-container:hover { + cursor: pointer; } .link-preview-title { - padding-left: 10px; - padding-right: 10px; - padding-bottom: 10px; + padding: 0 10px 10px 10px; +} + +.link-preview-title > small { + color: var(--font-secondary-color); + font-size: small; } .link-preview-image {