fix URI embed

This commit is contained in:
smolgrrr 2024-08-28 13:40:48 +10:00
parent 61c289792d
commit e62afd5286
4 changed files with 42 additions and 21 deletions

View File

@ -6,25 +6,36 @@ import { nip19 } from "nostr-tools";
import { parseContent } from "../../../utils/content"; import { parseContent } from "../../../utils/content";
import QuoteEmbed from "./QuoteEmbed"; import QuoteEmbed from "./QuoteEmbed";
import LinkModal from "./LinkPreview"; import LinkModal from "./LinkPreview";
import { EventPointer } from "nostr-tools/lib/types/nip19";
const RichText = ({ text, isExpanded, emojiMap }: { text: string; isExpanded: boolean; emojiMap: Record<string, any> }) => { const RichText = ({ text, isExpanded, emojiMap }: { text: string; isExpanded: boolean; emojiMap: Record<string, any> }) => {
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 ( return (
<> <>
{content.map((line, i) => ( {content.map((line, i) => (
<div key={i}> <div key={i}>
{line.split(' ').map((word, j) => {line.split(' ').map((word, j) => {
emojiMap[word] if (emojiMap[word]) {
? <img className="w-9 h-9 mx-0.5 inline" src={emojiMap[word]} alt={word} key={j} /> return <img className="w-9 h-9 mx-0.5 inline" src={emojiMap[word]} alt={word} key={j} />;
: `${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 <><a className="underline" href={`https://njump.me/${fullIdentifier.replace(/^(@|nostr:)/, '')}`} key={j} target="_blank" rel="noopener noreferrer">{displayText}</a>{' '}</>;
} else {
return `${word} `;
}
})}
</div> </div>
))} ))}
</> </>
); );
}; };
// ... rest of the file remains unchanged
const ContentPreview = ({ key, eventdata }: { key: string; eventdata: Event }) => { const ContentPreview = ({ key, eventdata }: { key: string; eventdata: Event }) => {
const { comment } = parseContent(eventdata); const { comment } = parseContent(eventdata);
const [finalComment, setFinalComment] = useState(comment); const [finalComment, setFinalComment] = useState(comment);
@ -45,12 +56,20 @@ const ContentPreview = ({ key, eventdata }: { key: string; eventdata: Event }) =
setFinalComment(finalComment.replace(findUrl[0], "").trim()); setFinalComment(finalComment.replace(findUrl[0], "").trim());
} }
const match = comment.match(/\bnostr:([a-z0-9]+)/i); const match = comment.match(/\bnostr:(?:nevent1|note1)([a-z0-9]+)/i);
const nostrQuoteID = match && match[1]; const nostrURI = match && match[1];
if (nostrQuoteID && nostrQuoteID.length > 0 && quoteEvents.length !== 0) { if (nostrURI && quoteEvents.length === 0) {
let id_to_hex = String(nip19.decode(nostrQuoteID as string).data); 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); subNoteOnce(id_to_hex, onEvent);
setFinalComment(finalComment.replace("nostr:" + nostrQuoteID, "").trim()); } 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<string, any> = {}; let newEmojiMap: Record<string, any> = {};

View File

@ -1,6 +1,6 @@
import React, { useState } from 'react'; import React, { useState } from 'react';
export const DefaultHashtags = ['asknostr', 'politics', 'technology', 'proofofwork','bitcoin', 'wired']; export const DefaultHashtags = ['asknostr', 'proofofwork', 'bitcoin'];
const Hashtags = () => { const Hashtags = () => {
const [addedHashtags, setAddedHashtags] = useState<string[]>(JSON.parse(localStorage.getItem('hashtags') as string) || []); const [addedHashtags, setAddedHashtags] = useState<string[]>(JSON.parse(localStorage.getItem('hashtags') as string) || []);

View File

@ -1,22 +1,24 @@
import NewNoteCard from "../forms/PostFormCard"; import NewNoteCard from "../forms/PostFormCard";
import { DEFAULT_DIFFICULTY } from "../../config"; import { DEFAULT_DIFFICULTY } from "../../config";
import PostCard from "../modals/PostCard"; import PostCard from "../modals/PostCard";
import { useState, useEffect } from "react"; import { useState, useEffect, useMemo } from "react";
import useProcessedEvents from "../../hooks/processedEvents"; import useProcessedEvents from "../../hooks/processedEvents";
const Home = () => { 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 { processedEvents } = useProcessedEvents(undefined, filterDifficulty);
const [isAnimating, setIsAnimating] = useState(true); const [isAnimating, setIsAnimating] = useState(true);
// Step 3: Use useEffect to remove the animation class after 3 seconds
useEffect(() => { useEffect(() => {
const timer = setTimeout(() => { const timer = setTimeout(() => {
setIsAnimating(false); setIsAnimating(false);
}, 4000); // 3000 milliseconds = 3 seconds }, 4000);
return () => clearTimeout(timer); // Cleanup the timer return () => clearTimeout(timer);
}, []); // Empty dependency array means this effect runs once on mount }, []);
// Render the component // Render the component
return ( return (

View File

@ -1,5 +1,5 @@
import { useState, useEffect } from "react"; 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 { uniqBy } from "../utils/otherUtils";
import { Event } from "nostr-tools"; import { Event } from "nostr-tools";