From ace1c4bc10851b5d2b5ef7855d96fee3bb3aef13 Mon Sep 17 00:00:00 2001 From: TheMhv Date: Sat, 14 Dec 2024 08:34:19 -0300 Subject: [PATCH 1/8] Feat: Add error alert on widget page --- src/components/Widget.tsx | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/src/components/Widget.tsx b/src/components/Widget.tsx index d12a244..5ca1677 100644 --- a/src/components/Widget.tsx +++ b/src/components/Widget.tsx @@ -1,6 +1,6 @@ "use client"; -import { useEffect, useRef, useState, useCallback } from "react"; +import React, { useEffect, useRef, useState, useCallback } from "react"; import { Client, Event, @@ -13,6 +13,7 @@ import { import { MsEdgeTTS } from "msedge-tts"; import { loadConfig, Settings } from "@/lib/config"; import { clientConnect } from "@/lib/nostr/client"; +import { TriangleAlert } from "lucide-react"; const config: Settings = loadConfig(); @@ -43,6 +44,24 @@ const createTTS = async (text: string) => { }); }; +const ErrorAlert: React.FC<{ text: string }> = ({ text }) => ( +
+
+ + + Error + +
+ Error! + {text} +
+
+
+); + export const TTSWidget: React.FC = ({ pubkey, onEventProcessed, @@ -51,6 +70,8 @@ export const TTSWidget: React.FC = ({ const [queue, setQueue] = useState([]); const [isVisible, setIsVisible] = useState(false); const [widgetText, setWidgetText] = useState(""); + const [errorText, setErrorText] = useState(); + const containerRef = useRef(null); const lastZapRef = useRef(); const isProcessingRef = useRef(false); @@ -82,6 +103,7 @@ export const TTSWidget: React.FC = ({ setQueue((prev) => [latestEvent, ...prev]); } } catch (error) { + setErrorText("Error processing audio stream"); console.error("Error fetching events:", error); } }, [client, pubkey]); @@ -124,10 +146,12 @@ export const TTSWidget: React.FC = ({ }); audioStream.on("error", (error) => { + setErrorText("Error processing audio stream"); console.error("Error processing audio stream:", error); isProcessingRef.current = false; }); } catch (error) { + setErrorText("Error processing event"); console.error("Error processing event:", error); isProcessingRef.current = false; } @@ -179,8 +203,12 @@ export const TTSWidget: React.FC = ({ }, []); return ( -
-
{widgetText}
-
+ <> +
+
{widgetText}
+
+ + {errorText && } + ); }; From 183e5aad2691d025da3b9e7f1f16bb3b91f0ddf4 Mon Sep 17 00:00:00 2001 From: TheMhv Date: Sat, 14 Dec 2024 08:52:32 -0300 Subject: [PATCH 2/8] Feat: Nostr client will no use signer by default --- src/components/NostrProvider.tsx | 38 ++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/components/NostrProvider.tsx b/src/components/NostrProvider.tsx index bb08753..c6b2358 100644 --- a/src/components/NostrProvider.tsx +++ b/src/components/NostrProvider.tsx @@ -1,44 +1,48 @@ "use client"; -import { createContext, useState, useEffect } from "react"; +import { createContext, useEffect, useState } from "react"; import { Client, loadWasmSync, Nip07Signer, NostrSigner, } from "@rust-nostr/nostr-sdk"; -import { Settings, loadConfig } from "@/lib/config"; interface NostrProviderProps { + withSigner?: boolean; + relays: string[]; children: React.ReactNode; } interface NostrContextProps { client: Client | null; - signer: Nip07Signer | null; } const NostrContext = createContext({ client: null, - signer: null, + pubKey: null, } as NostrContextProps); -const config: Settings = loadConfig(); - -const NostrProvider: React.FC = ({ children }) => { +const NostrProvider: React.FC = ({ + withSigner = false, + relays, + children, +}) => { const [client, setClient] = useState(null); - const [signer, setSigner] = useState(null); + const [signer, setSigner] = useState(null); loadWasmSync(); useEffect(() => { - const nip07Signer = new Nip07Signer(); - setSigner(nip07Signer); + if (withSigner) { + const nip07Signer = new Nip07Signer(); + + const newSigner = NostrSigner.nip07(nip07Signer); + setSigner(newSigner); + } - const newSigner = NostrSigner.nip07(nip07Signer); - const newClient = new Client(newSigner); + const newClient = new Client(signer || undefined); - const relays = config.RELAYS; relays.map(async (relay) => { await newClient.addRelay(relay); }); @@ -46,12 +50,12 @@ const NostrProvider: React.FC = ({ children }) => { newClient.connect().then(() => { setClient(newClient); }); - }, []); + }, [withSigner, relays]); + + const values: NostrContextProps = { client }; return ( - - {children} - + {children} ); }; From 790f78d4f80c277659c54c980e199a6c9b8290bc Mon Sep 17 00:00:00 2001 From: TheMhv Date: Sat, 14 Dec 2024 08:54:17 -0300 Subject: [PATCH 3/8] Feat: Widget will be consume by client --- src/app/profile/[npub]/widget/page.tsx | 13 ++++++++++--- src/components/Widget.tsx | 16 ++++++++++------ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/app/profile/[npub]/widget/page.tsx b/src/app/profile/[npub]/widget/page.tsx index 43d0a78..827eb17 100644 --- a/src/app/profile/[npub]/widget/page.tsx +++ b/src/app/profile/[npub]/widget/page.tsx @@ -1,7 +1,12 @@ -import { TTSWidget } from "@/components/Widget"; - import "./widget.css"; + import { RemoveLogo } from "@/components/utils/RemoveLogo"; +import { TTSWidget } from "@/components/Widget"; + +import { loadConfig, Settings } from "@/lib/config"; +import { NostrProvider } from "@/components/NostrProvider"; + +const config: Settings = loadConfig(); interface PageProps { params: { npub: string }; @@ -11,7 +16,9 @@ export default function widgetPage({ params }: PageProps) { return ( <> - + + + ); } diff --git a/src/components/Widget.tsx b/src/components/Widget.tsx index 5ca1677..cd24231 100644 --- a/src/components/Widget.tsx +++ b/src/components/Widget.tsx @@ -1,8 +1,13 @@ "use client"; -import React, { useEffect, useRef, useState, useCallback } from "react"; +import React, { + useEffect, + useRef, + useState, + useCallback, + useContext, +} from "react"; import { - Client, Event, EventSource, Filter, @@ -12,8 +17,8 @@ import { } from "@rust-nostr/nostr-sdk"; import { MsEdgeTTS } from "msedge-tts"; import { loadConfig, Settings } from "@/lib/config"; -import { clientConnect } from "@/lib/nostr/client"; import { TriangleAlert } from "lucide-react"; +import { NostrContext } from "./NostrProvider"; const config: Settings = loadConfig(); @@ -66,7 +71,8 @@ export const TTSWidget: React.FC = ({ pubkey, onEventProcessed, }) => { - const [client, setClient] = useState(undefined); + const { client } = useContext(NostrContext); + const [queue, setQueue] = useState([]); const [isVisible, setIsVisible] = useState(false); const [widgetText, setWidgetText] = useState(""); @@ -78,8 +84,6 @@ export const TTSWidget: React.FC = ({ // Fetch events handler const fetchEvents = useCallback(async () => { - setClient(await clientConnect()); - if (!client || !pubkey) return; try { From 936ee9924f7df643af6e5f3edbdd7cbb9df6d20a Mon Sep 17 00:00:00 2001 From: TheMhv Date: Sat, 14 Dec 2024 09:07:55 -0300 Subject: [PATCH 4/8] Fix: Profile badge will use signer (temporary unused) --- src/components/NostrProvider.tsx | 5 +++-- src/components/cornerMenu/ProfileBadge.tsx | 4 ++-- src/components/cornerMenu/cornerMenu.tsx | 8 +++++++- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/components/NostrProvider.tsx b/src/components/NostrProvider.tsx index c6b2358..1431fc3 100644 --- a/src/components/NostrProvider.tsx +++ b/src/components/NostrProvider.tsx @@ -16,11 +16,12 @@ interface NostrProviderProps { interface NostrContextProps { client: Client | null; + signer: NostrSigner | null; } const NostrContext = createContext({ client: null, - pubKey: null, + signer: null, } as NostrContextProps); const NostrProvider: React.FC = ({ @@ -52,7 +53,7 @@ const NostrProvider: React.FC = ({ }); }, [withSigner, relays]); - const values: NostrContextProps = { client }; + const values: NostrContextProps = { client, signer }; return ( {children} diff --git a/src/components/cornerMenu/ProfileBadge.tsx b/src/components/cornerMenu/ProfileBadge.tsx index f72e28e..95de319 100644 --- a/src/components/cornerMenu/ProfileBadge.tsx +++ b/src/components/cornerMenu/ProfileBadge.tsx @@ -119,7 +119,7 @@ export default function ProfileBadge() { const [error, setError] = useState(null); const [user, setUser] = useState(null); - const { signer, client } = useContext(NostrContext); + const { client, signer } = useContext(NostrContext); useEffect(() => { try { @@ -136,7 +136,7 @@ export default function ProfileBadge() { if (!signer) return; try { - const signerPubKey = await signer.getPublicKey(); + const signerPubKey = await signer.publicKey(); setPubkey(signerPubKey); } catch (error) { console.error("Error getting public key:", error); diff --git a/src/components/cornerMenu/cornerMenu.tsx b/src/components/cornerMenu/cornerMenu.tsx index 78b1175..eb00b73 100644 --- a/src/components/cornerMenu/cornerMenu.tsx +++ b/src/components/cornerMenu/cornerMenu.tsx @@ -1,9 +1,15 @@ +import { loadConfig, Settings } from "@/lib/config"; +import { NostrProvider } from "../NostrProvider"; import ProfileBadge from "./ProfileBadge"; +const config: Settings = loadConfig(); + export default function CornerMenu() { return (
- + + +
); } From c42e60f59a1dc8ece36453a1b3ec83cb09e2ba31 Mon Sep 17 00:00:00 2001 From: TheMhv Date: Sat, 14 Dec 2024 09:09:12 -0300 Subject: [PATCH 5/8] Fix: Form componnent not access configs directly --- src/app/goal/[eventId]/page.tsx | 6 +++++- src/app/profile/[npub]/page.tsx | 9 ++++++++- src/components/Form.tsx | 24 +++++++++++++++--------- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/app/goal/[eventId]/page.tsx b/src/app/goal/[eventId]/page.tsx index 6abae8f..5a77f16 100644 --- a/src/app/goal/[eventId]/page.tsx +++ b/src/app/goal/[eventId]/page.tsx @@ -90,7 +90,11 @@ export default async function GoalPage({ params }: PageProps) {
diff --git a/src/app/profile/[npub]/page.tsx b/src/app/profile/[npub]/page.tsx index 298bbe6..e9050f5 100644 --- a/src/app/profile/[npub]/page.tsx +++ b/src/app/profile/[npub]/page.tsx @@ -51,7 +51,14 @@ export default async function GoalPage({ params }: PageProps) { - + diff --git a/src/components/Form.tsx b/src/components/Form.tsx index cbefa84..ec9740e 100644 --- a/src/components/Form.tsx +++ b/src/components/Form.tsx @@ -4,8 +4,6 @@ import { ChangeEvent, FormEvent, useRef, useState } from "react"; import Image from "next/image"; import QRCode from "qrcode"; -import { Settings } from "@/lib/config"; - import { cn } from "@/lib/utils"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; @@ -31,11 +29,19 @@ interface FormData { interface FormProps { npub: string; - config: Settings; + options: { + MODELS: string[]; + MAX_TEXT_LENGTH: number; + MIN_SATOSHI_QNT: number; + }; eventId?: string; } -export default function Form({ npub, config, eventId = undefined }: FormProps) { +export default function Form({ + npub, + options, + eventId = undefined, +}: FormProps) { const [qrCode, setQRCode] = useState(""); const [paymentStatus, setPaymentStatus] = useState(false); const [isSubmitting, setIsSubmitting] = useState(false); @@ -240,7 +246,7 @@ export default function Form({ npub, config, eventId = undefined }: FormProps) { /> - {config.MODELS?.length > 0 && ( + {options.MODELS.length > 0 && ( <>