From 42c51fa11dedf3dea48d7f085830fb378a7da365 Mon Sep 17 00:00:00 2001 From: smolgrrr Date: Fri, 15 Dec 2023 23:13:43 +1100 Subject: [PATCH] notifs work --- client/src/App.tsx | 2 ++ client/src/components/Forms/handleSubmit.ts | 6 ++++ client/src/components/Header/Header.tsx | 15 +++++++-- client/src/utils/subscriptions.ts | 37 ++++++++++++++++++++- 4 files changed, 57 insertions(+), 3 deletions(-) diff --git a/client/src/App.tsx b/client/src/App.tsx index c189d16..586d1ae 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -5,6 +5,7 @@ import { BrowserRouter as Router, Route, Routes } from "react-router-dom"; import Thread from "./components/Thread"; import Header from "./components/Header/Header"; import AddToHomeScreenPrompt from "./components/Modals/CheckMobile/CheckMobile"; +import Notifications from "./components/Notifications"; function App() { @@ -15,6 +16,7 @@ function App() { } /> } /> } /> + } /> diff --git a/client/src/components/Forms/handleSubmit.ts b/client/src/components/Forms/handleSubmit.ts index 2f2b215..25a2591 100644 --- a/client/src/components/Forms/handleSubmit.ts +++ b/client/src/components/Forms/handleSubmit.ts @@ -39,6 +39,7 @@ export const useSubmitForm = (unsigned: UnsignedEvent, difficulty: string) => { const unsignedWithPubkey = { ...unsigned, pubkey: getPublicKey(sk) }; const powServer = useState(localStorage.getItem('powserver') || ''); const [unsignedPoWEvent, setUnsignedPoWEvent] = useState() + let storedKeys = JSON.parse(localStorage.getItem('usedKeys') || '[]'); // Initialize the worker outside of any effects const numCores = navigator.hardwareConcurrency || 4; @@ -85,6 +86,11 @@ export const useSubmitForm = (unsigned: UnsignedEvent, difficulty: string) => { } else { startWork(); } + + // Add the logic here + storedKeys.push([sk, getPublicKey(sk)]); + // Stringify the array and store it back to localStorage + localStorage.setItem('usedKeys', JSON.stringify(storedKeys)); }; useEffect(() => { diff --git a/client/src/components/Header/Header.tsx b/client/src/components/Header/Header.tsx index 0b434b1..d145ad5 100644 --- a/client/src/components/Header/Header.tsx +++ b/client/src/components/Header/Header.tsx @@ -1,5 +1,6 @@ import { - Cog6ToothIcon + Cog6ToothIcon, + BellIcon } from "@heroicons/react/24/outline"; export default function Header() { @@ -14,14 +15,24 @@ export default function Header() { + ); diff --git a/client/src/utils/subscriptions.ts b/client/src/utils/subscriptions.ts index 24c8473..a8477b4 100644 --- a/client/src/utils/subscriptions.ts +++ b/client/src/utils/subscriptions.ts @@ -234,4 +234,39 @@ export const subNotesOnce = ( }); pubkeys.clear(); }, 2000); -}; \ No newline at end of file +}; + +/** quick subscribe to a note id (nip-19) */ +export const subNotifications = ( + pubkeys: string[], + onEvent: SubCallback, +) => { + const replyPubkeys = new Set(); + sub({ + cb: (evt, relay) => { + replyPubkeys.add(evt.pubkey); + onEvent(evt, relay); + }, + filter: { + "#p": pubkeys, + kinds: [1], + limit: 50, + }, + unsub: true, + }); + + setTimeout(() => { + // get profile info + sub({ + cb: onEvent, + filter: { + authors: Array.from(replyPubkeys), + kinds: [0], + limit: replyPubkeys.size, + }, + unsub: true, + }); + replyPubkeys.clear(); + }, 2000); +}; +