snort/packages/app/src/Element/Embed/NostrLink.tsx

35 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-03-25 22:55:34 +00:00
import { Link } from "react-router-dom";
2023-06-13 13:46:16 +00:00
import { NostrPrefix, tryParseNostrLink } from "@snort/system";
2023-03-25 22:55:34 +00:00
2023-11-17 11:52:10 +00:00
import Mention from "@/Element/Embed/Mention";
import NoteQuote from "@/Element/Event/NoteQuote";
2023-03-25 22:55:34 +00:00
2023-04-18 21:20:13 +00:00
export default function NostrLink({ link, depth }: { link: string; depth?: number }) {
2023-06-13 13:46:16 +00:00
const nav = tryParseNostrLink(link);
2023-03-25 22:55:34 +00:00
if (nav?.type === NostrPrefix.PublicKey || nav?.type === NostrPrefix.Profile) {
2023-11-17 21:52:03 +00:00
if (nav.id.startsWith("npub")) {
2023-11-17 21:50:12 +00:00
// eslint-disable-next-line no-debugger
debugger;
}
return <Mention link={nav} />;
2023-04-14 23:27:19 +00:00
} else if (nav?.type === NostrPrefix.Note || nav?.type === NostrPrefix.Event || nav?.type === NostrPrefix.Address) {
2023-04-18 21:20:13 +00:00
if ((depth ?? 0) > 0) {
const evLink = nav.encode();
return (
<Link to={`/${evLink}`} onClick={e => e.stopPropagation()} state={{ from: location.pathname }}>
2023-04-18 21:20:13 +00:00
#{evLink.substring(0, 12)}
</Link>
);
} else {
return <NoteQuote link={nav} depth={depth} />;
}
2023-03-25 22:55:34 +00:00
} else {
return (
<a href={link} onClick={e => e.stopPropagation()} target="_blank" rel="noreferrer" className="ext">
{link}
</a>
);
}
}