Refactor relay URL handling and optimize pubkey retrieval in RootLayout

This commit is contained in:
mr0x50
2025-02-11 22:43:10 +01:00
parent 5d36039be7
commit fdfd2bb2bc

View File

@@ -10,43 +10,43 @@ import { Inter } from "next/font/google";
import { Toaster } from "@/components/ui/toaster"
import Script from "next/script";
import Umami from "@/components/Umami";
import { useMemo } from "react";
const inter = Inter({ subsets: ["latin"] });
const DEFAULT_RELAYS = [
"wss://relay.nostr.band",
"wss://relay.damus.io",
];
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
let pubkey = null;
if (typeof window !== 'undefined') {
pubkey = window.localStorage.getItem('pubkey');
}
const pubkey = typeof window !== 'undefined' ? window.localStorage.getItem('pubkey') : null;
const { events } = useNostrEvents({
filter: {
kinds: [10002],
authors: pubkey ? [pubkey] : [],
limit: 1,
},
});
let relayUrls = [
"wss://relay.nostr.band",
"wss://relay.damus.io",
];
const relayUrls = useMemo(() => {
if (!pubkey) return DEFAULT_RELAYS;
if (!events.length) return [];
if (pubkey) {
relayUrls = [];
const { events } = useNostrEvents({
filter: {
kinds: [10002],
limit: 1,
authors: [pubkey],
},
});
if (events.length > 0) {
const tags = events[0].tags;
for (let i = 0; i < tags.length; i++) {
if (tags[i][0] === "r") {
relayUrls.push(tags[i][1]);
}
const urls: string[] = [];
const tags = events[0].tags;
for (let i = 0; i < tags.length; i++) {
if (tags[i][0] === "r") {
urls.push(tags[i][1]);
}
}
}
return urls;
}, [events, pubkey]);
return (
<html lang="en">