From d3786e68b1ef5fc4ccfe9c10c19431e29bcd9641 Mon Sep 17 00:00:00 2001 From: cha0s Date: Thu, 10 Dec 2020 00:19:10 -0600 Subject: [PATCH] chore: unread back --- app/src/react/components/unread.jsx | 49 +++++++++++++++++++++++++++++ app/src/react/index.jsx | 3 +- 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 app/src/react/components/unread.jsx diff --git a/app/src/react/components/unread.jsx b/app/src/react/components/unread.jsx new file mode 100644 index 0000000..61e4a00 --- /dev/null +++ b/app/src/react/components/unread.jsx @@ -0,0 +1,49 @@ +import {useDispatch, useSelector} from 'react-redux'; + +import {useEffect} from 'react'; + +import { + blurredSelector, + setBlurred, + unreadChannelSelector, + unreadUserSelector, +} from '@reddichat/user/client'; + +const Unread = () => { + const dispatch = useDispatch(); + const blurred = useSelector(blurredSelector); + const unreadChannel = useSelector(unreadChannelSelector); + const unreadUser = useSelector(unreadUserSelector); + useEffect(() => { + window.addEventListener('blur', () => dispatch(setBlurred(true))); + window.addEventListener('focus', () => dispatch(setBlurred(false))); + }, [dispatch]); + useEffect(() => { + if (!blurred) { + return undefined; + } + if (unreadChannel + unreadUser > 0) { + const message = [ + ['chat', unreadChannel], + ['pm', unreadUser], + ] + .filter(([, value]) => value > 0) + .map(([key, value]) => `${value} ${key}${value > 1 ? 's' : ''}`) + .join(' • '); + const previousTitle = window.document.title; + const title = `✧~✧ ${message.toUpperCase()} ✧~✧ | ${previousTitle}`; + window.document.title = title; + const handle = setInterval(() => { + window.document.title = window.document.title === title ? previousTitle : title; + }, 1000); + return () => { + window.document.title = previousTitle; + clearInterval(handle); + }; + } + return undefined; + }, [blurred, unreadChannel, unreadUser]); + return null; +}; + +export default Unread; diff --git a/app/src/react/index.jsx b/app/src/react/index.jsx index a33df48..304901d 100644 --- a/app/src/react/index.jsx +++ b/app/src/react/index.jsx @@ -7,6 +7,7 @@ import {Provider} from 'react-redux'; import App from 'components/app'; import Dispatcher from 'components/dispatcher'; +import Unread from 'components/unread'; import {configureStore} from '@reddichat/state/client'; @@ -18,9 +19,9 @@ const Index = async (latus) => { - {/* */} + ); };