2023-11-02 08:34:37 +08:00
|
|
|
import Component from "@glimmer/component";
|
|
|
|
import { tracked } from "@glimmer/tracking";
|
|
|
|
import { action } from "@ember/object";
|
2024-03-07 01:05:11 +08:00
|
|
|
import { service } from "@ember/service";
|
2024-01-25 08:45:14 +08:00
|
|
|
import { ADMIN_NAV_MAP } from "discourse/lib/sidebar/admin-nav-map";
|
2023-11-02 08:34:37 +08:00
|
|
|
import {
|
|
|
|
buildAdminSidebar,
|
|
|
|
useAdminNavConfig,
|
2024-01-25 08:45:14 +08:00
|
|
|
} from "discourse/lib/sidebar/admin-sidebar";
|
2023-11-02 08:34:37 +08:00
|
|
|
import { resetPanelSections } from "discourse/lib/sidebar/custom-sections";
|
2024-01-25 08:45:14 +08:00
|
|
|
import { ADMIN_PANEL } from "discourse/lib/sidebar/panels";
|
2023-11-02 08:34:37 +08:00
|
|
|
|
2023-12-18 09:48:25 +08:00
|
|
|
// TODO (martin) (2024-02-01) Remove this experimental UI.
|
2023-11-02 08:34:37 +08:00
|
|
|
export default class AdminConfigAreaSidebarExperiment extends Component {
|
2024-03-14 09:28:08 +08:00
|
|
|
@service adminSidebarStateManager;
|
2023-11-02 08:34:37 +08:00
|
|
|
@service toasts;
|
2023-11-07 11:20:57 +08:00
|
|
|
@service router;
|
2023-11-02 08:34:37 +08:00
|
|
|
@tracked editedNavConfig;
|
|
|
|
|
2023-11-07 11:20:57 +08:00
|
|
|
validRouteNames = new Set();
|
|
|
|
|
2023-11-02 08:34:37 +08:00
|
|
|
get defaultAdminNav() {
|
|
|
|
return JSON.stringify(ADMIN_NAV_MAP, null, 2);
|
|
|
|
}
|
|
|
|
|
2023-11-07 11:20:57 +08:00
|
|
|
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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-11-02 08:34:37 +08:00
|
|
|
@action
|
|
|
|
loadDefaultNavConfig() {
|
2024-03-14 09:28:08 +08:00
|
|
|
const savedConfig = this.adminSidebarStateManager.navConfig;
|
2023-11-02 08:34:37 +08:00
|
|
|
this.editedNavConfig = savedConfig
|
|
|
|
? JSON.stringify(savedConfig, null, 2)
|
|
|
|
: this.defaultAdminNav;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
resetToDefault() {
|
|
|
|
this.editedNavConfig = this.defaultAdminNav;
|
|
|
|
this.#saveConfig(ADMIN_NAV_MAP);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
applyConfig() {
|
|
|
|
let config = null;
|
|
|
|
try {
|
|
|
|
config = JSON.parse(this.editedNavConfig);
|
|
|
|
} catch {
|
|
|
|
this.toasts.error({
|
|
|
|
duration: 3000,
|
|
|
|
data: {
|
|
|
|
message: "There was an error, make sure the structure is valid JSON.",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-07 11:20:57 +08:00
|
|
|
let invalidRoutes = [];
|
|
|
|
config.forEach((section) => {
|
|
|
|
section.links.forEach((link) => {
|
|
|
|
if (!link.route) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.validRouteNames.has(link.route)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-01-12 09:55:26 +08:00
|
|
|
// Using the private `_routerMicrolib` is not ideal, but Ember doesn't provide
|
|
|
|
// any other way for us to easily check for route validity.
|
2023-11-07 11:20:57 +08:00
|
|
|
try {
|
2024-01-24 22:30:03 +08:00
|
|
|
// eslint-disable-next-line ember/no-private-routing-service
|
2023-11-07 11:20:57 +08:00
|
|
|
this.router._router._routerMicrolib.recognizer.handlersFor(
|
|
|
|
link.route
|
|
|
|
);
|
|
|
|
this.validRouteNames.add(link.route);
|
2024-01-12 09:55:26 +08:00
|
|
|
} catch (err) {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.debug("[AdminSidebarExperiment]", err);
|
2023-11-07 11:20:57 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-11-02 08:34:37 +08:00
|
|
|
this.#saveConfig(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
#saveConfig(config) {
|
2024-03-14 09:28:08 +08:00
|
|
|
this.adminSidebarStateManager.navConfig = config;
|
2023-11-02 08:34:37 +08:00
|
|
|
resetPanelSections(
|
|
|
|
ADMIN_PANEL,
|
|
|
|
useAdminNavConfig(config),
|
|
|
|
buildAdminSidebar
|
|
|
|
);
|
|
|
|
this.toasts.success({
|
|
|
|
duration: 3000,
|
|
|
|
data: { message: "Sidebar navigation applied successfully!" },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|