From 308240be898e1a9a5ac1a97d20625d33ee466a49 Mon Sep 17 00:00:00 2001 From: Lisbet Alvarez <107949262+lisbet-alvarez@users.noreply.github.com> Date: Thu, 22 May 2025 16:34:40 -0700 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20refocus=20on=20route=20ch?= =?UTF-8?q?anges=20when=20attempting=20to=20search=20(#2839)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 🐛 refocus on route changes when attempting to search * refactor: 💡 update condition * refactor: 💡 prevent refocus for more use cases like filtering * refactor: 💡 use router service instead of window object * refactor: 💡 PR feedback (use param finder instead of regex) * test: 💍 update test title * test: 💍 refactor tests to use test.each --- ui/admin/app/controllers/application.js | 36 ++++++++++- ui/admin/app/templates/application.hbs | 5 +- ui/admin/package.json | 1 + .../unit/controllers/application-test.js | 63 +++++++++++++++++++ yarn.lock | 1 + 5 files changed, 103 insertions(+), 3 deletions(-) diff --git a/ui/admin/app/controllers/application.js b/ui/admin/app/controllers/application.js index 6a554d4d1..6797c1202 100644 --- a/ui/admin/app/controllers/application.js +++ b/ui/admin/app/controllers/application.js @@ -7,6 +7,8 @@ import Controller from '@ember/controller'; import { service } from '@ember/service'; import { getOwner } from '@ember/application'; import { action } from '@ember/object'; +import { defaultValidator } from 'ember-a11y-refocus'; +import { paramValueFinder } from 'admin/utils/param-value-finder'; const THEMES = [ { @@ -33,13 +35,18 @@ export default class ApplicationController extends Controller { @service router; /** - * Returns available themes + * Returns available themes. * @type {array} */ get themes() { return THEMES; } + /** + * Shows side navigation only for routes nested under a scope + * and if user has been authenticated. + * @type {boolean} + */ get showSideNav() { return ( this.router.currentRouteName.startsWith('scopes.scope') && @@ -94,7 +101,7 @@ export default class ApplicationController extends Controller { } /** - * + * Toggles on/off specified features. * @param {string} feature */ @action @@ -105,4 +112,29 @@ export default class ApplicationController extends Controller { this.features.enable(feature); } } + + /** + * Add custom route change validation to prevent refocus when + * user is attempting to search, filter, or sort. + * @param {object} transition + * @returns {boolean} + */ + customRouteChangeValidator(transition) { + if (!transition.to || !transition.from) { + return true; + } + if (transition.to.name === transition.from.name) { + const toParams = paramValueFinder( + transition.to.localName, + transition.to.parent, + ); + const fromParams = paramValueFinder( + transition.from.localName, + transition.from.parent, + ); + // Return false to prevent refocus when routes have equivalent dynamic segments (params). + return JSON.stringify(toParams) !== JSON.stringify(fromParams); + } + return defaultValidator(transition); + } } diff --git a/ui/admin/app/templates/application.hbs b/ui/admin/app/templates/application.hbs index 12585bb5f..542a0b806 100644 --- a/ui/admin/app/templates/application.hbs +++ b/ui/admin/app/templates/application.hbs @@ -7,7 +7,10 @@ - + <:header> <:logo> diff --git a/ui/admin/package.json b/ui/admin/package.json index a76e27f70..a0b364df3 100644 --- a/ui/admin/package.json +++ b/ui/admin/package.json @@ -57,6 +57,7 @@ "concurrently": "^9.1.0", "core": "workspace:*", "doctoc": "^2.2.0", + "ember-a11y-refocus": "^4.1.4", "ember-a11y-testing": "^7.1.2", "ember-auto-import": "^2.10.0", "ember-cli": "~5.12.0", diff --git a/ui/admin/tests/unit/controllers/application-test.js b/ui/admin/tests/unit/controllers/application-test.js index 5b09d92bc..8fd9b9598 100644 --- a/ui/admin/tests/unit/controllers/application-test.js +++ b/ui/admin/tests/unit/controllers/application-test.js @@ -14,6 +14,7 @@ module('Unit | Controller | application', function (hooks) { let featureEdition; let featuresService; let session; + let router; hooks.beforeEach(async function () { await authenticateSession({}); @@ -21,6 +22,7 @@ module('Unit | Controller | application', function (hooks) { featureEdition = this.owner.lookup('service:featureEdition'); featuresService = this.owner.lookup('service:features'); session = this.owner.lookup('service:session'); + router = this.owner.lookup('service:router'); }); test('it exists', function (assert) { @@ -58,4 +60,65 @@ module('Unit | Controller | application', function (hooks) { assert.true(featuresService.isEnabled('ssh-target')); }); + + test.each( + 'customRouteChangeValidator', + [ + { + from: '/scopes', + to: '/scopes', + expectedResult: false, + }, + { + from: '/scopes/global/scopes', + to: '/scopes/global/scopes', + expectedResult: false, + }, + { + from: '/scopes/global/scopes?search=xyz', + to: '/scopes/global/scopes', + expectedResult: false, + }, + { + from: '/scopes/global/scopes?search=xyz', + to: '/scopes/global/scopes?search=xy', + expectedResult: false, + }, + { + to: '/scopes/global/scopes', + expectedResult: true, + }, + { + from: '/scopes/global/scopes', + expectedResult: true, + }, + { + from: '/scopes/global/scopes', + to: '/scopes/global/auth-methods', + expectedResult: true, + }, + { + from: '/scopes/global/scopes', + to: '/scopes/o_12345/scopes', + expectedResult: true, + }, + { + from: '/scopes/global/roles/r_123/scopes', + to: '/scopes/global/roles/r_321/scopes', + expectedResult: true, + }, + ], + + async function (assert, input) { + const transition = { + from: input.from ? router.recognize(input.from) : undefined, + to: input.to ? router.recognize(input.to) : undefined, + }; + + assert.strictEqual( + controller.customRouteChangeValidator(transition), + input.expectedResult, + ); + }, + ); }); diff --git a/yarn.lock b/yarn.lock index 75a64cd63..c5d3fd70b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6478,6 +6478,7 @@ __metadata: concurrently: "npm:^9.1.0" core: "workspace:*" doctoc: "npm:^2.2.0" + ember-a11y-refocus: "npm:^4.1.4" ember-a11y-testing: "npm:^7.1.2" ember-auto-import: "npm:^2.10.0" ember-cli: "npm:~5.12.0"