Merge remote-tracking branch 'fiatjaf/event-encoding' into next

This commit is contained in:
hzrd149 2023-11-24 08:46:40 -06:00
commit ead0d512df
7 changed files with 27 additions and 16 deletions

View File

@ -9,6 +9,7 @@ import RawPre from "./raw-pre";
import userMetadataService from "../../services/user-metadata";
import { getUserDisplayName } from "../../helpers/user-metadata";
import { COMMUNITY_DEFINITION_KIND } from "../../helpers/nostr/communities";
import { getSharableEventAddress } from "../../helpers/nip19";
export default function CommunityPostDebugModal({
event,
@ -27,7 +28,8 @@ export default function CommunityPostDebugModal({
<ModalBody p="4">
<Flex gap="2" direction="column">
<RawValue heading="Event Id" value={event.id} />
<RawValue heading="Encoded id (NIP-19)" value={nip19.noteEncode(event.id)} />
<RawValue heading="NIP-19 Encoded Id" value={nip19.noteEncode(event.id)} />
<RawValue heading="NIP-19 Pointer" value={getSharableEventAddress(event)} />
<RawValue heading="Community Coordinate" value={communityCoordinate} />
<RawPre heading="Content" value={event.content} />
<RawJson heading="JSON" json={event} />

View File

@ -7,6 +7,7 @@ import { NostrEvent } from "../../types/nostr-event";
import RawJson from "./raw-json";
import RawValue from "./raw-value";
import RawPre from "./raw-pre";
import { getSharableEventAddress } from "../../helpers/nip19";
export default function NoteDebugModal({ event, ...props }: { event: NostrEvent } & Omit<ModalProps, "children">) {
return (
@ -17,7 +18,8 @@ export default function NoteDebugModal({ event, ...props }: { event: NostrEvent
<ModalBody p="4">
<Flex gap="2" direction="column">
<RawValue heading="Event Id" value={event.id} />
<RawValue heading="Encoded id (NIP-19)" value={nip19.noteEncode(event.id)} />
<RawValue heading="NIP-19 Encoded Id" value={nip19.noteEncode(event.id)} />
<RawValue heading="NIP-19 Pointer" value={getSharableEventAddress(event)} />
<RawPre heading="Content" value={event.content} />
<RawJson heading="JSON" json={event} />
<RawJson heading="References" json={getReferences(event)} />

View File

@ -1,20 +1,20 @@
import { useMemo } from "react";
import { Link, LinkProps } from "@chakra-ui/react";
import { Link as RouterLink } from "react-router-dom";
import { nip19 } from "nostr-tools";
import { truncatedId } from "../helpers/nostr/events";
import { getNeventCodeWithRelays } from "../helpers/nip19";
export type NoteLinkProps = LinkProps & {
noteId: string;
};
export const NoteLink = ({ children, noteId, color = "blue.500", ...props }: NoteLinkProps) => {
const encoded = useMemo(() => nip19.noteEncode(noteId), [noteId]);
const nevent = useMemo(() => getNeventCodeWithRelays(noteId), [noteId]);
return (
<Link as={RouterLink} to={`/n/${encoded}`} color={color} {...props}>
{children || truncatedId(nip19.noteEncode(noteId))}
<Link as={RouterLink} to={`/n/${nevent}`} color={color} {...props}>
{children || truncatedId(nevent)}
</Link>
);
};

View File

@ -97,7 +97,7 @@ export const Note = React.memo(
data-event-id={event.id}
{...props}
>
{clickable && <HoverLinkOverlay as={RouterLink} to={`/n/${nip19.noteEncode(event.id)}`} />}
{clickable && <HoverLinkOverlay as={RouterLink} to={`/n/${getSharableEventAddress(event)}`} />}
<CardHeader p="2">
<Flex flex="1" gap="2" alignItems="center">
<UserAvatarLink pubkey={event.pubkey} size={["xs", "sm"]} />
@ -108,7 +108,7 @@ export const Note = React.memo(
{!hideDrawerButton && (
<OpenInDrawerButton to={`/n/${getSharableEventAddress(event)}`} size="sm" variant="ghost" />
)}
<Link as={RouterLink} whiteSpace="nowrap" color="current" to={`/n/${nip19.noteEncode(event.id)}`}>
<Link as={RouterLink} whiteSpace="nowrap" color="current" to={`/n/${getSharableEventAddress(event)}`}>
<Timestamp timestamp={event.created_at} />
</Link>
</Flex>

View File

@ -83,6 +83,13 @@ export function getSharableEventAddress(event: NostrEvent) {
}
}
export function getNeventCodeWithRelays(eventId: string) {
const relays = getEventRelays(eventId).value;
const ranked = relayScoreboardService.getRankedRelays(relays);
const maxTwo = ranked.slice(0, 2);
return nip19.neventEncode({ id: eventId, relays: maxTwo });
}
export function encodePointer(pointer: DecodeResult) {
switch (pointer.type) {
case "naddr":

View File

@ -33,7 +33,6 @@ export default function CommunityPostMenu({
const { deleteEvent } = useDeleteEventContext();
const noteId = nip19.noteEncode(event.id);
const address = getSharableEventAddress(event);
return (
@ -56,11 +55,12 @@ export default function CommunityPostMenu({
<MenuItem onClick={() => window.navigator.clipboard.writeText("nostr:" + address)} icon={<RepostIcon />}>
Copy Share Link
</MenuItem>
{noteId && (
<MenuItem onClick={() => window.navigator.clipboard.writeText(noteId)} icon={<CopyToClipboardIcon />}>
Copy Note ID
</MenuItem>
)}
<MenuItem
onClick={() => window.navigator.clipboard.writeText(nip19.noteEncode(event.id))}
icon={<CopyToClipboardIcon />}
>
Copy Note ID
</MenuItem>
{account?.pubkey === event.pubkey && (
<MenuItem icon={<TrashIcon />} color="red.500" onClick={() => deleteEvent(event)}>
Delete Note

View File

@ -3,7 +3,7 @@ import { nip19 } from "nostr-tools";
import { useParams, Link as RouterLink } from "react-router-dom";
import Note from "../../components/note";
import { isHexKey } from "../../helpers/nip19";
import { getSharableEventAddress, isHexKey } from "../../helpers/nip19";
import { useThreadLoader } from "../../hooks/use-thread-loader";
import { ThreadPost } from "./components/thread-post";
import VerticalPageLayout from "../../components/vertical-page-layout";
@ -59,7 +59,7 @@ export default function NoteView() {
h="4rem"
w="full"
as={RouterLink}
to={`/n/${nip19.noteEncode(parentPosts[0].event.id)}`}
to={`/n/${getSharableEventAddress(parentPosts[0].event)}`}
>
View full thread ({parentPosts.length - 1})
</Button>