2021-02-04 20:36:29 +03:00
|
|
|
import Controller from '@ember/controller';
|
2021-02-25 12:57:46 +03:00
|
|
|
import {action} from '@ember/object';
|
2021-02-04 20:36:29 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2022-03-07 16:26:52 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2023-03-01 14:15:29 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
2022-05-17 10:34:34 +03:00
|
|
|
|
|
|
|
// Options 30 and 90 need an extra day to be able to distribute ticks/gridlines evenly
|
|
|
|
const DAYS_OPTIONS = [{
|
|
|
|
name: '7 Days',
|
|
|
|
value: 7
|
|
|
|
}, {
|
|
|
|
name: '30 Days',
|
|
|
|
value: 30 + 1
|
|
|
|
}, {
|
|
|
|
name: '90 Days',
|
|
|
|
value: 90 + 1
|
|
|
|
}];
|
2021-02-04 20:36:29 +03:00
|
|
|
|
|
|
|
export default class DashboardController extends Controller {
|
2022-05-17 10:34:34 +03:00
|
|
|
@service dashboardStats;
|
2024-03-27 20:37:37 +03:00
|
|
|
@service feature;
|
2022-09-28 14:56:38 +03:00
|
|
|
@service membersUtils;
|
2023-03-01 14:15:29 +03:00
|
|
|
@service mentionUtils;
|
2024-03-27 20:37:37 +03:00
|
|
|
@service onboarding;
|
|
|
|
@service store;
|
2023-03-01 14:15:29 +03:00
|
|
|
|
|
|
|
@tracked mentions = [];
|
|
|
|
@tracked hasNewMentions = false;
|
2021-02-18 17:17:10 +03:00
|
|
|
|
2022-05-17 10:34:34 +03:00
|
|
|
daysOptions = DAYS_OPTIONS;
|
2021-02-24 18:59:01 +03:00
|
|
|
|
2023-03-01 14:15:29 +03:00
|
|
|
@action
|
|
|
|
async loadMentions() {
|
|
|
|
if (!this.feature.get('webmentions')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.mentions = await this.store.query('mention', {unique: true, limit: 5, order: 'created_at desc'});
|
|
|
|
this.hasNewMentions = this.checkHasNewMentions();
|
|
|
|
|
|
|
|
// Load grouped mentions
|
|
|
|
await this.mentionUtils.loadGroupedMentions(this.mentions);
|
|
|
|
}
|
|
|
|
|
|
|
|
checkHasNewMentions() {
|
|
|
|
if (!this.mentions) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const firstMention = this.mentions.firstObject;
|
|
|
|
if (!firstMention) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const lastId = localStorage.getItem('lastMentionRead');
|
|
|
|
return firstMention.id !== lastId;
|
|
|
|
} catch (e) {
|
|
|
|
// localstorage disabled or not supported
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
markMentionsRead() {
|
|
|
|
try {
|
|
|
|
if (this.mentions) {
|
|
|
|
const firstMention = this.mentions.firstObject;
|
|
|
|
if (firstMention) {
|
|
|
|
localStorage.setItem('lastMentionRead', firstMention.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// localstorage disabled or not supported
|
|
|
|
}
|
|
|
|
|
|
|
|
// The opening of the popup breaks if we change hasNewMentions inside the handling (propably due to a rerender, so we need to delay it)
|
|
|
|
if (this.hasNewMentions) {
|
|
|
|
setTimeout(() => {
|
|
|
|
this.hasNewMentions = false;
|
|
|
|
}, 20);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-05-17 10:34:34 +03:00
|
|
|
@task
|
|
|
|
*loadSiteStatusTask() {
|
|
|
|
yield this.dashboardStats.loadSiteStatus();
|
|
|
|
return {};
|
2021-04-20 11:08:15 +03:00
|
|
|
}
|
|
|
|
|
2022-09-28 14:56:38 +03:00
|
|
|
@action
|
2022-05-17 10:34:34 +03:00
|
|
|
onDaysChange(selected) {
|
|
|
|
this.days = selected.value;
|
|
|
|
}
|
2022-03-07 16:26:52 +03:00
|
|
|
|
2022-05-17 10:34:34 +03:00
|
|
|
get days() {
|
|
|
|
return this.dashboardStats.chartDays;
|
|
|
|
}
|
2022-03-07 16:26:52 +03:00
|
|
|
|
2022-05-17 10:34:34 +03:00
|
|
|
set days(days) {
|
|
|
|
this.dashboardStats.chartDays = days;
|
2022-03-07 16:26:52 +03:00
|
|
|
}
|
|
|
|
|
2022-05-17 10:34:34 +03:00
|
|
|
get selectedDaysOption() {
|
|
|
|
return this.daysOptions.find(d => d.value === this.days);
|
2021-02-18 20:13:51 +03:00
|
|
|
}
|
|
|
|
|
2022-05-17 10:34:34 +03:00
|
|
|
get isLoading() {
|
|
|
|
return this.dashboardStats.siteStatus === null;
|
2021-02-24 18:59:01 +03:00
|
|
|
}
|
2021-02-25 12:57:46 +03:00
|
|
|
|
2022-05-17 10:34:34 +03:00
|
|
|
get totalMembers() {
|
|
|
|
return this.dashboardStats.memberCounts?.total ?? 0;
|
2021-02-25 12:57:46 +03:00
|
|
|
}
|
2022-03-07 16:26:52 +03:00
|
|
|
|
2022-05-17 10:34:34 +03:00
|
|
|
get isTotalMembersZero() {
|
|
|
|
return this.dashboardStats.memberCounts && this.totalMembers === 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
get hasPaidTiers() {
|
|
|
|
return this.dashboardStats.siteStatus?.hasPaidTiers;
|
|
|
|
}
|
|
|
|
|
|
|
|
get areNewslettersEnabled() {
|
|
|
|
return this.dashboardStats.siteStatus?.newslettersEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
get areMembersEnabled() {
|
|
|
|
return this.dashboardStats.siteStatus?.membersEnabled;
|
2022-03-07 16:26:52 +03:00
|
|
|
}
|
2021-02-18 17:17:10 +03:00
|
|
|
}
|