use new API

This commit is contained in:
Kieran 2023-04-04 13:50:18 +01:00
parent 3ceb83b318
commit 44b45bf0b7
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
2 changed files with 49 additions and 35 deletions

View File

@ -1,45 +1,51 @@
import { useEffect, useState } from "react"; 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) { async function fetchUrlPreviewInfo(url: string) {
// Hardcoded the dufflepud url here only for initial testing by Snort devs, const res = await fetch(`${ApiHost}/api/v1/preview?url=${encodeURIComponent(url)}`);
// will be more ideal for Snort to deploy its own instance of Dufflepud if (res.ok) {
// and link to it in an .env to not bombard dufflepud.onrender.com , which is return (await res.json()) as LinkPreviewData;
// 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 LinkPreview = ({ url }: { url: string }) => { const LinkPreview = ({ url }: { url: string }) => {
const [previewImage, setImage] = useState<string>(); const [preview, setPreview] = useState<LinkPreviewData>();
const [previewtitle, setTitle] = useState<string>();
useEffect(() => { useEffect(() => {
fetchUrlPreviewInfo(url) (async () => {
.then(data => { const data = await fetchUrlPreviewInfo(url);
if (data) { if (data) {
setImage(data.image || undefined); setPreview(data);
setTitle(data.title || undefined);
} }
}) })().catch(console.error);
.catch(console.error);
}, [url]); }, [url]);
return ( return (
<div className="link-preview-container"> <div className="link-preview-container">
<a href={url}> {preview && (
<img src={previewImage} className="link-preview-image"></img> <a href={url} onClick={e => e.stopPropagation()} target="_blank" rel="noreferrer" className="ext">
<p className="link-preview-title">{previewtitle}</p> {preview?.image && <ProxyImg src={preview?.image} className="link-preview-image" />}
<p className="link-preview-title">
{preview?.title}
{preview?.description && (
<>
<br />
<small>{preview?.description}</small>
</>
)}
</p>
</a> </a>
)}
{!preview && <Spinner className="f-center" />}
</div> </div>
); );
}; };

View File

@ -233,14 +233,22 @@
} }
.link-preview-container { .link-preview-container {
border: 1px solid #282828; border: 1px solid var(--gray);
border-radius: 10px; border-radius: 10px;
text-align: center;
}
.link-preview-container:hover {
cursor: pointer;
} }
.link-preview-title { .link-preview-title {
padding-left: 10px; padding: 0 10px 10px 10px;
padding-right: 10px; }
padding-bottom: 10px;
.link-preview-title > small {
color: var(--font-secondary-color);
font-size: small;
} }
.link-preview-image { .link-preview-image {