Files
5chan/src/hooks/useFeedStateString.js
T

96 lines
3.1 KiB
JavaScript
Raw Normal View History

2023-09-16 21:40:23 +02:00
import { useMemo } from 'react';
2023-05-09 21:20:30 +02:00
const useFeedStateString = (subplebbits) => {
2023-09-16 21:40:23 +02:00
return useMemo(() => {
const getClientHost = (clientUrl) => {
try {
clientUrl = new URL(clientUrl).hostname || clientUrl;
} catch (e) {}
return clientUrl;
};
const getClientUrls = (regex) => {
const clientUrls = new Set();
const addClientUrl = (client, clientUrl) => client?.state?.match?.(regex) && clientUrls.add(getClientHost(clientUrl));
for (const subplebbit of subplebbits) {
for (const clientUrl in subplebbit?.clients?.ipfsGateways) {
addClientUrl(subplebbit.clients.ipfsGateways[clientUrl], clientUrl);
}
for (const clientUrl in subplebbit?.clients?.ipfsClients) {
addClientUrl(subplebbit.clients.ipfsClients[clientUrl], clientUrl);
}
for (const chainTicker in subplebbit?.clients?.chainProviders) {
for (const clientUrl in subplebbit.clients.chainProviders[chainTicker]) {
addClientUrl(subplebbit.clients.chainProviders[chainTicker][clientUrl], clientUrl);
}
}
// find subplebbit pages states
if (subplebbit?.posts?.clients) {
for (const clientType in subplebbit.posts.clients) {
for (const sortType in subplebbit.posts.clients[clientType]) {
for (const clientUrl in subplebbit.posts.clients[clientType][sortType]) {
addClientUrl(subplebbit.posts.clients[clientType][sortType][clientUrl], clientUrl);
}
}
}
}
}
return [...clientUrls];
};
2023-05-09 21:20:30 +02:00
2023-09-16 21:40:23 +02:00
if (!subplebbits) {
return;
}
2023-05-09 21:20:30 +02:00
2023-09-16 21:40:23 +02:00
const states = {};
for (const subplebbit of subplebbits) {
states[subplebbit?.updatingState] = (states[subplebbit?.updatingState] || 0) + 1;
// find subplebbit pages states
for (const clientType in subplebbit?.posts?.clients) {
for (const sortType in subplebbit.posts.clients[clientType]) {
for (const clientUrl in subplebbit.posts.clients[clientType][sortType]) {
const state = subplebbit.posts.clients[clientType][sortType][clientUrl].state;
states[state] = (states[state] || 0) + 1;
}
}
}
}
2023-06-08 23:27:34 +00:00
2023-09-16 21:40:23 +02:00
// e.g. Resolving 2 addresses from infura.io, fetching 2 IPNS, 1 IPFS from cloudflare-ipfs.com, ipfs.io
let stateString = '';
if (states['resolving-address']) {
stateString += `resolving ${states['resolving-address']} addresses`;
const clientUrls = getClientUrls(/address/);
if (clientUrls.length) {
stateString += ` from ${clientUrls.join(', ')}`;
}
}
if (states['fetching-ipns'] || states['fetching-ipfs']) {
if (stateString) {
stateString += ', ';
}
stateString += `fetching `;
if (states['fetching-ipns']) {
stateString += `${states['fetching-ipns']} IPNS`;
}
if (states['fetching-ipfs']) {
if (states['fetching-ipns']) {
stateString += ', ';
}
stateString += `${states['fetching-ipfs']} IPFS`;
}
const clientUrls = getClientUrls(/ipfs|ipns/);
if (clientUrls.length) {
stateString += ` from ${clientUrls.join(', ')}`;
}
}
2023-06-08 23:27:34 +00:00
2023-09-16 21:40:23 +02:00
// capitalize first letter
stateString = stateString.charAt(0).toUpperCase() + stateString.slice(1);
2023-06-08 23:27:34 +00:00
2023-09-16 21:40:23 +02:00
// if string is empty, return undefined instead
return stateString || undefined;
}, [subplebbits]);
};
2023-05-09 21:20:30 +02:00
2023-09-16 21:40:23 +02:00
export default useFeedStateString;