snort/src/pages/Layout.js
2023-01-03 13:17:09 +00:00

82 lines
2.5 KiB
JavaScript

import "./Layout.css";
import { useEffect } from "react"
import { useDispatch, useSelector } from "react-redux";
import { useNavigate } from "react-router-dom";
import { faBell } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { System } from ".."
import ProfileImage from "../element/ProfileImage";
import { init } from "../state/Login";
import useLoginFeed from "../feed/LoginFeed";
import useUsersCache from "../feed/UsersFeed";
export default function Layout(props) {
const dispatch = useDispatch();
const navigate = useNavigate();
const isInit = useSelector(s => s.login.loggedOut);
const key = useSelector(s => s.login.publicKey);
const relays = useSelector(s => s.login.relays);
const notifications = useSelector(s => s.login.notifications);
const readNotifications = useSelector(s => s.login.readNotifications);
useUsersCache();
useLoginFeed();
useEffect(() => {
if (relays) {
for (let [k, v] of Object.entries(relays)) {
System.ConnectToRelay(k, v);
}
}
}, [relays]);
useEffect(() => {
dispatch(init());
}, []);
async function goToNotifications(e) {
e.stopPropagation();
// request permissions to send notifications
if (Notification.permission !== "granted") {
try {
let res = await Notification.requestPermission();
console.debug(res);
} catch (e) {
console.error(e);
}
}
navigate("/notifications");
}
function accountHeader() {
const unreadNotifications = notifications?.filter(a => (a.created_at * 1000) > readNotifications).length ?? 0;
return (
<>
<div className="btn btn-rnd notifications" onClick={(e) => goToNotifications(e)}>
<FontAwesomeIcon icon={faBell} size="xl" />
{unreadNotifications}
</div>
<ProfileImage pubkey={key} />
</>
)
}
if (typeof isInit !== "boolean") {
return null;
}
return (
<div className="page">
<div className="header">
<div onClick={() => navigate("/")}>n o s t r</div>
<div>
{key ? accountHeader() :
<div className="btn" onClick={() => navigate("/login")}>Login</div>
}
</div>
</div>
{props.children}
</div>
)
}