diff --git a/src/components/SettingsModal.jsx b/src/components/SettingsModal.jsx index cde39c7f..e7cc5458 100644 --- a/src/components/SettingsModal.jsx +++ b/src/components/SettingsModal.jsx @@ -30,6 +30,79 @@ const SettingsModal = ({ isOpen, closeModal }) => { const pubsubRef = useRef(); const dataPathRef = useRef(); + const isValidURL = (url) => { + try { + new URL(url); + return true; + } catch { + return false; + } + }; + + const handleSavePlebbitOptions = async () => { + const gatewayUrls = gatewayRef.current.value.split('\n').filter(url => url.trim()); + const ipfsClientsOptions = ipfsRef.current.value.split('\n').filter(url => url.trim()) || undefined; + const pubsubClientsOptions = pubsubRef.current.value.split('\n').filter(url => url.trim()); + + const invalidUrls = [ + ...gatewayUrls, + ...(ipfsClientsOptions || []), + ...pubsubClientsOptions, + ].filter((url) => !isValidURL(url)); + + if (invalidUrls.length > 0) { + setErrorMessage(`Invalid URL(s): ${invalidUrls.join(', ')}`); + return; + } + + const plebbitOptions = { + ipfsGatewayUrls: gatewayUrls, + ipfsHttpClientsOptions: ipfsClientsOptions, + pubsubHttpClientsOptions: pubsubClientsOptions, + }; + + try { + await setAccount({ ...account, plebbitOptions }); + localStorage.setItem("toastMessage", "Settings Saved"); + window.location.reload(); + } catch (error) { + setErrorMessage(error.message); + } + }; + + + const handleResetPlebbitOptions = async () => { + setErrorMessage(null); + setSuccessMessage(null); + + const defaultGatewayUrls = [ + 'https://ipfs.io', + 'https://ipfsgateway.xyz', + 'https://cloudflare-ipfs.com' + ]; + const defaultPubsubHttpClientsOptions = ['https://pubsubprovider.xyz/api/v0']; + + gatewayRef.current.value = defaultGatewayUrls.join('\n'); + ipfsRef.current.value = ""; + pubsubRef.current.value = defaultPubsubHttpClientsOptions.join('\n'); + dataPathRef.current.value = ""; + + try { + await setAccount({ + ...account, + plebbitOptions: { + ipfsGatewayUrls: defaultGatewayUrls, + ipfsHttpClientsOptions: undefined, + pubsubHttpClientsOptions: defaultPubsubHttpClientsOptions, + }, + }); + localStorage.setItem("toastMessage", "Settings Reset"); + window.location.reload(); + } catch (error) { + setErrorMessage(error.message); + } + }; + const handleCloseModal = () => { closeModal(); @@ -70,6 +143,15 @@ const SettingsModal = ({ isOpen, closeModal }) => { localStorage.removeItem("cacheCleared"); } }, []); + + useEffect(() => { + const successMessageFromStorage = localStorage.getItem("toastMessage"); + if (successMessageFromStorage) { + setSuccessMessage(successMessageFromStorage); + localStorage.removeItem("toastMessage"); + } + }, []); + return ( @@ -135,8 +217,8 @@ const SettingsModal = ({ isOpen, closeModal }) => {
- - + +