From d322f595fe6a15c87a87cbde282f100b80eb5f74 Mon Sep 17 00:00:00 2001 From: codytseng Date: Tue, 11 Feb 2025 10:15:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=92=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/NoteStats/LikeButton.tsx | 9 ++- .../PostEditor/NormalPostContent.tsx | 6 ++ .../PostEditor/PicturePostContent.tsx | 2 + src/components/PostEditor/PostOptions.tsx | 42 ++----------- .../PostEditor/SendOnlyToSwitch.tsx | 63 +++++++++++++++++++ src/lib/draft-event.ts | 5 ++ 6 files changed, 88 insertions(+), 39 deletions(-) create mode 100644 src/components/PostEditor/SendOnlyToSwitch.tsx diff --git a/src/components/NoteStats/LikeButton.tsx b/src/components/NoteStats/LikeButton.tsx index 3947761d..d52ba9c8 100644 --- a/src/components/NoteStats/LikeButton.tsx +++ b/src/components/NoteStats/LikeButton.tsx @@ -1,4 +1,5 @@ import { createReactionDraftEvent } from '@/lib/draft-event' +import { isProtectedEvent } from '@/lib/event' import { cn } from '@/lib/utils' import { useNostr } from '@/providers/NostrProvider' import { useNoteStats } from '@/providers/NoteStatsProvider' @@ -54,7 +55,13 @@ export default function LikeButton({ const targetRelayList = await client.fetchRelayList(event.pubkey) const reaction = createReactionDraftEvent(event) - await publish(reaction, { additionalRelayUrls: targetRelayList.read.slice(0, 3) }) + const isProtected = isProtectedEvent(event) + if (isProtected) { + const seenOn = client.getSeenEventRelayUrls(event.id) + await publish(reaction, { specifiedRelayUrls: seenOn }) + } else { + await publish(reaction, { additionalRelayUrls: targetRelayList.read.slice(0, 3) }) + } markNoteAsLiked(event.id) } catch (error) { console.error('like failed', error) diff --git a/src/components/PostEditor/NormalPostContent.tsx b/src/components/PostEditor/NormalPostContent.tsx index 52ff354a..4b2e201a 100644 --- a/src/components/PostEditor/NormalPostContent.tsx +++ b/src/components/PostEditor/NormalPostContent.tsx @@ -12,6 +12,7 @@ import TextareaWithMentions from '../TextareaWithMentions.tsx' 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' @@ -110,6 +111,11 @@ export default function NormalPostContent({ placeholder={t('Write something...')} /> {content && } +
void }) { textValue={content} placeholder={t('Write something...')} /> +
) } diff --git a/src/components/PostEditor/SendOnlyToSwitch.tsx b/src/components/PostEditor/SendOnlyToSwitch.tsx new file mode 100644 index 00000000..d6db868a --- /dev/null +++ b/src/components/PostEditor/SendOnlyToSwitch.tsx @@ -0,0 +1,63 @@ +import { Label } from '@/components/ui/label' +import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover' +import { Switch } from '@/components/ui/switch' +import { isProtectedEvent } from '@/lib/event' +import { simplifyUrl } from '@/lib/url' +import { useFeed } from '@/providers/FeedProvider' +import { Info } from 'lucide-react' +import { Event } from 'nostr-tools' +import { Dispatch, SetStateAction, useEffect } from 'react' +import { useTranslation } from 'react-i18next' +import { TPostOptions } from './types' + +export default function SendOnlyToSwitch({ + parentEvent, + postOptions, + setPostOptions +}: { + parentEvent?: Event + postOptions: TPostOptions + setPostOptions: Dispatch> +}) { + const { t } = useTranslation() + const { relayUrls } = useFeed() + + useEffect(() => { + const isProtected = parentEvent ? isProtectedEvent(parentEvent) : false + if (isProtected) { + setPostOptions((prev) => ({ ...prev, sendOnlyToCurrentRelays: true })) + } + }, []) + + return ( +
+
+ + {relayUrls.length > 1 && ( + + + + + + {relayUrls.map((url) => ( +
{simplifyUrl(url)}
+ ))} +
+
+ )} +
+ + setPostOptions((prev) => ({ ...prev, sendOnlyToCurrentRelays: checked })) + } + /> +
+ ) +} diff --git a/src/lib/draft-event.ts b/src/lib/draft-event.ts index 31baa97b..629f3c3c 100644 --- a/src/lib/draft-event.ts +++ b/src/lib/draft-event.ts @@ -9,6 +9,7 @@ import { extractImagesFromContent, extractMentions, getEventCoordinate, + isProtectedEvent, isReplaceable } from './event' @@ -25,6 +26,10 @@ export function createReactionDraftEvent(event: Event): TDraftEvent { tags.push(hint ? ['a', getEventCoordinate(event), hint] : ['a', getEventCoordinate(event)]) } + if (isProtectedEvent(event)) { + tags.push(['-']) + } + return { kind: kinds.Reaction, content: '+',