various-fe: add unread counts to title

This commit is contained in:
Liam Fitzgerald 2020-04-17 16:51:51 +10:00
parent 18878727b0
commit 63fc6ed513
4 changed files with 44 additions and 2 deletions

View File

@ -20,6 +20,7 @@ export class Root extends Component {
super(props); super(props);
this.state = store.state; this.state = store.state;
this.totalUnreads = 0;
store.setStateHandler(this.setState.bind(this)); store.setStateHandler(this.setState.bind(this));
} }
@ -33,6 +34,7 @@ export class Root extends Component {
let messagePreviews = {}; let messagePreviews = {};
let unreads = {}; let unreads = {};
let totalUnreads = 0;
Object.keys(state.inbox).forEach((stat) => { Object.keys(state.inbox).forEach((stat) => {
let envelopes = state.inbox[stat].envelopes; let envelopes = state.inbox[stat].envelopes;
@ -42,9 +44,16 @@ export class Root extends Component {
messagePreviews[stat] = envelopes[0]; messagePreviews[stat] = envelopes[0];
} }
unreads[stat] = const unread = Math.max(state.inbox[stat].config.length - state.inbox[stat].config.read, 0)
state.inbox[stat].config.length > state.inbox[stat].config.read; unreads[stat] = !!unread;
if(unread) {
totalUnreads += unread;
}
}); });
if(totalUnreads !== this.totalUnreads) {
document.title = totalUnreads > 0 ? `Chat - (${totalUnreads})` : 'Chat';
this.totalUnreads = totalUnreads;
}
let invites = !!state.invites ? state.invites : {'/chat': {}, '/contacts': {}}; let invites = !!state.invites ? state.invites : {'/chat': {}, '/contacts': {}};

View File

@ -23,6 +23,7 @@ export class Root extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.totalUnseen = 0;
this.state = store.state; this.state = store.state;
store.setStateHandler(this.setState.bind(this)); store.setStateHandler(this.setState.bind(this));
} }
@ -41,8 +42,23 @@ export class Root extends Component {
const associations = !!state.associations ? state.associations : {link: {}, contacts: {}}; const associations = !!state.associations ? state.associations : {link: {}, contacts: {}};
let links = !!state.links ? state.links : {}; let links = !!state.links ? state.links : {};
let comments = !!state.comments ? state.comments : {}; let comments = !!state.comments ? state.comments : {};
const seen = !!state.seen ? state.seen : {}; const seen = !!state.seen ? state.seen : {};
const totalUnseen = _.reduce(
seen,
(acc, links) => acc + _.reduce(links, (total, hasSeen) => total + (hasSeen ? 0 : 1), 0),
0
);
if(totalUnseen !== this.totalUnseen) {
document.title = totalUnseen !== 0 ? `Links - (${totalUnseen})` : 'Links';
this.totalUnseen = totalUnseen;
}
const invites = state.invites ? const invites = state.invites ?
state.invites : {}; state.invites : {};

View File

@ -68,9 +68,11 @@ export class LinkUpdateReducer {
// stub in a comment count, which is more or less guaranteed to be 0 // stub in a comment count, which is more or less guaranteed to be 0
data.pages = data.pages.map(submission => { data.pages = data.pages.map(submission => {
submission.commentCount = 0; submission.commentCount = 0;
state.seen[path][submission.url] = false;
return submission; return submission;
}); });
// add the new submissions to state, update totals // add the new submissions to state, update totals
state.links[path] = this._addNewItems( state.links[path] = this._addNewItems(
data.pages, state.links[path] data.pages, state.links[path]

View File

@ -1,5 +1,6 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import { BrowserRouter, Route, Link } from "react-router-dom"; import { BrowserRouter, Route, Link } from "react-router-dom";
import _ from 'lodash';
import { store } from '/store'; import { store } from '/store';
import { api } from '/api'; import { api } from '/api';
import { Skeleton } from '/components/skeleton'; import { Skeleton } from '/components/skeleton';
@ -15,6 +16,7 @@ export class Root extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.unreadTotal = 0;
this.state = store.state; this.state = store.state;
store.setStateHandler(this.setState.bind(this)); store.setStateHandler(this.setState.bind(this));
} }
@ -31,6 +33,19 @@ export class Root extends Component {
let associations = !!state.associations ? state.associations : {contacts: {}} let associations = !!state.associations ? state.associations : {contacts: {}}
let selectedGroups = !!state.selectedGroups ? state.selectedGroups : []; let selectedGroups = !!state.selectedGroups ? state.selectedGroups : [];
const unreadTotal = _.chain(state.notebooks)
.values()
.map(_.values)
.flatten() // flatten into array of notebooks
.map('num-unread')
.reduce((acc, count) => acc + count, 0)
.value();
if(this.unreadTotal !== unreadTotal) {
document.title = unreadTotal > 0 ? `Publish - (${unreadTotal})` : 'Publish';
this.unreadTotal = unreadTotal;
}
return ( return (
<BrowserRouter> <BrowserRouter>
<Route exact path="/~publish" <Route exact path="/~publish"