feat: 💨

This commit is contained in:
codytseng
2025-02-11 22:46:08 +08:00
parent 8f5c3ac4d6
commit 2cde70dff4
9 changed files with 64 additions and 86 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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')}

View File

@@ -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>
)

View File

@@ -1,4 +0,0 @@
export type TPostOptions = {
addClientTag?: boolean
sendOnlyToCurrentRelays?: boolean
}

View File

@@ -164,7 +164,7 @@ export default {
'Login to set': 'Login to set',
'Please login to view following feed': 'Please login to view following feed',
'Send only to r': 'Send only to {{r}}',
'Send only to current relays': 'Send only to current relays',
'Send only to these relays': 'Send only to these relays',
Explore: 'Explore',
'Search relays': 'Search relays',
relayInfoBadgeAuth: 'Auth',

View File

@@ -165,7 +165,7 @@ export default {
'Login to set': '登录后设置',
'Please login to view following feed': '请登录以查看关注动态',
'Send only to r': '只发送到 {{r}}',
'Send only to current relays': '只发送到当前服务器',
'Send only to these relays': '只发送到这些服务器',
Explore: '探索',
'Search relays': '搜索服务器',
relayInfoBadgeAuth: '需登陆',

View File

@@ -111,7 +111,9 @@ export function SecondaryPageTitlebar({
{title}
</div>
) : (
<BackButton>{title}</BackButton>
<div className="flex items-center flex-1 w-0">
<BackButton>{title}</BackButton>
</div>
)}
<div className="flex-shrink-0">{controls}</div>
</Titlebar>

View File

@@ -49,17 +49,17 @@ class ClientService extends EventTarget {
private profileEventCache = new LRUCache<string, Promise<NEvent | undefined>>({ max: 10000 })
private profileEventDataloader = new DataLoader<string, NEvent | undefined>(
(ids) => Promise.all(ids.map((id) => this._fetchProfileEvent(id))),
{ cacheMap: this.profileEventCache, maxBatchSize: 20 }
{ cacheMap: this.profileEventCache, maxBatchSize: 50 }
)
private fetchProfileEventFromDefaultRelaysDataloader = new DataLoader<string, NEvent | undefined>(
this.profileEventBatchLoadFn.bind(this),
{ cache: false, maxBatchSize: 20 }
{ cache: false, maxBatchSize: 50 }
)
private relayListEventDataLoader = new DataLoader<string, NEvent | undefined>(
this.relayListEventBatchLoadFn.bind(this),
{
cacheMap: new LRUCache<string, Promise<NEvent | undefined>>({ max: 10000 }),
maxBatchSize: 20
maxBatchSize: 50
}
)
private followListCache = new LRUCache<string, Promise<NEvent | undefined>>({
@@ -578,9 +578,9 @@ class ClientService extends EventTarget {
async initUserIndexFromFollowings(pubkey: string) {
const followings = await this.fetchFollowings(pubkey)
for (let i = 0; i * 20 < followings.length; i++) {
await this.profileEventDataloader.loadMany(followings.slice(i * 20, (i + 1) * 20))
await new Promise((resolve) => setTimeout(resolve, 200))
for (let i = 0; i * 50 < followings.length; i++) {
await this.profileEventDataloader.loadMany(followings.slice(i * 50, (i + 1) * 50))
await new Promise((resolve) => setTimeout(resolve, 30000))
}
}