discourse/app/assets/javascripts/admin/addon/models/admin-plugin.js
Martin Brennan 4e7a75a7ec
DEV: Single admin plugin page for consistent admin plugin UX (#26024)
This commit adds new plugin show routes (`/admin/plugins/:plugin_id`) as we move
towards every plugin having a consistent UI/landing page.

As part of this, we are introducing a consistent way for plugins
to show an inner sidebar in their config page, via a new plugin
API `register_admin_config_nav_routes`

This accepts an array of links with a label/text, and an
ember route. Once this commit is merged we can start the process
of conforming other plugins to follow this pattern, as well
as supporting a single-page version of this for simpler plugins
that don't require an inner sidebar.

Part of /t/122841 internally
2024-03-13 13:15:12 +10:00

94 lines
2.6 KiB
JavaScript

import { tracked } from "@glimmer/tracking";
import { capitalize } from "@ember/string";
import I18n from "discourse-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.isDiscourseOwned = args.is_discourse_owned;
this.label = args.label;
this.name = args.name;
this.url = args.url;
this.version = args.version;
this.metaUrl = args.meta_url;
this.authors = args.authors;
this.configNavRoutes = args.admin_config_nav_routes;
}
get snakeCaseName() {
return this.name.replaceAll("-", "_");
}
get translatedCategoryName() {
// 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.
return I18n.lookup(`admin.site_settings.categories.${this.snakeCaseName}`);
}
get settingCategoryName() {
if (this.translatedCategoryName) {
return this.snakeCaseName;
}
return "plugins";
}
get nameTitleized() {
// The category name is better in a lot of cases, as it's a human-inputted
// translation, and we can handle things like SAML instead of showing them
// as Saml from discourse-saml. We can fall back to the programmatic version
// though if needed.
let name;
if (this.translatedCategoryName) {
name = this.translatedCategoryName;
} else {
name = this.name
.split("-")
.map((word) => {
return capitalize(word);
})
.join(" ");
}
// Cuts down on repetition.
const discoursePrefix = "Discourse ";
if (name.startsWith(discoursePrefix)) {
name = name.slice(discoursePrefix.length);
}
return name;
}
get author() {
if (this.isOfficial || this.isDiscourseOwned) {
return I18n.t("admin.plugins.author", { author: "Discourse" });
}
return I18n.t("admin.plugins.author", { author: this.authors });
}
get linkUrl() {
return this.metaUrl || this.url;
}
}