mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
add state strings for subplebbit pages
This commit is contained in:
@@ -1,77 +1,77 @@
|
|||||||
import { useMemo } from "react"
|
import { useMemo } from "react"
|
||||||
|
|
||||||
const useFeedStateString = (subplebbits) => {
|
const useFeedStateString = (subplebbits) => {
|
||||||
return useMemo(() => {
|
return useMemo(() => {
|
||||||
const getClientHost = (clientUrl) => {
|
const getClientHost = (clientUrl) => {
|
||||||
try {
|
try {
|
||||||
clientUrl = new URL(clientUrl).hostname || clientUrl
|
clientUrl = new URL(clientUrl).hostname || clientUrl
|
||||||
}
|
}
|
||||||
catch (e) {}
|
catch (e) {}
|
||||||
return clientUrl
|
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)
|
||||||
}
|
}
|
||||||
const getClientUrls = (regex) => {
|
for (const clientUrl in subplebbit?.clients?.ipfsClients) {
|
||||||
const clientUrls = new Set()
|
addClientUrl(subplebbit.clients.ipfsClients[clientUrl], clientUrl)
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return [...clientUrls]
|
|
||||||
}
|
}
|
||||||
|
for (const chainTicker in subplebbit?.clients?.chainProviders) {
|
||||||
|
for (const clientUrl in subplebbit.clients.chainProviders[chainTicker]) {
|
||||||
|
addClientUrl(subplebbit.clients.chainProviders[chainTicker][clientUrl], clientUrl)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return [...clientUrls]
|
||||||
|
}
|
||||||
|
|
||||||
if (!subplebbits) {
|
if (!subplebbits) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
const states = {}
|
const states = {}
|
||||||
for (const subplebbit of subplebbits) {
|
for (const subplebbit of subplebbits) {
|
||||||
states[subplebbit?.updatingState] = (states[subplebbit?.updatingState] || 0) + 1
|
states[subplebbit?.updatingState] = (states[subplebbit?.updatingState] || 0) + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
// e.g. Resolving 2 addresses from infura.io, fetching 2 IPNS, 1 IPFS from cloudflare-ipfs.com, ipfs.io
|
// e.g. Resolving 2 addresses from infura.io, fetching 2 IPNS, 1 IPFS from cloudflare-ipfs.com, ipfs.io
|
||||||
let stateString = ''
|
let stateString = ''
|
||||||
if (states['resolving-address']) {
|
if (states['resolving-address']) {
|
||||||
stateString += `resolving ${states['resolving-address']} addresses`
|
stateString += `resolving ${states['resolving-address']} addresses`
|
||||||
const clientUrls = getClientUrls(/address/)
|
const clientUrls = getClientUrls(/address/)
|
||||||
if (clientUrls.length) {
|
if (clientUrls.length) {
|
||||||
stateString += ` from ${clientUrls.join(', ')}`
|
stateString += ` from ${clientUrls.join(', ')}`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (states['fetching-ipns'] || states['fetching-ipfs']) {
|
if (states['fetching-ipns'] || states['fetching-ipfs']) {
|
||||||
if (stateString) {
|
if (stateString) {
|
||||||
stateString += ', '
|
stateString += ', '
|
||||||
}
|
}
|
||||||
stateString += `fetching `
|
stateString += `fetching `
|
||||||
if (states['fetching-ipns']) {
|
if (states['fetching-ipns']) {
|
||||||
stateString += `${states['fetching-ipns']} IPNS`
|
stateString += `${states['fetching-ipns']} IPNS`
|
||||||
}
|
}
|
||||||
if (states['fetching-ipfs']) {
|
if (states['fetching-ipfs']) {
|
||||||
if (states['fetching-ipns']) {
|
if (states['fetching-ipns']) {
|
||||||
stateString += ', '
|
stateString += ', '
|
||||||
}
|
|
||||||
stateString += `${states['fetching-ipfs']} IPNS`
|
|
||||||
}
|
|
||||||
const clientUrls = getClientUrls(/ipfs|ipns/)
|
|
||||||
if (clientUrls.length) {
|
|
||||||
stateString += ` from ${clientUrls.join(', ')}`
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
stateString += `${states['fetching-ipfs']} IPNS`
|
||||||
|
}
|
||||||
|
const clientUrls = getClientUrls(/ipfs|ipns/)
|
||||||
|
if (clientUrls.length) {
|
||||||
|
stateString += ` from ${clientUrls.join(', ')}`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// capitalize first letter
|
// capitalize first letter
|
||||||
stateString = stateString.charAt(0).toUpperCase() + stateString.slice(1)
|
stateString = stateString.charAt(0).toUpperCase() + stateString.slice(1)
|
||||||
|
|
||||||
// if string is empty, return undefined instead
|
// if string is empty, return undefined instead
|
||||||
return stateString || undefined
|
return stateString || undefined
|
||||||
}, [subplebbits])
|
}, [subplebbits])
|
||||||
}
|
}
|
||||||
|
|
||||||
export default useFeedStateString
|
export default useFeedStateString
|
||||||
+83
-63
@@ -1,83 +1,103 @@
|
|||||||
import { useMemo } from 'react'
|
import { useMemo } from 'react'
|
||||||
|
|
||||||
const useStateString = (commentOrSubplebbit) => {
|
const useStateString = (commentOrSubplebbit) => {
|
||||||
return useMemo(() => {
|
return useMemo(() => {
|
||||||
if (!commentOrSubplebbit?.clients) {
|
if (!commentOrSubplebbit?.clients) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const clients = commentOrSubplebbit?.clients
|
const clients = commentOrSubplebbit?.clients
|
||||||
|
|
||||||
const states = {}
|
const states = {}
|
||||||
const addState = (state, clientUrl) => {
|
const addState = (state, clientUrl) => {
|
||||||
if (!state || state === 'stopped') {
|
if (!state || state === 'stopped') {
|
||||||
return
|
return
|
||||||
|
}
|
||||||
|
if (!states[state]) {
|
||||||
|
states[state] = []
|
||||||
|
}
|
||||||
|
states[state].push(clientUrl)
|
||||||
|
}
|
||||||
|
for (const clientUrl in clients?.ipfsGateways) {
|
||||||
|
addState(clients.ipfsGateways[clientUrl]?.state, clientUrl)
|
||||||
|
}
|
||||||
|
for (const clientUrl in clients?.ipfsClients) {
|
||||||
|
addState(clients.ipfsClients[clientUrl]?.state, clientUrl)
|
||||||
|
}
|
||||||
|
for (const chainTicker in clients?.chainProviders) {
|
||||||
|
for (const clientUrl in clients.chainProviders[chainTicker]) {
|
||||||
|
addState(clients.chainProviders[chainTicker][clientUrl]?.state, clientUrl)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// find subplebbit pages states
|
||||||
|
if (commentOrSubplebbit?.posts?.clients) {
|
||||||
|
for (const clientType in commentOrSubplebbit.posts.clients) {
|
||||||
|
for (const sortType in commentOrSubplebbit.posts.clients[clientType]) {
|
||||||
|
for (const clientUrl in commentOrSubplebbit.posts.clients[clientType][sortType]) {
|
||||||
|
let state = commentOrSubplebbit.posts.clients[clientType][sortType][clientUrl].state
|
||||||
|
if (state === 'stopped') {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
state += `-page-${sortType}`
|
||||||
|
console.log(clientType, sortType, clientUrl, state)
|
||||||
if (!states[state]) {
|
if (!states[state]) {
|
||||||
states[state] = []
|
states[state] = []
|
||||||
}
|
}
|
||||||
states[state].push(clientUrl)
|
states[state].push(clientUrl)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for (const clientUrl in clients?.ipfsGateways) {
|
}
|
||||||
addState(clients.ipfsGateways[clientUrl]?.state, clientUrl)
|
}
|
||||||
}
|
|
||||||
for (const clientUrl in clients?.ipfsClients) {
|
|
||||||
addState(clients.ipfsClients[clientUrl]?.state, clientUrl)
|
|
||||||
}
|
|
||||||
for (const chainTicker in clients?.chainProviders) {
|
|
||||||
for (const clientUrl in clients.chainProviders[chainTicker]) {
|
|
||||||
addState(clients.chainProviders[chainTicker][clientUrl]?.state, clientUrl)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const getClientHost = (clientUrl) => {
|
const getClientHost = (clientUrl) => {
|
||||||
try {
|
try {
|
||||||
clientUrl = new URL(clientUrl).hostname || clientUrl
|
clientUrl = new URL(clientUrl).hostname || clientUrl
|
||||||
}
|
}
|
||||||
catch (e) {}
|
catch (e) {}
|
||||||
return clientUrl
|
return clientUrl
|
||||||
}
|
}
|
||||||
|
|
||||||
let stateString = ''
|
let stateString = ''
|
||||||
for (const state in states) {
|
for (const state in states) {
|
||||||
const clientUrls = states[state]
|
const clientUrls = states[state]
|
||||||
const clientHosts = clientUrls.map(clientUrl => getClientHost(clientUrl))
|
const clientHosts = clientUrls.map(clientUrl => getClientHost(clientUrl))
|
||||||
|
|
||||||
// if there are no valid hosts, skip this state
|
// if there are no valid hosts, skip this state
|
||||||
if (clientHosts.length === 0) {
|
if (clientHosts.length === 0) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// separate 2 different states using ', '
|
// separate 2 different states using ', '
|
||||||
if (stateString) {
|
if (stateString) {
|
||||||
stateString += ', '
|
stateString += ', '
|
||||||
}
|
}
|
||||||
|
|
||||||
// e.g. 'fetching IPFS from cloudflare-ipfs.com, ipfs.io'
|
// e.g. 'fetching IPFS from cloudflare-ipfs.com, ipfs.io'
|
||||||
const formattedState = state.replaceAll('-', ' ').replace('ipfs', 'IPFS').replace('ipns', 'IPNS')
|
const formattedState = state.replaceAll('-', ' ').replace('ipfs', 'IPFS').replace('ipns', 'IPNS')
|
||||||
stateString += `${formattedState} from ${clientHosts.join(', ')}`
|
stateString += `${formattedState} from ${clientHosts.join(', ')}`
|
||||||
}
|
}
|
||||||
|
|
||||||
// fallback to comment or subplebbit state when possible
|
// fallback to comment or subplebbit state when possible
|
||||||
if (!stateString) {
|
if (!stateString) {
|
||||||
if (commentOrSubplebbit?.publishingState !== 'stopped') {
|
if (commentOrSubplebbit?.publishingState !== 'stopped') {
|
||||||
stateString = commentOrSubplebbit.publishingState
|
stateString = commentOrSubplebbit.publishingState
|
||||||
}
|
}
|
||||||
else if (commentOrSubplebbit?.updatingState !== 'stopped') {
|
else if (commentOrSubplebbit?.updatingState !== 'stopped') {
|
||||||
stateString = commentOrSubplebbit.updatingState
|
stateString = commentOrSubplebbit.updatingState
|
||||||
}
|
}
|
||||||
if (stateString) {
|
if (stateString) {
|
||||||
stateString = stateString.replaceAll('-', ' ').replace('ipfs', 'IPFS').replace('ipns', 'IPNS')
|
stateString = stateString.replaceAll('-', ' ').replace('ipfs', 'IPFS').replace('ipns', 'IPNS')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// capitalize first letter
|
// capitalize first letter
|
||||||
if (stateString) {
|
if (stateString) {
|
||||||
stateString = stateString.charAt(0).toUpperCase() + stateString.slice(1)
|
stateString = stateString.charAt(0).toUpperCase() + stateString.slice(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// if string is empty, return undefined instead
|
// if string is empty, return undefined instead
|
||||||
return stateString === '' ? undefined : stateString
|
return stateString === '' ? undefined : stateString
|
||||||
}, [commentOrSubplebbit])
|
}, [commentOrSubplebbit])
|
||||||
}
|
}
|
||||||
|
|
||||||
export default useStateString
|
export default useStateString
|
||||||
Reference in New Issue
Block a user