snort/packages/app/src/Pages/NostrLinkHandler.tsx

60 lines
1.7 KiB
TypeScript
Raw Normal View History

2023-03-25 22:55:34 +00:00
import { NostrPrefix } from "@snort/nostr";
2023-03-31 09:51:50 +00:00
import { useEffect, useState } from "react";
import { FormattedMessage } from "react-intl";
2023-02-14 10:34:43 +00:00
import { useNavigate, useParams } from "react-router-dom";
2023-03-25 22:55:34 +00:00
2023-03-31 09:51:50 +00:00
import Spinner from "Icons/Spinner";
2023-04-14 11:33:19 +00:00
import { parseNostrLink, profileLink } from "Util";
2023-03-31 09:51:50 +00:00
import { getNip05PubKey } from "Pages/Login";
2023-04-14 11:33:19 +00:00
import { System } from "System";
2023-01-29 19:44:53 +00:00
export default function NostrLinkHandler() {
2023-02-14 10:34:43 +00:00
const params = useParams();
const navigate = useNavigate();
2023-04-14 11:33:19 +00:00
const [loading, setLoading] = useState(true);
2023-02-16 16:32:56 +00:00
const link = decodeURIComponent(params["*"] ?? "").toLowerCase();
2023-01-29 19:44:53 +00:00
2023-03-31 09:51:50 +00:00
async function handleLink(link: string) {
const nav = parseNostrLink(link);
if (nav) {
if ((nav.relays?.length ?? 0) > 0) {
2023-04-14 11:33:19 +00:00
nav.relays?.map(a => System.ConnectEphemeralRelay(a));
2023-03-31 09:51:50 +00:00
}
if (nav.type === NostrPrefix.Event || nav.type === NostrPrefix.Note || nav.type === NostrPrefix.Address) {
navigate(`/e/${nav.encode()}`);
} else if (nav.type === NostrPrefix.PublicKey || nav.type === NostrPrefix.Profile) {
navigate(`/p/${nav.encode()}`);
}
} else {
try {
const pubkey = await getNip05PubKey(`${link}@snort.social`);
if (pubkey) {
navigate(profileLink(pubkey));
2023-02-16 16:32:56 +00:00
}
2023-03-31 09:51:50 +00:00
} catch {
//ignored
2023-02-14 10:34:43 +00:00
}
}
2023-03-31 09:51:50 +00:00
setLoading(false);
}
useEffect(() => {
if (link.length > 0) {
handleLink(link).catch(console.error);
}
2023-02-14 10:34:43 +00:00
}, [link]);
2023-01-29 19:44:53 +00:00
2023-03-31 09:51:50 +00:00
return (
<div className="flex f-center">
{loading ? (
<Spinner width={50} height={50} />
) : (
<b className="error">
<FormattedMessage defaultMessage="Nothing found :/" />
</b>
)}
</div>
);
2023-02-14 10:34:43 +00:00
}