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

31 lines
1.0 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-09-28 09:20:39 +00:00
import Mention from "Element/Embed/Mention";
2023-09-28 09:26:10 +00:00
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) {
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>
);
}
}