Merge pull request #142 from cheeaun/main

Update from main
This commit is contained in:
Chee Aun 2023-05-18 00:13:24 +08:00 committed by GitHub
commit 5ee926481a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -18,6 +18,7 @@ import { InView } from 'react-intersection-observer';
import { useLongPress } from 'use-long-press'; import { useLongPress } from 'use-long-press';
import useResizeObserver from 'use-resize-observer'; import useResizeObserver from 'use-resize-observer';
import { useSnapshot } from 'valtio'; import { useSnapshot } from 'valtio';
import { snapshot } from 'valtio/vanilla';
import AccountBlock from '../components/account-block'; import AccountBlock from '../components/account-block';
import Loader from '../components/loader'; import Loader from '../components/loader';
@ -1701,6 +1702,7 @@ const denylistDomains = /(twitter|github)\.com/i;
const failedUnfurls = {}; const failedUnfurls = {};
function _unfurlMastodonLink(instance, url) { function _unfurlMastodonLink(instance, url) {
const snapStates = snapshot(states);
if (denylistDomains.test(url)) { if (denylistDomains.test(url)) {
return; return;
} }
@ -1708,8 +1710,8 @@ function _unfurlMastodonLink(instance, url) {
return; return;
} }
const instanceRegex = new RegExp(instance + '/'); const instanceRegex = new RegExp(instance + '/');
if (instanceRegex.test(states.unfurledLinks[url]?.url)) { if (instanceRegex.test(snapStates.unfurledLinks[url]?.url)) {
return Promise.resolve(states.unfurledLinks[url]); return Promise.resolve(snapStates.unfurledLinks[url]);
} }
console.debug('🦦 Unfurling URL', url); console.debug('🦦 Unfurling URL', url);
@ -1723,29 +1725,15 @@ function _unfurlMastodonLink(instance, url) {
if (statusMatch) { if (statusMatch) {
const id = statusMatch[3]; const id = statusMatch[3];
const { masto } = api({ instance: domain }); const { masto } = api({ instance: domain });
remoteInstanceFetch = masto.v1.statuses remoteInstanceFetch = masto.v1.statuses.fetch(id).then((status) => {
.fetch(id)
.then((status) => {
if (status?.id) { if (status?.id) {
const statusURL = `/${domain}/s/${id}`; return {
const result = { status,
id,
instance: domain, instance: domain,
url: statusURL,
}; };
console.debug('🦦 Unfurled URL', url, id, statusURL);
states.unfurledLinks[url] = result;
saveStatus(status, domain, {
skipThreading: true,
});
return result;
} else { } else {
failedUnfurls[url] = true;
throw new Error('No results'); throw new Error('No results');
} }
})
.catch((e) => {
failedUnfurls[url] = true;
}); });
} }
@ -1760,34 +1748,41 @@ function _unfurlMastodonLink(instance, url) {
.then((results) => { .then((results) => {
if (results.statuses.length > 0) { if (results.statuses.length > 0) {
const status = results.statuses[0]; const status = results.statuses[0];
return {
status,
instance,
};
} else {
throw new Error('No results');
}
});
function handleFulfill(result) {
const { status, instance } = result;
const { id } = status; const { id } = status;
const statusURL = `/${instance}/s/${id}`; const url = `/${instance}/s/${id}`;
const result = { console.debug('🦦 Unfurled URL', url, id, url);
const data = {
id, id,
instance, instance,
url: statusURL, url,
}; };
console.debug('🦦 Unfurled URL', url, id, statusURL); states.unfurledLinks[url] = data;
states.unfurledLinks[url] = result;
saveStatus(status, instance, { saveStatus(status, instance, {
skipThreading: true, skipThreading: true,
}); });
return result; return data;
} else {
failedUnfurls[url] = true;
throw new Error('No results');
} }
}) function handleCatch(e) {
.catch((e) => {
failedUnfurls[url] = true; failedUnfurls[url] = true;
// console.warn(e); }
// Silently fail
});
if (remoteInstanceFetch) { if (remoteInstanceFetch) {
return Promise.any([remoteInstanceFetch, mastoSearchFetch]); return Promise.any([remoteInstanceFetch, mastoSearchFetch])
.then(handleFulfill)
.catch(handleCatch);
} else { } else {
return mastoSearchFetch; return mastoSearchFetch.then(handleFulfill).catch(handleCatch);
} }
} }
@ -1814,7 +1809,11 @@ function nicePostURL(url) {
); );
} }
const unfurlMastodonLink = throttle(_unfurlMastodonLink); const unfurlMastodonLink = throttle(
mem(_unfurlMastodonLink, {
cacheKey: (instance, url) => `${instance}:${url}`,
}),
);
const root = document.documentElement; const root = document.documentElement;
const defaultBoundingBoxPadding = 8; const defaultBoundingBoxPadding = 8;
@ -1958,11 +1957,14 @@ const QuoteStatuses = memo(({ id, instance, level = 0 }) => {
const snapStates = useSnapshot(states); const snapStates = useSnapshot(states);
const sKey = statusKey(id, instance); const sKey = statusKey(id, instance);
const quotes = snapStates.statusQuotes[sKey]; const quotes = snapStates.statusQuotes[sKey];
const uniqueQuotes = quotes?.filter(
(q, i, arr) => arr.findIndex((q2) => q2.url === q.url) === i,
);
if (!quotes?.length) return; if (!uniqueQuotes?.length) return;
if (level > 2) return; if (level > 2) return;
return quotes.map((q) => { return uniqueQuotes.map((q) => {
return ( return (
<Link <Link
key={q.instance + q.id} key={q.instance + q.id}