FIX: respect "external_links_in_new_tab" user preference... (#622)

... for AI conversations since they look & feel "external".

Internal ref - /t/128182
This commit is contained in:
Régis Hanol 2024-05-15 10:06:38 +02:00 committed by GitHub
parent 3db89bfdc8
commit 1e6d9ed634
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 79 additions and 4 deletions

View File

@ -1,8 +1,19 @@
import { service } from "@ember/service";
import DiscourseRoute from "discourse/routes/discourse";
export default DiscourseRoute.extend({
export default class DiscourseAiSharedConversationShowRoute extends DiscourseRoute {
@service currentUser;
beforeModel(transition) {
window.location = transition.intent.url;
if (this.currentUser?.user_option?.external_links_in_new_tab) {
window.open(transition.intent.url, "_blank");
} else {
this.redirect(transition.intent.url);
}
transition.abort();
},
});
}
redirect(url) {
window.location = url;
}
}

View File

@ -0,0 +1,64 @@
import { setupTest } from "ember-qunit";
import { module, test } from "qunit";
module(
"Unit | Route | discourse-ai-shared-conversation-show",
function (hooks) {
setupTest(hooks);
test("it redirects based on currentUser preference", function (assert) {
const transition = {
intent: { url: "https://www.discourse.org" },
abort() {
assert.ok(true, "transition.abort() was called");
},
};
const route = this.owner.lookup(
"route:discourse-ai-shared-conversation-show"
);
const originalOpen = window.open;
const originalRedirect = route.redirect;
// Test when external_links_in_new_tab is true
route.set("currentUser", {
user_option: {
external_links_in_new_tab: true,
},
});
window.open = (url, target) => {
assert.equal(
url,
"https://www.discourse.org",
"window.open was called with the correct URL"
);
assert.equal(target, "_blank", 'window.open was called with "_blank"');
};
route.beforeModel(transition);
// Test when external_links_in_new_tab is false
route.set("currentUser", {
user_option: {
external_links_in_new_tab: false,
},
});
route.redirect = (url) => {
assert.equal(
url,
"https://www.discourse.org",
"redirect was called with the correct URL"
);
};
route.beforeModel(transition);
// Reset
window.open = originalOpen;
route.redirect = originalRedirect;
});
}
);