import { fetchNip05Pubkey } from "@snort/shared"; import { NostrPrefix, tryParseNostrLink } from "@snort/system"; import React, { useEffect, useState } from "react"; import { FormattedMessage } from "react-intl"; import { useLocation, useParams } from "react-router-dom"; import { ThreadRoute } from "@/Components/Event/Thread"; import { GenericFeed } from "@/Components/Feed/Generic"; import Spinner from "@/Components/Icons/Spinner"; import ProfilePage from "@/Pages/Profile/ProfilePage"; export default function NostrLinkHandler() { const { state } = useLocation(); const { link } = useParams(); const determineInitialComponent = link => { const nav = tryParseNostrLink(link); if (nav) { switch (nav.type) { case NostrPrefix.Event: case NostrPrefix.Note: case NostrPrefix.Address: return ; case NostrPrefix.PublicKey: case NostrPrefix.Profile: return ; case NostrPrefix.Req: return ; default: return null; } } else { return state ? : null; } }; const initialRenderComponent = determineInitialComponent(link); const [loading, setLoading] = useState(initialRenderComponent ? false : true); const [renderComponent, setRenderComponent] = useState(initialRenderComponent); async function handleLink(link) { if (!tryParseNostrLink(link)) { try { const pubkey = await fetchNip05Pubkey(link, CONFIG.nip05Domain); if (pubkey) { setRenderComponent(); } } catch { // Ignored } setLoading(false); } } useEffect(() => { setRenderComponent(determineInitialComponent(link)); handleLink(link); }, [link]); // Depend only on 'link' if (renderComponent) { return renderComponent; } return (
{loading ? ( ) : ( )}
); }