Skip to content

Commit

Permalink
Offline troubleshoot when server disconnects
Browse files Browse the repository at this point in the history
Summary:
If there server disconnects, we used to show a red box message on the lower left section of the screen. It didn't say much other than the server had disconnected.
If you are aware of what the server is, then you may try to manually restart it.

Instead of doing that, a much better experience is to show the no connection troubleshoot with the button to start the server or with instructions on how to achieve this.

Reviewed By: antonk52

Differential Revision: D48467308

fbshipit-source-id: 0ffded95789c7548d9f1e1a9127409e02e72ab8c
  • Loading branch information
lblasa authored and facebook-github-bot committed Aug 18, 2023
1 parent c1c586d commit 4ff9279
Show file tree
Hide file tree
Showing 7 changed files with 275 additions and 28 deletions.
2 changes: 1 addition & 1 deletion desktop/flipper-ui-browser/src/HMRClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ function showCompileError() {
// Symbolicating compile errors is wasted effort
// because the stack trace is meaningless:
(error as any).preventSymbolication = true;
window.flipperShowError?.(message);
window.flipperShowMessage?.(message);
throw error;
}

Expand Down
5 changes: 3 additions & 2 deletions desktop/flipper-ui-browser/src/global.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ declare global {
debug: boolean;
};

flipperShowError?(error: string): void;
flipperHideError?(): void;
flipperShowMessage?(message: string): void;
flipperHideMessage?(): void;
flipperShowNoConnection?(): void;
}
}
12 changes: 6 additions & 6 deletions desktop/flipper-ui-browser/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ async function start() {
(state: FlipperServerState) => {
switch (state) {
case FlipperServerState.CONNECTING:
window.flipperShowError?.('Connecting to server...');
window.flipperShowMessage?.('Connecting to server...');
break;
case FlipperServerState.CONNECTED:
window?.flipperHideError?.();
window?.flipperHideMessage?.();
break;
case FlipperServerState.DISCONNECTED:
window?.flipperShowError?.('Lost connection to server');
window?.flipperShowNoConnection?.();
break;
}
},
Expand All @@ -78,17 +78,17 @@ async function start() {
// This prevent issues where the render host is referred at module initialisation level,
// but not set yet, which might happen when using normal imports.
// TODO: remove
window.flipperShowError?.('Connected to Flipper Server successfully');
window.flipperShowMessage?.('Connected to Flipper Server successfully');

// @ts-ignore
// eslint-disable-next-line import/no-commonjs
require('flipper-ui-core').startFlipperDesktop(flipperServer);
window.flipperHideError?.();
window.flipperHideMessage?.();
}

start().catch((e) => {
console.error('Failed to start flipper-ui-browser', e);
window.flipperShowError?.('Failed to start flipper-ui-browser: ' + e);
window.flipperShowMessage?.('Failed to start flipper-ui-browser: ' + e);
});

async function initializePWA() {
Expand Down
2 changes: 1 addition & 1 deletion desktop/flipper-ui-browser/src/initializeRenderHost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export function initializeRenderHost(
);
},
restartFlipper() {
window.flipperShowError?.(
window.flipperShowMessage?.(
'Flipper settings have changed, please restart flipper server for the changes to take effect',
);
},
Expand Down
130 changes: 122 additions & 8 deletions desktop/static/index.web.dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
}
</script>
<style>
#loading {
.message {
-webkit-app-region: drag;
z-index: 999999;
position: absolute;
Expand All @@ -39,12 +39,92 @@
text-align: center;
}

.console {
font-family: 'Fira Mono';
width: 600px;
height: 250px;
box-sizing: border-box;
margin: auto;
}

.console header {
border-top-left-radius: 15px;
border-top-right-radius: 15px;
background-color: #9254de;
height: 45px;
line-height: 45px;
text-align: center;
color: white;
}

.console .consolebody {
border-bottom-left-radius: 15px;
border-bottom-right-radius: 15px;
box-sizing: border-box;
padding: 0px 10px;
height: calc(100% - 40px);
overflow: scroll;
background-color: #000;
color: white;
text-align: left;
}

input[type="submit"] {
background-color: #9254de;
color: white;
font-family: system-ui;
font-size: 15px;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
}

input[type="submit"]:hover {
background-color: #722ed1;
}

input[type="submit"]:active {
background-color: #722ed1;
}

#troubleshoot {
display: none;
background-color: white;
}

</style>
</head>

<body>
<div id="troubleshoot" class="message">
<div>
<b>Flipper is not running in the background</b>

<p>It can be started by clicking the button below.</p>
<form action="flipper-launcher://start-server">
<input type="submit" value="Start" />
</form>
<br />
<p>
Flipper will automatically reload once the connection is re-established.
</p>
<p>If is taking a bit longer than it should, you can manually start Flipper.</p>
<p>From the terminal, please run:</p>

<div class="console">
<header>
<p>shell</p>
</header>
<div class="consolebody">
<p>> open -a 'Flipper' --args '--server'</p>
</div>
</div>
</div>
</div>

<div id="root">
<div id="loading">
<div id="loading" class="message">
Connecting...
</div>
</div>
Expand All @@ -56,6 +136,39 @@
let suppressErrors = false;
let connected = false;

// Listen to changes in the network state, reload when online.
// This handles the case when the device is completely offline
// i.e. no network connection.
window.addEventListener('online', () => {
window.location.reload();
});

// Check if the server is responding & reload the page if it is.
// This handles the case when the device is online, but the server
// is offline or misbehaving.
async function checkNetworkAndReload() {
try {
const response = await fetch('.');
if (response.status >= 200 && response.status < 500) {
window.location.reload();
return;
}
} catch {
// Unable to connect to the server, ignore.
}
window.setTimeout(checkNetworkAndReload, 2500);
}

function showNoConnection() {
const root = document.getElementById('root');
root.remove();

const troubleshoot = document.getElementById('troubleshoot');
troubleshoot.style.display = 'flex';

checkNetworkAndReload();
}

const params = new URL(location.href).searchParams;
let token = params.get('token');
if (!token) {
Expand All @@ -73,7 +186,7 @@
if (typeof message.event === 'string') {
switch (message.event) {
case 'hasErrors': {
openError(message.payload);
showMessage(message.payload);
suppressErrors = true;
break;
}
Expand All @@ -90,10 +203,10 @@

socket.addEventListener('error', (e) => {
if (!connected) {
openError('Socket failed to connect. Is the server running? Have you provided a valid authentication token?');
showMessage('Socket failed to connect. Is the server running? Have you provided a valid authentication token?');
}
else {
openError('Socket failed with error.');
showMessage('Socket failed with error.');
}

suppressErrors = true;
Expand All @@ -103,7 +216,7 @@
connected = true;
})

function openError(text) {
function showMessage(text) {
if (suppressErrors) {
return;
}
Expand All @@ -113,7 +226,8 @@
box.textContent = text;
}
}
window.flipperShowError = openError;
window.flipperShowMessage = showMessage;
window.flipperShowNoConnection = showNoConnection;

// load correct theme (n.b. this doesn't handle system value specifically, will assume light in such cases)
try {
Expand All @@ -132,7 +246,7 @@
script.src = window.flipperConfig.entryPoint;

script.onerror = (e) => {
openError('Failed to load entry point. Check Chrome console for more info.');
showMessage('Failed to load entry point. Check Chrome console for more info.');
};

document.body.appendChild(script);
Expand Down
Loading

0 comments on commit 4ff9279

Please sign in to comment.