From 5fc93b95cc1a89f65a0493a5d0afbdbdaeb3739d Mon Sep 17 00:00:00 2001 From: Martin Brennan Date: Mon, 28 Aug 2023 10:48:59 +1000 Subject: [PATCH] FIX: Improve admin plugin list and modernize code (#23256) This commit contains a few improvements: * Use LinkTo instead of a button with a weird action referencing the controller to navigate to the filtered settings for a plugin * Add an AdminPlugin model with tracked properties and use that when toggling the setting on/off and in the templates * Make it so the Settings button for a plugin navigates to the correct site setting category instead of always just going to the generic "plugins" one if possible --- .../addon/components/plugin-commit-hash.hbs | 2 +- .../addon/components/plugin-commit-hash.js | 2 +- .../addon/controllers/admin-plugins-index.js | 11 ++--- .../admin/addon/controllers/admin-plugins.js | 12 +++-- .../admin/addon/models/admin-plugin.js | 46 +++++++++++++++++++ .../admin/addon/routes/admin-plugins.js | 23 ++-------- .../admin/addon/templates/plugins-index.hbs | 23 ++++++---- 7 files changed, 77 insertions(+), 42 deletions(-) create mode 100644 app/assets/javascripts/admin/addon/models/admin-plugin.js diff --git a/app/assets/javascripts/admin/addon/components/plugin-commit-hash.hbs b/app/assets/javascripts/admin/addon/components/plugin-commit-hash.hbs index f9dabfbfba6..8bfb11c40bd 100644 --- a/app/assets/javascripts/admin/addon/components/plugin-commit-hash.hbs +++ b/app/assets/javascripts/admin/addon/components/plugin-commit-hash.hbs @@ -1,6 +1,6 @@ {{#if this.commitHash}} this.routeExists(r.full_location)); + return this.allAdminRoutes.filter((route) => + this.routeExists(route.full_location) + ); } get brokenAdminRoutes() { return this.allAdminRoutes.filter( - (r) => !this.routeExists(r.full_location) + (route) => !this.routeExists(route.full_location) ); } get allAdminRoutes() { return this.model - .filter((p) => p?.enabled) - .map((p) => { - return p.admin_route; + .filter((plugin) => plugin?.enabled) + .map((plugin) => { + return plugin.adminRoute; }) .filter(Boolean); } diff --git a/app/assets/javascripts/admin/addon/models/admin-plugin.js b/app/assets/javascripts/admin/addon/models/admin-plugin.js new file mode 100644 index 00000000000..eac325862e3 --- /dev/null +++ b/app/assets/javascripts/admin/addon/models/admin-plugin.js @@ -0,0 +1,46 @@ +import { tracked } from "@glimmer/tracking"; +import I18n from "I18n"; + +export default class AdminPlugin { + static create(args = {}) { + return new AdminPlugin(args); + } + + @tracked enabled; + + constructor(args = {}) { + this.about = args.about; + this.adminRoute = args.admin_route; + this.commitHash = args.commit_hash; + this.commitUrl = args.commit_url; + this.enabled = args.enabled; + this.enabledSetting = args.enabled_setting; + this.hasSettings = args.has_settings; + this.id = args.id; + this.isOfficial = args.is_official; + this.name = args.name; + this.url = args.url; + this.version = args.version; + } + + get settingCategoryName() { + const snakeCaseName = this.name.replaceAll("-", "_"); + + // We do this because the site setting list is grouped by category, + // with plugins that have their root site setting key defined as `plugins:` + // being grouped under the generic "plugins" category. + // + // If a site setting has defined a proper root key and translated category name, + // we can use that instead to go directly to the setting category. + // + // Over time, no plugins should be missing this data. + const translationAttempt = I18n.lookup( + `admin.site_settings.categories.${snakeCaseName}` + ); + if (translationAttempt) { + return snakeCaseName; + } + + return "plugins"; + } +} diff --git a/app/assets/javascripts/admin/addon/routes/admin-plugins.js b/app/assets/javascripts/admin/addon/routes/admin-plugins.js index 45e2e04314d..ba021be4b93 100644 --- a/app/assets/javascripts/admin/addon/routes/admin-plugins.js +++ b/app/assets/javascripts/admin/addon/routes/admin-plugins.js @@ -1,28 +1,13 @@ -import { action } from "@ember/object"; import Route from "@ember/routing/route"; +import AdminPlugin from "admin/models/admin-plugin"; import { inject as service } from "@ember/service"; export default class AdminPluginsRoute extends Route { @service router; model() { - return this.store.findAll("plugin"); - } - - @action - showSettings(plugin) { - const controller = this.controllerFor("adminSiteSettings"); - this.router - .transitionTo("adminSiteSettingsCategory", "plugins") - .then(() => { - if (plugin) { - // filterContent() is normally on a debounce from typing. - // Because we don't want the default of "All Results", we tell it - // to skip the next debounce. - controller.set("filter", `plugin:${plugin.id}`); - controller.set("_skipBounce", true); - controller.filterContentNow("plugins"); - } - }); + return this.store + .findAll("plugin") + .then((plugins) => plugins.map((plugin) => AdminPlugin.create(plugin))); } } diff --git a/app/assets/javascripts/admin/addon/templates/plugins-index.hbs b/app/assets/javascripts/admin/addon/templates/plugins-index.hbs index 239f2a2f06c..30bcea8a171 100644 --- a/app/assets/javascripts/admin/addon/templates/plugins-index.hbs +++ b/app/assets/javascripts/admin/addon/templates/plugins-index.hbs @@ -15,7 +15,7 @@ {{#each this.model as |plugin|}} - {{#if plugin.is_official}} + {{#if plugin.isOfficial}} {{d-icon "check-circle" title="admin.plugins.official" @@ -47,7 +47,7 @@
{{i18n "admin.plugins.enabled"}}
- {{#if plugin.enabled_setting}} + {{#if plugin.enabledSetting}} {{#if this.currentUser.admin}} - {{#if plugin.has_settings}} - + {{#if plugin.hasSettings}} + + {{d-icon "cog"}} + {{i18n "admin.plugins.change_settings_short"}} + {{/if}} {{/if}}