Browser notifications
This commit is contained in:
parent
f62c471848
commit
71327fc77a
@ -1,3 +1,4 @@
|
|||||||
|
import Nostrich from "../nostrich.jpg";
|
||||||
import { useEffect, useMemo } from "react";
|
import { useEffect, useMemo } from "react";
|
||||||
import { useDispatch, useSelector } from "react-redux";
|
import { useDispatch, useSelector } from "react-redux";
|
||||||
import EventKind from "../nostr/EventKind";
|
import EventKind from "../nostr/EventKind";
|
||||||
@ -12,10 +13,10 @@ import { mapEventToProfile } from "./UsersFeed";
|
|||||||
*/
|
*/
|
||||||
export default function useLoginFeed() {
|
export default function useLoginFeed() {
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
const pubKey = useSelector(s => s.login.publicKey);
|
const [pubKey, readNotifications] = useSelector(s => [s.login.publicKey, s.login.readNotifications]);
|
||||||
|
|
||||||
const sub = useMemo(() => {
|
const sub = useMemo(() => {
|
||||||
if(pubKey === null) {
|
if (pubKey === null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,7 +42,7 @@ export default function useLoginFeed() {
|
|||||||
let notifications = notes.filter(a => a.kind === EventKind.TextNote);
|
let notifications = notes.filter(a => a.kind === EventKind.TextNote);
|
||||||
let metadata = notes.filter(a => a.kind === EventKind.SetMetadata).map(a => mapEventToProfile(a));
|
let metadata = notes.filter(a => a.kind === EventKind.SetMetadata).map(a => mapEventToProfile(a));
|
||||||
|
|
||||||
for(let cl of contactList) {
|
for (let cl of contactList) {
|
||||||
if (cl.content !== "") {
|
if (cl.content !== "") {
|
||||||
let relays = JSON.parse(cl.content);
|
let relays = JSON.parse(cl.content);
|
||||||
dispatch(setRelays(relays));
|
dispatch(setRelays(relays));
|
||||||
@ -50,6 +51,12 @@ export default function useLoginFeed() {
|
|||||||
dispatch(setFollows(pTags));
|
dispatch(setFollows(pTags));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (Notification.permission === "granted") {
|
||||||
|
for (let nx in notifications.filter(a => (a.created_at * 1000) > readNotifications)) {
|
||||||
|
let n = new Notification(`New reply!`, { body: nx.content, icon: Nostrich });
|
||||||
|
console.log(n);
|
||||||
|
}
|
||||||
|
}
|
||||||
dispatch(addNotifications(notifications));
|
dispatch(addNotifications(notifications));
|
||||||
dispatch(setUserData(metadata));
|
dispatch(setUserData(metadata));
|
||||||
}, [notes]);
|
}, [notes]);
|
||||||
|
@ -34,11 +34,25 @@ export default function Layout(props) {
|
|||||||
dispatch(init());
|
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() {
|
function accountHeader() {
|
||||||
const unreadNotifications = notifications?.filter(a => (a.created_at * 1000) > readNotifications).length ?? 0;
|
const unreadNotifications = notifications?.filter(a => (a.created_at * 1000) > readNotifications).length ?? 0;
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div className="btn btn-rnd notifications" onClick={() => navigate("/notifications")}>
|
<div className="btn btn-rnd notifications" onClick={(e) => goToNotifications(e)}>
|
||||||
<FontAwesomeIcon icon={faBell} size="xl" />
|
<FontAwesomeIcon icon={faBell} size="xl" />
|
||||||
{unreadNotifications}
|
{unreadNotifications}
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
Reference in New Issue
Block a user