Skip to content

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

FIX: Topic voting for nested replies topics (#40449)

Mark VanLandingham773e6ae4444b
diff --git a/frontend/discourse/app/routes/nested.js b/frontend/discourse/app/routes/nested.js
index 894a1ec5c48..7437a446852 100644
--- a/frontend/discourse/app/routes/nested.js
+++ b/frontend/discourse/app/routes/nested.js
@@ -1,3 +1,4 @@
+import { action } from "@ember/object";
import { getOwner } from "@ember/owner";
import Route from "@ember/routing/route";
import { schedule } from "@ember/runloop";
@@ -13,6 +14,7 @@ export default class NestedRoute extends Route {
@service composer;
@service header;
@service nestedViewCache;
+ @service router;
@service screenTrack;
@service site;
@service siteSettings;
@@ -136,7 +138,22 @@ export default class NestedRoute extends Route {
controller.unsubscribe();
this.screenTrack.stop();
- this.header.clearTopic();
+ }
+
+ @action
+ willTransition(transition) {
+ transition.followRedirects().finally(() => {
+ const routeName = this.router.currentRouteName;
+
+ if (
+ !routeName?.startsWith("topic.") &&
+ !routeName?.startsWith("nested")
+ ) {
+ this.header.clearTopic();
+ }
+ });
+
+ return true;
}
_saveToCache(controller) {
diff --git a/frontend/discourse/app/routes/topic/from-params.js b/frontend/discourse/app/routes/topic/from-params.js
index 3f34ea0a720..d85cc5610f0 100644
--- a/frontend/discourse/app/routes/topic/from-params.js
+++ b/frontend/discourse/app/routes/topic/from-params.js
@@ -189,7 +189,12 @@ export default class TopicFromParams extends DiscourseRoute {
this.controllerFor("topic").set("previousURL", document.location.pathname);
transition.followRedirects().finally(() => {
- if (!this.router.currentRouteName.startsWith("topic.")) {
+ const routeName = this.router.currentRouteName;
+
+ if (
+ !routeName?.startsWith("topic.") &&
+ !routeName?.startsWith("nested")
+ ) {
this.header.clearTopic();
}
});
diff --git a/frontend/discourse/tests/unit/routes/topic-header-cleanup-test.js b/frontend/discourse/tests/unit/routes/topic-header-cleanup-test.js
new file mode 100644
index 00000000000..ac457dc4558
--- /dev/null
+++ b/frontend/discourse/tests/unit/routes/topic-header-cleanup-test.js
@@ -0,0 +1,88 @@
+import Service from "@ember/service";
+import { setupTest } from "ember-qunit";
+import { module, test } from "qunit";
+import sinon from "sinon";
+
+class HeaderStub extends Service {
+ clearCount = 0;
+
+ clearTopic() {
+ this.clearCount++;
+ }
+}
+
+function completedTransition() {
+ return {
+ followRedirects() {
+ return Promise.resolve();
+ },
+ };
+}
+
+async function waitForTransitionCleanup() {
+ await Promise.resolve();
+ await Promise.resolve();
+}
+
+function setCurrentRouteName(route, currentRouteName) {
+ sinon.stub(route.router, "currentRouteName").value(currentRouteName);
+}
+
+module("Unit | Route | topic header cleanup", function (hooks) {
+ setupTest(hooks);
+
+ hooks.beforeEach(function () {
+ this.owner.register("service:header", HeaderStub);
+ });
+
+ test("nested route does not clear header topic info when transitioning to flat topic route", async function (assert) {
+ const route = this.owner.lookup("route:nested");
+ const header = this.owner.lookup("service:header");
+
+ setCurrentRouteName(route, "topic.fromParams");
+
+ route.willTransition(completedTransition());
+ await waitForTransitionCleanup();
+
+ assert.strictEqual(
+ header.clearCount,
+ 0,
+ "topic info remains registered for the flat topic route"
+ );
+ });
+
+ test("flat topic route does not clear header topic info when transitioning to nested route", async function (assert) {
+ const route = this.owner.lookup("route:topic.from-params");
+ const header = this.owner.lookup("service:header");
+
+ setCurrentRouteName(route, "nested");
+ route.controllerFor = () => ({
+ set() {},
+ });
+
+ route.willTransition(completedTransition());
+ await waitForTransitionCleanup();
+
+ assert.strictEqual(
+ header.clearCount,
+ 0,
+ "topic info remains registered for the nested topic route"
+ );
+ });
+
+ test("nested route clears header topic info when transitioning away from topic routes", async function (assert) {
+ const route = this.owner.lookup("route:nested");
+ const header = this.owner.lookup("service:header");
+
+ setCurrentRouteName(route, "discovery.latest");
+
+ route.willTransition(completedTransition());
+ await waitForTransitionCleanup();
+
+ assert.strictEqual(
+ header.clearCount,
+ 1,
+ "topic info is cleared outside topic routes"
+ );
+ });
+});
diff --git a/plugins/discourse-topic-voting/assets/javascripts/discourse/connectors/topic-title/topic-title-voting.gjs b/plugins/discourse-topic-voting/assets/javascripts/discourse/connectors/topic-title/topic-title-voting.gjs
index 5f6c072faa6..834a059f038 100644
--- a/plugins/discourse-topic-voting/assets/javascripts/discourse/connectors/topic-title/topic-title-voting.gjs
+++ b/plugins/discourse-topic-voting/assets/javascripts/discourse/connectors/topic-title/topic-title-voting.gjs
@@ -1,13 +1,20 @@
+import { and, or } from "discourse/truth-helpers";
import VoteBox from "../../components/vote-box";
<template>
- {{#if @outletArgs.model.can_vote}}
- {{#if @outletArgs.model.postStream.loaded}}
- {{#if @outletArgs.model.postStream.firstPostPresent}}
- <div class="voting title-voting">
- <VoteBox @topic={{@outletArgs.model}} />
- </div>
- {{/if}}
+ {{#let @outletArgs.model as |topic|}}
+ {{#if
+ (and
+ topic.can_vote
+ (or
+ topic.is_nested_view
+ (and topic.postStream.loaded topic.postStream.firstPostPresent)
+ )
+ )
+ }}
+ <div class="voting title-voting">
+ <VoteBox @topic={{topic}} />
+ </div>
{{/if}}
- {{/if}}
+ {{/let}}
</template>
diff --git a/plugins/discourse-topic-voting/assets/stylesheets/common/topic-voting.scss b/plugins/discourse-topic-voting/assets/stylesheets/common/topic-voting.scss
index 85f8d3863ae..1ff80eeb34a 100644
--- a/plugins/discourse-topic-voting/assets/stylesheets/common/topic-voting.scss
+++ b/plugins/discourse-topic-voting/assets/stylesheets/common/topic-voting.scss
@@ -75,7 +75,8 @@
}
// grid layout for voting in topic title
-#topic-title:has(.voting) {
+#topic-title:has(.voting),
+.nested-view__header:has(.voting) {
display: grid;
grid-template-columns: auto 1fr;
gap: 0 var(--topic-body-width-padding);
diff --git a/plugins/discourse-topic-voting/test/javascripts/integration/connectors/topic-title-voting-test.gjs b/plugins/discourse-topic-voting/test/javascripts/integration/connectors/topic-title-voting-test.gjs
new file mode 100644
index 00000000000..d37e009ef72
--- /dev/null
+++ b/plugins/discourse-topic-voting/test/javascripts/integration/connectors/topic-title-voting-test.gjs
@@ -0,0 +1,75 @@
+import { render } from "@ember/test-helpers";
+import { module, test } from "qunit";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import TopicTitleVoting from "discourse/plugins/discourse-topic-voting/discourse/connectors/topic-title/topic-title-voting";
+
+function buildTopic(overrides = {}) {
+ return {
+ id: 1,
+ can_vote: true,
+ closed: false,
+ is_nested_view: false,
+ postStream: {
+ firstPostPresent: false,
+ loaded: false,
+ },
+ user_voted: false,
+ vote_count: 0,
+ ...overrides,
+ };
+}
+
+module("Integration | Connector | TopicTitleVoting", function (hooks) {
+ setupRenderingTest(hooks);
+
+ hooks.beforeEach(function () {
+ this.siteSettings.topic_voting_show_who_voted = false;
+ });
+
+ test("renders after the first post is present in the flat topic stream", async function (assert) {
+ this.outletArgs = {
+ model: buildTopic({
+ postStream: {
+ firstPostPresent: true,
+ loaded: true,
+ },
+ }),
+ };
+
+ await render(
+ <template><TopicTitleVoting @outletArgs={{this.outletArgs}} /></template>
+ );
+
+ assert
+ .dom(".title-voting .voting-wrapper")
+ .exists("the vote box renders in the topic title");
+ });
+
+ test("does not render before the first post is present in the flat topic stream", async function (assert) {
+ this.outletArgs = {
+ model: buildTopic(),
+ };
+
+ await render(
+ <template><TopicTitleVoting @outletArgs={{this.outletArgs}} /></template>
+ );
+
+ assert
+ .dom(".title-voting .voting-wrapper")
+ .doesNotExist("the flat topic guard is preserved");
+ });
+
+ test("renders for nested topics before the flat topic stream is loaded", async function (assert) {
+ this.outletArgs = {
+ model: buildTopic({ is_nested_view: true }),
+ };
+
+ await render(
+ <template><TopicTitleVoting @outletArgs={{this.outletArgs}} /></template>
+ );
+
+ assert
+ .dom(".title-voting .voting-wrapper")
+ .exists("nested topics can render the vote box from topic metadata");
+ });
+});