2023-03-25 22:55:34 +00:00
|
|
|
import { NostrPrefix } from "@snort/nostr";
|
2023-01-29 19:44:53 +00:00
|
|
|
import { useEffect } from "react";
|
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-02-16 16:32:56 +00:00
|
|
|
import { setRelays } from "State/Login";
|
2023-03-25 22:55:34 +00:00
|
|
|
import { parseNostrLink, unixNowMs, unwrap } from "Util";
|
2023-01-29 19:44:53 +00:00
|
|
|
|
|
|
|
export default function NostrLinkHandler() {
|
2023-02-14 10:34:43 +00:00
|
|
|
const params = useParams();
|
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-02-14 10:34:43 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (link.length > 0) {
|
2023-03-25 22:55:34 +00:00
|
|
|
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(),
|
|
|
|
})
|
|
|
|
);
|
2023-02-16 16:32:56 +00:00
|
|
|
}
|
2023-03-25 22:55:34 +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()}`);
|
2023-02-16 16:32:56 +00:00
|
|
|
}
|
2023-02-14 10:34:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [link]);
|
2023-01-29 19:44:53 +00:00
|
|
|
|
2023-02-14 10:34:43 +00:00
|
|
|
return <>Could not handle {link}</>;
|
|
|
|
}
|