snort/src/pages/Layout.js

82 lines
2.6 KiB
JavaScript
Raw Normal View History

2022-12-29 15:36:40 +00:00
import "./Layout.css";
2022-12-30 23:35:02 +00:00
import { useEffect } from "react"
2022-12-27 23:46:13 +00:00
import { useDispatch, useSelector } from "react-redux";
2022-12-18 14:51:47 +00:00
import { useNavigate } from "react-router-dom";
2022-12-29 15:36:40 +00:00
import { faBell } from "@fortawesome/free-solid-svg-icons";
2023-01-03 10:54:18 +00:00
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
2022-12-29 15:36:40 +00:00
2022-12-30 23:35:02 +00:00
import { System } from ".."
2022-12-27 23:46:13 +00:00
import ProfileImage from "../element/ProfileImage";
import { init } from "../state/Login";
2022-12-29 22:23:41 +00:00
import useLoginFeed from "../feed/LoginFeed";
import useUsersCache from "../feed/UsersFeed";
2022-12-18 14:51:47 +00:00
export default function Layout(props) {
2022-12-27 23:46:13 +00:00
const dispatch = useDispatch();
2022-12-18 14:51:47 +00:00
const navigate = useNavigate();
2023-01-02 11:59:03 +00:00
const isInit = useSelector(s => s.login.loggedOut);
2022-12-27 23:46:13 +00:00
const key = useSelector(s => s.login.publicKey);
2022-12-18 14:51:47 +00:00
const relays = useSelector(s => s.login.relays);
2022-12-30 23:35:02 +00:00
const notifications = useSelector(s => s.login.notifications);
2023-01-02 11:15:13 +00:00
const readNotifications = useSelector(s => s.login.readNotifications);
2022-12-28 23:28:28 +00:00
useUsersCache();
useLoginFeed();
2022-12-18 14:51:47 +00:00
useEffect(() => {
2022-12-30 23:35:02 +00:00
if (relays) {
2022-12-28 23:28:28 +00:00
for (let [k, v] of Object.entries(relays)) {
2022-12-30 23:35:02 +00:00
System.ConnectToRelay(k, v);
2022-12-18 14:51:47 +00:00
}
}
2022-12-30 23:35:02 +00:00
}, [relays]);
2022-12-18 14:51:47 +00:00
2022-12-27 23:46:13 +00:00
useEffect(() => {
dispatch(init());
}, []);
2023-01-03 13:17:09 +00:00
async function goToNotifications(e) {
e.stopPropagation();
// request permissions to send notifications
2023-01-06 15:10:02 +00:00
if ("Notification" in window && Notification.permission !== "granted") {
2023-01-03 13:17:09 +00:00
try {
let res = await Notification.requestPermission();
console.debug(res);
} catch (e) {
console.error(e);
}
}
navigate("/notifications");
}
2022-12-29 15:36:40 +00:00
function accountHeader() {
2023-01-02 22:56:21 +00:00
const unreadNotifications = notifications?.filter(a => (a.created_at * 1000) > readNotifications).length ?? 0;
2022-12-29 15:36:40 +00:00
return (
<>
2023-01-03 13:17:09 +00:00
<div className="btn btn-rnd notifications" onClick={(e) => goToNotifications(e)}>
2022-12-29 15:36:40 +00:00
<FontAwesomeIcon icon={faBell} size="xl" />
2023-01-02 11:15:13 +00:00
{unreadNotifications}
2022-12-29 15:36:40 +00:00
</div>
2023-01-01 19:57:27 +00:00
<ProfileImage pubkey={key} />
2022-12-29 15:36:40 +00:00
</>
)
}
2023-01-02 11:59:03 +00:00
if (typeof isInit !== "boolean") {
return null;
}
2022-12-18 14:51:47 +00:00
return (
<div className="page">
<div className="header">
2023-01-09 11:00:23 +00:00
<div onClick={() => navigate("/")}>snort</div>
2022-12-18 14:51:47 +00:00
<div>
2022-12-29 15:36:40 +00:00
{key ? accountHeader() :
2022-12-27 23:46:13 +00:00
<div className="btn" onClick={() => navigate("/login")}>Login</div>
}
2022-12-18 14:51:47 +00:00
</div>
</div>
{props.children}
</div>
)
}