2022-10-19 05:21:03 +03:00
|
|
|
/**
|
|
|
|
* This component is responsible for updating the title of the page when
|
|
|
|
* different state changes occur.
|
|
|
|
* If the stream live state changes, or chat messages come in while the
|
|
|
|
* page is backgrounded, this component will update the title to reflect it. *
|
|
|
|
* @component
|
|
|
|
*/
|
|
|
|
import { FC, useEffect } from 'react';
|
|
|
|
import { useRecoilValue } from 'recoil';
|
|
|
|
import { serverStatusState, chatMessagesAtom } from '../stores/ClientConfigStore';
|
|
|
|
|
|
|
|
export const TitleNotifier: FC = () => {
|
|
|
|
const chatMessages = useRecoilValue(chatMessagesAtom);
|
|
|
|
const serverStatus = useRecoilValue(serverStatusState);
|
|
|
|
|
|
|
|
let backgrounded = false;
|
|
|
|
let defaultTitle = '';
|
|
|
|
|
2022-12-25 08:21:39 +03:00
|
|
|
const setTitle = (title: string) => {
|
|
|
|
console.trace('Debug: Setting title to', title);
|
|
|
|
window.document.title = title;
|
|
|
|
};
|
|
|
|
|
2022-10-19 05:21:03 +03:00
|
|
|
const onBlur = () => {
|
2022-12-25 08:21:39 +03:00
|
|
|
console.log('onBlur: Saving defaultTitle as', window.document.title);
|
2022-10-19 05:21:03 +03:00
|
|
|
backgrounded = true;
|
2022-10-24 09:08:46 +03:00
|
|
|
defaultTitle = window.document.title;
|
2022-10-19 05:21:03 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const onFocus = () => {
|
|
|
|
backgrounded = false;
|
2022-12-25 08:21:39 +03:00
|
|
|
setTitle(defaultTitle);
|
2022-10-19 05:21:03 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const listenForEvents = () => {
|
|
|
|
// Listen for events that should update the title
|
|
|
|
window.addEventListener('blur', onBlur);
|
|
|
|
window.addEventListener('focus', onFocus);
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-12-25 08:21:39 +03:00
|
|
|
console.log('useEffect: Saving defaultTitle as', window.document.title);
|
|
|
|
|
2022-10-19 05:21:03 +03:00
|
|
|
defaultTitle = window.document.title;
|
|
|
|
listenForEvents();
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener('focus', onFocus);
|
|
|
|
window.removeEventListener('blur', onBlur);
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const { online } = serverStatus;
|
|
|
|
|
|
|
|
if (!backgrounded || !online) {
|
|
|
|
return;
|
|
|
|
}
|
2022-12-25 08:21:39 +03:00
|
|
|
setTitle(`💬 :: ${defaultTitle}`);
|
2022-10-19 05:21:03 +03:00
|
|
|
}, [chatMessages]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!backgrounded) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { online } = serverStatus;
|
|
|
|
|
|
|
|
if (online) {
|
2022-12-25 08:21:39 +03:00
|
|
|
setTitle(` 🟢 :: ${defaultTitle}`);
|
2022-10-19 05:21:03 +03:00
|
|
|
} else if (!online) {
|
2022-12-25 08:21:39 +03:00
|
|
|
setTitle(` 🔴 :: ${defaultTitle}`);
|
2022-10-19 05:21:03 +03:00
|
|
|
}
|
|
|
|
}, [serverStatusState]);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|