FEATURE: Show warning banner for critical JS deprecations to admins (#25091)

Ported from d95706b25a

This is enabled by default, but can be disabled via the `warn_critical_js_deprecations` hidden site setting.

The `warn_critical_js_deprecations_message` site setting can be used by hosting providers to add a sentence to the warning message (e.g. a date when they will be deploying the Ember 5 upgrade).
This commit is contained in:
David Taylor
2024-01-03 11:41:09 +00:00
committed by GitHub
parent b9f6e6d637
commit 07caa5bc03
5 changed files with 148 additions and 2 deletions

View File

@@ -1,5 +1,6 @@
export default {
initialize(owner) {
owner.lookup("service:client-error-handler");
owner.lookup("service:deprecation-warning-handler");
},
};

View File

@@ -0,0 +1,111 @@
import { registerDeprecationHandler } from "@ember/debug";
import Service, { inject as service } from "@ember/service";
import { addGlobalNotice } from "discourse/components/global-notice";
import identifySource from "discourse/lib/source-identifier";
import { escapeExpression } from "discourse/lib/utilities";
import { registerDeprecationHandler as registerDiscourseDeprecationHandler } from "discourse-common/lib/deprecated";
import { bind } from "discourse-common/utils/decorators";
import I18n from "discourse-i18n";
// Deprecations matching patterns on this list will trigger warnings for admins.
// To avoid 'crying wolf', we should only add values here when we're sure they're
// not being triggered by core or official themes/plugins.
export const CRITICAL_DEPRECATIONS = [
/^discourse.modal-controllers$/,
/^(?!discourse\.)/, // All unsilenced ember deprecations
];
// Deprecation handling APIs don't have any way to unregister handlers, so we set up permenant
// handlers and link them up to the application lifecycle using module-local state.
let handler;
registerDeprecationHandler((message, opts, next) => {
handler?.(message, opts);
return next(message, opts);
});
registerDiscourseDeprecationHandler((message, opts) =>
handler?.(message, opts)
);
export default class DeprecationWarningHandler extends Service {
@service currentUser;
@service siteSettings;
#adminWarned = false;
constructor() {
super(...arguments);
handler = this.handle;
}
willDestroy() {
handler = null;
}
@bind
handle(message, opts) {
const workflowConfigs = window.deprecationWorkflow?.config?.workflow;
const matchingConfig = workflowConfigs.find(
(config) => config.matchId === opts.id
);
if (matchingConfig && matchingConfig.handler === "silence") {
return;
}
const source = identifySource();
if (source?.type === "browser-extension") {
return;
}
this.maybeNotifyAdmin(opts.id, source);
}
maybeNotifyAdmin(id, source) {
if (this.#adminWarned) {
return;
}
if (!this.currentUser?.admin) {
return;
}
if (!this.siteSettings.warn_critical_js_deprecations) {
return;
}
if (CRITICAL_DEPRECATIONS.some((pattern) => pattern.test(id))) {
this.notifyAdmin(id, source);
}
}
notifyAdmin(id, source) {
this.#adminWarned = true;
let notice = I18n.t("critical_deprecation.notice");
if (this.siteSettings.warn_critical_js_deprecations_message) {
notice += " " + this.siteSettings.warn_critical_js_deprecations_message;
}
if (source?.type === "theme") {
notice +=
" " +
I18n.t("critical_deprecation.theme_source", {
name: escapeExpression(source.name),
path: source.path,
});
} else if (source?.type === "plugin") {
notice +=
" " +
I18n.t("critical_deprecation.plugin_source", {
name: escapeExpression(source.name),
});
}
addGlobalNotice(notice, "critical-deprecation", {
dismissable: true,
dismissDuration: moment.duration(1, "day"),
level: "warn",
});
}
}