Skip to content

A platform for community discussion. Free, open, simple.

UX: Show keyboard shortcut on full admin search page (#31646)

Martin Brennan60607aa2314a
diff --git a/app/assets/javascripts/admin/addon/controllers/admin-search-index.js b/app/assets/javascripts/admin/addon/controllers/admin-search-index.js
index 7828695df32..384229ef80d 100644
--- a/app/assets/javascripts/admin/addon/controllers/admin-search-index.js
+++ b/app/assets/javascripts/admin/addon/controllers/admin-search-index.js
@@ -1,5 +1,11 @@
import Controller from "@ember/controller";
+import { PLATFORM_KEY_MODIFIER } from "discourse/lib/keyboard-shortcuts";
+import { translateModKey } from "discourse/lib/utilities";
export default class AdminSearchIndexController extends Controller {
queryParams = ["filter"];
+
+ get shortcutHTML() {
+ return `<kbd>${translateModKey(PLATFORM_KEY_MODIFIER)}</kbd> + <kbd>/</kbd>`;
+ }
}
diff --git a/app/assets/javascripts/admin/addon/services/admin-search-data-source.js b/app/assets/javascripts/admin/addon/services/admin-search-data-source.js
index 8aba3f0862b..aec3f1847a0 100644
--- a/app/assets/javascripts/admin/addon/services/admin-search-data-source.js
+++ b/app/assets/javascripts/admin/addon/services/admin-search-data-source.js
@@ -15,16 +15,200 @@ const MIN_FILTER_LENGTH = 2;
const MAX_TYPE_RESULT_COUNT_LOW = 15;
const MAX_TYPE_RESULT_COUNT_HIGH = 50;
+function labelOrText(obj, fallback = "") {
+ return obj.text || (obj.label ? i18n(obj.label) : fallback);
+}
+
+function buildKeywords(...keywords) {
+ return keywords
+ .map((kw) => {
+ if (Array.isArray(kw)) {
+ return kw.join(" ");
+ }
+ return kw;
+ })
+ .join(" ")
+ .toLowerCase();
+}
+
+export class PageLinkFormatter {
+ /**
+ * @param {DiscourseRouter} router - The ember router service
+ * @param {Object} navMapSection - The section of the admin nav map that the link belongs to,
+ * used for the parent label.
+ * @param {Object} link - The link object from the admin nav map, with name/route/label etc.
+ * @param {String} parentLabel - The parent label of the link, if any, this should be translated.
+ */
+ constructor(router, navMapSection, link, parentLabel = null) {
+ this.router = router;
+ this.navMapSection = navMapSection;
+ this.link = link;
+ this.parentLabel = parentLabel;
+ }
+
+ format() {
+ let url;
+ if (this.link.route) {
+ if (this.link.routeModels) {
+ url = this.router.urlFor(this.link.route, ...this.link.routeModels);
+ } else {
+ url = this.router.urlFor(this.link.route);
+ }
+ } else if (this.link.href) {
+ url = getURL(this.link.href);
+ }
+
+ const sectionLabel = labelOrText(this.navMapSection);
+ const linkLabel = labelOrText(this.link);
+
+ let label;
+ if (this.parentLabel) {
+ label = sectionLabel;
+ if (sectionLabel) {
+ label += ` ${SEPARATOR} `;
+ }
+ label += `${this.parentLabel} ${SEPARATOR} ${linkLabel}`;
+ } else {
+ label = sectionLabel + (sectionLabel ? ` ${SEPARATOR} ` : "") + linkLabel;
+ }
+
+ let keywords = this.link.keywords
+ ? i18n(this.link.keywords).toLowerCase().replaceAll("|", " ")
+ : "";
+ const description = this.link.description
+ ? this.link.description.includes(" ")
+ ? this.link.description
+ : i18n(this.link.description)
+ : "";
+
+ keywords = buildKeywords(
+ keywords,
+ url,
+ label.replace(SEPARATOR, "").toLowerCase().replace(/ +/g, " "),
+ description
+ );
+
+ return { url, label, keywords, description };
+ }
+}
+
+export class SettingLinkFormatter {
+ /**
+ * @param {DiscourseRouter} router - The ember router service
+ * @param {Object} setting - The setting object from the site settings API
+ * @param {Object} plugins - The plugins object built from the visible plugins in preload store
+ * @param {Object} settingPageMap - The setting page map object with categories and areas.
+ * This points to the URL of the relevant setting page
+ */
+ constructor(router, setting, plugins, settingPageMap) {
+ this.router = router;
+ this.setting = setting;
+ this.plugins = plugins;
+ this.settingPageMap = settingPageMap;
+ this.settingPluginNames = {};
+ }
+
+ format() {
+ if (this.setting.plugin) {
+ if (!this.settingPluginNames[this.setting.plugin]) {
+ this.settingPluginNames[this.setting.plugin] =
+ this.setting.plugin.replaceAll("_", "-");
+ }
+ }
+
+ const [rootLabel, fullLabel] = this.buildLabel();
+ const url = this.buildURL();
+
+ const keywords = buildKeywords(
+ this.setting.setting,
+ humanizedSettingName(this.setting.setting),
+ this.setting.description,
+ this.setting.keywords,
+ rootLabel
+ );
+
+ return {
+ label: fullLabel,
+ description: this.setting.description,
+ url,
+ keywords,
+ };
+ }
+
+ buildLabel() {
+ let rootLabel;
+
+ if (this.setting.plugin) {
+ const plugin = this.plugins[this.settingPluginNames[this.setting.plugin]];
+ if (plugin) {
+ rootLabel = plugin.admin_route?.label
+ ? i18n(plugin.admin_route?.label)
+ : i18n("admin.plugins.title");
+ } else {
+ rootLabel = i18n("admin.plugins.title");
+ }
+ } else if (this.setting.primary_area) {
+ rootLabel =
+ I18n.lookup(`admin.config.${this.setting.primary_area}.title`) ||
+ i18n(`admin.site_settings.categories.${this.setting.category}`);
+ } else {
+ rootLabel = i18n(
+ `admin.site_settings.categories.${this.setting.category}`
+ );
+ }
+
+ return [
+ rootLabel,
+ `${rootLabel} ${SEPARATOR} ${humanizedSettingName(this.setting.setting)}`,
+ ];
+ }
+
+ buildURL() {
+ // TODO (martin) These URLs will need to change eventually to anchors
+ // to focus/highlight on a specific element on the page, for now though the filter is fine.
+ let url;
+ if (this.setting.plugin) {
+ const plugin = this.plugins[this.settingPluginNames[this.setting.plugin]];
+ if (plugin) {
+ url = plugin.admin_route.use_new_show_route
+ ? this.router.urlFor(
+ `adminPlugins.show.settings`,
+ plugin.admin_route.location,
+ { queryParams: { filter: this.setting.setting } }
+ )
+ : this.router.urlFor(`adminPlugins.${plugin.admin_route.location}`);
+ } else {
+ url = getURL(
+ `/admin/site_settings/category/all_results?filter=${this.setting.setting}`
+ );
+ }
+ } else if (this.settingPageMap.areas[this.setting.primary_area]) {
+ url =
+ this.settingPageMap.areas[this.setting.primary_area] +
+ `?filter=${this.setting.setting}`;
+ } else if (this.settingPageMap.categories[this.setting.category]) {
+ url =
+ this.settingPageMap.categories[this.setting.category] +
+ `?filter=${this.setting.setting}`;
+ } else {
+ url = getURL(
+ `/admin/site_settings/category/all_results?filter=${this.setting.setting}`
+ );
+ }
+ return url;
+ }
+}
+
export default class AdminSearchDataSource extends Service {
@service router;
@service siteSettings;
plugins = {};
- pageMapItems = [];
- settingMapItems = [];
- themeMapItems = [];
- componentMapItems = [];
- reportMapItems = [];
+ pageDataSourceItems = [];
+ settingDataSourceItems = [];
+ themeDataSourceItems = [];
+ componentDataSourceItems = [];
+ reportDataSourceItems = [];
settingPageMap = {
categories: {},
areas: {},
@@ -36,23 +220,23 @@ export default class AdminSearchDataSource extends Service {
}
async buildMap() {
- if (this._mapCached) {
+ if (this.isLoaded) {
return;
}
- ADMIN_NAV_MAP.forEach((mapItem) => {
- mapItem.links.forEach((link) => {
- let parentLabel = this.#addPageLink(mapItem, link);
+ ADMIN_NAV_MAP.forEach((navMapSection) => {
+ navMapSection.links.forEach((link) => {
+ let parentLabel = this.#addPageLink(navMapSection, link);
link.links?.forEach((subLink) => {
- this.#addPageLink(mapItem, subLink, parentLabel);
+ this.#addPageLink(navMapSection, subLink, parentLabel);
});
});
});
// TODO (martin) Handle plugin enabling/disabling via MessageBus for this
// and the setting list?
- (PreloadStore.get("visiblePlugins") || {}).forEach((plugin) => {
+ (PreloadStore.get("visiblePlugins") || []).forEach((plugin) => {
if (
plugin.admin_route &&
plugin.enabled &&
@@ -89,14 +273,14 @@ export default class AdminSearchDataSource extends Service {
opts.types.forEach((type) => {
let typeItemCount = 0;
- this[`${type}MapItems`].forEach((mapItem) => {
+ this[`${type}DataSourceItems`].forEach((dataSourceItem) => {
// TODO (martin) There is likely a much better way of doing this matching
// that will support fuzzy searches, for now let's go with the most basic thing.
if (
- mapItem.keywords.match(escapedFilterRegExp) &&
+ dataSourceItem.keywords.match(escapedFilterRegExp) &&
typeItemCount <= perTypeLimit
) {
- filteredResults.push(mapItem);
+ filteredResults.push(dataSourceItem);
typeItemCount++;
}
});
@@ -105,148 +289,57 @@ export default class AdminSearchDataSource extends Service {
return filteredResults;
}
- #addPageLink(mapItem, link, parentLabel = "") {
- let url;
- if (link.route) {
- if (link.routeModels) {
- url = this.router.urlFor(link.route, ...link.routeModels);
- } else {
- url = this.router.urlFor(link.route);
- }
- } else if (link.href) {
- url = getURL(link.href);
- }
-
- const mapItemLabel = this.#labelOrText(mapItem);
- const linkLabel = this.#labelOrText(link);
-
- let label;
- if (parentLabel) {
- label = mapItemLabel;
- if (mapItemLabel) {
- label += ` ${SEPARATOR} `;
- }
- label += `${parentLabel} ${SEPARATOR} ${linkLabel}`;
- } else {
- label = mapItemLabel + (mapItemLabel ? ` ${SEPARATOR} ` : "") + linkLabel;
- }
-
- if (link.settings_area) {
+ #addPageLink(navMapSection, link, parentLabel = "") {
+ const formattedPageLink = new PageLinkFormatter(
+ this.router,
+ navMapSection,
+ link,
+ parentLabel
+ ).format();
+
+ // Cache the setting area + category URLs for later use
+ // when building the setting list via #processSettings.
+ if (link.settings_area && !this.settingPageMap.areas[this.settings_area]) {
this.settingPageMap.areas[link.settings_area] = link.multi_tabbed
- ? `${url}/settings`
- : url;
+ ? `${formattedPageLink.url}/settings`
+ : formattedPageLink.url;
}
- if (link.settings_category) {
+ if (
+ link.settings_category &&
+ !this.settingPageMap.categories[link.settings_category]
+ ) {
this.settingPageMap.categories[link.settings_category] = link.multi_tabbed
- ? `${url}/settings`
- : url;
+ ? `${formattedPageLink.url}/settings`
+ : formattedPageLink.url;
}
- const linkKeywords = link.keywords
- ? i18n(link.keywords).toLowerCase().replaceAll("|", " ")
- : "";
- const linkDescription = link.description
- ? link.description.includes(" ")
- ? link.description
- : i18n(link.description)
- : "";
-
- this.pageMapItems.push({
- label,
- url,
- keywords: this.#buildKeywords(
- linkKeywords,
- url,
- label.replace(SEPARATOR, "").toLowerCase().replace(/ +/g, " "),
- linkDescription
- ),
+ this.pageDataSourceItems.push({
+ label: formattedPageLink.label,
+ url: formattedPageLink.url,
+ keywords: formattedPageLink.keywords,
type: "page",
icon: link.icon,
- description: linkDescription,
+ description: formattedPageLink.description,
});
- return linkLabel;
+ return formattedPageLink.label;
}
#processSettings(settings) {
- const settingPluginNames = {};
-
settings.forEach((setting) => {
- let plugin;
-
- let rootLabel;
- if (setting.plugin) {
- if (!settingPluginNames[setting.plugin]) {
- settingPluginNames[setting.plugin] = setting.plugin.replaceAll(
- "_",
- "-"
- );
- }
-
- plugin = this.plugins[settingPluginNames[setting.plugin]];
-
- if (plugin) {
- rootLabel = plugin.admin_route?.label
- ? i18n(plugin.admin_route?.label)
- : i18n("admin.plugins.title");
- } else {
- rootLabel = i18n("admin.plugins.title");
- }
- } else if (setting.primary_area) {
- rootLabel =
- I18n.lookup(`admin.config.${setting.primary_area}.title`) ||
- i18n(`admin.site_settings.categories.${setting.category}`);
- } else {
- rootLabel = i18n(`admin.site_settings.categories.${setting.category}`);
- }
-
- const label = `${rootLabel} ${SEPARATOR} ${humanizedSettingName(
- setting.setting
- )}`;
-
- // TODO (martin) These URLs will need to change eventually to anchors
- // to focus on a specific element on the page, for now though the filter is fine.
- let url;
- if (setting.plugin) {
- if (plugin) {
- url = plugin.admin_route.use_new_show_route
- ? this.router.urlFor(
- `adminPlugins.show.settings`,
- plugin.admin_route.location,
- { queryParams: { filter: setting.setting } }
- )
- : this.router.urlFor(`adminPlugins.${plugin.admin_route.location}`);
- } else {
- url = getURL(
- `/admin/site_settings/category/all_results?filter=${setting.setting}`
- );
- }
- } else if (this.settingPageMap.areas[setting.primary_area]) {
- url =
- this.settingPageMap.areas[setting.primary_area] +
- `?filter=${setting.setting}`;
- } else if (this.settingPageMap.categories[setting.category]) {
- url =
- this.settingPageMap.categories[setting.category] +
- `?filter=${setting.setting}`;
- } else {
- url = getURL(
- `/admin/site_settings/category/all_results?filter=${setting.setting}`
- );
- }
-
- this.settingMapItems.push({
- label,
- description: setting.description,
- url,
- keywords: this.#buildKeywords(
- setting.setting,
- humanizedSettingName(setting.setting),
- setting.description,
- setting.keywords,
- rootLabel
- ),
+ const formattedSettingLink = new SettingLinkFormatter(
+ this.router,
+ setting,
+ this.plugins,
+ this.settingPageMap
+ ).format();
+
+ this.settingDataSourceItems.push({
+ label: formattedSettingLink.label,
+ description: formattedSettingLink.description,
+ url: formattedSettingLink.url,
+ keywords: formattedSettingLink.keywords,
type: "setting",
icon: "gear",
});
@@ -256,11 +349,11 @@ export default class AdminSearchDataSource extends Service {
#processThemesAndComponents(themesAndComponents) {
themesAndComponents.forEach((themeOrComponent) => {
if (themeOrComponent.component) {
- this.componentMapItems.push({
+ this.componentDataSourceItems.push({
label: themeOrComponent.name,
description: themeOrComponent.description,
url: getURL(`/admin/customize/components/${themeOrComponent.id}`),
- keywords: this.#buildKeywords(
+ keywords: buildKeywords(
"component",
themeOrComponent.description,
themeOrComponent.name
@@ -269,11 +362,11 @@ export default class AdminSearchDataSource extends Service {
icon: "puzzle-piece",
});
} else {
- this.themeMapItems.push({
+ this.themeDataSourceItems.push({
label: themeOrComponent.name,
description: themeOrComponent.description,
url: getURL(`/admin/customize/themes/${themeOrComponent.id}`),
- keywords: this.#buildKeywords(
+ keywords: buildKeywords(
"theme",
themeOrComponent.description,
themeOrComponent.name
@@ -287,34 +380,14 @@ export default class AdminSearchDataSource extends Service {
#processReports(reports) {
reports.forEach((report) => {
- this.reportMapItems.push({
+ this.reportDataSourceItems.push({
label: report.title,
description: report.description,
url: getURL(`/admin/reports/${report.type}`),
icon: "chart-bar",
- keywords: this.#buildKeywords(
- report.title,
- report.description,
- report.type
- ),
+ keywords: buildKeywords(report.title, report.description, report.type),
type: "report",
});
});
}
-
- #labelOrText(obj, fallback = "") {
- return obj.text || (obj.label ? i18n(obj.label) : fallback);
- }
-
- #buildKeywords(...keywords) {
- return keywords
- .map((kw) => {
- if (Array.isArray(kw)) {
- return kw.join(" ");
- }
- return kw;
- })
- .join(" ")
- .toLowerCase();
- }
}
diff --git a/app/assets/javascripts/admin/addon/templates/search-index.gjs b/app/assets/javascripts/admin/addon/templates/search-index.gjs
index 23568d91329..afa6a17b592 100644
--- a/app/assets/javascripts/admin/addon/templates/search-index.gjs
+++ b/app/assets/javascripts/admin/addon/templates/search-index.gjs
@@ -10,6 +10,7 @@ export default RouteTemplate(
@titleLabel={{i18n "admin.config.search_everything.title"}}
@descriptionLabel={{i18n
"admin.config.search_everything.header_description"
+ shortcutHTML=this.shortcutHTML
}}
@shouldDisplay={{true}}
>
diff --git a/app/assets/javascripts/discourse/app/lib/sidebar/admin-sidebar.js b/app/assets/javascripts/discourse/app/lib/sidebar/admin-sidebar.js
index f6428f3b2b8..284f82a44e4 100644
--- a/app/assets/javascripts/discourse/app/lib/sidebar/admin-sidebar.js
+++ b/app/assets/javascripts/discourse/app/lib/sidebar/admin-sidebar.js
@@ -281,6 +281,9 @@ function pluginAdminRouteLinks(router) {
if (pluginNavLinks.length) {
pluginNavLinks = pluginNavLinks
.map((link) => {
+ if (!link.icon) {
+ link.icon = "gear";
+ }
if (link.route !== `${pluginAdminRoute}.${plugin.name}`) {
link.routeModels = [plugin.name];
return link;
diff --git a/app/assets/javascripts/discourse/tests/fixtures/admin-search-fixtures.js b/app/assets/javascripts/discourse/tests/fixtures/admin-search-fixtures.js
new file mode 100644
index 00000000000..03e3b653051
--- /dev/null
+++ b/app/assets/javascripts/discourse/tests/fixtures/admin-search-fixtures.js
@@ -0,0 +1,74 @@
+export default {
+ "/admin/search/all.json": {
+ settings: [
+ {
+ setting: "title",
+ description:
+ "The name of this site. Visible to all visitors including anonymous users.",
+ keywords: [],
+ category: "required",
+ primary_area: "about",
+ },
+ {
+ setting: "site_description",
+ description:
+ "Describe this site in one sentence. Visible to all visitors including anonymous users.",
+ keywords: [],
+ category: "required",
+ primary_area: "about",
+ },
+ ],
+ themes_and_components: [
+ {
+ id: 57,
+ name: "Air Theme",
+ description: "A clean and modern theme for Discourse",
+ created_at: "2024-10-16T05:10:28.695Z",
+ updated_at: "2024-10-16T05:10:36.298Z",
+ component: false,
+ },
+ {
+ id: 59,
+ name: "Clickable Topic",
+ description: null,
+ created_at: "2024-10-16T05:10:33.445Z",
+ updated_at: "2024-10-16T05:10:33.445Z",
+ component: true,
+ },
+ ],
+ reports: [
+ {
+ type: "staff_logins",
+ title: "Admin Logins",
+ description: "List of admin login times with locations.",
+ description_link: null,
+ },
+ {
+ type: "page_view_anon_browser_reqs",
+ title: "Anonymous Browser Pageviews",
+ description:
+ "Number of pageviews by anonymous visitors using real browsers.",
+ description_link: null,
+ },
+ {
+ type: "bookmarks",
+ title: "Bookmarks",
+ description: "Number of new topics and posts bookmarked.",
+ description_link: null,
+ },
+ {
+ type: "consolidated_api_requests",
+ title: "Consolidated API Requests",
+ description: "API requests for regular API keys and user API keys.",
+ description_link: null,
+ },
+ {
+ type: "dau_by_mau",
+ title: "DAU/MAU",
+ description:
+ "Number of members that logged in in the last day divided by number of members that logged in in the last month – returns a % which indicates community 'stickiness'. Aim for < 20%.",
+ description_link: null,
+ },
+ ],
+ },
+};
diff --git a/app/assets/javascripts/discourse/tests/helpers/create-pretender.js b/app/assets/javascripts/discourse/tests/helpers/create-pretender.js
index 6a2d3172982..94a483a7bd7 100644
--- a/app/assets/javascripts/discourse/tests/helpers/create-pretender.js
+++ b/app/assets/javascripts/discourse/tests/helpers/create-pretender.js
@@ -78,6 +78,10 @@ export function applyDefaultHandlers(pretender) {
pretender.get("/admin/plugins", () => response({ plugins: [] }));
+ pretender.get("/admin/search/all.json", () => {
+ return response(cloneJSON(fixturesByUrl["/admin/search/all.json"]));
+ });
+
pretender.get("/composer_messages", () =>
response({ composer_messages: [] })
);
diff --git a/app/assets/javascripts/discourse/tests/integration/components/admin-search-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/admin-search-test.gjs
new file mode 100644
index 00000000000..a913493e4c7
--- /dev/null
+++ b/app/assets/javascripts/discourse/tests/integration/components/admin-search-test.gjs
@@ -0,0 +1,90 @@
+import { click, fillIn, render, triggerKeyEvent } from "@ember/test-helpers";
+import { module, test } from "qunit";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import AdminSearch from "admin/components/admin-search";
+
+function filterButtonCss(filterType) {
+ return `.admin-search__filter.--${filterType} button`;
+}
+
+function assertFilterActive(assert, filterType, isActive = true) {
+ if (isActive) {
+ assert
+ .dom(`${filterButtonCss(filterType)}.admin-search__filter-item.is-active`)
+ .exists();
+ } else {
+ assert
+ .dom(`${filterButtonCss(filterType)}.admin-search__filter-item.is-active`)
+ .doesNotExist();
+ }
+}
+
+module("Integration | Component | AdminSearch", function (hooks) {
+ setupRenderingTest(hooks);
+
+ test("remembers last toggled filters, and opens filter controls by default", async function (assert) {
+ await render(<template><AdminSearch /></template>);
+
+ assert.dom(".admin-search__filters").exists();
+ assertFilterActive(assert, "page");
+ assertFilterActive(assert, "setting");
+ assertFilterActive(assert, "theme");
+ assertFilterActive(assert, "component");
+ assertFilterActive(assert, "report");
+
+ await click(filterButtonCss("page"));
+ await click(filterButtonCss("setting"));
+
+ assertFilterActive(assert, "page", false);
+ assertFilterActive(assert, "setting", false);
+
+ await render(<template><AdminSearch /></template>);
+
+ assertFilterActive(assert, "page", false);
+ assertFilterActive(assert, "setting", false);
+ });
+
+ test("filters different types of search results", async function (assert) {
+ await render(<template><AdminSearch /></template>);
+ await fillIn(".admin-search__input-field", "title");
+
+ assert.dom(".admin-search__results").exists();
+ assert.dom(".admin-search__result").exists({ count: 1 });
+
+ await click(filterButtonCss("setting"));
+ assert.dom(".admin-search__result").doesNotExist();
+
+ await fillIn(".admin-search__input-field", "admin login");
+ assert.dom(".admin-search__result").exists({ count: 1 });
+
+ await click(filterButtonCss("report"));
+ assert.dom(".admin-search__result").doesNotExist();
+ });
+
+ test("navigates search results with keyboard, getting back to the input when reaching the end of results", async function (assert) {
+ await render(<template><AdminSearch /></template>);
+ await fillIn(".admin-search__input-field", "site");
+ assert.dom(".admin-search__results").exists();
+
+ await triggerKeyEvent(".admin-search__input-field", "keydown", "ArrowDown");
+ assert
+ .dom(
+ ".admin-search__result .admin-search__result-link[href='/admin/site_settings']"
+ )
+ .isFocused();
+ await triggerKeyEvent(document.activeElement, "keydown", "ArrowDown");
+ assert
+ .dom(
+ ".admin-search__result .admin-search__result-link[href='/admin/backups']"
+ )
+ .isFocused();
+ await triggerKeyEvent(document.activeElement, "keydown", "ArrowUp");
+ assert
+ .dom(
+ ".admin-search__result .admin-search__result-link[href='/admin/site_settings']"
+ )
+ .isFocused();
+ await triggerKeyEvent(document.activeElement, "keydown", "ArrowUp");
+ assert.dom(".admin-search__input-field").isFocused();
+ });
+});
diff --git a/app/assets/javascripts/discourse/tests/unit/services/admin-search-data-source-test.js b/app/assets/javascripts/discourse/tests/unit/services/admin-search-data-source-test.js
new file mode 100644
index 00000000000..b382e0aec60
--- /dev/null
+++ b/app/assets/javascripts/discourse/tests/unit/services/admin-search-data-source-test.js
@@ -0,0 +1,379 @@
+import { getOwner } from "@ember/owner";
+import { setupTest } from "ember-qunit";
+import { module, test } from "qunit";
+import sinon from "sinon";
+import PreloadStore from "discourse/lib/preload-store";
+import { ADMIN_NAV_MAP } from "discourse/lib/sidebar/admin-nav-map";
+import { humanizedSettingName } from "discourse/lib/site-settings-utils";
+import { i18n } from "discourse-i18n";
+import {
+ PageLinkFormatter,
+ SettingLinkFormatter,
+} from "admin/services/admin-search-data-source";
+
+// NOTE: This test relies on `/admin/search/all.json` from admin-search-fixtures.js
+
+function fabricateVisiblePlugins() {
+ return [
+ {
+ admin_route: {
+ auto_generated: false,
+ full_location: "adminPlugins.show",
+ label: "chat.admin.title",
+ location: "chat",
+ use_new_show_route: true,
+ },
+ description:
+ "Adds chat functionality to your site so it can natively support both long-form and short-form communication needs of your online community",
+ enabled: true,
+ name: "chat",
+ humanized_name: "Chat",
+ },
+ {
+ name: "discourse-new-features-feeds",
+ humanized_name: "New features feeds",
+ admin_route: {
+ location: "discourse-new-features-feeds",
+ label: "new_feature_feeds.sidebar_plugin_name",
+ use_new_show_route: true,
+ auto_generated: false,
+ full_location: "adminPlugins.show",
+ },
+ enabled: true,
+ description:
+ "Create feeds for new feature notices consumed by other Discourse instances.",
+ },
+ {
+ name: "discourse-calendar",
+ humanized_name: "Calendar",
+ enabled: false,
+ description:
+ "Adds the ability to create a dynamic calendar with events in a topic.",
+ },
+ {
+ name: "badplugin",
+ humanized_name: "Badplugin",
+ admin_route: {
+ location: "blahblahnogood",
+ label: "badplugin.title",
+ use_new_show_route: true,
+ auto_generated: true,
+ full_location: "somemesseduproute",
+ },
+ enabled: true,
+ description: "Does things",
+ },
+ ];
+}
+
+module("Unit | Service | AdminSearchDataSource", function (hooks) {
+ setupTest(hooks);
+
+ hooks.beforeEach(function () {
+ this.subject = getOwner(this).lookup("service:admin-search-data-source");
+ });
+
+ test("buildMap - is a noop if already cached", async function (assert) {
+ await this.subject.buildMap();
+ sinon.stub(ADMIN_NAV_MAP, "forEach");
+ await this.subject.buildMap();
+ assert.false(ADMIN_NAV_MAP.forEach.called);
+ });
+
+ test("buildMap - makes a key/value object of preloaded plugins, excluding disabled and invalid ones", async function (assert) {
+ PreloadStore.store("visiblePlugins", fabricateVisiblePlugins());
+ await this.subject.buildMap();
+ assert.deepEqual(Object.keys(this.subject.plugins), [
+ "chat",
+ "discourse-new-features-feeds",
+ ]);
+ assert.deepEqual(
+ this.subject.plugins["chat"],
+ fabricateVisiblePlugins()[0]
+ );
+ });
+
+ test("buildMap - uses ADMIN_NAV_MAP to build up a list of page links including sub-pages", async function (assert) {
+ await this.subject.buildMap();
+
+ assert.true(this.subject.pageDataSourceItems.length > ADMIN_NAV_MAP.length);
+
+ assert.deepEqual(this.subject.pageDataSourceItems[0], {
+ label: "Dashboard",
+ url: "/admin",
+ keywords:
+ " /admin dashboard the dashboard provides a snapshot of your community’s health, including traffic, user activity, and other key metrics",
+ type: "page",
+ icon: "house",
+ description:
+ "The dashboard provides a snapshot of your community’s health, including traffic, user activity, and other key metrics",
+ });
+
+ assert.notStrictEqual(
+ this.subject.pageDataSourceItems.find(
+ (page) => page.url === "/admin/backups/logs"
+ ),
+ null
+ );
+ });
+
+ test("search - returns empty array if the search term is too small", async function (assert) {
+ await this.subject.buildMap();
+ assert.deepEqual(this.subject.search("a"), []);
+ });
+
+ test("search - limits the returned types", async function (assert) {
+ await this.subject.buildMap();
+ let results = this.subject.search("anonymous");
+ assert.deepEqual(results.length, 3);
+
+ results = this.subject.search("anonymous", { types: ["report"] });
+ assert.deepEqual(results.length, 1);
+ assert.deepEqual(
+ results[0].url,
+ "/admin/reports/page_view_anon_browser_reqs"
+ );
+ });
+});
+
+module(
+ "Unit | Service | AdminSearchDataSource | PageLinkFormatter",
+ function (hooks) {
+ setupTest(hooks);
+
+ hooks.beforeEach(function () {
+ this.router = getOwner(this).lookup("service:router");
+ });
+
+ test("url is correct based on href/route/route models", function (assert) {
+ const navMapSection = {
+ label: "admin.config_sections.account.title",
+ name: "root",
+ };
+ let link = {
+ route: "admin.dashboard.general",
+ };
+ let formatter = new PageLinkFormatter(this.router, navMapSection, link);
+ assert.deepEqual(formatter.format().url, "/admin");
+
+ link = {
+ route: "adminConfig.flags.edit",
+ routeModels: [{ flag_id: 1 }],
+ };
+ formatter = new PageLinkFormatter(this.router, navMapSection, link);
+ assert.deepEqual(formatter.format().url, "/admin/config/flags/1");
+
+ link = {
+ href: "/admin/something",
+ };
+ formatter = new PageLinkFormatter(this.router, navMapSection, link);
+ assert.deepEqual(formatter.format().url, "/admin/something");
+ });
+
+ test("label is correct based on section label, link label, and parent label", async function (assert) {
+ const navMapSection = {
+ label: "admin.config_sections.account.title",
+ name: "root",
+ };
+ let link = {
+ label: "admin.config.backups.title",
+ };
+ let formatter = new PageLinkFormatter(this.router, navMapSection, link);
+ assert.deepEqual(
+ formatter.format().label,
+ i18n(navMapSection.label) + " > " + i18n(link.label),
+ "link uses the section label and link label"
+ );
+
+ link = {
+ label: "admin.config.backups.sub_pages.logs.title",
+ };
+ formatter = new PageLinkFormatter(
+ this.router,
+ navMapSection,
+ link,
+ i18n("admin.config.backups.title")
+ );
+ assert.deepEqual(
+ formatter.format().label,
+ i18n(navMapSection.label) +
+ " > " +
+ i18n("admin.config.backups.title") +
+ " > " +
+ i18n(link.label),
+ "link uses the section label, parent label, and link label for sub-pages"
+ );
+
+ link = {
+ text: "Already translated",
+ };
+ formatter = new PageLinkFormatter(this.router, navMapSection, link);
+ assert.deepEqual(
+ formatter.format().label,
+ i18n(navMapSection.label) + " > " + "Already translated",
+ "link uses the text property if available"
+ );
+ });
+
+ test("keywords are correct using the link keywords, url, label, and description", async function (assert) {
+ const navMapSection = {
+ label: "admin.config_sections.account.title",
+ name: "root",
+ };
+ let link = {
+ label: "admin.config.flags.title",
+ description: "admin.config.flags.header_description",
+ route: "admin.dashboard.general",
+ keywords: "admin.config.flags.keywords",
+ };
+
+ let formatter = new PageLinkFormatter(this.router, navMapSection, link);
+ assert.deepEqual(
+ formatter.format().keywords,
+ `flag review spam illegal /admin ${i18n("admin.config_sections.account.title").toLowerCase()} ${i18n("admin.config.flags.title").toLowerCase()} ${i18n("admin.config.flags.header_description").toLowerCase()}`
+ );
+ });
+ }
+);
+
+module(
+ "Unit | Service | AdminSearchDataSource | SettingLinkFormatter",
+ function (hooks) {
+ setupTest(hooks);
+
+ hooks.beforeEach(function () {
+ this.router = getOwner(this).lookup("service:router");
+ this.plugins = { chat: fabricateVisiblePlugins()[0] };
+ });
+
+ test("label is correct for a setting that comes from a plugin", async function (assert) {
+ let setting = {
+ plugin: "chat",
+ setting: "enable_chat",
+ };
+ let formatter = new SettingLinkFormatter(
+ this.router,
+ setting,
+ this.plugins,
+ {}
+ );
+ assert.deepEqual(
+ formatter.format().label,
+ i18n("chat.admin.title") +
+ " > " +
+ humanizedSettingName(setting.setting),
+ "label uses the plugin admin route label and setting name"
+ );
+ });
+
+ test("label is correct for a setting that has a primary area", async function (assert) {
+ let setting = {
+ setting: "enable_chat",
+ primary_area: "about",
+ };
+ const settingPageMap = {
+ categores: {},
+ areas: { about: "/admin/plugins/chat/settings" },
+ };
+ let formatter = new SettingLinkFormatter(
+ this.router,
+ setting,
+ this.plugins,
+ settingPageMap
+ );
+ assert.deepEqual(
+ formatter.format().label,
+ i18n("admin.config.about.title") +
+ " > " +
+ humanizedSettingName(setting.setting),
+ "label uses the primary area and setting name"
+ );
+ });
+
+ test("label is correct for a setting that just belongs to a category", async function (assert) {
+ let setting = {
+ setting: "enable_chat",
+ category: "required",
+ };
+ const settingPageMap = {
+ categories: { required: "/admin/plugins/chat" },
+ areas: {},
+ };
+ let formatter = new SettingLinkFormatter(
+ this.router,
+ setting,
+ this.plugins,
+ settingPageMap
+ );
+ assert.deepEqual(
+ formatter.format().label,
+ i18n("admin.site_settings.categories.required") +
+ " > " +
+ humanizedSettingName(setting.setting),
+ "label uses the category and setting name"
+ );
+ });
+
+ test("url is correct for a setting that belongs to a plugin", async function (assert) {
+ let setting = {
+ plugin: "chat",
+ setting: "enable_chat",
+ };
+ let formatter = new SettingLinkFormatter(
+ this.router,
+ setting,
+ this.plugins,
+ {}
+ );
+ assert.deepEqual(
+ formatter.format().url,
+ "/admin/plugins/chat/settings?filter=enable_chat",
+ "url uses the plugin admin route location and setting"
+ );
+ });
+
+ test("url is correct for a setting that has a primary area", async function (assert) {
+ let setting = {
+ setting: "enable_chat",
+ primary_area: "about",
+ };
+ const settingPageMap = {
+ categores: {},
+ areas: { about: "/admin/plugins/chat/settings" },
+ };
+ let formatter = new SettingLinkFormatter(
+ this.router,
+ setting,
+ this.plugins,
+ settingPageMap
+ );
+ assert.deepEqual(
+ formatter.format().url,
+ "/admin/plugins/chat/settings?filter=enable_chat",
+ "url uses the primary area and setting"
+ );
+ });
+
+ test("url is correct for a setting that only belongs to a category", async function (assert) {
+ let setting = {
+ setting: "enable_chat",
+ category: "required",
+ };
+ const settingPageMap = {
+ categories: { required: "/admin/plugins/chat" },
+ areas: {},
+ };
+ let formatter = new SettingLinkFormatter(
+ this.router,
+ setting,
+ this.plugins,
+ settingPageMap
+ );
+ assert.deepEqual(
+ formatter.format().url,
+ "/admin/plugins/chat?filter=enable_chat",
+ "url uses the category and setting"
+ );
+ });
+ }
+);
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 6ed77f8da7c..62c195c6222 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -5346,7 +5346,7 @@ en:
header_description: "Configure search settings including logging and tokenization for Chinese and Japanese languages"
search_everything:
title: "Search"
- header_description: "Find anything in the admin interface here. You can filter the results below, and your preferences will be remembered."
+ header_description: "Find anything in the admin interface here. You can filter the results below, and your preferences will be remembered. To open this search anywhere in the admin interface, press %{shortcutHTML}."
security:
title: "Security"
header_description: "Configure security settings, including two-factor authentication, moderator privileges, and content security policies"