feat: 💨
This commit is contained in:
parent
8f5c3ac4d6
commit
2cde70dff4
9 changed files with 64 additions and 86 deletions
|
|
@ -1,10 +1,8 @@
|
|||
import { Button } from '@/components/ui/button'
|
||||
import { useToast } from '@/hooks/use-toast'
|
||||
import { createCommentDraftEvent, createShortTextNoteDraftEvent } from '@/lib/draft-event'
|
||||
import { useFeed } from '@/providers/FeedProvider.tsx'
|
||||
import { useNostr } from '@/providers/NostrProvider'
|
||||
import client from '@/services/client.service'
|
||||
import relayInfoService from '@/services/relay-info.service'
|
||||
import { ChevronDown, ImageUp, LoaderCircle } from 'lucide-react'
|
||||
import { Event, kinds } from 'nostr-tools'
|
||||
import { useState } from 'react'
|
||||
|
|
@ -14,7 +12,6 @@ import Mentions from './Mentions'
|
|||
import PostOptions from './PostOptions.tsx'
|
||||
import Preview from './Preview'
|
||||
import SendOnlyToSwitch from './SendOnlyToSwitch.tsx'
|
||||
import { TPostOptions } from './types.ts'
|
||||
import Uploader from './Uploader'
|
||||
|
||||
export default function NormalPostContent({
|
||||
|
|
@ -29,12 +26,12 @@ export default function NormalPostContent({
|
|||
const { t } = useTranslation()
|
||||
const { toast } = useToast()
|
||||
const { publish, checkLogin } = useNostr()
|
||||
const { relayUrls } = useFeed()
|
||||
const [content, setContent] = useState(defaultContent)
|
||||
const [pictureInfos, setPictureInfos] = useState<{ url: string; tags: string[][] }[]>([])
|
||||
const [posting, setPosting] = useState(false)
|
||||
const [showMoreOptions, setShowMoreOptions] = useState(false)
|
||||
const [postOptions, setPostOptions] = useState<TPostOptions>({})
|
||||
const [addClientTag, setAddClientTag] = useState(false)
|
||||
const [specifiedRelayUrls, setSpecifiedRelayUrls] = useState<string[] | undefined>(undefined)
|
||||
const [uploadingPicture, setUploadingPicture] = useState(false)
|
||||
const canPost = !!content && !posting
|
||||
|
||||
|
|
@ -53,25 +50,20 @@ export default function NormalPostContent({
|
|||
const relayList = await client.fetchRelayList(parentEvent.pubkey)
|
||||
additionalRelayUrls.push(...relayList.read.slice(0, 5))
|
||||
}
|
||||
let protectedEvent = false
|
||||
if (postOptions.sendOnlyToCurrentRelays) {
|
||||
const relayInfos = await relayInfoService.getRelayInfos(relayUrls)
|
||||
protectedEvent = relayInfos.every((info) => info?.supported_nips?.includes(70))
|
||||
}
|
||||
const draftEvent =
|
||||
parentEvent && parentEvent.kind !== kinds.ShortTextNote
|
||||
? await createCommentDraftEvent(content, parentEvent, pictureInfos, {
|
||||
addClientTag: postOptions.addClientTag,
|
||||
protectedEvent
|
||||
addClientTag,
|
||||
protectedEvent: !!specifiedRelayUrls
|
||||
})
|
||||
: await createShortTextNoteDraftEvent(content, pictureInfos, {
|
||||
parentEvent,
|
||||
addClientTag: postOptions.addClientTag,
|
||||
protectedEvent
|
||||
addClientTag,
|
||||
protectedEvent: !!specifiedRelayUrls
|
||||
})
|
||||
await publish(draftEvent, {
|
||||
additionalRelayUrls,
|
||||
specifiedRelayUrls: postOptions.sendOnlyToCurrentRelays ? relayUrls : undefined
|
||||
specifiedRelayUrls
|
||||
})
|
||||
setContent('')
|
||||
close()
|
||||
|
|
@ -114,8 +106,8 @@ export default function NormalPostContent({
|
|||
{content && <Preview content={content} />}
|
||||
<SendOnlyToSwitch
|
||||
parentEvent={parentEvent}
|
||||
postOptions={postOptions}
|
||||
setPostOptions={setPostOptions}
|
||||
specifiedRelayUrls={specifiedRelayUrls}
|
||||
setSpecifiedRelayUrls={setSpecifiedRelayUrls}
|
||||
/>
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex gap-2 items-center">
|
||||
|
|
@ -163,8 +155,8 @@ export default function NormalPostContent({
|
|||
</div>
|
||||
<PostOptions
|
||||
show={showMoreOptions}
|
||||
postOptions={postOptions}
|
||||
setPostOptions={setPostOptions}
|
||||
addClientTag={addClientTag}
|
||||
setAddClientTag={setAddClientTag}
|
||||
/>
|
||||
<div className="flex gap-2 items-center justify-around sm:hidden">
|
||||
<Button
|
||||
|
|
|
|||
|
|
@ -2,9 +2,7 @@ import { Button } from '@/components/ui/button'
|
|||
import { useToast } from '@/hooks/use-toast'
|
||||
import { createPictureNoteDraftEvent } from '@/lib/draft-event'
|
||||
import { cn } from '@/lib/utils'
|
||||
import { useFeed } from '@/providers/FeedProvider.tsx'
|
||||
import { useNostr } from '@/providers/NostrProvider'
|
||||
import relayInfoService from '@/services/relay-info.service'
|
||||
import { ChevronDown, Loader, LoaderCircle, Plus, X } from 'lucide-react'
|
||||
import { Dispatch, SetStateAction, useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
|
|
@ -13,19 +11,18 @@ import TextareaWithMentions from '../TextareaWithMentions.tsx'
|
|||
import Mentions from './Mentions'
|
||||
import PostOptions from './PostOptions.tsx'
|
||||
import SendOnlyToSwitch from './SendOnlyToSwitch.tsx'
|
||||
import { TPostOptions } from './types.ts'
|
||||
import Uploader from './Uploader'
|
||||
|
||||
export default function PicturePostContent({ close }: { close: () => void }) {
|
||||
const { t } = useTranslation()
|
||||
const { toast } = useToast()
|
||||
const { publish, checkLogin } = useNostr()
|
||||
const { relayUrls } = useFeed()
|
||||
const [content, setContent] = useState('')
|
||||
const [pictureInfos, setPictureInfos] = useState<{ url: string; tags: string[][] }[]>([])
|
||||
const [posting, setPosting] = useState(false)
|
||||
const [showMoreOptions, setShowMoreOptions] = useState(false)
|
||||
const [postOptions, setPostOptions] = useState<TPostOptions>({})
|
||||
const [addClientTag, setAddClientTag] = useState(false)
|
||||
const [specifiedRelayUrls, setSpecifiedRelayUrls] = useState<string[] | undefined>(undefined)
|
||||
const canPost = !!content && !posting && pictureInfos.length > 0
|
||||
|
||||
const post = async (e: React.MouseEvent) => {
|
||||
|
|
@ -41,18 +38,11 @@ export default function PicturePostContent({ close }: { close: () => void }) {
|
|||
if (!pictureInfos.length) {
|
||||
throw new Error(t('Picture note requires images'))
|
||||
}
|
||||
let protectedEvent = false
|
||||
if (postOptions.sendOnlyToCurrentRelays) {
|
||||
const relayInfos = await relayInfoService.getRelayInfos(relayUrls)
|
||||
protectedEvent = relayInfos.every((info) => info?.supported_nips?.includes(70))
|
||||
}
|
||||
const draftEvent = await createPictureNoteDraftEvent(content, pictureInfos, {
|
||||
addClientTag: postOptions.addClientTag,
|
||||
protectedEvent
|
||||
})
|
||||
await publish(draftEvent, {
|
||||
specifiedRelayUrls: postOptions.sendOnlyToCurrentRelays ? relayUrls : undefined
|
||||
addClientTag,
|
||||
protectedEvent: !!specifiedRelayUrls
|
||||
})
|
||||
await publish(draftEvent, { specifiedRelayUrls })
|
||||
setContent('')
|
||||
close()
|
||||
} catch (error) {
|
||||
|
|
@ -95,7 +85,10 @@ export default function PicturePostContent({ close }: { close: () => void }) {
|
|||
textValue={content}
|
||||
placeholder={t('Write something...')}
|
||||
/>
|
||||
<SendOnlyToSwitch postOptions={postOptions} setPostOptions={setPostOptions} />
|
||||
<SendOnlyToSwitch
|
||||
specifiedRelayUrls={specifiedRelayUrls}
|
||||
setSpecifiedRelayUrls={setSpecifiedRelayUrls}
|
||||
/>
|
||||
<div className="flex items-center justify-between">
|
||||
<Button
|
||||
variant="link"
|
||||
|
|
@ -126,8 +119,8 @@ export default function PicturePostContent({ close }: { close: () => void }) {
|
|||
</div>
|
||||
<PostOptions
|
||||
show={showMoreOptions}
|
||||
postOptions={postOptions}
|
||||
setPostOptions={setPostOptions}
|
||||
addClientTag={addClientTag}
|
||||
setAddClientTag={setAddClientTag}
|
||||
/>
|
||||
<div className="flex gap-2 items-center justify-around sm:hidden">
|
||||
<Button
|
||||
|
|
|
|||
|
|
@ -3,30 +3,26 @@ import { Switch } from '@/components/ui/switch'
|
|||
import { StorageKey } from '@/constants'
|
||||
import { Dispatch, SetStateAction, useEffect } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { TPostOptions } from './types'
|
||||
|
||||
export default function PostOptions({
|
||||
show,
|
||||
postOptions,
|
||||
setPostOptions
|
||||
addClientTag,
|
||||
setAddClientTag
|
||||
}: {
|
||||
show: boolean
|
||||
postOptions: TPostOptions
|
||||
setPostOptions: Dispatch<SetStateAction<TPostOptions>>
|
||||
addClientTag: boolean
|
||||
setAddClientTag: Dispatch<SetStateAction<boolean>>
|
||||
}) {
|
||||
const { t } = useTranslation()
|
||||
|
||||
useEffect(() => {
|
||||
setPostOptions((prev) => ({
|
||||
...prev,
|
||||
addClientTag: window.localStorage.getItem(StorageKey.ADD_CLIENT_TAG) === 'true'
|
||||
}))
|
||||
setAddClientTag(window.localStorage.getItem(StorageKey.ADD_CLIENT_TAG) === 'true')
|
||||
}, [])
|
||||
|
||||
if (!show) return null
|
||||
|
||||
const onAddClientTagChange = (checked: boolean) => {
|
||||
setPostOptions((prev) => ({ ...prev, addClientTag: checked }))
|
||||
setAddClientTag(checked)
|
||||
window.localStorage.setItem(StorageKey.ADD_CLIENT_TAG, checked.toString())
|
||||
}
|
||||
|
||||
|
|
@ -34,11 +30,7 @@ export default function PostOptions({
|
|||
<div className="space-y-2">
|
||||
<div className="flex items-center space-x-2">
|
||||
<Label htmlFor="add-client-tag">{t('Add client tag')}</Label>
|
||||
<Switch
|
||||
id="add-client-tag"
|
||||
checked={postOptions.addClientTag}
|
||||
onCheckedChange={onAddClientTagChange}
|
||||
/>
|
||||
<Switch id="add-client-tag" checked={addClientTag} onCheckedChange={onAddClientTagChange} />
|
||||
</div>
|
||||
<div className="text-muted-foreground text-xs">
|
||||
{t('Show others this was sent via Jumble')}
|
||||
|
|
|
|||
|
|
@ -4,46 +4,51 @@ import { Switch } from '@/components/ui/switch'
|
|||
import { isProtectedEvent } from '@/lib/event'
|
||||
import { simplifyUrl } from '@/lib/url'
|
||||
import { useFeed } from '@/providers/FeedProvider'
|
||||
import client from '@/services/client.service'
|
||||
import { Info } from 'lucide-react'
|
||||
import { Event } from 'nostr-tools'
|
||||
import { Dispatch, SetStateAction, useEffect } from 'react'
|
||||
import { Dispatch, SetStateAction, useMemo } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { TPostOptions } from './types'
|
||||
|
||||
export default function SendOnlyToSwitch({
|
||||
parentEvent,
|
||||
postOptions,
|
||||
setPostOptions
|
||||
specifiedRelayUrls,
|
||||
setSpecifiedRelayUrls
|
||||
}: {
|
||||
parentEvent?: Event
|
||||
postOptions: TPostOptions
|
||||
setPostOptions: Dispatch<SetStateAction<TPostOptions>>
|
||||
specifiedRelayUrls?: string[]
|
||||
setSpecifiedRelayUrls: Dispatch<SetStateAction<string[] | undefined>>
|
||||
}) {
|
||||
const { t } = useTranslation()
|
||||
const { relayUrls } = useFeed()
|
||||
|
||||
useEffect(() => {
|
||||
const isProtected = parentEvent ? isProtectedEvent(parentEvent) : false
|
||||
if (isProtected) {
|
||||
setPostOptions((prev) => ({ ...prev, sendOnlyToCurrentRelays: true }))
|
||||
const urls = useMemo(() => {
|
||||
if (!parentEvent) return relayUrls
|
||||
const isProtected = isProtectedEvent(parentEvent)
|
||||
const seenOn = client.getSeenEventRelayUrls(parentEvent.id)
|
||||
if (isProtected && seenOn.length) {
|
||||
setSpecifiedRelayUrls(seenOn)
|
||||
return seenOn
|
||||
}
|
||||
}, [])
|
||||
return relayUrls
|
||||
}, [parentEvent, relayUrls])
|
||||
|
||||
if (!urls.length) return null
|
||||
|
||||
return (
|
||||
<div className="flex items-center gap-2">
|
||||
<div className="flex items-center gap-1">
|
||||
<Label htmlFor="send-only-to-current-relays" className="truncate">
|
||||
{relayUrls.length === 1
|
||||
? t('Send only to r', { r: simplifyUrl(relayUrls[0]) })
|
||||
: t('Send only to current relays')}
|
||||
{urls.length === 1
|
||||
? t('Send only to r', { r: simplifyUrl(urls[0]) })
|
||||
: t('Send only to these relays')}
|
||||
</Label>
|
||||
{relayUrls.length > 1 && (
|
||||
{urls.length > 1 && (
|
||||
<Popover>
|
||||
<PopoverTrigger>
|
||||
<Info size={14} />
|
||||
</PopoverTrigger>
|
||||
<PopoverContent className="w-fit text-sm">
|
||||
{relayUrls.map((url) => (
|
||||
{urls.map((url) => (
|
||||
<div key={url}>{simplifyUrl(url)}</div>
|
||||
))}
|
||||
</PopoverContent>
|
||||
|
|
@ -53,10 +58,8 @@ export default function SendOnlyToSwitch({
|
|||
<Switch
|
||||
className="shrink-0"
|
||||
id="send-only-to-current-relays"
|
||||
checked={postOptions.sendOnlyToCurrentRelays}
|
||||
onCheckedChange={(checked) =>
|
||||
setPostOptions((prev) => ({ ...prev, sendOnlyToCurrentRelays: checked }))
|
||||
}
|
||||
checked={!!specifiedRelayUrls}
|
||||
onCheckedChange={(checked) => setSpecifiedRelayUrls(checked ? urls : undefined)}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,4 +0,0 @@
|
|||
export type TPostOptions = {
|
||||
addClientTag?: boolean
|
||||
sendOnlyToCurrentRelays?: boolean
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue