FIX: Retry updating webview background color (#28912)

In some very rare cases, the header element doesn't yet have the bg
when this code is run. This PR adds a simple retry mechanism.

No tests because this relies on specific load timing from the browser.
This commit is contained in:
Penar Musaraj 2024-09-16 11:58:36 -04:00 committed by GitHub
parent 7f55e8b2be
commit 501f07ab1f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -4,6 +4,7 @@ import discourseLater from "discourse-common/lib/later";
// Send bg color to webview so iOS status bar matches site theme
export default {
after: "inject-objects",
retryCount: 0,
initialize(owner) {
const caps = owner.lookup("service:capabilities");
@ -15,19 +16,27 @@ export default {
}
},
teardown() {
window
.matchMedia("(prefers-color-scheme: dark)")
.removeEventListener("change", this.updateAppBackground);
updateAppBackground(delay = 500) {
discourseLater(() => {
if (this.headerBgColor()) {
postRNWebviewMessage("headerBg", this.headerBgColor());
} else {
this.retry();
}
}, delay);
},
updateAppBackground() {
discourseLater(() => {
const header = document.querySelector(".d-header-wrap .d-header");
if (header) {
const styles = window.getComputedStyle(header);
postRNWebviewMessage("headerBg", styles.backgroundColor);
}
}, 500);
headerBgColor() {
const header = document.querySelector(".d-header-wrap .d-header");
if (header) {
return window.getComputedStyle(header)?.backgroundColor;
}
},
retry() {
if (this.retryCount < 2) {
this.retryCount++;
this.updateAppBackground(1000);
}
},
};