2023-05-09 12:59:00 +02:00
|
|
|
import { useMemo } from 'react'
|
|
|
|
|
|
2023-05-15 04:44:33 +00:00
|
|
|
const useStateString = (commentOrSubplebbit) => {
|
2023-05-09 21:20:30 +02:00
|
|
|
return useMemo(() => {
|
2023-05-15 04:44:33 +00:00
|
|
|
if (!commentOrSubplebbit?.clients) {
|
2023-05-09 12:59:00 +02:00
|
|
|
return
|
|
|
|
|
}
|
2023-05-15 04:44:33 +00:00
|
|
|
const clients = commentOrSubplebbit?.clients
|
2023-05-09 21:20:30 +02:00
|
|
|
|
2023-05-09 12:59:00 +02:00
|
|
|
const states = {}
|
|
|
|
|
for (const clientType in clients) {
|
|
|
|
|
for (const clientUrl in clients[clientType]) {
|
|
|
|
|
const state = clients[clientType][clientUrl].state
|
|
|
|
|
if (state === 'stopped') {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if (!states[state]) {
|
|
|
|
|
states[state] = []
|
|
|
|
|
}
|
|
|
|
|
states[state].push(clientUrl)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-10 23:13:24 +00:00
|
|
|
const getClientHost = (clientUrl) => {
|
|
|
|
|
try {
|
|
|
|
|
return new URL(clientUrl).hostname || clientUrl
|
|
|
|
|
}
|
|
|
|
|
catch (e) {
|
|
|
|
|
return clientUrl
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-09 12:59:00 +02:00
|
|
|
let stateString = ''
|
|
|
|
|
for (const state in states) {
|
|
|
|
|
const clientUrls = states[state]
|
2023-05-15 04:44:33 +00:00
|
|
|
const clientHosts = clientUrls.map(clientUrl => getClientHost(clientUrl))
|
2023-05-09 21:20:30 +02:00
|
|
|
|
|
|
|
|
// if there are no valid hosts, skip this state
|
|
|
|
|
if (clientHosts.length === 0) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2023-05-09 12:59:00 +02:00
|
|
|
|
|
|
|
|
// separate 2 different states using ', '
|
|
|
|
|
if (stateString) {
|
|
|
|
|
stateString += ', '
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// e.g. 'fetching IPFS from cloudflare-ipfs.com, ipfs.io'
|
|
|
|
|
const formattedState = state.replaceAll('-', ' ').replace('ipfs', 'IPFS').replace('ipns', 'IPNS')
|
|
|
|
|
stateString += `${formattedState} from ${clientHosts.join(', ')}`
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-15 04:44:33 +00:00
|
|
|
// fallback to comment or subplebbit state when possible
|
|
|
|
|
if (!stateString) {
|
|
|
|
|
if (commentOrSubplebbit?.publishingState !== 'stopped') {
|
|
|
|
|
stateString = commentOrSubplebbit.publishingState
|
|
|
|
|
}
|
|
|
|
|
else if (commentOrSubplebbit?.updatingState !== 'stopped') {
|
|
|
|
|
stateString = commentOrSubplebbit.updatingState
|
|
|
|
|
}
|
|
|
|
|
if (stateString) {
|
|
|
|
|
stateString = stateString.replaceAll('-', ' ').replace('ipfs', 'IPFS').replace('ipns', 'IPNS')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-09 12:59:00 +02:00
|
|
|
// capitalize first letter
|
2023-05-09 21:20:30 +02:00
|
|
|
if (stateString) {
|
|
|
|
|
stateString = stateString.charAt(0).toUpperCase() + stateString.slice(1)
|
|
|
|
|
}
|
2023-05-09 12:59:00 +02:00
|
|
|
|
|
|
|
|
// if string is empty, return undefined instead
|
2023-05-09 21:20:30 +02:00
|
|
|
return stateString === '' ? undefined : stateString
|
2023-05-15 04:44:33 +00:00
|
|
|
}, [commentOrSubplebbit])
|
2023-05-09 12:59:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default useStateString
|