feat: automatically add nostr: prefix to nip19 stuff
This commit is contained in:
@@ -13,6 +13,7 @@ import PostOptions from './PostOptions'
|
||||
import Preview from './Preview'
|
||||
import SendOnlyToSwitch from './SendOnlyToSwitch'
|
||||
import Uploader from './Uploader'
|
||||
import { preprocessContent } from './utils'
|
||||
|
||||
export default function NormalPostContent({
|
||||
defaultContent = '',
|
||||
@@ -27,6 +28,7 @@ export default function NormalPostContent({
|
||||
const { toast } = useToast()
|
||||
const { publish, checkLogin } = useNostr()
|
||||
const [content, setContent] = useState('')
|
||||
const [processedContent, setProcessedContent] = useState('')
|
||||
const [pictureInfos, setPictureInfos] = useState<{ url: string; tags: string[][] }[]>([])
|
||||
const [posting, setPosting] = useState(false)
|
||||
const [showMoreOptions, setShowMoreOptions] = useState(false)
|
||||
@@ -39,9 +41,10 @@ export default function NormalPostContent({
|
||||
const canPost = !!content && !posting
|
||||
|
||||
useEffect(() => {
|
||||
const cachedContent = postContentCache.getNormalPostCache({ defaultContent, parentEvent })
|
||||
if (cachedContent) {
|
||||
setContent(cachedContent)
|
||||
const cached = postContentCache.getNormalPostCache({ defaultContent, parentEvent })
|
||||
if (cached) {
|
||||
setContent(cached.content || '')
|
||||
setPictureInfos(cached.pictureInfos || [])
|
||||
}
|
||||
if (defaultContent) {
|
||||
setCursorOffset(defaultContent.length)
|
||||
@@ -52,9 +55,10 @@ export default function NormalPostContent({
|
||||
}, [defaultContent, parentEvent])
|
||||
|
||||
useEffect(() => {
|
||||
setProcessedContent(preprocessContent(content))
|
||||
if (!initializedRef.current) return
|
||||
postContentCache.setNormalPostCache({ defaultContent, parentEvent }, content)
|
||||
}, [content])
|
||||
postContentCache.setNormalPostCache({ defaultContent, parentEvent }, content, pictureInfos)
|
||||
}, [content, pictureInfos])
|
||||
|
||||
const post = async (e: React.MouseEvent) => {
|
||||
e.stopPropagation()
|
||||
@@ -68,11 +72,11 @@ export default function NormalPostContent({
|
||||
try {
|
||||
const draftEvent =
|
||||
parentEvent && parentEvent.kind !== kinds.ShortTextNote
|
||||
? await createCommentDraftEvent(content, parentEvent, pictureInfos, mentions, {
|
||||
? await createCommentDraftEvent(processedContent, parentEvent, pictureInfos, mentions, {
|
||||
addClientTag,
|
||||
protectedEvent: !!specifiedRelayUrls
|
||||
})
|
||||
: await createShortTextNoteDraftEvent(content, pictureInfos, mentions, {
|
||||
: await createShortTextNoteDraftEvent(processedContent, pictureInfos, mentions, {
|
||||
parentEvent,
|
||||
addClientTag,
|
||||
protectedEvent: !!specifiedRelayUrls
|
||||
@@ -117,7 +121,7 @@ export default function NormalPostContent({
|
||||
placeholder={t('Write something...')}
|
||||
cursorOffset={cursorOffset}
|
||||
/>
|
||||
{content && <Preview content={content} />}
|
||||
{processedContent && <Preview content={processedContent} />}
|
||||
<SendOnlyToSwitch
|
||||
parentEvent={parentEvent}
|
||||
specifiedRelayUrls={specifiedRelayUrls}
|
||||
@@ -150,7 +154,7 @@ export default function NormalPostContent({
|
||||
</div>
|
||||
<div className="flex gap-2 items-center">
|
||||
<Mentions
|
||||
content={content}
|
||||
content={processedContent}
|
||||
parentEvent={parentEvent}
|
||||
mentions={mentions}
|
||||
setMentions={setMentions}
|
||||
|
||||
@@ -13,12 +13,14 @@ import Mentions from './Mentions'
|
||||
import PostOptions from './PostOptions'
|
||||
import SendOnlyToSwitch from './SendOnlyToSwitch'
|
||||
import Uploader from './Uploader'
|
||||
import { preprocessContent } from './utils'
|
||||
|
||||
export default function PicturePostContent({ close }: { close: () => void }) {
|
||||
const { t } = useTranslation()
|
||||
const { toast } = useToast()
|
||||
const { publish, checkLogin } = useNostr()
|
||||
const [content, setContent] = useState('')
|
||||
const [processedContent, setProcessedContent] = useState('')
|
||||
const [pictureInfos, setPictureInfos] = useState<{ url: string; tags: string[][] }[]>([])
|
||||
const [posting, setPosting] = useState(false)
|
||||
const [showMoreOptions, setShowMoreOptions] = useState(false)
|
||||
@@ -38,6 +40,7 @@ export default function PicturePostContent({ close }: { close: () => void }) {
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
setProcessedContent(preprocessContent(content))
|
||||
if (!initializedRef.current) return
|
||||
postContentCache.setPicturePostCache(content, pictureInfos)
|
||||
}, [content, pictureInfos])
|
||||
@@ -55,10 +58,15 @@ export default function PicturePostContent({ close }: { close: () => void }) {
|
||||
if (!pictureInfos.length) {
|
||||
throw new Error(t('Picture note requires images'))
|
||||
}
|
||||
const draftEvent = await createPictureNoteDraftEvent(content, pictureInfos, mentions, {
|
||||
addClientTag,
|
||||
protectedEvent: !!specifiedRelayUrls
|
||||
})
|
||||
const draftEvent = await createPictureNoteDraftEvent(
|
||||
processedContent,
|
||||
pictureInfos,
|
||||
mentions,
|
||||
{
|
||||
addClientTag,
|
||||
protectedEvent: !!specifiedRelayUrls
|
||||
}
|
||||
)
|
||||
await publish(draftEvent, { specifiedRelayUrls })
|
||||
setContent('')
|
||||
setPictureInfos([])
|
||||
@@ -117,7 +125,7 @@ export default function PicturePostContent({ close }: { close: () => void }) {
|
||||
<ChevronDown className={`transition-transform ${showMoreOptions ? 'rotate-180' : ''}`} />
|
||||
</Button>
|
||||
<div className="flex gap-2 items-center">
|
||||
<Mentions content={content} mentions={mentions} setMentions={setMentions} />
|
||||
<Mentions content={processedContent} mentions={mentions} setMentions={setMentions} />
|
||||
<div className="flex gap-2 items-center max-sm:hidden">
|
||||
<Button
|
||||
variant="secondary"
|
||||
|
||||
6
src/components/PostEditor/utils.ts
Normal file
6
src/components/PostEditor/utils.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export function preprocessContent(content: string) {
|
||||
const regex = /(?<=^|\s)(nevent|naddr|nprofile|npub)[a-zA-Z0-9]+/g
|
||||
return content.replace(regex, (match) => {
|
||||
return `nostr:${match}`
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user