discourse/app/assets/javascripts/admin/addon/controllers/admin-dashboard.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

138 lines
3.5 KiB
JavaScript
Raw Normal View History

import Controller, { inject as controller } from "@ember/controller";
import { action, computed } from "@ember/object";
import { inject as service } from "@ember/service";
import { setting } from "discourse/lib/computed";
import discourseComputed from "discourse-common/utils/decorators";
2019-04-01 18:39:49 +08:00
import AdminDashboard from "admin/models/admin-dashboard";
import VersionCheck from "admin/models/version-check";
2019-04-01 18:39:49 +08:00
const PROBLEMS_CHECK_MINUTES = 1;
export default class AdminDashboardController extends Controller {
@service router;
@service siteSettings;
@controller("exception") exceptionController;
isLoading = false;
dashboardFetchedAt = null;
@setting("version_checks") showVersionChecks;
2019-04-01 18:39:49 +08:00
@discourseComputed(
"lowPriorityProblems.length",
"highPriorityProblems.length"
)
foundProblems(lowPriorityProblemsLength, highPriorityProblemsLength) {
const problemsLength =
lowPriorityProblemsLength + highPriorityProblemsLength;
return this.currentUser.admin && problemsLength > 0;
}
2019-04-01 18:39:49 +08:00
@computed("siteSettings.dashboard_visible_tabs")
get visibleTabs() {
return (this.siteSettings.dashboard_visible_tabs || "")
.split("|")
.filter(Boolean);
}
@computed("visibleTabs")
get isModerationTabVisible() {
return this.visibleTabs.includes("moderation");
}
@computed("visibleTabs")
get isSecurityTabVisible() {
return this.visibleTabs.includes("security");
}
@computed("visibleTabs")
get isReportsTabVisible() {
return this.visibleTabs.includes("reports");
}
@computed("visibleTabs")
get isNewFeaturesTabVisible() {
return this.visibleTabs.includes("features");
}
2019-04-01 18:39:49 +08:00
fetchProblems() {
if (this.isLoadingProblems) {
return;
}
2019-04-01 18:39:49 +08:00
if (
!this.problemsFetchedAt ||
2019-04-01 18:39:49 +08:00
moment().subtract(PROBLEMS_CHECK_MINUTES, "minutes").toDate() >
this.problemsFetchedAt
2019-04-01 18:39:49 +08:00
) {
this._loadProblems();
}
}
fetchDashboard() {
2019-04-01 18:39:49 +08:00
const versionChecks = this.siteSettings.version_checks;
if (this.isLoading || !versionChecks) {
return;
}
2019-04-01 18:39:49 +08:00
if (
!this.dashboardFetchedAt ||
moment().subtract(30, "minutes").toDate() > this.dashboardFetchedAt
) {
2019-04-01 18:39:49 +08:00
this.set("isLoading", true);
2018-06-15 23:03:24 +08:00
2019-04-01 18:39:49 +08:00
AdminDashboard.fetch()
.then((model) => {
let properties = {
dashboardFetchedAt: new Date(),
};
2018-06-15 23:03:24 +08:00
2019-04-01 18:39:49 +08:00
if (versionChecks) {
properties.versionCheck = VersionCheck.create(model.version_check);
}
properties.hasUnseenFeatures = model.hasUnseenFeatures;
2018-06-15 23:03:24 +08:00
2019-04-01 18:39:49 +08:00
this.setProperties(properties);
})
.catch((e) => {
this.exceptionController.set("thrown", e.jqXHR);
this.router.replaceWith("exception");
})
.finally(() => {
2019-04-01 18:39:49 +08:00
this.set("isLoading", false);
});
}
}
2019-04-01 18:39:49 +08:00
_loadProblems() {
this.setProperties({
loadingProblems: true,
problemsFetchedAt: new Date(),
});
AdminDashboard.fetchProblems()
.then((model) => {
this.set(
"highPriorityProblems",
model.problems.filterBy("priority", "high")
);
this.set(
"lowPriorityProblems",
model.problems.filterBy("priority", "low")
);
})
2019-04-01 18:39:49 +08:00
.finally(() => this.set("loadingProblems", false));
}
2019-04-01 18:39:49 +08:00
@discourseComputed("problemsFetchedAt")
2019-04-01 18:39:49 +08:00
problemsTimestamp(problemsFetchedAt) {
return moment(problemsFetchedAt).locale("en").format("LLL");
}
2013-09-17 02:08:55 +08:00
@action
refreshProblems() {
this._loadProblems();
}
}