wip: refactor
This commit is contained in:
@@ -4,6 +4,7 @@ import { useQuery } from '@tanstack/react-query';
|
||||
import { useNDK } from '@libs/ndk/provider';
|
||||
|
||||
import { parser } from '@utils/parser';
|
||||
import { RichContent } from '@utils/types';
|
||||
|
||||
export function useEvent(id: string, embed?: string) {
|
||||
const { ndk } = useNDK();
|
||||
@@ -12,18 +13,18 @@ export function useEvent(id: string, embed?: string) {
|
||||
async () => {
|
||||
if (embed) {
|
||||
const event: NDKEvent = JSON.parse(embed);
|
||||
// @ts-expect-error, #TODO: convert NDKEvent to ExNDKEvent
|
||||
if (event.kind === 1) event.content = parser(event);
|
||||
let richContent: RichContent;
|
||||
if (event.kind === 1) richContent = parser(event);
|
||||
|
||||
return event as unknown as NDKEvent;
|
||||
return { event: event as NDKEvent, richContent: richContent };
|
||||
}
|
||||
|
||||
const event = (await ndk.fetchEvent(id)) as NDKEvent;
|
||||
if (!event) throw new Error('event not found');
|
||||
// @ts-expect-error, #TODO: convert NDKEvent to ExNDKEvent
|
||||
if (event.kind === 1) event.content = parser(event);
|
||||
let richContent: RichContent;
|
||||
if (event.kind === 1) richContent = parser(event);
|
||||
|
||||
return event as NDKEvent;
|
||||
return { event: event as NDKEvent, richContent: richContent };
|
||||
},
|
||||
{
|
||||
staleTime: Infinity,
|
||||
|
||||
3
src/utils/isImage.tsx
Normal file
3
src/utils/isImage.tsx
Normal file
@@ -0,0 +1,3 @@
|
||||
export function isImage(url: string) {
|
||||
return /\.(jpg|jpeg|gif|png|webp|avif)$/.test(url);
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
import { NDKEvent } from '@nostr-dev-kit/ndk';
|
||||
import getUrls from 'get-urls';
|
||||
import { Event, parseReferences } from 'nostr-tools';
|
||||
import ReactPlayer from 'react-player';
|
||||
|
||||
import { RichContent } from '@utils/types';
|
||||
|
||||
@@ -26,19 +25,23 @@ export function parser(event: NDKEvent) {
|
||||
content.images.push(url);
|
||||
// remove url from original content
|
||||
content.parsed = content.parsed.replace(url, '');
|
||||
} else if (ReactPlayer.canPlay(url)) {
|
||||
}
|
||||
|
||||
if (url.match(/\.(mp4|mov|webm|wmv|flv|mts|avi|ogv|mkv|mp3|m3u8)$/)) {
|
||||
// video
|
||||
content.videos.push(url);
|
||||
// remove url from original content
|
||||
content.parsed = content.parsed.replace(url, '');
|
||||
} else {
|
||||
if (content.links.length < 1) {
|
||||
// push to store
|
||||
content.links.push(url);
|
||||
// remove url from original content
|
||||
content.parsed = content.parsed.replace(url, '');
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
if (content.links.length < 1) {
|
||||
// push to store
|
||||
content.links.push(url);
|
||||
// remove url from original content
|
||||
content.parsed = content.parsed.replace(url, '');
|
||||
}
|
||||
*/
|
||||
});
|
||||
|
||||
// parse hashtag
|
||||
|
||||
Reference in New Issue
Block a user