1
0
Fork 0
mirror of https://github.com/TryGhost/Ghost-Admin.git synced 2023-12-14 02:33:04 +01:00
Ghost-Admin/app/services/whats-new.js
Kevin Ansfield a63943edd6
Added "What's new" indicator and modal to highlight recent updates (#1292)
no issue

- adds `whats-new` service that fetches the changelog from ghost.org and exposes the latest changelog entries
- trigger a background fetch of the changelog from ghost.org when first loading the admin when logged in, or after signing in
- adds a "What's new" menu item next to the user popup menu
- adds an indicator to the user menu button and what's new menu item if there are unseen changelog entries
- closing the changelog modal will update the "last seen date", clearing both indicators
2019-08-23 10:01:27 +01:00

94 lines
2.8 KiB
JavaScript

import Service from '@ember/service';
import fetch from 'fetch';
import moment from 'moment';
import {action} from '@ember/object';
import {computed} from '@ember/object';
import {isEmpty} from '@ember/utils';
import {inject as service} from '@ember/service';
import {task} from 'ember-concurrency';
export default Service.extend({
session: service(),
entries: null,
changelogUrl: 'https://ghost.org/blog/',
isShowingModal: false,
_user: null,
init() {
this._super(...arguments);
this.entries = [];
},
whatsNewSettings: computed('_user.accessibility', function () {
let settingsJson = this.get('_user.accessibility') || '{}';
let settings = JSON.parse(settingsJson);
return settings.whatsNew;
}),
hasNew: computed('whatsNewSettings.lastSeenDate', 'entries.[]', function () {
if (isEmpty(this.entries)) {
return false;
}
let [latestEntry] = this.entries;
let lastSeenDate = this.get('whatsNewSettings.lastSeenDate') || '2019-01-01 00:00:00';
let lastSeenMoment = moment(lastSeenDate);
let latestDate = latestEntry.published_at;
let latestMoment = moment(latestDate || lastSeenDate);
return latestMoment.isAfter(lastSeenMoment);
}),
showModal: action(function () {
this.set('isShowingModal', true);
}),
closeModal: action(function () {
this.set('isShowingModal', false);
this.updateLastSeen.perform();
}),
fetchLatest: task(function* () {
try {
// we should already be logged in at this point so lets grab the user
// record and store it locally so that we don't have to deal with
// session.user being a promise and causing issues with CPs
let user = yield this.session.user;
this.set('_user', user);
let response = yield fetch('https://ghost.org/changelog.json');
if (!response.ok) {
// eslint-disable-next-line
return console.error('Failed to fetch changelog', {response});
}
let result = yield response.json();
this.set('entries', result.posts || []);
this.set('changelogUrl', result.changelogUrl);
} catch (e) {
console.error(e); // eslint-disable-line
}
}),
updateLastSeen: task(function* () {
let settingsJson = this._user.accessibility || '{}';
let settings = JSON.parse(settingsJson);
let [latestEntry] = this.entries;
if (!latestEntry) {
return;
}
if (!settings.whatsNew) {
settings.whatsNew = {};
}
settings.whatsNew.lastSeenDate = latestEntry.published_at;
this._user.set('accessibility', JSON.stringify(settings));
yield this._user.save();
})
});