A platform for community discussion. Free, open, simple.
FIX: Typos and dead code in the topic timeline
Régis Hanolab4d1fd8411b
diff --git a/app/assets/stylesheets/common/topic-timeline.scss b/app/assets/stylesheets/common/topic-timeline.scssindex dd807743499..7a8066e7547 100644--- a/app/assets/stylesheets/common/topic-timeline.scss+++ b/app/assets/stylesheets/common/topic-timeline.scss@@ -299,10 +299,6 @@cursor: ns-resize;display: flex;align-items: center;-- .back-button {- margin-top: var(--space-4);- }}.timeline-replies {diff --git a/frontend/discourse/app/components/topic-timeline/container.gjs b/frontend/discourse/app/components/topic-timeline/container.gjsindex b3193b3605f..cfd6faeb882 100644--- a/frontend/discourse/app/components/topic-timeline/container.gjs+++ b/frontend/discourse/app/components/topic-timeline/container.gjs@@ -60,7 +60,6 @@ export default class TopicTimelineScrollArea extends Component {@service currentUser;@service composer;- @tracked showButton = false;@tracked current;@tracked percentage = this._percentFor(this.args.model,@@ -72,9 +71,9 @@ export default class TopicTimelineScrollArea extends Component {/** @type {number | null} */@tracked lastReadPercentage = null;@tracked lastRead;- @tracked lastReadTop;@tracked before;@tracked after;+ @tracked scrollareaHeight;@tracked timelineScrollareaStyle;@tracked dragging = false;@tracked excerpt = "";@@ -95,9 +94,7 @@ export default class TopicTimelineScrollArea extends Component {super(...arguments);if (this.site.desktopView) {- // listen for scrolling event to update timelinethis.appEvents.on("topic:current-post-scrolled", this.postScrolled);- // listen for composer sizing changes to update timelinethis.appEvents.on("composer:opened", this.calculatePosition);this.appEvents.on("composer:resized", this.calculatePosition);this.appEvents.on("composer:closed", this.calculatePosition);@@ -125,8 +122,8 @@ export default class TopicTimelineScrollArea extends Component {document.querySelector("#topic-bottom"),].filter(Boolean);- for (let i = 0; i < elements.length; i++) {- this.intersectionObserver.observe(elements[i]);+ for (const element of elements) {+ this.intersectionObserver.observe(element);}this.calculatePosition();@@ -136,10 +133,9 @@ export default class TopicTimelineScrollArea extends Component {willDestroy() {super.willDestroy(...arguments);- if (this.site.desktopView) {- this.intersectionObserver?.disconnect();- this.intersectionObserver = null;+ this.intersectionObserver.disconnect();+ if (this.site.desktopView) {this.appEvents.off("composer:opened", this.calculatePosition);this.appEvents.off("composer:resized", this.calculatePosition);this.appEvents.off("composer:closed", this.calculatePosition);@@ -151,7 +147,7 @@ export default class TopicTimelineScrollArea extends Component {get displaySummary() {return (this.siteSettings.summary_timeline_button &&- !this.args.fullScreen &&+ !this.args.fullscreen &&this.args.model.has_summary &&!this.args.model.postStream.summary);@@ -193,10 +189,6 @@ export default class TopicTimelineScrollArea extends Component {);}- get style() {- return trustHTML(`height: ${this.scrollareaHeight}px`);- }-get beforePadding() {return trustHTML(`height: ${this.before}px`);}@@ -205,18 +197,18 @@ export default class TopicTimelineScrollArea extends Component {return trustHTML(`height: ${this.after}px`);}- get showDockedButton() {- return this.site.desktopView && this.hasBackPosition && !this.showButton;+ get hasBackPosition() {+ return (+ this.lastRead - this.current > 3 &&+ Math.abs(this.lastRead - this.total) > 1+ );}- get hasBackPosition() {+ get showLastRead() {return (- this.lastRead &&- this.lastRead > 3 &&- this.lastRead > this.current &&- Math.abs(this.lastRead - this.current) > 3 &&- Math.abs(this.lastRead - this.total) > 1 &&- this.lastRead !== this.total+ this.hasBackPosition &&+ (this.before + SCROLLER_HEIGHT - 5 < this.lastReadHeight ||+ this.before > this.lastReadHeight + 25));}@@ -228,31 +220,7 @@ export default class TopicTimelineScrollArea extends Component {get topPosition() {const bottom = this.scrollareaHeight - LAST_READ_HEIGHT / 2;- return this.lastReadTop > bottom ? bottom : this.lastReadTop;- }-- get scrollareaHeight() {- const composerHeight = this.composer.isPreviewActive- ? document.getElementById("reply-control")?.offsetHeight || 0- : 0;- const headerHeight = document.querySelector(".d-header")?.offsetHeight || 0;-- // scrollarea takes up about half of the timeline's height- const availableHeight =- (window.innerHeight - composerHeight - headerHeight) / 2;-- const minHeight = this.site.mobileView- ? DEFAULT_MIN_SCROLLAREA_HEIGHT- : this.composer.isPreviewActive- ? desktopMinScrollAreaHeight- : DEFAULT_MIN_SCROLLAREA_HEIGHT;- const maxHeight = this.site.mobileView- ? DEFAULT_MAX_SCROLLAREA_HEIGHT- : this.composer.isPreviewActive- ? desktopMaxScrollAreaHeight- : DEFAULT_MAX_SCROLLAREA_HEIGHT;-- return Math.max(minHeight, Math.min(availableHeight, maxHeight));+ return Math.min(this.lastReadHeight, bottom);}get startDate() {@@ -279,6 +247,7 @@ export default class TopicTimelineScrollArea extends Component {@bindcalculatePosition() {+ this.scrollareaHeight = this.#measureScrollareaHeight();this.timelineScrollareaStyle = trustHTML(`height: ${this.scrollareaHeight}px`);@@ -320,42 +289,27 @@ export default class TopicTimelineScrollArea extends Component {this.lastReadPercentage = this._percentFor(topic, idx);}- if (this.position !== this.scrollPosition) {- this.position = this.scrollPosition;+ if (this.position !== this.current) {+ this.position = this.current;this.updateScrollPosition(this.current);}- this.before = this.scrollareaRemaining() * this.percentage;+ this.before = (this.scrollareaHeight - SCROLLER_HEIGHT) * this.percentage;this.after = this.scrollareaHeight - this.before - SCROLLER_HEIGHT;-- if (this.percentage === null) {- return;- }-- if (this.hasBackPosition) {- this.lastReadTop = Math.round(- this.lastReadPercentage * this.scrollareaHeight- );- this.showButton =- this.before + SCROLLER_HEIGHT - 5 < this.lastReadTop ||- this.before > this.lastReadTop + 25;- }}@debounce(50)updateScrollPosition(scrollPosition) {- // only ran on mobileif (!this.args.fullscreen) {return;}const stream = this.args.model.postStream;- if (!this.position === scrollPosition) {+ if (this.position !== scrollPosition) {return;}- // we have an off by one, stream is zero based,stream.excerpt(scrollPosition - 1).then((info) => {if (info && this.position === scrollPosition) {let excerpt = "";@@ -379,11 +333,7 @@ export default class TopicTimelineScrollArea extends Component {updatePercentage(e) {e.preventDefault();- // Both a pointer drag and a click on the timeline track land here, and both- // carry `pageY` directly.- const currentCursorY = e.pageY;-- const desiredScrollerCentre = currentCursorY - this.dragOffset;+ const desiredScrollerCentre = e.pageY - this.dragOffset;const areaTop = domUtils.offset(this.scrollareaElement).top;const areaHeight = this.scrollareaElement.offsetHeight;@@ -394,20 +344,18 @@ export default class TopicTimelineScrollArea extends Component {const scrollableHeight = areaHeight - scrollerHeight;this.percentage = this.clamp(- parseFloat(desiredScrollerCentre - scrollableTop) / scrollableHeight+ (desiredScrollerCentre - scrollableTop) / scrollableHeight);this.commit();}@binddidStartDrag(event) {- const y = event.pageY;-const scrollerCentre =domUtils.offset(this.scrollerElement).top +this.scrollerElement.offsetHeight / 2;- this.dragOffset = y - scrollerCentre;+ this.dragOffset = event.pageY - scrollerCentre;this.dragging = true;}@@ -440,7 +388,7 @@ export default class TopicTimelineScrollArea extends Component {dockCheck() {const timeline = document.querySelector(".timeline-container");- const timelineHeight = (timeline && timeline.offsetHeight) || 400;+ const timelineHeight = timeline?.offsetHeight || 400;const prevDockAt = this.dockAt;const positionTop = headerOffset() + window.pageYOffset;@@ -448,15 +396,15 @@ export default class TopicTimelineScrollArea extends Component {const postStream = this.args.model.postStream;const allPostsLoaded = postStream.loadedAllPosts;- this.dockBottom = false;+ let dockBottom = false;if (positionTop < this.topicTop) {this.dockAt = parseInt(this.topicTop, 10);} else if (allPostsLoaded && currentPosition > this.topicBottom) {- this.dockAt = parseInt(this.topicBottom - timelineHeight, 10);- this.dockBottom = true;- if (this.dockAt < 0) {- this.dockAt = 0;- }+ this.dockAt = Math.max(+ 0,+ parseInt(this.topicBottom - timelineHeight, 10)+ );+ dockBottom = true;} else {this.dockAt = null;}@@ -464,7 +412,7 @@ export default class TopicTimelineScrollArea extends Component {if (this.dockAt !== prevDockAt) {if (this.dockAt) {this.args.setDocked(true);- if (this.dockBottom) {+ if (dockBottom) {this.args.setDockedBottom(true);}} else {@@ -490,10 +438,6 @@ export default class TopicTimelineScrollArea extends Component {return Math.max(Math.min(p, max), min);}- scrollareaRemaining() {- return this.scrollareaHeight - SCROLLER_HEIGHT;- }-@actionregisterScrollarea(element) {this.scrollareaElement = element;@@ -504,18 +448,36 @@ export default class TopicTimelineScrollArea extends Component {this.scrollerElement = element;}+ #measureScrollareaHeight() {+ const composerHeight = this.composer.isPreviewActive+ ? document.getElementById("reply-control")?.offsetHeight || 0+ : 0;+ const headerHeight = document.querySelector(".d-header")?.offsetHeight || 0;++ const availableHeight =+ (window.innerHeight - composerHeight - headerHeight) / 2;++ const desktopPreview =+ !this.site.mobileView && this.composer.isPreviewActive;+ const minHeight = desktopPreview+ ? desktopMinScrollAreaHeight+ : DEFAULT_MIN_SCROLLAREA_HEIGHT;+ const maxHeight = desktopPreview+ ? desktopMaxScrollAreaHeight+ : DEFAULT_MAX_SCROLLAREA_HEIGHT;++ return Math.max(minHeight, Math.min(availableHeight, maxHeight));+ }+_percentFor(topic, postIndex) {const total = topic.postStream.filteredPostsCount;switch (postIndex) {- // if first post, no top paddingcase 0:return 0;- // if last, no bottom paddingcase total - 1:return 1;- // otherwise, calculatedefault:- return this.clamp(parseFloat(postIndex) / total);+ return this.clamp(postIndex / total);}}@@ -643,8 +605,6 @@ export default class TopicTimelineScrollArea extends Component {@didStartDrag={{this.didStartDrag}}@dragMove={{this.dragMove}}@fullscreen={{@fullscreen}}- @onGoBack={{this.onGoBack}}- @showDockedButton={{this.showDockedButton}}@total={{this.total}}{{didInsert this.registerScroller}}/>@@ -656,7 +616,7 @@ export default class TopicTimelineScrollArea extends Component {{{on "click" this.updatePercentage}}></div>- {{#if (and this.hasBackPosition this.showButton)}}+ {{#if this.showLastRead}}<div class="timeline-last-read" style={{this.lastReadStyle}}>{{dIcon "minus" class="progress"}}<BackButton @onGoBack={{this.goBack}} />@@ -688,15 +648,13 @@ export default class TopicTimelineScrollArea extends Component {/>{{/if}}- {{#if (and this.currentUser (not @fullscreen))}}- {{#if this.canCreatePost}}- <DButton- class="btn-default create reply-to-post"- title={{i18n "topic.reply.help"}}- @action={{fn @replyToPost null}}- @icon="reply"- />- {{/if}}+ {{#if (and this.currentUser (not @fullscreen) this.canCreatePost)}}+ <DButton+ class="btn-default create reply-to-post"+ title={{i18n "topic.reply.help"}}+ @action={{fn @replyToPost null}}+ @icon="reply"+ />{{/if}}{{#if @fullscreen}}diff --git a/frontend/discourse/app/components/topic-timeline/scroller.gjs b/frontend/discourse/app/components/topic-timeline/scroller.gjsindex 1430a03f1ae..9247f34e14b 100644--- a/frontend/discourse/app/components/topic-timeline/scroller.gjs+++ b/frontend/discourse/app/components/topic-timeline/scroller.gjs@@ -6,7 +6,6 @@ import {} from "discourse/components/topic-timeline/container";import dPointerDrag from "discourse/ui-kit/modifiers/d-pointer-drag";import { i18n } from "discourse-i18n";-import BackButton from "./back-button.gjs";export default class TopicTimelineScroller extends Component {style = trustHTML(`height: ${SCROLLER_HEIGHT}px`);@@ -37,36 +36,21 @@ export default class TopicTimelineScroller extends Component {bodyClass="dragging"}}>- {{#if @fullscreen}}- <div class="timeline-scroller-content">- <div class="timeline-replies">- {{this.repliesShort}}- </div>- {{#if @date}}- <div class="timeline-ago">- {{this.timelineAgo}}- </div>- {{/if}}- {{#if (and @showDockedButton (not @dragging))}}- <BackButton @onGoBack={{@onGoBack}} />- {{/if}}- </div>- <div class="timeline-handle"></div>- {{else}}+ {{#unless @fullscreen}}<div class="timeline-handle"></div>- <div class="timeline-scroller-content">- <div class="timeline-replies">- {{this.repliesShort}}- </div>- {{#if @date}}- <div class="timeline-ago">- {{this.timelineAgo}}- </div>- {{/if}}- {{#if (and @showDockedButton (not @dragging))}}- <BackButton @onGoBack={{@onGoBack}} />- {{/if}}+ {{/unless}}+ <div class="timeline-scroller-content">+ <div class="timeline-replies">+ {{this.repliesShort}}</div>+ {{#if @date}}+ <div class="timeline-ago">+ {{this.timelineAgo}}+ </div>+ {{/if}}+ </div>+ {{#if @fullscreen}}+ <div class="timeline-handle"></div>{{/if}}</div></template>diff --git a/frontend/discourse/tests/integration/components/topic-timeline-test.gjs b/frontend/discourse/tests/integration/components/topic-timeline-test.gjsnew file mode 100644index 00000000000..db90bdb1cbb--- /dev/null+++ b/frontend/discourse/tests/integration/components/topic-timeline-test.gjs@@ -0,0 +1,45 @@+import { getOwner } from "@ember/owner";+import { render } from "@ember/test-helpers";+import { module, test } from "qunit";+import sinon from "sinon";+import TopicTimeline from "discourse/components/topic-timeline";+import { setupRenderingTest } from "discourse/tests/helpers/component-test";++const POSTS_COUNT = 100;++function createMegaTopic(context, attrs = {}) {+ const topic = getOwner(context)+ .lookup("service:store")+ .createRecord("topic", {+ id: 1,+ highest_post_number: POSTS_COUNT,+ ...attrs,+ });+ topic.postStream.isMegaTopic = true;+ return topic;+}++module("Integration | Component | topic-timeline", function (hooks) {+ setupRenderingTest(hooks);++ test("fullscreen hides the summary button and shows the last post excerpt", async function (assert) {+ this.siteSettings.summary_timeline_button = true;+ const topic = createMegaTopic(this, { has_summary: true });+ sinon.stub(topic.postStream, "excerpt").resolves({ excerpt: "Last post" });+ const jumpTop = () => {};++ await render(+ <template>+ <TopicTimeline+ @enteredIndex={{99}}+ @fullscreen={{true}}+ @jumpTop={{jumpTop}}+ @model={{topic}}+ />+ </template>+ );++ assert.dom(".show-summary").doesNotExist();+ assert.dom(".post-excerpt").hasText("Last post");+ });+});