2023-03-28 09:52:44 +00:00
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
|
|
|
|
async function fetchUrlPreviewInfo(url: string) {
|
2023-03-28 10:11:21 +00:00
|
|
|
// 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
|
2023-03-28 09:52:44 +00:00
|
|
|
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 LinkPreview = ({ url }: { url: string }) => {
|
|
|
|
const [previewImage, setImage] = useState<string>();
|
|
|
|
const [previewtitle, setTitle] = useState<string>();
|
|
|
|
useEffect(() => {
|
|
|
|
fetchUrlPreviewInfo(url)
|
|
|
|
.then(data => {
|
|
|
|
if (data) {
|
|
|
|
setImage(data.image || undefined);
|
|
|
|
setTitle(data.title || undefined);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(console.error);
|
|
|
|
}, [url]);
|
|
|
|
return (
|
2023-03-28 10:11:21 +00:00
|
|
|
<div className="link-preview-container">
|
|
|
|
<a href={url}>
|
|
|
|
<img src={previewImage} className="link-preview-image"></img>
|
|
|
|
<p className="link-preview-title">{previewtitle}</p>
|
|
|
|
</a>
|
|
|
|
</div>
|
2023-03-28 09:52:44 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default LinkPreview;
|