From e62afd5286c38f18db80a5d1ebe36ca1ad64a3a4 Mon Sep 17 00:00:00 2001 From: smolgrrr Date: Wed, 28 Aug 2024 13:40:48 +1000 Subject: [PATCH] fix URI embed --- .../modals/CardModals/TextModal.tsx | 45 +++++++++++++------ client/src/components/routes/Hashtags.tsx | 2 +- client/src/components/routes/Home.tsx | 14 +++--- client/src/hooks/useFetchEvents.ts | 2 +- 4 files changed, 42 insertions(+), 21 deletions(-) diff --git a/client/src/components/modals/CardModals/TextModal.tsx b/client/src/components/modals/CardModals/TextModal.tsx index 063ade8..67bff8b 100644 --- a/client/src/components/modals/CardModals/TextModal.tsx +++ b/client/src/components/modals/CardModals/TextModal.tsx @@ -6,25 +6,36 @@ import { nip19 } from "nostr-tools"; import { parseContent } from "../../../utils/content"; import QuoteEmbed from "./QuoteEmbed"; import LinkModal from "./LinkPreview"; +import { EventPointer } from "nostr-tools/lib/types/nip19"; const RichText = ({ text, isExpanded, emojiMap }: { text: string; isExpanded: boolean; emojiMap: Record }) => { - const content = isExpanded ? text.split('\n') : text.slice(0, 350).split('\n'); - + let content = isExpanded ? text.split('\n') : text.slice(0, 350).split('\n'); + return ( <> {content.map((line, i) => (
- {line.split(' ').map((word, j) => - emojiMap[word] - ? {word} - : `${word} ` - )} + {line.split(' ').map((word, j) => { + if (emojiMap[word]) { + return {word}; + } + const match = word.match(/(?:nostr:(?:nevent1|note1|npub1|nprofile1|naddr1)|@(?:nevent1|note1|npub1|nprofile1|naddr1))([a-z0-9]+)/i); + if (match) { + const fullIdentifier = match[0]; + const displayText = `@${fullIdentifier.replace(/^(@|nostr:)/, '').slice(0, 9)}`; + return <>{displayText}{' '}; + } else { + return `${word} `; + } + })}
))} ); }; +// ... rest of the file remains unchanged + const ContentPreview = ({ key, eventdata }: { key: string; eventdata: Event }) => { const { comment } = parseContent(eventdata); const [finalComment, setFinalComment] = useState(comment); @@ -45,12 +56,20 @@ const ContentPreview = ({ key, eventdata }: { key: string; eventdata: Event }) = setFinalComment(finalComment.replace(findUrl[0], "").trim()); } - const match = comment.match(/\bnostr:([a-z0-9]+)/i); - const nostrQuoteID = match && match[1]; - if (nostrQuoteID && nostrQuoteID.length > 0 && quoteEvents.length !== 0) { - let id_to_hex = String(nip19.decode(nostrQuoteID as string).data); - subNoteOnce(id_to_hex, onEvent); - setFinalComment(finalComment.replace("nostr:" + nostrQuoteID, "").trim()); + const match = comment.match(/\bnostr:(?:nevent1|note1)([a-z0-9]+)/i); + const nostrURI = match && match[1]; + if (nostrURI && quoteEvents.length === 0) { + if (match[1].startsWith('note')) { + setFinalComment(finalComment.replace("nostr:" + nostrURI, "").trim()); + let id_to_hex = String(nip19.decode(nostrURI).data); + subNoteOnce(id_to_hex, onEvent); + } else if (match[1].startsWith('nevent')) { + setFinalComment(finalComment.replace("nostr:" + nostrURI, "").trim()); + let { type, data } = nip19.decode(nostrURI) as { type: string, data: EventPointer }; + if (data.kind === 1) { + subNoteOnce(data.id, onEvent); + } + } } let newEmojiMap: Record = {}; diff --git a/client/src/components/routes/Hashtags.tsx b/client/src/components/routes/Hashtags.tsx index 15ea117..22c31d3 100644 --- a/client/src/components/routes/Hashtags.tsx +++ b/client/src/components/routes/Hashtags.tsx @@ -1,6 +1,6 @@ import React, { useState } from 'react'; -export const DefaultHashtags = ['asknostr', 'politics', 'technology', 'proofofwork','bitcoin', 'wired']; +export const DefaultHashtags = ['asknostr', 'proofofwork', 'bitcoin']; const Hashtags = () => { const [addedHashtags, setAddedHashtags] = useState(JSON.parse(localStorage.getItem('hashtags') as string) || []); diff --git a/client/src/components/routes/Home.tsx b/client/src/components/routes/Home.tsx index ce3d79d..b9dbef1 100644 --- a/client/src/components/routes/Home.tsx +++ b/client/src/components/routes/Home.tsx @@ -1,22 +1,24 @@ import NewNoteCard from "../forms/PostFormCard"; import { DEFAULT_DIFFICULTY } from "../../config"; import PostCard from "../modals/PostCard"; -import { useState, useEffect } from "react"; +import { useState, useEffect, useMemo } from "react"; import useProcessedEvents from "../../hooks/processedEvents"; const Home = () => { - const filterDifficulty = Number(localStorage.getItem('filterDifficulty')) || DEFAULT_DIFFICULTY; + const filterDifficulty = useMemo(() => { + return Number(localStorage.getItem('filterDifficulty')) || DEFAULT_DIFFICULTY; + }, []); + const { processedEvents } = useProcessedEvents(undefined, filterDifficulty); const [isAnimating, setIsAnimating] = useState(true); - // Step 3: Use useEffect to remove the animation class after 3 seconds useEffect(() => { const timer = setTimeout(() => { setIsAnimating(false); - }, 4000); // 3000 milliseconds = 3 seconds + }, 4000); - return () => clearTimeout(timer); // Cleanup the timer - }, []); // Empty dependency array means this effect runs once on mount + return () => clearTimeout(timer); + }, []); // Render the component return ( diff --git a/client/src/hooks/useFetchEvents.ts b/client/src/hooks/useFetchEvents.ts index 4368234..e08a38b 100644 --- a/client/src/hooks/useFetchEvents.ts +++ b/client/src/hooks/useFetchEvents.ts @@ -1,5 +1,5 @@ import { useState, useEffect } from "react"; -import { subGlobalFeed, subHashtagFeed, subNote, subNotifications, subNotesOnce} from "../utils/subscriptions"; +import { subGlobalFeed, subHashtagFeed, subNote, subNotifications} from "../utils/subscriptions"; import { uniqBy } from "../utils/otherUtils"; import { Event } from "nostr-tools";