diff --git a/lib/gui/app/components/settings/settings.tsx b/lib/gui/app/components/settings/settings.tsx index 52e6c418..0fc132e1 100644 --- a/lib/gui/app/components/settings/settings.tsx +++ b/lib/gui/app/components/settings/settings.tsx @@ -19,15 +19,15 @@ import * as _ from 'lodash'; import * as os from 'os'; import * as React from 'react'; import { Box, Button, Flex, Checkbox, Txt } from 'rendition'; -import { faTimes } from '@fortawesome/free-solid-svg-icons' - import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' +import { faTimes } from '@fortawesome/free-solid-svg-icons'; +import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { version, packageType } from '../../../../../package.json'; import * as settings from '../../models/settings'; import * as analytics from '../../modules/analytics'; import { open as openExternal } from '../../os/open-external/services/open-external'; import { Modal } from '../../styled-components'; -import { unlinkSync, readFileSync } from 'fs' +import { unlinkSync, readFileSync } from 'fs'; const platform = os.platform(); @@ -71,11 +71,13 @@ interface SettingsModalProps { export function SettingsModal({ toggleModal }: SettingsModalProps): any { const [settingsList, setCurrentSettingsList] = React.useState([]); - const [showDiagScreen, setShowDiagScreen] = React.useState(false); - const [diagApiIsUp, setDiagApiIsUp] = React.useState(false); - const [showDiagButton, setShowDiagButton] = React.useState(false); - const [currentSettings, setCurrentSettings] = React.useState<_.Dictionary>({}); - const [errorMessage, setErrorMessage] = React.useState(""); + const [showDiagScreen, setShowDiagScreen] = React.useState(false); + const [diagApiIsUp, setDiagApiIsUp] = React.useState(false); + const [showDiagButton, setShowDiagButton] = React.useState(false); + const [currentSettings, setCurrentSettings] = React.useState< + _.Dictionary + >({}); + const [errorMessage, setErrorMessage] = React.useState(''); let diagCount = 0; React.useEffect(() => { @@ -85,7 +87,7 @@ export function SettingsModal({ toggleModal }: SettingsModalProps): any { } })(); }); - + React.useEffect(() => { (async () => { if (_.isEmpty(currentSettings)) { @@ -95,12 +97,12 @@ export function SettingsModal({ toggleModal }: SettingsModalProps): any { }); React.useEffect(() => { - (async() => { + (async () => { try { - let result = await fetch('http://localhost:3000/api/ping') + const result = await fetch('http://localhost:3000/api/ping'); if (result.ok) { - setShowDiagButton(true) - setDiagApiIsUp(true) + setShowDiagButton(true); + setDiagApiIsUp(true); } } catch { // no diag container @@ -130,66 +132,79 @@ export function SettingsModal({ toggleModal }: SettingsModalProps): any { }; const closeDiagFrame = () => { - setShowDiagScreen(false); - } + setShowDiagScreen(false); + }; const openDiagFrame = () => { - setShowDiagScreen(true); - } + setShowDiagScreen(true); + }; const prepareDiag = () => { if (++diagCount > 5) { setShowDiagButton(true); } - } + }; const startDiag = async () => { - unlinkSync("/usr/src/diag-data/startup.lock"); + unlinkSync('/usr/src/diag-data/startup.lock'); - const supUrl: string = readFileSync("/usr/src/diag-data/start.url", {encoding:'utf8', flag:'r'}) - const startRes = await fetch(supUrl, { - method: "POST", + const supUrl: string = readFileSync('/usr/src/diag-data/start.url', { + encoding: 'utf8', + flag: 'r', + }); + const startRes = await fetch(supUrl, { + method: 'POST', body: JSON.stringify({ serviceName: 'diag-runner', force: true }), headers: { - 'Content-Type': 'application/json' - } - }) + 'Content-Type': 'application/json', + }, + }); if (startRes.ok) { // good } else { - setErrorMessage(`${errorMessage} :: ${startRes.statusText}`) + setErrorMessage(`${errorMessage} :: ${startRes.statusText}`); } - } + }; const removeDiag = async () => { - setErrorMessage(""); + setErrorMessage(''); try { - const supervisorUrl = await (await fetch(`http://localhost:3000/api/supervisor/url`)).text() - const supervisorApiKey = await (await fetch(`http://localhost:3000/api/supervisor/apiKey`)).text() - const appId = await (await fetch(`http://localhost:3000/api/supervisor/appid`)).text() - const createLock = await fetch(`http://localhost:3000/api/supervisor/createlock`) - - const stopRes = await fetch(`${supervisorUrl}/v2/applications/${appId}/stop-service?apikey=${supervisorApiKey}`, { - method: "POST", - body: JSON.stringify({ serviceName: 'diag-runner', force: true }), - headers: { - 'Content-Type': 'application/json' - } - }) + const supervisorUrl = await ( + await fetch(`http://localhost:3000/api/supervisor/url`) + ).text(); + const supervisorApiKey = await ( + await fetch(`http://localhost:3000/api/supervisor/apiKey`) + ).text(); + const appId = await ( + await fetch(`http://localhost:3000/api/supervisor/appid`) + ).text(); + const createLock = await fetch( + `http://localhost:3000/api/supervisor/createlock`, + ); + + const stopRes = await fetch( + `${supervisorUrl}/v2/applications/${appId}/stop-service?apikey=${supervisorApiKey}`, + { + method: 'POST', + body: JSON.stringify({ serviceName: 'diag-runner', force: true }), + headers: { + 'Content-Type': 'application/json', + }, + }, + ); if (!stopRes.ok) { - setErrorMessage(`Stop call failed | ${stopRes.statusText}`) + setErrorMessage(`Stop call failed | ${stopRes.statusText}`); } if (!createLock.ok) { - setErrorMessage(`${errorMessage} :: Create lock file failed :: `) + setErrorMessage(`${errorMessage} :: Create lock file failed :: `); } - } catch (err) { - setErrorMessage(err) + setErrorMessage(err); } - } + }; return ( { + onClick={() => { openExternal( 'https://github.com/balena-io/etcher/blob/master/CHANGELOG.md', ); prepareDiag(); - } - } + }} > - {version} + {version} - {showDiagButton ? - {diagApiIsUp ? - <> - - - : - <> - - - } - {errorMessage} - : <>} + {showDiagButton ? ( + + {diagApiIsUp ? ( + <> + + + + ) : ( + <> + + + )} + {errorMessage} + + ) : ( + <> + )} - {showDiagScreen ? <> -