DEV: Make admin experimental sidebar config more forgiving (#24236)

Followup to b53449eac9,
it was too easy to add broken routes which would break
configuration for the whole site, so now we validate ember
routes on save.
This commit is contained in:
Martin Brennan 2023-11-07 13:20:57 +10:00 committed by GitHub
parent a86833fe91
commit 3fe8cc811c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 1 deletions

View File

@ -14,7 +14,17 @@
or a
<code>href</code>, if you want to link to a specific page but don't know the
Ember route. You can also use the Ember Inspector extension to find route
names, for example for plugin routes which are not auto-generated here.</p>
names, for example for plugin routes which are not auto-generated here.
<br /><br />
<code>routeModels</code>
is an array of values that correspond to parts of the route; for example the
topic route may be
<code>/t/:id</code>, so to get a link to topic with ID 123 you would do
<code>routeModels: [123]</code>.</p>
<p>All configuration options for a section and its links looks like this:</p>
<pre><code>{{this.exampleJson}}</code></pre>
<DButton
@action={{this.resetToDefault}}

View File

@ -13,12 +13,36 @@ import { ADMIN_PANEL } from "discourse/services/sidebar-state";
export default class AdminConfigAreaSidebarExperiment extends Component {
@service adminSidebarExperimentStateManager;
@service toasts;
@service router;
@tracked editedNavConfig;
validRouteNames = new Set();
get defaultAdminNav() {
return JSON.stringify(ADMIN_NAV_MAP, null, 2);
}
get exampleJson() {
return JSON.stringify(
{
name: "section-name",
text: "Section Name",
links: [
{
name: "admin-revamp",
route: "admin-revamp",
routeModels: [123],
text: "Revamp",
href: "https://forum.site.com/t/123",
icon: "rocket",
},
],
},
null,
2
);
}
@action
loadDefaultNavConfig() {
const savedConfig = this.adminSidebarExperimentStateManager.navConfig;
@ -48,6 +72,40 @@ export default class AdminConfigAreaSidebarExperiment extends Component {
return;
}
let invalidRoutes = [];
config.forEach((section) => {
section.links.forEach((link) => {
if (!link.route) {
return;
}
if (this.validRouteNames.has(link.route)) {
return;
}
try {
this.router._router._routerMicrolib.recognizer.handlersFor(
link.route
);
this.validRouteNames.add(link.route);
} catch {
invalidRoutes.push(link.route);
}
});
});
if (invalidRoutes.length) {
this.toasts.error({
duration: 3000,
data: {
message: `There was an error with one or more of the routes provided: ${invalidRoutes.join(
", "
)}`,
},
});
return;
}
this.#saveConfig(config);
}