import { memo, RefObject, useRef, useState } from 'react'; import { setAccount, useAccount, usePkcRpcSettings } from '@bitsocial/bitsocial-react-hooks'; import { useTranslation } from 'react-i18next'; import { getBrowserGatewayPkcOptions, getBrowserPureP2PPkcOptions, setPureP2PBrowserPreference } from '../../../lib/p2p-browser-config'; import { canConfigureBrowserPureP2P, isBrowserPureP2PEnabled } from '../../../lib/p2p-runtime'; import styles from './advanced-settings.module.css'; interface SettingsProps { ipfsGatewayUrlsRef?: RefObject; mediaIpfsGatewayUrlRef?: RefObject; pubsubProvidersRef?: RefObject; httpRoutersRef?: RefObject; ethRpcRef?: RefObject; p2pRpcRef?: RefObject; p2pDataPathRef?: RefObject; onPureP2PBrowserChange?: (enabled: boolean) => void; pureP2PBrowserEnabled?: boolean; pureP2PBrowserRef?: RefObject; } type AccountProtocolOptions = { chainProviders?: Record; dataPath?: string; httpRoutersOptions?: string[]; ipfsGatewayUrls?: string[]; kuboRpcClientsOptions?: unknown[]; libp2pJsClientsOptions?: unknown[]; pkcRpcClientsOptions?: string[]; pubsubHttpClientsOptions?: string[]; pubsubKuboRpcClientsOptions?: string[]; }; type AccountShape = { chainProviders?: AccountProtocolOptions['chainProviders']; mediaIpfsGatewayUrl?: string; pkcOptions?: AccountProtocolOptions; }; type RpcSettingsShape = { pkcOptions?: { dataPath?: string }; }; const getProtocolOptions = (account?: AccountShape) => account?.pkcOptions; const getChainProviders = (account?: AccountShape) => account?.chainProviders ?? getProtocolOptions(account)?.chainProviders; const getNodeRpcClientsOptions = (protocolOptions?: AccountProtocolOptions) => protocolOptions?.pkcRpcClientsOptions; const getPubsubRpcClientsOptions = (protocolOptions?: AccountProtocolOptions) => protocolOptions?.pubsubKuboRpcClientsOptions ?? protocolOptions?.pubsubHttpClientsOptions; const getRpcSettingsDataPath = (rpcSettings?: RpcSettingsShape) => rpcSettings?.pkcOptions?.dataPath ?? ''; const IPFSGatewaysSettings = ({ ipfsGatewayUrlsRef, mediaIpfsGatewayUrlRef }: SettingsProps) => { const account = useAccount() as AccountShape | undefined; const protocolOptions = getProtocolOptions(account); const { ipfsGatewayUrls } = protocolOptions || {}; const { mediaIpfsGatewayUrl } = account || {}; const pkcRpc = usePkcRpcSettings(); const isConnectedToRpc = pkcRpc?.state === 'connected'; const ipfsGatewayUrlsDefaultValue = ipfsGatewayUrls?.join('\n'); return (