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-16 16:32:56 +00:00
|
|
|
import { useDispatch } from "react-redux";
|
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-02-16 16:32:56 +00:00
|
|
|
import { setRelays } from "State/Login";
|
2023-03-31 09:51:50 +00:00
|
|
|
import { parseNostrLink, profileLink, unixNowMs, unwrap } from "Util";
|
|
|
|
import { getNip05PubKey } from "Pages/Login";
|
2023-01-29 19:44:53 +00:00
|
|
|
|
|
|
|
export default function NostrLinkHandler() {
|
2023-02-14 10:34:43 +00:00
|
|
|
const params = useParams();
|
2023-03-31 09:51:50 +00:00
|
|
|
const [loading, setLoading] = useState(true);
|
2023-02-16 16:32:56 +00:00
|
|
|
const dispatch = useDispatch();
|
2023-02-14 10:34:43 +00:00
|
|
|
const navigate = useNavigate();
|
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) {
|
|
|
|
// todo: add as ephemerial connection
|
|
|
|
dispatch(
|
|
|
|
setRelays({
|
|
|
|
relays: Object.fromEntries(unwrap(nav.relays).map(a => [a, { read: true, write: false }])),
|
|
|
|
createdAt: unixNowMs(),
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|