|
| 1 | +import { useState, useContext } from "react"; |
| 2 | +import { useDispatch, useSelector } from 'react-redux' |
| 3 | +import type { RootState } from '../redux/store' |
| 4 | +import { setMode } from '../redux/slices/Mode'; |
| 5 | +import logger from "../utils/logger"; |
| 6 | +import WalletContext from "../context/wallet"; |
| 7 | +import { setCurrency } from '../redux/slices/Balance'; |
| 8 | +import { useNavigate } from "react-router"; |
| 9 | +import LoadingContext from '../context/loader'; |
| 10 | +import NProgress from 'nprogress'; |
| 11 | +// import { DownloadTransactionsCSV } from "../services/DownloadQR"; |
| 12 | +import Alerts from "./Alerts"; |
| 13 | + |
| 14 | + |
| 15 | +export default function BasicSettings() { |
| 16 | + const dispatch = useDispatch() |
| 17 | + const { metaData } = useSelector((state: RootState) => state.federationdetails) |
| 18 | + const [enabledLocation, setEnabledLocation] = useState(localStorage.getItem('locationAccess') === 'true' ? true : false) |
| 19 | + const { mode } = useSelector((state: RootState) => state.Mode) |
| 20 | + const { wallet, isDebug, toggleDebug } = useContext(WalletContext) |
| 21 | + const { currency } = useSelector((state: RootState) => state.balance) |
| 22 | + const { federationId } = useSelector((state: RootState) => state.activeFederation) |
| 23 | + const { setLoading } = useContext(LoadingContext); |
| 24 | + const [error, setError] = useState('') |
| 25 | + const navigate = useNavigate() |
| 26 | + |
| 27 | + |
| 28 | + const handleCurrencyChange = (e: React.ChangeEvent<HTMLSelectElement>) => { |
| 29 | + const selectedCurrency = e.target.value; |
| 30 | + dispatch(setCurrency(selectedCurrency)); |
| 31 | + localStorage.setItem('walletCurrency', selectedCurrency) |
| 32 | + }; |
| 33 | + |
| 34 | + const handleToggleLocation = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 35 | + const newValue = e.target.checked; |
| 36 | + if (enabledLocation === true && newValue === false) { |
| 37 | + if (confirm("all the geoLocation data will be removed from your storage")) { |
| 38 | + localStorage.setItem('locationAccess', newValue.toString()); |
| 39 | + localStorage.removeItem('paymentLocations') |
| 40 | + setEnabledLocation(newValue); |
| 41 | + } |
| 42 | + } else { |
| 43 | + setEnabledLocation(newValue); |
| 44 | + localStorage.setItem('locationAccess', newValue.toString()); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + const toggleMode = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 49 | + const newValue = e.target.checked |
| 50 | + dispatch(setMode(newValue)) |
| 51 | + localStorage.setItem('appMode', JSON.stringify(newValue)) |
| 52 | + logger.log("mode toggled") |
| 53 | + } |
| 54 | + |
| 55 | + const handleLeaveFederations = async () => { |
| 56 | + try { |
| 57 | + NProgress.start() |
| 58 | + setLoading(true) |
| 59 | + await wallet.cleanup(); |
| 60 | + logger.log('wallet cleanup called') |
| 61 | + await wallet.waitForOpen().catch(() => { |
| 62 | + logger.log('Wallet is closed'); |
| 63 | + }); |
| 64 | + indexedDB.deleteDatabase(`${localStorage.getItem('walletName')}`); |
| 65 | + localStorage.removeItem('walletName') |
| 66 | + localStorage.removeItem('activeFederation') |
| 67 | + localStorage.removeItem('WalletNostrKeys') |
| 68 | + localStorage.removeItem('ClientRelayKeys') |
| 69 | + localStorage.removeItem('nwcRelays') |
| 70 | + localStorage.removeItem('nwcEnabled') |
| 71 | + if (!wallet.isOpen()) { |
| 72 | + logger.log('wallet open ', wallet.isOpen()) |
| 73 | + navigate('/') |
| 74 | + window.location.reload() |
| 75 | + } |
| 76 | + } catch (err) { |
| 77 | + logger.log("an error occured") |
| 78 | + setError(err instanceof Error ? err.message : String(err)) |
| 79 | + setTimeout(() => { |
| 80 | + setError('') |
| 81 | + }, 3000); |
| 82 | + } finally { |
| 83 | + NProgress.done() |
| 84 | + setLoading(false) |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + const handleDownloadTransactions = async () => { |
| 89 | + // try { |
| 90 | + // NProgress.start() |
| 91 | + // setLoading(true) |
| 92 | + // const transactions = await wallet.federation.listTransactions() |
| 93 | + // if (transactions.length === 0) throw new Error("0 Transactions found") |
| 94 | + // DownloadTransactionsCSV(transactions) |
| 95 | + // } catch (err) { |
| 96 | + // logger.log('an error occured') |
| 97 | + // setError(err instanceof Error ? err.message : String(err)) |
| 98 | + // setTimeout(() => { |
| 99 | + // setError('') |
| 100 | + // }, 3000); |
| 101 | + // } finally { |
| 102 | + // NProgress.done() |
| 103 | + // setLoading(false) |
| 104 | + // } |
| 105 | + } |
| 106 | + |
| 107 | + return ( |
| 108 | + <> |
| 109 | + {error && <Alerts Error={error} Result='' />} |
| 110 | + |
| 111 | + <div className="settings-container"> |
| 112 | + {/* Federation Information Section */} |
| 113 | + {(metaData?.federation_expiry_timestamp || metaData?.pinned_message || metaData?.welcome_message) && ( |
| 114 | + <div className="settings-section"> |
| 115 | + <h2 className="section-title">Federation Information</h2> |
| 116 | + <div className="info-cards"> |
| 117 | + {metaData?.federation_expiry_timestamp && ( |
| 118 | + <div className="info-card"> |
| 119 | + <div className="info-header"> |
| 120 | + <h3>Federation Expiry</h3> |
| 121 | + </div> |
| 122 | + <div className="info-content"> |
| 123 | + <p>{metaData.federation_expiry_timestamp}</p> |
| 124 | + </div> |
| 125 | + </div> |
| 126 | + )} |
| 127 | + |
| 128 | + {metaData?.pinned_message && ( |
| 129 | + <div className="info-card"> |
| 130 | + <div className="info-header"> |
| 131 | + <h3>Pinned Message</h3> |
| 132 | + </div> |
| 133 | + <div className="info-content"> |
| 134 | + <p>{metaData.pinned_message}</p> |
| 135 | + </div> |
| 136 | + </div> |
| 137 | + )} |
| 138 | + |
| 139 | + {metaData?.welcome_message && ( |
| 140 | + <div className="info-card"> |
| 141 | + <div className="info-header"> |
| 142 | + <h3>Welcome Message</h3> |
| 143 | + </div> |
| 144 | + <div className="info-content"> |
| 145 | + <p>{metaData.welcome_message}</p> |
| 146 | + </div> |
| 147 | + </div> |
| 148 | + )} |
| 149 | + </div> |
| 150 | + </div> |
| 151 | + )} |
| 152 | + |
| 153 | + {/* Preferences Section */} |
| 154 | + <div className="settings-section"> |
| 155 | + <h2 className="section-title">Preferences</h2> |
| 156 | + <div className="settings-grid"> |
| 157 | + <div className="setting-item"> |
| 158 | + <div className="setting-info"> |
| 159 | + <h3>Geolocation</h3> |
| 160 | + <p>Fedimint will not track users data. Location will be saved locally.</p> |
| 161 | + </div> |
| 162 | + <div className="setting-control"> |
| 163 | + <label className="toggle-switch"> |
| 164 | + <input |
| 165 | + type="checkbox" |
| 166 | + checked={enabledLocation} |
| 167 | + onChange={handleToggleLocation} |
| 168 | + /> |
| 169 | + <span className="toggle-slider"></span> |
| 170 | + </label> |
| 171 | + </div> |
| 172 | + </div> |
| 173 | + |
| 174 | + <div className="setting-item"> |
| 175 | + <div className="setting-info"> |
| 176 | + <h3>Theme</h3> |
| 177 | + <p>Enable {mode === true ? 'Dark' : 'Light'} mode</p> |
| 178 | + </div> |
| 179 | + <div className="setting-control"> |
| 180 | + <label className="toggle-switch"> |
| 181 | + <input |
| 182 | + type="checkbox" |
| 183 | + checked={mode} |
| 184 | + onChange={toggleMode} |
| 185 | + /> |
| 186 | + <span className="toggle-slider"></span> |
| 187 | + </label> |
| 188 | + </div> |
| 189 | + </div> |
| 190 | + |
| 191 | + <div className="setting-item"> |
| 192 | + <div className="setting-info"> |
| 193 | + <h3>Developer Mode</h3> |
| 194 | + <p>{isDebug === true ? 'Disable' : 'Enable'} Developer mode</p> |
| 195 | + </div> |
| 196 | + <div className="setting-control"> |
| 197 | + <label className="toggle-switch"> |
| 198 | + <input |
| 199 | + type="checkbox" |
| 200 | + checked={isDebug} |
| 201 | + onChange={toggleDebug} |
| 202 | + /> |
| 203 | + <span className="toggle-slider"></span> |
| 204 | + </label> |
| 205 | + </div> |
| 206 | + </div> |
| 207 | + </div> |
| 208 | + </div> |
| 209 | + |
| 210 | + {/* Configuration Section */} |
| 211 | + <div className="settings-section"> |
| 212 | + <h2 className="section-title">Configuration</h2> |
| 213 | + <div className="config-grid"> |
| 214 | + <div className="config-item"> |
| 215 | + <label className="config-label">Display Currency</label> |
| 216 | + <select className="config-select" value={currency} onChange={handleCurrencyChange}> |
| 217 | + <option value={'msat'}>msat</option> |
| 218 | + <option value={'sat'}>sat</option> |
| 219 | + <option value={'usd'}>USD</option> |
| 220 | + <option value={'euro'}>EURO</option> |
| 221 | + </select> |
| 222 | + </div> |
| 223 | + |
| 224 | + <div className="config-item"> |
| 225 | + <label className="config-label">Export Transactions</label> |
| 226 | + <button className="export-btn" onClick={handleDownloadTransactions}> |
| 227 | + <i className="fa-solid fa-download"></i> |
| 228 | + Export |
| 229 | + </button> |
| 230 | + </div> |
| 231 | + </div> |
| 232 | + </div> |
| 233 | + |
| 234 | + {/* Federations Section */} |
| 235 | + <div className="settings-section"> |
| 236 | + <h2 className="section-title">Federations</h2> |
| 237 | + <div className="federation-container"> |
| 238 | + {metaData && ( |
| 239 | + <div className="federation-card"> |
| 240 | + <div className="federation-info"> |
| 241 | + <h3>{metaData.federation_name}</h3> |
| 242 | + <span className="federation-status">Active</span> |
| 243 | + </div> |
| 244 | + <div className="federation-actions"> |
| 245 | + <a href={`/fedimint-web-wallet/federation/${federationId || localStorage.getItem('activeFederation')}`} className="action-btn view-btn"> |
| 246 | + {/* <i className="fa-solid fa-arrow-up-right-from-square"></i> */} |
| 247 | + View |
| 248 | + </a> |
| 249 | + <button className="action-btn leave-btn" onClick={handleLeaveFederations} title="Leave Federation"> |
| 250 | + {/* <i className="fa-solid fa-arrow-right-from-bracket"></i> */} |
| 251 | + Leave |
| 252 | + </button> |
| 253 | + </div> |
| 254 | + </div> |
| 255 | + )} |
| 256 | + </div> |
| 257 | + </div> |
| 258 | + </div> |
| 259 | + </> |
| 260 | + ) |
| 261 | +} |
0 commit comments