wip: refactor

This commit is contained in:
Ren Amamiya
2023-08-18 07:37:11 +07:00
parent 414dd50a5c
commit 5626579b3f
17 changed files with 156 additions and 307 deletions

View File

@@ -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
View File

@@ -0,0 +1,3 @@
export function isImage(url: string) {
return /\.(jpg|jpeg|gif|png|webp|avif)$/.test(url);
}

View File

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