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

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