Skip to content

Commit f412a8c

Browse files
authored
Backport changes from Pro: Remove auto redirect hostname (#1947)
This PR backports the changes from https://github.com/tiny-pilot/tinypilot-pro/pull/1854, Remove auto redirect hostname – see https://github.com/tiny-pilot/tinypilot-pro/issues/1844 for details. <a data-ca-tag href="https://codeapprove.com/pr/tiny-pilot/tinypilot/1947"><img src="https://codeapprove.com/external/github-tag-allbg.png" alt="Review on CodeApprove" /></a>
1 parent 589d236 commit f412a8c

1 file changed

Lines changed: 30 additions & 48 deletions

File tree

app/templates/custom-elements/change-hostname-dialog.html

Lines changed: 30 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#initializing,
66
#prompt,
7-
#changing {
7+
#success {
88
display: none;
99
}
1010

@@ -16,7 +16,7 @@
1616
display: block;
1717
}
1818

19-
:host([state="changing"]) #changing {
19+
:host([state="success"]) #success {
2020
display: block;
2121
}
2222

@@ -68,28 +68,40 @@ <h3>Change Hostname</h3>
6868
<button id="cancel-hostname-change" type="button">Close</button>
6969
</div>
7070

71-
<div id="changing">
72-
<h3>Changing Hostname</h3>
73-
<p>
74-
Waiting for TinyPilot to reboot at
75-
<a href="" id="future-location"><!-- Filled programmatically --></a>
76-
<br />
77-
You will be redirected automatically.
78-
</p>
79-
<progress-spinner></progress-spinner>
71+
<div id="success">
72+
<h3>Hostname Changed</h3>
73+
<div>
74+
<p>
75+
TinyPilot is restarting. After it’s restarted, open TinyPilot at its new
76+
URL:
77+
</p>
78+
<p>
79+
<a href="" id="future-location" target="_blank"
80+
><!-- Filled programmatically --></a
81+
>
82+
</p>
83+
<p>
84+
<inline-message show="" variant="info">
85+
<strong>Note:</strong> This process usually takes about 30 seconds. If
86+
the link does not work yet, wait a few seconds and try again.
87+
</inline-message>
88+
</p>
89+
<!-- We don’t show a “Close” button here, because the user is supposed to
90+
navigate to the new location, as TinyPilot isn’t reachable anymore
91+
under the now outdated hostname of the current browser tab. -->
92+
</div>
8093
</div>
8194
</template>
8295

8396
<script type="module">
84-
import { poll } from "/js/poll.js";
8597
import {
8698
DialogClosedEvent,
8799
DialogFailedEvent,
88100
DialogCloseStateChangedEvent,
101+
DialogVariantChangedEvent,
89102
} from "/js/events.js";
90103
import {
91104
changeHostname,
92-
checkStatus,
93105
determineHostname,
94106
shutdown,
95107
} from "/js/controllers.js";
@@ -104,10 +116,10 @@ <h3>Changing Hostname</h3>
104116
_states = {
105117
INITIALIZING: "initializing",
106118
PROMPT: "prompt",
107-
CHANGING: "changing",
119+
SUCCESS: "success",
108120
};
109121
_statesWithoutDialogClose = new Set([
110-
this._states.CHANGING,
122+
this._states.SUCCESS,
111123
this._states.INITIALIZING,
112124
]);
113125

@@ -165,6 +177,7 @@ <h3>Changing Hostname</h3>
165177
}
166178

167179
_initialize() {
180+
this.dispatchEvent(new DialogVariantChangedEvent("default"));
168181
this._elements.inputError.hide();
169182
this._state = this._states.INITIALIZING;
170183
determineHostname()
@@ -208,8 +221,8 @@ <h3>Changing Hostname</h3>
208221
.then((redirectURL) => {
209222
this._elements.futureLocation.innerText = redirectURL;
210223
this._elements.futureLocation.href = redirectURL;
211-
this._state = this._states.CHANGING;
212-
return this._waitForReboot(redirectURL);
224+
this.dispatchEvent(new DialogVariantChangedEvent("success"));
225+
this._state = this._states.SUCCESS;
213226
})
214227
.catch((error) => {
215228
if (error.code === "INVALID_HOSTNAME") {
@@ -227,37 +240,6 @@ <h3>Changing Hostname</h3>
227240
);
228241
});
229242
}
230-
231-
_waitForReboot(futureLocation) {
232-
// Try to reach the future location. Note:
233-
// - The timeout behaviour of `fetch` is browser-dependent.
234-
// - It’s not possible to tell exactly when the new hostname will
235-
// actually be reachable, because of DNS propagation and caching.
236-
// - The `status` endpoint is the only one that allows requests
237-
// from everywhere (due to CORS), so it’s safe to request from
238-
// the “old” location.
239-
return poll({
240-
fn: () => checkStatus(futureLocation),
241-
validate: (isUpAndRunning) => isUpAndRunning === true,
242-
interval: 3 * 1000,
243-
timeout: 180 * 1000,
244-
})
245-
.then(() => {
246-
window.location = futureLocation;
247-
})
248-
.catch((error) => {
249-
this.dispatchEvent(
250-
new DialogFailedEvent({
251-
title: "Failed to Redirect",
252-
message:
253-
"Cannot reach TinyPilot under the new hostname. The device " +
254-
"may have failed to reboot, or your browser is failing to " +
255-
"resolve the new hostname.",
256-
details: error,
257-
})
258-
);
259-
});
260-
}
261243
}
262244
);
263245
})();

0 commit comments

Comments
 (0)