Files

214 lines
7.1 KiB
Plaintext
Raw Permalink Normal View History

2024-12-13 17:00:17 +01:00
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
2024-12-20 18:56:02 +01:00
import { on } from "@ember/modifier";
2024-12-13 17:00:17 +01:00
import { action } from "@ember/object";
import didInsert from "@ember/render-modifiers/modifiers/did-insert";
2025-11-26 15:12:59 -03:00
import willDestroy from "@ember/render-modifiers/modifiers/will-destroy";
2024-12-13 17:51:06 +01:00
import DButton from "discourse/components/d-button";
import concatClass from "discourse/helpers/concat-class";
2024-12-13 17:00:17 +01:00
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
2025-11-18 09:03:41 -05:00
import { eq } from "discourse/truth-helpers";
import { i18n } from "discourse-i18n";
2024-12-20 18:56:02 +01:00
import ActivityCalendar from "discourse/plugins/discourse-rewind/discourse/components/reports/activity-calendar";
import BestPosts from "discourse/plugins/discourse-rewind/discourse/components/reports/best-posts";
import BestTopics from "discourse/plugins/discourse-rewind/discourse/components/reports/best-topics";
import FBFF from "discourse/plugins/discourse-rewind/discourse/components/reports/fbff";
2025-01-13 10:48:23 -06:00
import RewindHeader from "discourse/plugins/discourse-rewind/discourse/components/reports/header";
import MostViewedCategories from "discourse/plugins/discourse-rewind/discourse/components/reports/most-viewed-categories";
import MostViewedTags from "discourse/plugins/discourse-rewind/discourse/components/reports/most-viewed-tags";
2024-12-13 17:51:06 +01:00
import Reactions from "discourse/plugins/discourse-rewind/discourse/components/reports/reactions";
2024-12-20 18:56:02 +01:00
import ReadingTime from "discourse/plugins/discourse-rewind/discourse/components/reports/reading-time";
2025-01-13 13:12:12 +01:00
import TopWords from "discourse/plugins/discourse-rewind/discourse/components/reports/top-words";
2024-12-13 17:00:17 +01:00
export default class Rewind extends Component {
@tracked rewind = [];
2024-12-13 17:51:06 +01:00
@tracked fullScreen = true;
2024-12-20 18:56:02 +01:00
@tracked loadingRewind = false;
2025-11-26 15:12:59 -03:00
@tracked totalAvailable = 0;
@tracked loadingNextReport = false;
BUFFER_SIZE = 3;
// Arrow function for event listener - maintains 'this' binding
handleScroll = () => {
if (!this.scrollWrapper || this.loadingRewind) {
return;
}
const scrollTop = this.scrollWrapper.scrollTop;
const scrollHeight = this.scrollWrapper.scrollHeight;
const clientHeight = this.scrollWrapper.clientHeight;
// Trigger preload when scrolled 70% through content
const scrollPercentage = (scrollTop + clientHeight) / scrollHeight;
if (scrollPercentage > 0.7) {
this.preloadNextReports();
}
};
2024-12-20 18:56:02 +01:00
@action
registerScrollWrapper(element) {
this.scrollWrapper = element;
2025-11-26 15:12:59 -03:00
this.scrollWrapper.addEventListener("scroll", this.handleScroll);
2024-12-20 18:56:02 +01:00
}
2024-12-13 17:51:06 +01:00
2024-12-13 17:00:17 +01:00
@action
async loadRewind() {
try {
2024-12-20 18:56:02 +01:00
this.loadingRewind = true;
2025-11-26 15:12:59 -03:00
const response = await ajax("/rewinds");
this.rewind = response.reports;
this.totalAvailable = response.total_available;
// Preload next report to maintain buffer
this.preloadNextReports();
2024-12-13 17:00:17 +01:00
} catch (e) {
popupAjaxError(e);
2024-12-20 18:56:02 +01:00
} finally {
this.loadingRewind = false;
2024-12-13 17:00:17 +01:00
}
}
2025-11-26 15:12:59 -03:00
@action
async preloadNextReports() {
// Load reports to maintain BUFFER_SIZE ahead of current position
const currentIndex = this.rewind.length;
const targetIndex = currentIndex + this.BUFFER_SIZE;
if (
this.loadingNextReport ||
currentIndex >= this.totalAvailable ||
targetIndex > this.totalAvailable
) {
return;
}
try {
this.loadingNextReport = true;
const response = await ajax(`/rewinds/${currentIndex}`);
this.rewind = [...this.rewind, response.report];
// Continue preloading if we haven't reached buffer size yet
if (this.rewind.length < targetIndex) {
this.preloadNextReports();
}
} catch (e) {
// Silently fail for preloading - user already has content to view
// eslint-disable-next-line no-console
console.error("Failed to preload report:", e);
} finally {
this.loadingNextReport = false;
}
}
2024-12-13 17:51:06 +01:00
@action
toggleFullScreen() {
this.fullScreen = !this.fullScreen;
}
2024-12-20 18:56:02 +01:00
@action
handleEscape(event) {
if (this.fullScreen && event.key === "Escape") {
this.fullScreen = false;
2024-12-13 17:51:06 +01:00
}
}
2025-01-03 09:42:42 -06:00
@action
2025-11-18 09:03:41 -05:00
handleBackdropClick(event) {
if (this.fullScreen && event.target === event.currentTarget) {
this.fullScreen = false;
2025-01-03 16:52:21 +01:00
}
}
@action
registerRewindContainer(element) {
this.rewindContainer = element;
2025-01-03 09:42:42 -06:00
}
2025-11-26 15:12:59 -03:00
@action
cleanup() {
if (this.scrollWrapper) {
this.scrollWrapper.removeEventListener("scroll", this.handleScroll);
}
}
2024-12-13 17:00:17 +01:00
<template>
2024-12-13 17:51:06 +01:00
<div
2024-12-20 18:56:02 +01:00
class={{concatClass
"rewind-container"
(if this.fullScreen "-fullscreen")
}}
2024-12-13 17:51:06 +01:00
{{didInsert this.loadRewind}}
2025-11-26 15:12:59 -03:00
{{willDestroy this.cleanup}}
2024-12-20 18:56:02 +01:00
{{on "keydown" this.handleEscape}}
2025-11-18 09:03:41 -05:00
{{on "click" this.handleBackdropClick}}
2025-01-03 16:52:21 +01:00
{{didInsert this.registerRewindContainer}}
2024-12-20 18:56:02 +01:00
tabindex="0"
2024-12-13 17:51:06 +01:00
>
2024-12-20 18:56:02 +01:00
<div class="rewind">
2025-01-13 10:48:23 -06:00
<RewindHeader />
2024-12-20 18:56:02 +01:00
{{#if this.loadingRewind}}
<div class="rewind-loader">
<div class="spinner small"></div>
2025-11-18 09:03:41 -05:00
<div class="rewind-loader__text">
{{i18n "discourse_rewind.loading"}}
</div>
2024-12-13 17:51:06 +01:00
</div>
2024-12-20 18:56:02 +01:00
{{else}}
<DButton
2025-11-18 09:03:41 -05:00
class="btn-default rewind__exit-fullscreen-btn --special-kbd"
2024-12-20 18:56:02 +01:00
@icon={{if this.fullScreen "discourse-compress" "discourse-expand"}}
@action={{this.toggleFullScreen}}
/>
<div
class="rewind__scroll-wrapper"
{{didInsert this.registerScrollWrapper}}
>
2025-01-09 16:52:32 +01:00
2024-12-20 18:56:02 +01:00
{{#each this.rewind as |report|}}
2025-01-02 22:15:10 +01:00
<div class={{concatClass "rewind-report" report.identifier}}>
2025-01-09 17:55:58 +01:00
{{#if (eq report.identifier "fbff")}}
<FBFF @report={{report}} />
{{else if (eq report.identifier "reactions")}}
2024-12-20 18:56:02 +01:00
<Reactions @report={{report}} />
2025-01-13 13:12:12 +01:00
{{else if (eq report.identifier "top-words")}}
<TopWords @report={{report}} />
2025-01-03 17:25:18 +01:00
{{else if (eq report.identifier "best-posts")}}
<BestPosts @report={{report}} />
{{else if (eq report.identifier "best-topics")}}
<BestTopics @report={{report}} />
2024-12-20 18:56:02 +01:00
{{else if (eq report.identifier "activity-calendar")}}
<ActivityCalendar @report={{report}} />
{{else if (eq report.identifier "most-viewed-tags")}}
<MostViewedTags @report={{report}} />
2025-01-07 23:26:17 +01:00
{{else if (eq report.identifier "reading-time")}}
<ReadingTime @report={{report}} />
{{else if (eq report.identifier "most-viewed-categories")}}
<MostViewedCategories @report={{report}} />
2024-12-20 18:56:02 +01:00
{{/if}}
</div>
{{/each}}
</div>
{{#if this.showPrev}}
<DButton
class="rewind__prev-btn"
@icon="chevron-left"
@action={{this.prev}}
/>
{{/if}}
{{#if this.showNext}}
<DButton
class="rewind__next-btn"
@icon="chevron-right"
@action={{this.next}}
/>
{{/if}}
2024-12-13 17:51:06 +01:00
{{/if}}
2024-12-20 18:56:02 +01:00
</div>
2024-12-13 17:00:17 +01:00
</div>
</template>
}