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

36 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-03-25 22:55:34 +00:00
import { Link } from "react-router-dom";
2023-04-18 21:20:13 +00:00
import { EventKind, NostrPrefix } from "@snort/nostr";
2023-03-25 22:55:34 +00:00
import Mention from "Element/Mention";
import NostrFileHeader from "Element/NostrFileHeader";
2023-04-14 23:28:22 +00:00
import { parseNostrLink } from "Util";
2023-04-18 21:20:13 +00:00
import NoteQuote from "Element/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-03-25 22:55:34 +00:00
const nav = parseNostrLink(link);
if (nav?.type === NostrPrefix.PublicKey || nav?.type === NostrPrefix.Profile) {
return <Mention pubkey={nav.id} relays={nav.relays} />;
2023-04-14 23:27:19 +00:00
} else if (nav?.type === NostrPrefix.Note || nav?.type === NostrPrefix.Event || nav?.type === NostrPrefix.Address) {
if (nav.kind === EventKind.FileHeader) {
return <NostrFileHeader link={nav} />;
}
2023-04-18 21:20:13 +00:00
if ((depth ?? 0) > 0) {
const evLink = nav.encode();
return (
<Link to={`/e/${evLink}`} onClick={e => e.stopPropagation()} state={{ from: location.pathname }}>
#{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>
);
}
}