/
topics.js
182 lines (149 loc) · 4.33 KB
/
topics.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import { action } from "@ember/object";
import { inject as service } from "@ember/service";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { filterTypeForMode } from "discourse/lib/filter-mode";
import { userPath } from "discourse/lib/url";
import I18n from "discourse-i18n";
export default class DiscoveryTopics extends Component {
@service router;
@service composer;
@service modal;
@service currentUser;
@service topicTrackingState;
@service site;
@tracked loadingNew;
get redirectedReason() {
return this.currentUser?.user_option.redirected_to_top?.reason;
}
get order() {
return this.args.model.get("params.order");
}
get ascending() {
return this.args.model.get("params.ascending") === "true";
}
get hasTopics() {
return this.args.model.get("topics.length") > 0;
}
get allLoaded() {
return !this.args.model.get("more_topics_url");
}
get latest() {
return filterTypeForMode(this.args.model.filter) === "latest";
}
get top() {
return filterTypeForMode(this.args.model.filter) === "top";
}
get hot() {
return filterTypeForMode(this.args.model.filter) === "hot";
}
get new() {
return filterTypeForMode(this.args.model.filter) === "new";
}
// Show newly inserted topics
@action
async showInserted(event) {
event?.preventDefault();
if (this.args.model.loadingBefore) {
return; // Already loading
}
const { topicTrackingState } = this;
try {
const topicIds = [...topicTrackingState.newIncoming];
await this.args.model.loadBefore(topicIds, true);
topicTrackingState.clearIncoming(topicIds);
} catch (e) {
popupAjaxError(e);
}
}
get showTopicsAndRepliesToggle() {
return this.new && this.currentUser?.new_new_view_enabled;
}
get newRepliesCount() {
this.topicTrackingState.get("messageCount"); // Autotrack this
if (this.currentUser?.new_new_view_enabled) {
return this.topicTrackingState.countUnread({
categoryId: this.args.category?.id,
noSubcategories: this.args.noSubcategories,
tagId: this.args.tag?.id,
});
} else {
return 0;
}
}
get newTopicsCount() {
this.topicTrackingState.get("messageCount"); // Autotrack this
if (this.currentUser?.new_new_view_enabled) {
return this.topicTrackingState.countNew({
categoryId: this.args.category?.id,
noSubcategories: this.args.noSubcategories,
tagId: this.args.tag?.id,
});
} else {
return 0;
}
}
get showTopicPostBadges() {
return !this.new || this.currentUser?.new_new_view_enabled;
}
get footerMessage() {
const topicsLength = this.args.model.get("topics.length");
if (!this.allLoaded) {
return;
}
const { category, tag } = this.args;
if (category) {
return I18n.t("topics.bottom.category", {
category: category.get("name"),
});
} else if (tag) {
return I18n.t("topics.bottom.tag", {
tag: tag.id,
});
} else {
const split = (this.args.model.get("filter") || "").split("/");
if (topicsLength === 0) {
return I18n.t("topics.none." + split[0], {
category: split[1],
});
} else {
return I18n.t("topics.bottom." + split[0], {
category: split[1],
});
}
}
}
get footerEducation() {
const topicsLength = this.args.model.get("topics.length");
if (!this.allLoaded || topicsLength > 0 || !this.currentUser) {
return;
}
const segments = (this.args.model.get("filter") || "").split("/");
let tab = segments[segments.length - 1];
if (tab !== "new" && tab !== "unread") {
return;
}
if (tab === "new" && this.currentUser.new_new_view_enabled) {
tab = "new_new";
}
return I18n.t("topics.none.educate." + tab, {
userPrefsUrl: userPath(
`${this.currentUser.get("username_lower")}/preferences/tracking`
),
});
}
get renderNewListHeaderControls() {
return (
this.site.mobileView &&
this.showTopicsAndRepliesToggle &&
!this.args.bulkSelectEnabled
);
}
get expandGloballyPinned() {
return !this.expandAllPinned;
}
get expandAllPinned() {
return this.args.tag || this.args.category;
}
}