2022-12-29 15:36:40 +00:00
|
|
|
import "./Layout.css";
|
2023-02-05 12:32:34 +00:00
|
|
|
import { useEffect, useState } from "react"
|
2022-12-27 23:46:13 +00:00
|
|
|
import { useDispatch, useSelector } from "react-redux";
|
2023-01-12 12:00:44 +00:00
|
|
|
import { Outlet, useNavigate } from "react-router-dom";
|
2023-01-28 21:43:56 +00:00
|
|
|
import Envelope from "Icons/Envelope";
|
|
|
|
import Bell from "Icons/Bell";
|
|
|
|
import Search from "Icons/Search";
|
2022-12-29 15:36:40 +00:00
|
|
|
|
2023-01-20 11:11:50 +00:00
|
|
|
import { RootState } from "State/Store";
|
2023-02-05 06:29:46 +00:00
|
|
|
import { init, setRelays } from "State/Login";
|
2023-01-20 11:11:50 +00:00
|
|
|
import { System } from "Nostr/System"
|
|
|
|
import ProfileImage from "Element/ProfileImage";
|
|
|
|
import useLoginFeed from "Feed/LoginFeed";
|
|
|
|
import { totalUnread } from "Pages/MessagesPage";
|
2023-02-05 06:29:46 +00:00
|
|
|
import { SearchRelays, SnortPubKey } from 'Const';
|
2023-01-23 01:45:53 +00:00
|
|
|
import useEventPublisher from "Feed/EventPublisher";
|
2023-01-28 21:43:56 +00:00
|
|
|
import useModeration from "Hooks/useModeration";
|
2023-02-01 19:48:35 +00:00
|
|
|
import { IndexedUDB, useDb } from "State/Users/Db";
|
2023-02-01 20:34:23 +00:00
|
|
|
import { db } from "Db";
|
2023-02-05 06:29:46 +00:00
|
|
|
import { bech32ToHex } from "Util";
|
2023-02-05 12:32:34 +00:00
|
|
|
import { NoteCreator } from "Element/NoteCreator";
|
|
|
|
import Plus from "Icons/Plus";
|
2023-01-28 21:43:56 +00:00
|
|
|
|
2022-12-18 14:51:47 +00:00
|
|
|
|
2023-01-16 18:14:08 +00:00
|
|
|
export default function Layout() {
|
2023-02-05 12:32:34 +00:00
|
|
|
const [show, setShow] = useState(false)
|
2022-12-27 23:46:13 +00:00
|
|
|
const dispatch = useDispatch();
|
2022-12-18 14:51:47 +00:00
|
|
|
const navigate = useNavigate();
|
2023-02-05 06:29:46 +00:00
|
|
|
const { loggedOut, publicKey, relays, notifications, readNotifications, dms, preferences, newUserKey } = useSelector((s: RootState) => s.login);
|
2023-01-28 21:43:56 +00:00
|
|
|
const { isMuted } = useModeration();
|
|
|
|
const filteredDms = dms.filter(a => !isMuted(a.pubkey))
|
2023-02-01 19:48:35 +00:00
|
|
|
const usingDb = useDb();
|
2023-01-23 01:45:53 +00:00
|
|
|
const pub = useEventPublisher();
|
2022-12-28 23:28:28 +00:00
|
|
|
useLoginFeed();
|
2022-12-18 14:51:47 +00:00
|
|
|
|
2023-01-28 16:58:52 +00:00
|
|
|
useEffect(() => {
|
2023-01-25 13:37:35 +00:00
|
|
|
System.nip42Auth = pub.nip42Auth
|
2023-02-01 19:48:35 +00:00
|
|
|
}, [pub])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
System.UserDb = usingDb;
|
|
|
|
}, [usingDb])
|
2023-01-25 13:37:35 +00:00
|
|
|
|
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
|
|
|
}
|
2023-01-16 11:53:15 +00:00
|
|
|
for (let [k, v] of System.Sockets) {
|
2023-01-24 12:33:18 +00:00
|
|
|
if (!relays[k] && !SearchRelays.has(k)) {
|
2023-01-09 12:40:10 +00:00
|
|
|
System.DisconnectRelay(k);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2023-01-20 18:59:08 +00:00
|
|
|
function setTheme(theme: "light" | "dark") {
|
2023-01-20 17:07:14 +00:00
|
|
|
const elm = document.documentElement;
|
2023-01-20 18:59:08 +00:00
|
|
|
if (theme === "light" && !elm.classList.contains("light")) {
|
2023-01-20 17:07:14 +00:00
|
|
|
elm.classList.add("light");
|
2023-01-20 18:59:08 +00:00
|
|
|
} else if (theme === "dark" && elm.classList.contains("light")) {
|
2023-01-20 17:07:14 +00:00
|
|
|
elm.classList.remove("light");
|
|
|
|
}
|
2023-01-20 18:59:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
let osTheme = window.matchMedia("(prefers-color-scheme: light)");
|
2023-02-05 06:29:46 +00:00
|
|
|
setTheme(preferences.theme === "system" && osTheme.matches ? "light" : preferences.theme === "light" ? "light" : "dark");
|
2023-01-20 18:59:08 +00:00
|
|
|
|
|
|
|
osTheme.onchange = (e) => {
|
2023-02-05 06:29:46 +00:00
|
|
|
if (preferences.theme === "system") {
|
2023-01-20 18:59:08 +00:00
|
|
|
setTheme(e.matches ? "light" : "dark");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return () => { osTheme.onchange = null; }
|
2023-02-05 06:29:46 +00:00
|
|
|
}, [preferences.theme]);
|
2023-01-20 17:07:14 +00:00
|
|
|
|
2022-12-27 23:46:13 +00:00
|
|
|
useEffect(() => {
|
2023-02-01 19:48:35 +00:00
|
|
|
// check DB support then init
|
|
|
|
IndexedUDB.isAvailable()
|
2023-02-01 20:34:23 +00:00
|
|
|
.then(async a => {
|
|
|
|
const dbType = a ? "indexdDb" : "redux";
|
|
|
|
|
|
|
|
// cleanup on load
|
|
|
|
if (dbType === "indexdDb") {
|
|
|
|
await db.feeds.clear();
|
|
|
|
const now = Math.floor(new Date().getTime() / 1000);
|
|
|
|
|
|
|
|
const cleanupEvents = await db.events
|
|
|
|
.where("created_at")
|
|
|
|
.above(now - (60 * 60))
|
|
|
|
.primaryKeys();
|
|
|
|
console.debug(`Cleanup ${cleanupEvents.length} events`);
|
|
|
|
await db.events.bulkDelete(cleanupEvents)
|
|
|
|
}
|
|
|
|
|
|
|
|
console.debug(`Using db: ${dbType}`);
|
|
|
|
dispatch(init(dbType));
|
2023-02-01 19:48:35 +00:00
|
|
|
})
|
|
|
|
|
2022-12-27 23:46:13 +00:00
|
|
|
}, []);
|
|
|
|
|
2023-02-05 06:29:46 +00:00
|
|
|
async function handleNewUser() {
|
|
|
|
try {
|
|
|
|
let rsp = await fetch("https://api.nostr.watch/v1/online");
|
|
|
|
if (rsp.ok) {
|
|
|
|
let online: string[] = await rsp.json();
|
|
|
|
let pickRandom = online.sort((a, b) => Math.random() >= 0.5 ? 1 : -1).slice(0, 4); // pick 4 random relays
|
|
|
|
|
|
|
|
let relayObjects = pickRandom.map(a => [a, { read: true, write: true }]);
|
|
|
|
dispatch(setRelays({
|
|
|
|
relays: Object.fromEntries(relayObjects),
|
|
|
|
createdAt: 1
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.warn(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
const ev = await pub.addFollow(bech32ToHex(SnortPubKey));
|
|
|
|
pub.broadcast(ev);
|
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (newUserKey === true) {
|
|
|
|
handleNewUser().catch(console.warn);
|
|
|
|
}
|
|
|
|
}, [newUserKey]);
|
|
|
|
|
2023-01-16 18:14:08 +00:00
|
|
|
async function goToNotifications(e: any) {
|
2023-01-03 13:17:09 +00:00
|
|
|
e.stopPropagation();
|
|
|
|
// request permissions to send notifications
|
2023-01-19 11:03:51 +00:00
|
|
|
if ("Notification" in window) {
|
2023-01-03 13:17:09 +00:00
|
|
|
try {
|
2023-01-19 11:03:51 +00:00
|
|
|
if (Notification.permission !== "granted") {
|
|
|
|
let res = await Notification.requestPermission();
|
|
|
|
console.debug(res);
|
|
|
|
}
|
2023-01-03 13:17:09 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
navigate("/notifications");
|
|
|
|
}
|
|
|
|
|
2022-12-29 15:36:40 +00:00
|
|
|
function accountHeader() {
|
2023-01-10 07:09:09 +00:00
|
|
|
const unreadNotifications = notifications?.filter(a => (a.created_at * 1000) > readNotifications).length;
|
2023-02-05 06:29:46 +00:00
|
|
|
const unreadDms = publicKey ? totalUnread(filteredDms, publicKey) : 0;
|
2022-12-29 15:36:40 +00:00
|
|
|
return (
|
2023-01-25 18:08:53 +00:00
|
|
|
<div className="header-actions">
|
2023-01-28 21:43:56 +00:00
|
|
|
<div className="btn btn-rnd" onClick={(e) => navigate("/search")}>
|
|
|
|
<Search />
|
|
|
|
</div>
|
|
|
|
<div className="btn btn-rnd" onClick={(e) => navigate("/messages")}>
|
2023-01-25 18:08:53 +00:00
|
|
|
<Envelope />
|
|
|
|
{unreadDms > 0 && (<span className="has-unread"></span>)}
|
2023-01-12 09:48:39 +00:00
|
|
|
</div>
|
2023-01-28 21:43:56 +00:00
|
|
|
<div className="btn btn-rnd" onClick={(e) => goToNotifications(e)}>
|
2023-01-25 18:08:53 +00:00
|
|
|
<Bell />
|
2023-01-26 07:30:23 +00:00
|
|
|
{unreadNotifications > 0 && (<span className="has-unread"></span>)}
|
2022-12-29 15:36:40 +00:00
|
|
|
</div>
|
2023-02-05 06:29:46 +00:00
|
|
|
<ProfileImage pubkey={publicKey || ""} showUsername={false} />
|
2023-01-25 18:08:53 +00:00
|
|
|
</div>
|
2022-12-29 15:36:40 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-02-05 06:29:46 +00:00
|
|
|
if (typeof loggedOut !== "boolean") {
|
2023-01-02 11:59:03 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-12-18 14:51:47 +00:00
|
|
|
return (
|
|
|
|
<div className="page">
|
2023-01-25 18:08:53 +00:00
|
|
|
<header>
|
|
|
|
<div className="logo" onClick={() => navigate("/")}>Snort</div>
|
2022-12-18 14:51:47 +00:00
|
|
|
<div>
|
2023-02-05 06:29:46 +00:00
|
|
|
{publicKey ? accountHeader() :
|
2023-01-25 18:08:53 +00:00
|
|
|
<button type="button" onClick={() => navigate("/login")}>Login</button>
|
2022-12-27 23:46:13 +00:00
|
|
|
}
|
2022-12-18 14:51:47 +00:00
|
|
|
</div>
|
2023-01-25 18:08:53 +00:00
|
|
|
</header>
|
2023-01-16 18:14:08 +00:00
|
|
|
<Outlet />
|
2023-02-05 12:32:34 +00:00
|
|
|
|
|
|
|
<button className="note-create-button" type="button" onClick={() => setShow(!show)}>
|
|
|
|
<Plus />
|
|
|
|
</button>
|
|
|
|
<NoteCreator replyTo={undefined} autoFocus={true} show={show} setShow={setShow} />
|
2022-12-18 14:51:47 +00:00
|
|
|
</div>
|
|
|
|
)
|
2023-01-19 23:19:09 +00:00
|
|
|
}
|