2023-02-16 16:32:56 +00:00
|
|
|
import { decodeTLV, NostrPrefix, TLVEntryType } 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-02-16 16:32:56 +00:00
|
|
|
import { setRelays } from "State/Login";
|
|
|
|
import { eventLink, profileLink } 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-02-16 16:32:56 +00:00
|
|
|
const entity = link.startsWith("web+nostr:") ? link.split(":")[1] : link;
|
|
|
|
if (entity.startsWith(NostrPrefix.PublicKey)) {
|
2023-02-14 10:34:43 +00:00
|
|
|
navigate(`/p/${entity}`);
|
2023-02-16 16:32:56 +00:00
|
|
|
} else if (entity.startsWith(NostrPrefix.Note)) {
|
2023-02-14 10:34:43 +00:00
|
|
|
navigate(`/e/${entity}`);
|
2023-02-16 16:32:56 +00:00
|
|
|
} else if (entity.startsWith(NostrPrefix.Profile) || entity.startsWith(NostrPrefix.Event)) {
|
|
|
|
const decoded = decodeTLV(entity);
|
|
|
|
console.debug(decoded);
|
|
|
|
|
|
|
|
const id = decoded.find(a => a.type === TLVEntryType.Special)?.value as string;
|
|
|
|
const relays = decoded.filter(a => a.type === TLVEntryType.Relay);
|
|
|
|
if (relays.length > 0) {
|
|
|
|
const relayObj = {
|
|
|
|
relays: Object.fromEntries(relays.map(a => [a.value, { read: true, write: false }])),
|
|
|
|
createdAt: new Date().getTime(),
|
|
|
|
};
|
|
|
|
dispatch(setRelays(relayObj));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entity.startsWith(NostrPrefix.Profile)) {
|
|
|
|
navigate(profileLink(id));
|
|
|
|
} else if (entity.startsWith(NostrPrefix.Event)) {
|
|
|
|
navigate(eventLink(id));
|
|
|
|
}
|
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}</>;
|
|
|
|
}
|