forked from Kieran/snort
Show display name in notification
This commit is contained in:
parent
5eed5a65c4
commit
4714912df0
@ -7,6 +7,7 @@ import { hexToBech32, profileLink } from "../Util";
|
|||||||
import Avatar from "./Avatar"
|
import Avatar from "./Avatar"
|
||||||
import Nip05 from "./Nip05";
|
import Nip05 from "./Nip05";
|
||||||
import { HexKey } from "../nostr";
|
import { HexKey } from "../nostr";
|
||||||
|
import { MetadataCache } from "../db/User";
|
||||||
|
|
||||||
export interface ProfileImageProps {
|
export interface ProfileImageProps {
|
||||||
pubkey: HexKey,
|
pubkey: HexKey,
|
||||||
@ -21,13 +22,7 @@ export default function ProfileImage({ pubkey, subHeader, showUsername = true, c
|
|||||||
const user = useProfile(pubkey)?.get(pubkey);
|
const user = useProfile(pubkey)?.get(pubkey);
|
||||||
|
|
||||||
const name = useMemo(() => {
|
const name = useMemo(() => {
|
||||||
let name = hexToBech32("npub", pubkey).substring(0, 12);
|
return getDisplayName(user, pubkey);
|
||||||
if ((user?.display_name?.length ?? 0) > 0) {
|
|
||||||
name = user!.display_name!;
|
|
||||||
} else if ((user?.name?.length ?? 0) > 0) {
|
|
||||||
name = user!.name!;
|
|
||||||
}
|
|
||||||
return name;
|
|
||||||
}, [user, pubkey]);
|
}, [user, pubkey]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -48,3 +43,13 @@ export default function ProfileImage({ pubkey, subHeader, showUsername = true, c
|
|||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getDisplayName(user: MetadataCache | undefined, pubkey: HexKey) {
|
||||||
|
let name = hexToBech32("npub", pubkey).substring(0, 12);
|
||||||
|
if ((user?.display_name?.length ?? 0) > 0) {
|
||||||
|
name = user!.display_name!;
|
||||||
|
} else if ((user?.name?.length ?? 0) > 0) {
|
||||||
|
name = user!.name!;
|
||||||
|
}
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import Nostrich from "../nostrich.jpg";
|
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 { HexKey } from "../nostr";
|
import { HexKey, TaggedRawEvent } from "../nostr";
|
||||||
import EventKind from "../nostr/EventKind";
|
import EventKind from "../nostr/EventKind";
|
||||||
import { Subscriptions } from "../nostr/Subscriptions";
|
import { Subscriptions } from "../nostr/Subscriptions";
|
||||||
import { addDirectMessage, addNotifications, setFollows, setRelays } from "../state/Login";
|
import { addDirectMessage, addNotifications, setFollows, setRelays } from "../state/Login";
|
||||||
@ -9,6 +9,8 @@ import { RootState } from "../state/Store";
|
|||||||
import { db } from "../db";
|
import { db } from "../db";
|
||||||
import useSubscription from "./Subscription";
|
import useSubscription from "./Subscription";
|
||||||
import { mapEventToProfile, MetadataCache } from "../db/User";
|
import { mapEventToProfile, MetadataCache } from "../db/User";
|
||||||
|
import { hexToBech32 } from "../Util";
|
||||||
|
import { getDisplayName } from "../element/ProfileImage";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Managed loading data for the current logged in user
|
* Managed loading data for the current logged in user
|
||||||
@ -63,18 +65,8 @@ export default function useLoginFeed() {
|
|||||||
|
|
||||||
if ("Notification" in window && Notification.permission === "granted") {
|
if ("Notification" in window && Notification.permission === "granted") {
|
||||||
for (let nx of notifications.filter(a => (a.created_at * 1000) > readNotifications)) {
|
for (let nx of notifications.filter(a => (a.created_at * 1000) > readNotifications)) {
|
||||||
if (Notification.permission === "granted") {
|
sendNotification(nx)
|
||||||
let body = nx.content.substring(0, 50);
|
.catch(console.warn);
|
||||||
let title = "Snort"
|
|
||||||
navigator.serviceWorker.ready.then(worker => {
|
|
||||||
worker.showNotification(title, {
|
|
||||||
body: body,
|
|
||||||
icon: Nostrich,
|
|
||||||
tag: "notification",
|
|
||||||
});
|
|
||||||
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
dispatch(addNotifications(notifications));
|
dispatch(addNotifications(notifications));
|
||||||
@ -96,3 +88,31 @@ export default function useLoginFeed() {
|
|||||||
})().catch(console.warn);
|
})().catch(console.warn);
|
||||||
}, [main]);
|
}, [main]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function makeNotification(ev: TaggedRawEvent) {
|
||||||
|
switch (ev.kind) {
|
||||||
|
case EventKind.TextNote: {
|
||||||
|
let from = await db.users.get(ev.pubkey);
|
||||||
|
let name = getDisplayName(from, ev.pubkey);
|
||||||
|
return {
|
||||||
|
title: `Reply from ${name}`,
|
||||||
|
body: ev.content.substring(0, 50)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function sendNotification(ev: TaggedRawEvent) {
|
||||||
|
let n = await makeNotification(ev);
|
||||||
|
if (n != null && Notification.permission === "granted") {
|
||||||
|
let worker = await navigator.serviceWorker.ready;
|
||||||
|
worker.showNotification(n.title, {
|
||||||
|
body: n.body,
|
||||||
|
icon: Nostrich,
|
||||||
|
tag: "notification",
|
||||||
|
timestamp: ev.created_at * 1000,
|
||||||
|
vibrate: [500]
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user