|
| 1 | +import Auth from "@aws-amplify/auth"; |
| 2 | +import { useEffect, useState } from "react"; |
| 3 | +import { Badge, Card, CardBody, CardHeader, Col, Row } from "reactstrap"; |
| 4 | +import { destroyDb, destroyRemoteDb, getDb, getRemoteDb, setRemoteUser, exportToJson, exportToCsv } from "trace-search"; |
| 5 | + |
| 6 | +function Settings() { |
| 7 | + const [currentUser, setCurrentUser] = useState(null); |
| 8 | + const [localDbInfo, setLocalDbInfo] = useState(null); |
| 9 | + const [remoteDbInfo, setRemoteDbInfo] = useState(null); |
| 10 | + const [currentSettings, setCurrentSettings] = useState(null); |
| 11 | + const [refreshData, setRefreshData] = useState(false); |
| 12 | + |
| 13 | + useEffect(() => { |
| 14 | + (async () => { |
| 15 | + try { |
| 16 | + const user = await Auth.currentUserPoolUser(); |
| 17 | + setCurrentUser(user); |
| 18 | + |
| 19 | + await setRemoteUser(user); |
| 20 | + const db = await getRemoteDb(); |
| 21 | + const dbInfo = await db.info(); |
| 22 | + |
| 23 | + setRemoteDbInfo(dbInfo); |
| 24 | + |
| 25 | + } catch (e) { |
| 26 | + console.error(e); |
| 27 | + } |
| 28 | + |
| 29 | + try { |
| 30 | + const db = await getDb(); |
| 31 | + const dbInfo = await db.info(); |
| 32 | + const settings = await db.get('settings'); |
| 33 | + |
| 34 | + console.log(dbInfo); |
| 35 | + console.log(settings); |
| 36 | + |
| 37 | + setLocalDbInfo(dbInfo); |
| 38 | + setCurrentSettings(settings); |
| 39 | + } catch (e) { |
| 40 | + console.error(e); |
| 41 | + } |
| 42 | + })(); |
| 43 | + }, [refreshData]); |
| 44 | + |
| 45 | + const isChrome = window.navigator.userAgent.includes('Chrome'); |
| 46 | + const isFirefox = window.navigator.userAgent.includes('Firefox'); |
| 47 | + |
| 48 | + let extensionUrl = 'https://github.com/TRACE-Digital/TRACE-ext'; |
| 49 | + if (isChrome) extensionUrl = 'https://chrome.google.com/webstore/detail/trace/klhmocgplcpemcdfeefpaikihedmikgk'; |
| 50 | + if (isFirefox) extensionUrl = 'https://addons.mozilla.org/en-US/firefox/addon/trace-digital/'; |
| 51 | + |
| 52 | + const downloadJson = async () => { |
| 53 | + const text = await exportToJson(); |
| 54 | + openBlob(text, '.json', 'application/json'); |
| 55 | + }; |
| 56 | + |
| 57 | + const downloadCsv = async () => { |
| 58 | + const text = await exportToCsv(); |
| 59 | + openBlob(text, '.csv', 'text/plain'); |
| 60 | + }; |
| 61 | + |
| 62 | + const openBlob = (text, extension, contentType) => { |
| 63 | + const fileName = `trace-${new Date().toJSON()}${extension}`; |
| 64 | + const props = { type: contentType }; |
| 65 | + |
| 66 | + let blob; |
| 67 | + try { |
| 68 | + // Use File in newer browsers so we can specify a name |
| 69 | + blob = new File([text], fileName, props); |
| 70 | + } catch (e) { |
| 71 | + console.log(e); |
| 72 | + blob = new Blob([text], props); |
| 73 | + } |
| 74 | + |
| 75 | + const url = URL.createObjectURL(blob); |
| 76 | + console.log(url); |
| 77 | + |
| 78 | + // Trigger the download |
| 79 | + const link = document.createElement('a'); |
| 80 | + link.href = url; |
| 81 | + link.download = fileName; |
| 82 | + link.target = 'blank'; |
| 83 | + link.click(); |
| 84 | + |
| 85 | + // Also show the content |
| 86 | + window.open(url, 'blank'); |
| 87 | + }; |
| 88 | + |
| 89 | + const closeAccount = async () => { |
| 90 | + if (!window.confirm('Are you sure you want to permanently close your account? ALL account data will be deleted.')) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + await destroyDb(); |
| 95 | + await destroyRemoteDb(); |
| 96 | + |
| 97 | + try { |
| 98 | + const remoteDb = await getRemoteDb(); |
| 99 | + const settings = { |
| 100 | + _id: 'settings', |
| 101 | + accountClosed: true |
| 102 | + }; |
| 103 | + await remoteDb.put(settings); |
| 104 | + } catch (e) { |
| 105 | + alert(`Could not close account!\n${e}`); |
| 106 | + } |
| 107 | + |
| 108 | + setRefreshData(prev => !prev); |
| 109 | + |
| 110 | + await Auth.signOut(); |
| 111 | + window.location.href = '/login'; |
| 112 | + }; |
| 113 | + |
| 114 | + const deleteLocalData = async () => { |
| 115 | + if (!confirmDelete()) { |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + try { |
| 120 | + await destroyDb(); |
| 121 | + } catch (e) { |
| 122 | + alert(`Could not delete local data!\n${e}`); |
| 123 | + } |
| 124 | + |
| 125 | + setRefreshData(prev => !prev); |
| 126 | + }; |
| 127 | + |
| 128 | + const deleteRemoteData = async () => { |
| 129 | + if (!confirmDelete()) { |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + try { |
| 134 | + if (currentUser) { |
| 135 | + await destroyRemoteDb(); |
| 136 | + } |
| 137 | + } catch (e) { |
| 138 | + alert(`Could not delete remote data!\n${e}`); |
| 139 | + } |
| 140 | + |
| 141 | + setRefreshData(prev => !prev); |
| 142 | + }; |
| 143 | + |
| 144 | + const confirmDelete = () => { |
| 145 | + const confirmation = window.confirm('Are you sure? This action is irreversible.'); |
| 146 | + return confirmation; |
| 147 | + } |
| 148 | + |
| 149 | + return ( |
| 150 | + <div className="content" style={{ lineHeight: '2.3' }}> |
| 151 | + <div className="header"> |
| 152 | + <h1>Settings</h1> |
| 153 | + </div> |
| 154 | + <Card> |
| 155 | + <CardHeader>TRACE</CardHeader> |
| 156 | + <CardBody> |
| 157 | + <Row> |
| 158 | + <Col>Account name:</Col> |
| 159 | + <Col>{currentUser ? currentUser.attributes.email : 'None (local only)'}</Col> |
| 160 | + </Row> |
| 161 | + <Row> |
| 162 | + <Col>Account ID:</Col> |
| 163 | + <Col>{currentUser ? currentUser.attributes.sub : 'None (local only)'}</Col> |
| 164 | + </Row> |
| 165 | + <Row> |
| 166 | + <Col>Version:</Col> |
| 167 | + <Col>{currentSettings?.version}</Col> |
| 168 | + </Row> |
| 169 | + <Row> |
| 170 | + <Col>Browser extension:</Col> |
| 171 | + <Col> |
| 172 | + <a href={extensionUrl} target='blank'> |
| 173 | + {window.__TRACE_EXTENSION_HOOK__ ? |
| 174 | + 'v' + window.__TRACE_EXTENSION_HOOK__.getVersionStr() : |
| 175 | + 'Install the extension' |
| 176 | + } |
| 177 | + </a> |
| 178 | + </Col> |
| 179 | + </Row> |
| 180 | + </CardBody> |
| 181 | + </Card> |
| 182 | + |
| 183 | + <Card> |
| 184 | + <CardHeader>Your Data</CardHeader> |
| 185 | + <CardBody> |
| 186 | + <Row> |
| 187 | + <Col>Local data:</Col> |
| 188 | + <Col> |
| 189 | + {localDbInfo && localDbInfo.doc_count + ' document' + (localDbInfo.doc_count === 1 ? '' : 's')} |
| 190 | + </Col> |
| 191 | + </Row> |
| 192 | + <Row> |
| 193 | + <Col>Sync enabled:</Col> |
| 194 | + <Col> |
| 195 | + {currentSettings?.syncEnabled ? 'Yes' : 'No'} |
| 196 | + </Col> |
| 197 | + </Row> |
| 198 | + <Row> |
| 199 | + <Col>Remote data:</Col> |
| 200 | + <Col> |
| 201 | + {remoteDbInfo && remoteDbInfo.doc_count + ' document' + (remoteDbInfo.doc_count === 1 ? '' : 's')} |
| 202 | + </Col> |
| 203 | + </Row> |
| 204 | + <Row> |
| 205 | + <Col>Export:</Col> |
| 206 | + <Col> |
| 207 | + <Badge href="#" color="info" onClick={downloadJson}>Export to JSON</Badge> |
| 208 | + <Badge href="#" color="info" onClick={downloadCsv}>Export to CSV</Badge> |
| 209 | + </Col> |
| 210 | + </Row> |
| 211 | + </CardBody> |
| 212 | + </Card> |
| 213 | + |
| 214 | + <Card> |
| 215 | + <CardHeader>Danger Zone</CardHeader> |
| 216 | + <CardBody> |
| 217 | + <Row> |
| 218 | + <Col>Local data:</Col> |
| 219 | + <Col> |
| 220 | + <Badge href="#" color="danger" onClick={deleteLocalData}>Delete local data</Badge> |
| 221 | + </Col> |
| 222 | + </Row> |
| 223 | + {currentUser && |
| 224 | + <> |
| 225 | + <Row> |
| 226 | + <Col>Remote data:</Col> |
| 227 | + <Col> |
| 228 | + <Badge href="#" color="danger" onClick={deleteRemoteData}>Delete remote data</Badge> |
| 229 | + </Col> |
| 230 | + </Row> |
| 231 | + <Row> |
| 232 | + <Col>Close your account:</Col> |
| 233 | + <Col> |
| 234 | + <Badge href="#" color="danger" onClick={closeAccount}>Close account</Badge> |
| 235 | + </Col> |
| 236 | + </Row> |
| 237 | + </> |
| 238 | + } |
| 239 | + </CardBody> |
| 240 | + </Card> |
| 241 | + </div> |
| 242 | + ); |
| 243 | +} |
| 244 | + |
| 245 | +export default Settings; |
0 commit comments