mirror of
https://github.com/hzrd149/nostrudel.git
synced 2025-03-17 21:31:43 +01:00
small cleanup for text note posting
This commit is contained in:
parent
58cafd67b2
commit
73aa6add1b
@ -32,7 +32,7 @@ import dayjs from "dayjs";
|
||||
import { useForm } from "react-hook-form";
|
||||
import { kinds, UnsignedEvent } from "nostr-tools";
|
||||
import { useThrottle } from "react-use";
|
||||
import { useObservable } from "applesauce-react/hooks";
|
||||
import { useEventFactory, useObservable } from "applesauce-react/hooks";
|
||||
|
||||
import { ChevronDownIcon, ChevronUpIcon, UploadImageIcon } from "../icons";
|
||||
import PublishAction from "../../classes/nostr-publish-action";
|
||||
@ -64,6 +64,7 @@ import localSettings from "../../services/local-settings";
|
||||
import useLocalStorageDisclosure from "../../hooks/use-localstorage-disclosure";
|
||||
import InsertGifButton from "../gif/insert-gif-button";
|
||||
import InsertImageButton from "./insert-image-button";
|
||||
import { unixNow } from "applesauce-core/helpers";
|
||||
|
||||
type FormValues = {
|
||||
subject: string;
|
||||
@ -101,6 +102,7 @@ export default function PostModal({
|
||||
const emojis = useContextEmojis();
|
||||
const moreOptions = useDisclosure();
|
||||
|
||||
const factory = useEventFactory();
|
||||
const [draft, setDraft] = useState<UnsignedEvent>();
|
||||
const { getValues, setValue, watch, register, handleSubmit, formState, reset } = useForm<FormValues>({
|
||||
defaultValues: {
|
||||
@ -131,10 +133,10 @@ export default function PostModal({
|
||||
const { content, nsfw, nsfwReason, community, split, subject } = values;
|
||||
|
||||
let draft = finalizeNote({
|
||||
content: correctContentMentions(content),
|
||||
kind: kinds.ShortTextNote,
|
||||
content,
|
||||
tags: [],
|
||||
created_at: dayjs().unix(),
|
||||
created_at: unixNow(),
|
||||
});
|
||||
|
||||
if (nsfw) draft.tags.push(nsfwReason ? ["content-warning", nsfwReason] : ["content-warning"]);
|
||||
@ -152,7 +154,7 @@ export default function PostModal({
|
||||
setDraft(unsigned);
|
||||
return unsigned;
|
||||
},
|
||||
[getValues, emojis, finalizeDraft, setDraft],
|
||||
[getValues, emojis, finalizeDraft, setDraft, factory],
|
||||
);
|
||||
|
||||
// throttle update the draft every 500ms
|
||||
|
@ -86,7 +86,7 @@ export function ensureNotifyPubkeys(draft: EventTemplate, pubkeys: string[]) {
|
||||
}
|
||||
|
||||
export function correctContentMentions(content: string) {
|
||||
return content.replace(/(\s|^)(?:@)?(npub1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]{58})/gi, "$1nostr:$2");
|
||||
return content.replaceAll(/(\s|^)(?:@)?(npub1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]{58})/gi, "$1nostr:$2");
|
||||
}
|
||||
|
||||
export function getPubkeysMentionedInContent(content: string, direct = false) {
|
||||
@ -219,7 +219,6 @@ export function addPubkeyRelayHints(draft: EventTemplate) {
|
||||
export function finalizeNote(draft: EventTemplate) {
|
||||
let updated: EventTemplate = { ...draft, tags: Array.from(draft.tags) };
|
||||
updated.content = correctContentMentions(updated.content);
|
||||
updated = createHashtagTags(updated);
|
||||
updated = addPubkeyRelayHints(updated);
|
||||
updated = ensureTagContentMentions(updated);
|
||||
return updated;
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { BehaviorSubject, Observable } from "rxjs";
|
||||
import { EventFactory } from "applesauce-factory";
|
||||
import { Account } from "../classes/accounts/account";
|
||||
import { getEventRelayHints } from "./event-relay-hint";
|
||||
import { getEventRelayHint, getPubkeyRelayHint } from "./event-relay-hint";
|
||||
import { NIP_89_CLIENT_APP } from "../const";
|
||||
import accountService from "./account";
|
||||
|
||||
@ -19,7 +19,8 @@ class EventFactoryService {
|
||||
this.subject.next(
|
||||
new EventFactory({
|
||||
signer: current.signer,
|
||||
getRelayHint: (event) => getEventRelayHints(event, 1)[0],
|
||||
getRelayHint: getEventRelayHint,
|
||||
getPubkeyRelayHint: getPubkeyRelayHint,
|
||||
client: NIP_89_CLIENT_APP,
|
||||
}),
|
||||
);
|
||||
|
@ -63,6 +63,11 @@ export function getEventRelayHint(event: NostrEvent): string | undefined {
|
||||
return getEventRelayHints(event, 1)[0];
|
||||
}
|
||||
|
||||
/** Returns a relay hint for a single pubkey */
|
||||
export function getPubkeyRelayHint(pubkey: string): string | undefined {
|
||||
return getAuthorHints(pubkey)[0];
|
||||
}
|
||||
|
||||
/** Returns a nevent or naddr for an event */
|
||||
export function getSharableEventAddress(event: NostrEvent, relays?: Iterable<string>) {
|
||||
relays = relays || getEventRelayHints(event, 2);
|
||||
|
Loading…
x
Reference in New Issue
Block a user