From 3455eb701f145cdb9de0a54c3e3e0fa6ae2eb51e Mon Sep 17 00:00:00 2001 From: Ren Amamiya <123083837+reyamir@users.noreply.github.com> Date: Wed, 23 Aug 2023 15:18:59 +0700 Subject: [PATCH] rename some files and add nip 94 widget --- src/app/space/components/widgets/article.tsx | 2 +- src/app/space/components/widgets/feed.tsx | 2 +- src/app/space/components/widgets/file.tsx | 90 +++++++++++++++++++ src/app/space/components/widgets/hashtag.tsx | 2 +- src/app/space/components/widgets/thread.tsx | 2 +- .../components/widgets/trendingNotes.tsx | 2 +- .../components/widgets/trendingProfile.tsx | 2 +- src/app/space/index.tsx | 3 + src/shared/composer/modal.tsx | 4 +- src/shared/notes/kinds/file.tsx | 56 +++++++----- src/shared/notes/preview/link.tsx | 1 - src/stores/{composer.tsx => composer.ts} | 0 src/stores/{constants.tsx => constants.ts} | 0 src/stores/{onboarding.tsx => onboarding.ts} | 0 src/stores/{stronghold.tsx => stronghold.ts} | 0 src/stores/{widgets.tsx => widgets.ts} | 0 ...ctiveAccount.tsx => checkActiveAccount.ts} | 0 ...BlobFromFile.tsx => createBlobFromFile.ts} | 0 src/utils/{createdAt.tsx => createdAt.ts} | 0 src/utils/{date.tsx => date.ts} | 0 src/utils/hooks/{useEvent.tsx => useEvent.ts} | 0 ...eNetworkStatus.tsx => useNetworkStatus.ts} | 0 src/utils/hooks/{useNostr.tsx => useNostr.ts} | 0 .../{useOpenGraph.tsx => useOpenGraph.ts} | 0 .../hooks/{useProfile.tsx => useProfile.ts} | 0 .../hooks/{useSocial.tsx => useSocial.ts} | 0 .../hooks/{useUploader.tsx => useUploader.ts} | 0 src/utils/isImage.tsx | 3 - src/utils/nip94.ts | 11 +++ .../{notification.tsx => notification.ts} | 0 src/utils/{number.tsx => number.ts} | 0 src/utils/{parser.tsx => parser.ts} | 0 src/utils/{shortenKey.tsx => shortenKey.ts} | 0 src/utils/{transform.tsx => transform.ts} | 0 34 files changed, 145 insertions(+), 35 deletions(-) create mode 100644 src/app/space/components/widgets/file.tsx rename src/stores/{composer.tsx => composer.ts} (100%) rename src/stores/{constants.tsx => constants.ts} (100%) rename src/stores/{onboarding.tsx => onboarding.ts} (100%) rename src/stores/{stronghold.tsx => stronghold.ts} (100%) rename src/stores/{widgets.tsx => widgets.ts} (100%) rename src/utils/{checkActiveAccount.tsx => checkActiveAccount.ts} (100%) rename src/utils/{createBlobFromFile.tsx => createBlobFromFile.ts} (100%) rename src/utils/{createdAt.tsx => createdAt.ts} (100%) rename src/utils/{date.tsx => date.ts} (100%) rename src/utils/hooks/{useEvent.tsx => useEvent.ts} (100%) rename src/utils/hooks/{useNetworkStatus.tsx => useNetworkStatus.ts} (100%) rename src/utils/hooks/{useNostr.tsx => useNostr.ts} (100%) rename src/utils/hooks/{useOpenGraph.tsx => useOpenGraph.ts} (100%) rename src/utils/hooks/{useProfile.tsx => useProfile.ts} (100%) rename src/utils/hooks/{useSocial.tsx => useSocial.ts} (100%) rename src/utils/hooks/{useUploader.tsx => useUploader.ts} (100%) delete mode 100644 src/utils/isImage.tsx create mode 100644 src/utils/nip94.ts rename src/utils/{notification.tsx => notification.ts} (100%) rename src/utils/{number.tsx => number.ts} (100%) rename src/utils/{parser.tsx => parser.ts} (100%) rename src/utils/{shortenKey.tsx => shortenKey.ts} (100%) rename src/utils/{transform.tsx => transform.ts} (100%) diff --git a/src/app/space/components/widgets/article.tsx b/src/app/space/components/widgets/article.tsx index 808047bf..81bda09c 100644 --- a/src/app/space/components/widgets/article.tsx +++ b/src/app/space/components/widgets/article.tsx @@ -47,7 +47,7 @@ export function ArticleWidget({ params }: { params: Widget }) { ); return ( -
+
{status === 'loading' ? ( diff --git a/src/app/space/components/widgets/feed.tsx b/src/app/space/components/widgets/feed.tsx index 95a7c3c6..1858dba3 100644 --- a/src/app/space/components/widgets/feed.tsx +++ b/src/app/space/components/widgets/feed.tsx @@ -116,7 +116,7 @@ export function FeedWidget({ params }: { params: Widget }) { ); return ( -
+
{status === 'loading' ? ( diff --git a/src/app/space/components/widgets/file.tsx b/src/app/space/components/widgets/file.tsx new file mode 100644 index 00000000..e3a82853 --- /dev/null +++ b/src/app/space/components/widgets/file.tsx @@ -0,0 +1,90 @@ +import { NDKEvent } from '@nostr-dev-kit/ndk'; +import { useQuery } from '@tanstack/react-query'; +import { useVirtualizer } from '@tanstack/react-virtual'; +import { useCallback, useRef } from 'react'; + +import { useNDK } from '@libs/ndk/provider'; + +import { FileNote, NoteSkeleton, NoteWrapper } from '@shared/notes'; +import { TitleBar } from '@shared/titleBar'; + +import { Widget } from '@utils/types'; + +export function FileWidget({ params }: { params: Widget }) { + const { ndk } = useNDK(); + const { status, data } = useQuery(['file-widget', params.content], async () => { + const events = await ndk.fetchEvents({ + kinds: [1063], + limit: 100, + }); + return [...events] as unknown as NDKEvent[]; + }); + + const parentRef = useRef(null); + const virtualizer = useVirtualizer({ + count: data ? data.length : 0, + getScrollElement: () => parentRef.current, + estimateSize: () => 650, + overscan: 4, + }); + const items = virtualizer.getVirtualItems(); + + // render event match event kind + const renderItem = useCallback( + (index: string | number) => { + const event: NDKEvent = data[index]; + if (!event) return; + + return ( +
+ + + +
+ ); + }, + [data] + ); + + return ( +
+ +
+ {status === 'loading' ? ( +
+
+ +
+
+ ) : items.length === 0 ? ( +
+
+
+

+ There have been no new files in the last 24 hours. +

+
+
+
+ ) : ( +
+
+ {items.map((item) => renderItem(item.index))} +
+
+ )} +
+
+ ); +} diff --git a/src/app/space/components/widgets/hashtag.tsx b/src/app/space/components/widgets/hashtag.tsx index b82532cb..7614d523 100644 --- a/src/app/space/components/widgets/hashtag.tsx +++ b/src/app/space/components/widgets/hashtag.tsx @@ -109,7 +109,7 @@ export function HashtagWidget({ params }: { params: Widget }) { ); return ( -
+
{status === 'loading' ? ( diff --git a/src/app/space/components/widgets/thread.tsx b/src/app/space/components/widgets/thread.tsx index d07c32a8..51505656 100644 --- a/src/app/space/components/widgets/thread.tsx +++ b/src/app/space/components/widgets/thread.tsx @@ -41,7 +41,7 @@ export function ThreadBlock({ params }: { params: Widget }) { ); return ( -
+
{status === 'loading' ? ( diff --git a/src/app/space/components/widgets/trendingNotes.tsx b/src/app/space/components/widgets/trendingNotes.tsx index 3710c698..2717a7e5 100644 --- a/src/app/space/components/widgets/trendingNotes.tsx +++ b/src/app/space/components/widgets/trendingNotes.tsx @@ -31,7 +31,7 @@ export function TrendingNotesWidget({ params }: { params: Widget }) { ); return ( -
+
{status === 'loading' ? ( diff --git a/src/app/space/components/widgets/trendingProfile.tsx b/src/app/space/components/widgets/trendingProfile.tsx index 72e65abf..be07c0b9 100644 --- a/src/app/space/components/widgets/trendingProfile.tsx +++ b/src/app/space/components/widgets/trendingProfile.tsx @@ -32,7 +32,7 @@ export function TrendingProfilesWidget({ params }: { params: Widget }) { ); return ( -
+
{status === 'loading' ? ( diff --git a/src/app/space/index.tsx b/src/app/space/index.tsx index 06fb9ac2..c2be9fe4 100644 --- a/src/app/space/index.tsx +++ b/src/app/space/index.tsx @@ -5,6 +5,7 @@ import { FeedWidgetForm } from '@app/space/components/forms/feed'; import { HashTagWidgetForm } from '@app/space/components/forms/hashtag'; import { ArticleWidget } from '@app/space/components/widgets/article'; import { FeedWidget } from '@app/space/components/widgets/feed'; +import { FileWidget } from '@app/space/components/widgets/file'; import { HashtagWidget } from '@app/space/components/widgets/hashtag'; import { NetworkWidget } from '@app/space/components/widgets/network'; import { ThreadBlock } from '@app/space/components/widgets/thread'; @@ -48,6 +49,8 @@ export function SpaceScreen() { return ; case WidgetKinds.article: return ; + case WidgetKinds.file: + return ; case WidgetKinds.xhashtag: return ; case WidgetKinds.xfeed: diff --git a/src/shared/composer/modal.tsx b/src/shared/composer/modal.tsx index 7d6871ee..085ca64c 100644 --- a/src/shared/composer/modal.tsx +++ b/src/shared/composer/modal.tsx @@ -21,7 +21,7 @@ export function ComposerModal() {
toggle(false)} - className="inline-flex h-8 w-8 items-center justify-center rounded-md hover:bg-zinc-800" + className="inline-flex h-8 w-8 items-center justify-center rounded-md hover:bg-white/10" > diff --git a/src/shared/notes/kinds/file.tsx b/src/shared/notes/kinds/file.tsx index d530db94..de8e1b77 100644 --- a/src/shared/notes/kinds/file.tsx +++ b/src/shared/notes/kinds/file.tsx @@ -1,35 +1,45 @@ import { NDKEvent } from '@nostr-dev-kit/ndk'; +import ReactPlayer from 'react-player'; import { Image } from '@shared/image'; -import { NoteActions, NoteMetadata } from '@shared/notes'; -import { User } from '@shared/user'; -import { isImage } from '@utils/isImage'; +import { fileType } from '@utils/nip94'; export function FileNote({ event }: { event: NDKEvent }) { const url = event.tags.find((el) => el[0] === 'url')[1]; + const type = fileType(url); + + if (type === 'image') { + return ( +
+ {event.content} +
+ ); + } + + if (type === 'video') { + return ( +
+ +
+ ); + } return ( -
-
-
- -
-
-
- {isImage(url) && ( - image - )} - -
-
- -
-
+
+

{url}

); } diff --git a/src/shared/notes/preview/link.tsx b/src/shared/notes/preview/link.tsx index 3ad7eaef..1c105dfe 100644 --- a/src/shared/notes/preview/link.tsx +++ b/src/shared/notes/preview/link.tsx @@ -38,7 +38,6 @@ export function LinkPreview({ urls }: { urls: string[] }) { {data.image && ( {urls[0]} diff --git a/src/stores/composer.tsx b/src/stores/composer.ts similarity index 100% rename from src/stores/composer.tsx rename to src/stores/composer.ts diff --git a/src/stores/constants.tsx b/src/stores/constants.ts similarity index 100% rename from src/stores/constants.tsx rename to src/stores/constants.ts diff --git a/src/stores/onboarding.tsx b/src/stores/onboarding.ts similarity index 100% rename from src/stores/onboarding.tsx rename to src/stores/onboarding.ts diff --git a/src/stores/stronghold.tsx b/src/stores/stronghold.ts similarity index 100% rename from src/stores/stronghold.tsx rename to src/stores/stronghold.ts diff --git a/src/stores/widgets.tsx b/src/stores/widgets.ts similarity index 100% rename from src/stores/widgets.tsx rename to src/stores/widgets.ts diff --git a/src/utils/checkActiveAccount.tsx b/src/utils/checkActiveAccount.ts similarity index 100% rename from src/utils/checkActiveAccount.tsx rename to src/utils/checkActiveAccount.ts diff --git a/src/utils/createBlobFromFile.tsx b/src/utils/createBlobFromFile.ts similarity index 100% rename from src/utils/createBlobFromFile.tsx rename to src/utils/createBlobFromFile.ts diff --git a/src/utils/createdAt.tsx b/src/utils/createdAt.ts similarity index 100% rename from src/utils/createdAt.tsx rename to src/utils/createdAt.ts diff --git a/src/utils/date.tsx b/src/utils/date.ts similarity index 100% rename from src/utils/date.tsx rename to src/utils/date.ts diff --git a/src/utils/hooks/useEvent.tsx b/src/utils/hooks/useEvent.ts similarity index 100% rename from src/utils/hooks/useEvent.tsx rename to src/utils/hooks/useEvent.ts diff --git a/src/utils/hooks/useNetworkStatus.tsx b/src/utils/hooks/useNetworkStatus.ts similarity index 100% rename from src/utils/hooks/useNetworkStatus.tsx rename to src/utils/hooks/useNetworkStatus.ts diff --git a/src/utils/hooks/useNostr.tsx b/src/utils/hooks/useNostr.ts similarity index 100% rename from src/utils/hooks/useNostr.tsx rename to src/utils/hooks/useNostr.ts diff --git a/src/utils/hooks/useOpenGraph.tsx b/src/utils/hooks/useOpenGraph.ts similarity index 100% rename from src/utils/hooks/useOpenGraph.tsx rename to src/utils/hooks/useOpenGraph.ts diff --git a/src/utils/hooks/useProfile.tsx b/src/utils/hooks/useProfile.ts similarity index 100% rename from src/utils/hooks/useProfile.tsx rename to src/utils/hooks/useProfile.ts diff --git a/src/utils/hooks/useSocial.tsx b/src/utils/hooks/useSocial.ts similarity index 100% rename from src/utils/hooks/useSocial.tsx rename to src/utils/hooks/useSocial.ts diff --git a/src/utils/hooks/useUploader.tsx b/src/utils/hooks/useUploader.ts similarity index 100% rename from src/utils/hooks/useUploader.tsx rename to src/utils/hooks/useUploader.ts diff --git a/src/utils/isImage.tsx b/src/utils/isImage.tsx deleted file mode 100644 index 9a5d4d60..00000000 --- a/src/utils/isImage.tsx +++ /dev/null @@ -1,3 +0,0 @@ -export function isImage(url: string) { - return /\.(jpg|jpeg|gif|png|webp|avif)$/.test(url); -} diff --git a/src/utils/nip94.ts b/src/utils/nip94.ts new file mode 100644 index 00000000..8582a319 --- /dev/null +++ b/src/utils/nip94.ts @@ -0,0 +1,11 @@ +export function fileType(url: string) { + if (url.match(/\.(jpg|jpeg|gif|png|webp|avif)$/)) { + return 'image'; + } + + if (url.match(/\.(mp4|mov|webm|wmv|flv|mts|avi|ogv|mkv|mp3|m3u8)$/)) { + return 'video'; + } + + return 'link'; +} diff --git a/src/utils/notification.tsx b/src/utils/notification.ts similarity index 100% rename from src/utils/notification.tsx rename to src/utils/notification.ts diff --git a/src/utils/number.tsx b/src/utils/number.ts similarity index 100% rename from src/utils/number.tsx rename to src/utils/number.ts diff --git a/src/utils/parser.tsx b/src/utils/parser.ts similarity index 100% rename from src/utils/parser.tsx rename to src/utils/parser.ts diff --git a/src/utils/shortenKey.tsx b/src/utils/shortenKey.ts similarity index 100% rename from src/utils/shortenKey.tsx rename to src/utils/shortenKey.ts diff --git a/src/utils/transform.tsx b/src/utils/transform.ts similarity index 100% rename from src/utils/transform.tsx rename to src/utils/transform.ts