diff --git a/package.json b/package.json index bdc074fd..63f20bef 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "plebchan", - "version": "0.1.6", + "version": "0.1.7", "private": true, "dependencies": { "@babel/core": "7.21.3", @@ -8,7 +8,7 @@ "@babel/plugin-transform-react-jsx": "7.21.0", "@capacitor/android": "3.6.0", "@capacitor/core": "3.6.0", - "@plebbit/plebbit-react-hooks": "https://github.com/plebbit/plebbit-react-hooks.git#5949b8cac5f786017395437960f842f4f9a9b1c2", + "@plebbit/plebbit-react-hooks": "https://github.com/plebbit/plebbit-react-hooks.git#06077b77cf0394ba925a7a292605d7a20048bfcc", "@testing-library/dom": "9.0.1", "@testing-library/jest-dom": "5.14.1", "@testing-library/react": "14.0.0", diff --git a/src/hooks/useFeedStateString.js b/src/hooks/useFeedStateString.js index 62860894..18411c9a 100644 --- a/src/hooks/useFeedStateString.js +++ b/src/hooks/useFeedStateString.js @@ -1,64 +1,77 @@ import { useMemo } from "react" const useFeedStateString = (subplebbits) => { - return useMemo(() => { - const getClientUrls = (regex) => { - const clientUrls = new Set() - for (const clientType in subplebbits.clients) { - for (const clientUrl in subplebbits.clients[clientType]) { - const client = subplebbits.clients[clientType][clientUrl] - if (client.state.match(regex)) { - clientUrls.add(clientUrl) - } + return useMemo(() => { + const getClientHost = (clientUrl) => { + try { + clientUrl = new URL(clientUrl).hostname || clientUrl + } + catch (e) {} + return clientUrl } - } - return [...clientUrls] - } - - if (!subplebbits) { - return undefined; - } - - const states = {} - for (const subplebbit of subplebbits) { - 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 - 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 += ', ' + 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) + } + } + } + return [...clientUrls] } - stateString += `${states['fetching-ipfs']} IPNS` - } - const clientUrls = getClientUrls(/ipfs|ipns/) - if (clientUrls.length) { - stateString += ` from ${clientUrls.join(', ')}` - } - } - // capitalize first letter - stateString = stateString.charAt(0).toUpperCase() + stateString.slice(1) + if (!subplebbits) { + return undefined; + } - // if string is empty, return undefined instead - return stateString || undefined - }, [subplebbits]) + const states = {} + for (const subplebbit of subplebbits) { + 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 + 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']} IPNS` + } + const clientUrls = getClientUrls(/ipfs|ipns/) + if (clientUrls.length) { + stateString += ` from ${clientUrls.join(', ')}` + } + } + + // capitalize first letter + stateString = stateString.charAt(0).toUpperCase() + stateString.slice(1) + + // if string is empty, return undefined instead + return stateString || undefined + }, [subplebbits]) } export default useFeedStateString \ No newline at end of file diff --git a/src/hooks/useStateString.js b/src/hooks/useStateString.js index 31a74278..57bd2b17 100644 --- a/src/hooks/useStateString.js +++ b/src/hooks/useStateString.js @@ -1,76 +1,83 @@ import { useMemo } from 'react' const useStateString = (commentOrSubplebbit) => { - return useMemo(() => { - if (!commentOrSubplebbit?.clients) { - return - } - const clients = commentOrSubplebbit?.clients - - const states = {} - for (const clientType in clients) { - for (const clientUrl in clients[clientType]) { - const state = clients[clientType][clientUrl].state - if (state === 'stopped') { - continue + return useMemo(() => { + if (!commentOrSubplebbit?.clients) { + return } - if (!states[state]) { - states[state] = [] + const clients = commentOrSubplebbit?.clients + + const states = {} + const addState = (state, clientUrl) => { + if (!state || state === 'stopped') { + 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) + } } - states[state].push(clientUrl) - } - } - const getClientHost = (clientUrl) => { - try { - return new URL(clientUrl).hostname || clientUrl - } - catch (e) { - return clientUrl - } - } + const getClientHost = (clientUrl) => { + try { + clientUrl = new URL(clientUrl).hostname || clientUrl + } + catch (e) {} + return clientUrl + } - let stateString = '' - for (const state in states) { - const clientUrls = states[state] - const clientHosts = clientUrls.map(clientUrl => getClientHost(clientUrl)) + let stateString = '' + for (const state in states) { + const clientUrls = states[state] + const clientHosts = clientUrls.map(clientUrl => getClientHost(clientUrl)) - // if there are no valid hosts, skip this state - if (clientHosts.length === 0) { - continue - } + // if there are no valid hosts, skip this state + if (clientHosts.length === 0) { + continue + } - // separate 2 different states using ', ' - if (stateString) { - stateString += ', ' - } + // 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(', ')}` - } + // 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(', ')}` + } - // 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') - } - } + // 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') + } + } - // capitalize first letter - if (stateString) { - stateString = stateString.charAt(0).toUpperCase() + stateString.slice(1) - } + // capitalize first letter + if (stateString) { + stateString = stateString.charAt(0).toUpperCase() + stateString.slice(1) + } - // if string is empty, return undefined instead - return stateString === '' ? undefined : stateString - }, [commentOrSubplebbit]) + // if string is empty, return undefined instead + return stateString === '' ? undefined : stateString + }, [commentOrSubplebbit]) } export default useStateString \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index d20f4e55..461d14db 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2613,9 +2613,9 @@ mkdirp "^1.0.4" rimraf "^3.0.2" -"@plebbit/plebbit-js@https://github.com/plebbit/plebbit-js.git#12def70256a61bc40500593f397f9d23975019e6": +"@plebbit/plebbit-js@https://github.com/plebbit/plebbit-js.git#27df54b8c591c9245deb91e131d15de2bc8bf4d3": version "0.0.3" - resolved "https://github.com/plebbit/plebbit-js.git#12def70256a61bc40500593f397f9d23975019e6" + resolved "https://github.com/plebbit/plebbit-js.git#27df54b8c591c9245deb91e131d15de2bc8bf4d3" dependencies: "@keyv/sqlite" "3.6.2" "@plebbit/plebbit-logger" "github:plebbit/plebbit-logger" @@ -2637,7 +2637,7 @@ jose "4.11.0" js-sha256 "0.9.0" keyv "4.5.2" - knex "2.3.0" + knex "2.4.2" libp2p-crypto "0.21.2" limiter "2.1.0" localforage "1.10.0" @@ -2649,7 +2649,7 @@ retry "0.13.1" safe-stable-stringify "2.4.1" skia-canvas "1.0.0" - sqlite3 "5.1.2" + sqlite3 "5.1.6" tiny-typed-emitter "2.1.0" tinycache "1.1.2" ts-custom-error "3.3.1" @@ -2668,11 +2668,11 @@ dependencies: debug "4.3.3" -"@plebbit/plebbit-react-hooks@https://github.com/plebbit/plebbit-react-hooks.git#5949b8cac5f786017395437960f842f4f9a9b1c2": +"@plebbit/plebbit-react-hooks@https://github.com/plebbit/plebbit-react-hooks.git#06077b77cf0394ba925a7a292605d7a20048bfcc": version "0.0.1" - resolved "https://github.com/plebbit/plebbit-react-hooks.git#5949b8cac5f786017395437960f842f4f9a9b1c2" + resolved "https://github.com/plebbit/plebbit-react-hooks.git#06077b77cf0394ba925a7a292605d7a20048bfcc" dependencies: - "@plebbit/plebbit-js" "https://github.com/plebbit/plebbit-js.git#12def70256a61bc40500593f397f9d23975019e6" + "@plebbit/plebbit-js" "https://github.com/plebbit/plebbit-js.git#27df54b8c591c9245deb91e131d15de2bc8bf4d3" "@plebbit/plebbit-logger" "https://github.com/plebbit/plebbit-logger.git" assert "2.0.0" ethers "5.6.9" @@ -9607,10 +9607,10 @@ klona@^2.0.4, klona@^2.0.5: resolved "https://registry.yarnpkg.com/klona/-/klona-2.0.6.tgz#85bffbf819c03b2f53270412420a4555ef882e22" integrity sha512-dhG34DXATL5hSxJbIexCft8FChFXtmskoZYnoPWjXQuebWYCNkVeV3KkGegCK9CP1oswI/vQibS2GY7Em/sJJA== -knex@2.3.0: - version "2.3.0" - resolved "https://registry.yarnpkg.com/knex/-/knex-2.3.0.tgz#87fa2a9553d7cafb125d7a0645256fbe29ef5967" - integrity sha512-WMizPaq9wRMkfnwKXKXgBZeZFOSHGdtoSz5SaLAVNs3WRDfawt9O89T4XyH52PETxjV8/kRk0Yf+8WBEP/zbYw== +knex@2.4.2: + version "2.4.2" + resolved "https://registry.yarnpkg.com/knex/-/knex-2.4.2.tgz#a34a289d38406dc19a0447a78eeaf2d16ebedd61" + integrity sha512-tMI1M7a+xwHhPxjbl/H9K1kHX+VncEYcvCx5K00M16bWvpYPKAZd6QrCu68PtHAdIZNQPWZn0GVhqVBEthGWCg== dependencies: colorette "2.0.19" commander "^9.1.0" @@ -13302,18 +13302,7 @@ sprintf-js@~1.0.2: resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c" integrity sha512-D9cPgkvLlV3t3IzL0D0YLvGA9Ahk4PcvVwUbN0dSGr1aP0Nrt4AEnTUbuGvquEC0mA64Gqt1fzirlRs5ibXx8g== -sqlite3@5.1.2: - version "5.1.2" - resolved "https://registry.yarnpkg.com/sqlite3/-/sqlite3-5.1.2.tgz#f50d5b1482b6972fb650daf6f718e6507c6cfb0f" - integrity sha512-D0Reg6pRWAFXFUnZKsszCI67tthFD8fGPewRddDCX6w4cYwz3MbvuwRICbL+YQjBAh9zbw+lJ/V9oC8nG5j6eg== - dependencies: - "@mapbox/node-pre-gyp" "^1.0.0" - node-addon-api "^4.2.0" - tar "^6.1.11" - optionalDependencies: - node-gyp "8.x" - -sqlite3@^5.1.2: +sqlite3@5.1.6, sqlite3@^5.1.2: version "5.1.6" resolved "https://registry.yarnpkg.com/sqlite3/-/sqlite3-5.1.6.tgz#1d4fbc90fe4fbd51e952e0a90fd8f6c2b9098e97" integrity sha512-olYkWoKFVNSSSQNvxVUfjiVbz3YtBwTJj+mfV5zpHmqW3sELx2Cf4QCdirMelhM5Zh+KDVaKgQHqCxrqiWHybw==