From 0cdf199cb5effe0d29bf86e8a6ab420f5b5a21e0 Mon Sep 17 00:00:00 2001 From: reya Date: Thu, 9 Nov 2023 18:02:25 +0700 Subject: [PATCH] wip: rework widget --- src/app.tsx | 4 +- src/app/auth/onboarding/hashtag.tsx | 10 +- src/app/{space => home}/index.tsx | 75 +++------- src/app/space/components/widgetList.tsx | 128 ------------------ src/shared/notes/actions.tsx | 2 +- src/shared/notes/mentions/hashtag.tsx | 2 +- src/shared/notes/mentions/note.tsx | 2 +- src/shared/notes/mentions/user.tsx | 9 +- src/shared/notes/notify.tsx | 2 +- src/shared/notes/text.tsx | 2 +- .../{local/articles.tsx => article.tsx} | 30 ++-- .../widgets/{local/files.tsx => file.tsx} | 30 ++-- src/shared/widgets/global/articles.tsx | 106 --------------- src/shared/widgets/global/files.tsx | 106 --------------- .../widgets/{local/feeds.tsx => group.tsx} | 9 +- src/shared/widgets/{global => }/hashtag.tsx | 10 +- src/shared/widgets/index.ts | 26 ++-- src/shared/widgets/newsfeed.tsx | 3 +- .../widgets/nostrBand/trendingAccounts.tsx | 5 +- .../widgets/nostrBand/trendingNotes.tsx | 6 +- .../widgets/{ => other}/liveUpdater.tsx | 0 .../{ => other}/nostrBandUserProfile.tsx | 0 .../widgets/other/toggleWidgetList.tsx} | 2 +- src/shared/widgets/other/widgetList.tsx | 30 ++++ src/shared/widgets/{ => other}/wrapper.tsx | 0 src/shared/widgets/{local => }/thread.tsx | 8 +- src/shared/widgets/tmp/feeds.tsx | 2 +- src/shared/widgets/tmp/hashtag.tsx | 2 +- src/shared/widgets/topic.tsx | 2 +- src/shared/widgets/{local => }/user.tsx | 10 +- src/stores/constants.ts | 41 ++---- 31 files changed, 155 insertions(+), 509 deletions(-) rename src/app/{space => home}/index.tsx (56%) delete mode 100644 src/app/space/components/widgetList.tsx rename src/shared/widgets/{local/articles.tsx => article.tsx} (84%) rename src/shared/widgets/{local/files.tsx => file.tsx} (84%) delete mode 100644 src/shared/widgets/global/articles.tsx delete mode 100644 src/shared/widgets/global/files.tsx rename src/shared/widgets/{local/feeds.tsx => group.tsx} (93%) rename src/shared/widgets/{global => }/hashtag.tsx (94%) rename src/shared/widgets/{ => other}/liveUpdater.tsx (100%) rename src/shared/widgets/{ => other}/nostrBandUserProfile.tsx (100%) rename src/{app/space/components/toggle.tsx => shared/widgets/other/toggleWidgetList.tsx} (91%) create mode 100644 src/shared/widgets/other/widgetList.tsx rename src/shared/widgets/{ => other}/wrapper.tsx (100%) rename src/shared/widgets/{local => }/thread.tsx (88%) rename src/shared/widgets/{local => }/user.tsx (92%) diff --git a/src/app.tsx b/src/app.tsx index 04f75c39..409fa4da 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -54,8 +54,8 @@ export default function App() { { path: '', async lazy() { - const { SpaceScreen } = await import('@app/space'); - return { Component: SpaceScreen }; + const { HomeScreen } = await import('@app/home'); + return { Component: HomeScreen }; }, }, { diff --git a/src/app/auth/onboarding/hashtag.tsx b/src/app/auth/onboarding/hashtag.tsx index 92b4fd57..789c3a90 100644 --- a/src/app/auth/onboarding/hashtag.tsx +++ b/src/app/auth/onboarding/hashtag.tsx @@ -2,16 +2,12 @@ import { message } from '@tauri-apps/plugin-dialog'; import { useState } from 'react'; import { useNavigate } from 'react-router-dom'; -import { useStorage } from '@libs/storage/provider'; - import { ArrowLeftIcon, CheckCircleIcon, LoaderIcon } from '@shared/icons'; -import { HASHTAGS, WIDGET_KIND } from '@stores/constants'; +import { HASHTAGS } from '@stores/constants'; import { useOnboarding } from '@stores/onboarding'; export function OnboardHashtagScreen() { - const { db } = useStorage(); - const [loading, setLoading] = useState(false); const [tags, setTags] = useState(new Set()); @@ -34,10 +30,6 @@ export function OnboardHashtagScreen() { try { setLoading(true); - for (const tag of tags) { - await db.createWidget(WIDGET_KIND.global.hashtag, tag, tag.replace('#', '')); - } - setHashtag(); navigate(-1); } catch (e) { diff --git a/src/app/space/index.tsx b/src/app/home/index.tsx similarity index 56% rename from src/app/space/index.tsx rename to src/app/home/index.tsx index 8535cfdf..ce43131c 100644 --- a/src/app/space/index.tsx +++ b/src/app/home/index.tsx @@ -2,34 +2,21 @@ import { useQuery } from '@tanstack/react-query'; import { useCallback, useRef, useState } from 'react'; import { VList, VListHandle } from 'virtua'; -import { ToggleWidgetList } from '@app/space/components/toggle'; -import { WidgetList } from '@app/space/components/widgetList'; - import { useStorage } from '@libs/storage/provider'; import { LoaderIcon } from '@shared/icons'; import { - GlobalArticlesWidget, - GlobalFilesWidget, - GlobalHashtagWidget, - LocalArticlesWidget, - LocalFeedsWidget, - LocalFilesWidget, - LocalThreadWidget, - LocalUserWidget, NewsfeedWidget, NotificationWidget, - TrendingAccountsWidget, - TrendingNotesWidget, - XfeedsWidget, - XhashtagWidget, + ToggleWidgetList, + WidgetList, } from '@shared/widgets'; import { WIDGET_KIND } from '@stores/constants'; import { Widget } from '@utils/types'; -export function SpaceScreen() { +export function HomeScreen() { const ref = useRef(null); const [selectedIndex, setSelectedIndex] = useState(-1); @@ -40,16 +27,16 @@ export function SpaceScreen() { const dbWidgets = await db.getWidgets(); const defaultWidgets = [ { - id: '9998', - title: 'Notification', - content: '', - kind: WIDGET_KIND.local.notification, - }, - { - id: '9999', + id: '99999', title: 'Newsfeed', content: '', - kind: WIDGET_KIND.local.network, + kind: WIDGET_KIND.newsfeed, + }, + { + id: '99998', + title: 'Notification', + content: '', + kind: WIDGET_KIND.notification, }, ]; @@ -63,36 +50,12 @@ export function SpaceScreen() { const renderItem = useCallback((widget: Widget) => { switch (widget.kind) { - case WIDGET_KIND.local.feeds: - return ; - case WIDGET_KIND.local.files: - return ; - case WIDGET_KIND.local.articles: - return ; - case WIDGET_KIND.local.user: - return ; - case WIDGET_KIND.local.thread: - return ; - case WIDGET_KIND.global.hashtag: - return ; - case WIDGET_KIND.global.articles: - return ; - case WIDGET_KIND.global.files: - return ; - case WIDGET_KIND.nostrBand.trendingAccounts: - return ; - case WIDGET_KIND.nostrBand.trendingNotes: - return ; - case WIDGET_KIND.tmp.xfeed: - return ; - case WIDGET_KIND.tmp.xhashtag: - return ; - case WIDGET_KIND.tmp.list: - return ; - case WIDGET_KIND.local.notification: + case WIDGET_KIND.notification: return ; - case WIDGET_KIND.local.network: + case WIDGET_KIND.newsfeed: return ; + case WIDGET_KIND.list: + return ; default: return null; } @@ -108,14 +71,15 @@ export function SpaceScreen() { return ( { if (!ref.current) return; switch (e.code) { + case 'ArrowUp': case 'ArrowLeft': { e.preventDefault(); const prevIndex = Math.max(selectedIndex - 1, 0); @@ -126,6 +90,7 @@ export function SpaceScreen() { }); break; } + case 'ArrowDown': case 'ArrowRight': { e.preventDefault(); const nextIndex = Math.min(selectedIndex + 1, data.length - 1); @@ -136,6 +101,8 @@ export function SpaceScreen() { }); break; } + default: + break; } }} > diff --git a/src/app/space/components/widgetList.tsx b/src/app/space/components/widgetList.tsx deleted file mode 100644 index 31733d7e..00000000 --- a/src/app/space/components/widgetList.tsx +++ /dev/null @@ -1,128 +0,0 @@ -import { useCallback } from 'react'; - -import { - ArticleIcon, - BellIcon, - FileIcon, - FollowsIcon, - GroupFeedsIcon, - HashtagIcon, - ThreadsIcon, - TrendingIcon, -} from '@shared/icons'; -import { TitleBar } from '@shared/titleBar'; -import { WidgetWrapper } from '@shared/widgets'; - -import { DEFAULT_WIDGETS, WIDGET_KIND } from '@stores/constants'; - -import { useWidget } from '@utils/hooks/useWidget'; -import { Widget, WidgetGroup, WidgetGroupItem } from '@utils/types'; - -export function WidgetList({ params }: { params: Widget }) { - const { addWidget, removeWidget } = useWidget(); - - const open = (item: WidgetGroupItem) => { - addWidget.mutate({ - kind: item.kind, - title: item.title, - content: JSON.stringify(item.content), - }); - removeWidget.mutate(params.id); - }; - - const renderIcon = useCallback((kind: number) => { - switch (kind) { - case WIDGET_KIND.tmp.xfeed: - return ( - - ); - case WIDGET_KIND.local.follows: - return ; - case WIDGET_KIND.local.files: - case WIDGET_KIND.global.files: - return ; - case WIDGET_KIND.local.articles: - case WIDGET_KIND.global.articles: - return ; - case WIDGET_KIND.tmp.xhashtag: - return ; - case WIDGET_KIND.nostrBand.trendingAccounts: - case WIDGET_KIND.nostrBand.trendingNotes: - return ( - - ); - case WIDGET_KIND.local.notification: - return ; - case WIDGET_KIND.other.learnNostr: - return ; - default: - return null; - } - }, []); - - const renderItem = useCallback((row: WidgetGroup, index: number) => { - return ( -
-

{row.title}

-
- {row.data.map((item, index) => ( - - ))} -
-
- ); - }, []); - - return ( - - -
-
- {DEFAULT_WIDGETS.map((row: WidgetGroup, index: number) => - renderItem(row, index) - )} -
- -
-
-
-
- ); -} diff --git a/src/shared/notes/actions.tsx b/src/shared/notes/actions.tsx index a08f3980..b2455841 100644 --- a/src/shared/notes/actions.tsx +++ b/src/shared/notes/actions.tsx @@ -34,7 +34,7 @@ export function NoteActions({ type="button" onClick={() => addWidget.mutate({ - kind: WIDGET_KIND.local.thread, + kind: WIDGET_KIND.thread, title: 'Thread', content: id, }) diff --git a/src/shared/notes/mentions/hashtag.tsx b/src/shared/notes/mentions/hashtag.tsx index 27af94f3..801ceed2 100644 --- a/src/shared/notes/mentions/hashtag.tsx +++ b/src/shared/notes/mentions/hashtag.tsx @@ -10,7 +10,7 @@ export function Hashtag({ tag }: { tag: string }) { type="button" onClick={() => addWidget.mutate({ - kind: WIDGET_KIND.global.hashtag, + kind: WIDGET_KIND.hashtag, title: tag, content: tag.replace('#', ''), }) diff --git a/src/shared/notes/mentions/note.tsx b/src/shared/notes/mentions/note.tsx index 78be3202..8b624685 100644 --- a/src/shared/notes/mentions/note.tsx +++ b/src/shared/notes/mentions/note.tsx @@ -50,7 +50,7 @@ export const MentionNote = memo(function MentionNote({ id }: { id: string }) { type="button" onClick={() => addWidget.mutate({ - kind: WIDGET_KIND.local.thread, + kind: WIDGET_KIND.thread, title: 'Thread', content: data.id, }) diff --git a/src/shared/notes/mentions/user.tsx b/src/shared/notes/mentions/user.tsx index ec3cf392..1108560d 100644 --- a/src/shared/notes/mentions/user.tsx +++ b/src/shared/notes/mentions/user.tsx @@ -14,14 +14,7 @@ export const MentionUser = memo(function MentionUser({ pubkey }: { pubkey: strin type="button" onClick={() => addWidget.mutate({ - kind: WIDGET_KIND.local.user, - title: user?.name || user?.display_name || user?.displayName, - content: pubkey, - }) - } - onKeyDown={() => - addWidget.mutate({ - kind: WIDGET_KIND.local.user, + kind: WIDGET_KIND.user, title: user?.name || user?.display_name || user?.displayName, content: pubkey, }) diff --git a/src/shared/notes/notify.tsx b/src/shared/notes/notify.tsx index 57cae43d..2f7480f8 100644 --- a/src/shared/notes/notify.tsx +++ b/src/shared/notes/notify.tsx @@ -91,7 +91,7 @@ export function NotifyNote({ event }: { event: NDKEvent }) { type="button" onClick={() => addWidget.mutate({ - kind: WIDGET_KIND.local.thread, + kind: WIDGET_KIND.thread, title: 'Thread', content: data.id, }) diff --git a/src/shared/notes/text.tsx b/src/shared/notes/text.tsx index 6e820822..2540773c 100644 --- a/src/shared/notes/text.tsx +++ b/src/shared/notes/text.tsx @@ -30,7 +30,7 @@ export function TextNote({ event }: { event: NDKEvent }) { type="button" onClick={() => addWidget.mutate({ - kind: WIDGET_KIND.local.thread, + kind: WIDGET_KIND.thread, title: 'Thread', content: thread.rootEventId, }) diff --git a/src/shared/widgets/local/articles.tsx b/src/shared/widgets/article.tsx similarity index 84% rename from src/shared/widgets/local/articles.tsx rename to src/shared/widgets/article.tsx index 1a9a9470..2432e275 100644 --- a/src/shared/widgets/local/articles.tsx +++ b/src/shared/widgets/article.tsx @@ -1,5 +1,6 @@ import { NDKEvent, NDKKind } from '@nostr-dev-kit/ndk'; import { useInfiniteQuery } from '@tanstack/react-query'; +import { FetchFilter } from 'nostr-fetch'; import { useMemo } from 'react'; import { VList } from 'virtua'; @@ -15,12 +16,12 @@ import { FETCH_LIMIT } from '@stores/constants'; import { Widget } from '@utils/types'; -export function LocalArticlesWidget({ params }: { params: Widget }) { +export function ArticleWidget({ widget }: { widget: Widget }) { const { db } = useStorage(); const { ndk, relayUrls, fetcher } = useNDK(); const { status, data, hasNextPage, isFetchingNextPage, fetchNextPage } = useInfiniteQuery({ - queryKey: ['local-articles'], + queryKey: ['widget-' + widget.id], initialPageParam: 0, queryFn: async ({ signal, @@ -29,15 +30,24 @@ export function LocalArticlesWidget({ params }: { params: Widget }) { signal: AbortSignal; pageParam: number; }) => { - const events = await fetcher.fetchLatestEvents( - relayUrls, - { + let filter: FetchFilter; + const content = JSON.parse(widget.content); + + if (content.global) { + filter = { + kinds: [NDKKind.Article], + }; + } else { + filter = { kinds: [NDKKind.Article], authors: db.account.circles, - }, - FETCH_LIMIT, - { asOf: pageParam === 0 ? undefined : pageParam, abortSignal: signal } - ); + }; + } + + const events = await fetcher.fetchLatestEvents(relayUrls, filter, FETCH_LIMIT, { + asOf: pageParam === 0 ? undefined : pageParam, + abortSignal: signal, + }); const ndkEvents = events.map((event) => { return new NDKEvent(ndk, event); @@ -61,7 +71,7 @@ export function LocalArticlesWidget({ params }: { params: Widget }) { return ( - + {status === 'pending' ? (
diff --git a/src/shared/widgets/local/files.tsx b/src/shared/widgets/file.tsx similarity index 84% rename from src/shared/widgets/local/files.tsx rename to src/shared/widgets/file.tsx index c9d4a7d0..ce65f5ab 100644 --- a/src/shared/widgets/local/files.tsx +++ b/src/shared/widgets/file.tsx @@ -1,5 +1,6 @@ import { NDKEvent } from '@nostr-dev-kit/ndk'; import { useInfiniteQuery } from '@tanstack/react-query'; +import { FetchFilter } from 'nostr-fetch'; import { useMemo } from 'react'; import { VList } from 'virtua'; @@ -15,12 +16,12 @@ import { FETCH_LIMIT } from '@stores/constants'; import { Widget } from '@utils/types'; -export function LocalFilesWidget({ params }: { params: Widget }) { +export function FileWidget({ widget }: { widget: Widget }) { const { db } = useStorage(); const { ndk, relayUrls, fetcher } = useNDK(); const { status, data, hasNextPage, isFetchingNextPage, fetchNextPage } = useInfiniteQuery({ - queryKey: ['local-files'], + queryKey: ['widget-' + widget.id], initialPageParam: 0, queryFn: async ({ signal, @@ -29,15 +30,24 @@ export function LocalFilesWidget({ params }: { params: Widget }) { signal: AbortSignal; pageParam: number; }) => { - const events = await fetcher.fetchLatestEvents( - relayUrls, - { + let filter: FetchFilter; + const content = JSON.parse(widget.content); + + if (content.global) { + filter = { + kinds: [1063], + }; + } else { + filter = { kinds: [1063], authors: db.account.circles, - }, - FETCH_LIMIT, - { asOf: pageParam === 0 ? undefined : pageParam, abortSignal: signal } - ); + }; + } + + const events = await fetcher.fetchLatestEvents(relayUrls, filter, FETCH_LIMIT, { + asOf: pageParam === 0 ? undefined : pageParam, + abortSignal: signal, + }); const ndkEvents = events.map((event) => { return new NDKEvent(ndk, event); @@ -61,7 +71,7 @@ export function LocalFilesWidget({ params }: { params: Widget }) { return ( - + {status === 'pending' ? (
diff --git a/src/shared/widgets/global/articles.tsx b/src/shared/widgets/global/articles.tsx deleted file mode 100644 index 6250d822..00000000 --- a/src/shared/widgets/global/articles.tsx +++ /dev/null @@ -1,106 +0,0 @@ -import { NDKEvent, NDKKind } from '@nostr-dev-kit/ndk'; -import { useInfiniteQuery } from '@tanstack/react-query'; -import { useMemo } from 'react'; -import { VList } from 'virtua'; - -import { useNDK } from '@libs/ndk/provider'; - -import { ArrowRightCircleIcon, LoaderIcon } from '@shared/icons'; -import { MemoizedArticleNote } from '@shared/notes'; -import { TitleBar } from '@shared/titleBar'; -import { WidgetWrapper } from '@shared/widgets'; - -import { FETCH_LIMIT } from '@stores/constants'; - -import { Widget } from '@utils/types'; - -export function GlobalArticlesWidget({ params }: { params: Widget }) { - const { ndk, relayUrls, fetcher } = useNDK(); - const { status, data, hasNextPage, isFetchingNextPage, fetchNextPage } = - useInfiniteQuery({ - queryKey: ['global-articles'], - initialPageParam: 0, - queryFn: async ({ - signal, - pageParam, - }: { - signal: AbortSignal; - pageParam: number; - }) => { - const events = await fetcher.fetchLatestEvents( - relayUrls, - { - kinds: [NDKKind.Article], - }, - FETCH_LIMIT, - { asOf: pageParam === 0 ? undefined : pageParam, abortSignal: signal } - ); - - const ndkEvents = events.map((event) => { - return new NDKEvent(ndk, event); - }); - - return ndkEvents.sort((a, b) => b.created_at - a.created_at); - }, - getNextPageParam: (lastPage) => { - const lastEvent = lastPage.at(-1); - if (!lastEvent) return; - return lastEvent.created_at - 1; - }, - refetchOnWindowFocus: false, - refetchOnReconnect: false, - }); - - const allEvents = useMemo( - () => (data ? data.pages.flatMap((page) => page) : []), - [data] - ); - - return ( - - - - {status === 'pending' ? ( -
- -
- ) : allEvents.length === 0 ? ( -
-
- empty feeds -
-

- Oops, it looks like there are no articles. -

-

- You can close this widget -

-
-
-
- ) : ( - allEvents.map((item) => ) - )} -
- {hasNextPage ? ( - - ) : null} -
-
-
- ); -} diff --git a/src/shared/widgets/global/files.tsx b/src/shared/widgets/global/files.tsx deleted file mode 100644 index 2a2b5342..00000000 --- a/src/shared/widgets/global/files.tsx +++ /dev/null @@ -1,106 +0,0 @@ -import { NDKEvent } from '@nostr-dev-kit/ndk'; -import { useInfiniteQuery } from '@tanstack/react-query'; -import { useMemo } from 'react'; -import { VList } from 'virtua'; - -import { useNDK } from '@libs/ndk/provider'; - -import { ArrowRightCircleIcon, LoaderIcon } from '@shared/icons'; -import { MemoizedFileNote } from '@shared/notes'; -import { TitleBar } from '@shared/titleBar'; -import { WidgetWrapper } from '@shared/widgets'; - -import { FETCH_LIMIT } from '@stores/constants'; - -import { Widget } from '@utils/types'; - -export function GlobalFilesWidget({ params }: { params: Widget }) { - const { ndk, relayUrls, fetcher } = useNDK(); - const { status, data, hasNextPage, isFetchingNextPage, fetchNextPage } = - useInfiniteQuery({ - queryKey: ['global-files'], - initialPageParam: 0, - queryFn: async ({ - signal, - pageParam, - }: { - signal: AbortSignal; - pageParam: number; - }) => { - const events = await fetcher.fetchLatestEvents( - relayUrls, - { - kinds: [1063], - }, - FETCH_LIMIT, - { asOf: pageParam === 0 ? undefined : pageParam, abortSignal: signal } - ); - - const ndkEvents = events.map((event) => { - return new NDKEvent(ndk, event); - }); - - return ndkEvents.sort((a, b) => b.created_at - a.created_at); - }, - getNextPageParam: (lastPage) => { - const lastEvent = lastPage.at(-1); - if (!lastEvent) return; - return lastEvent.created_at - 1; - }, - refetchOnWindowFocus: false, - refetchOnReconnect: false, - }); - - const allEvents = useMemo( - () => (data ? data.pages.flatMap((page) => page) : []), - [data] - ); - - return ( - - - - {status === 'pending' ? ( -
- -
- ) : allEvents.length === 0 ? ( -
-
- empty feeds -
-

- Oops, it looks like there are no files. -

-

- You can close this widget -

-
-
-
- ) : ( - allEvents.map((item) => ) - )} -
- {hasNextPage ? ( - - ) : null} -
-
-
- ); -} diff --git a/src/shared/widgets/local/feeds.tsx b/src/shared/widgets/group.tsx similarity index 93% rename from src/shared/widgets/local/feeds.tsx rename to src/shared/widgets/group.tsx index d129d99c..704a9afd 100644 --- a/src/shared/widgets/local/feeds.tsx +++ b/src/shared/widgets/group.tsx @@ -19,11 +19,11 @@ import { FETCH_LIMIT } from '@stores/constants'; import { Widget } from '@utils/types'; -export function LocalFeedsWidget({ params }: { params: Widget }) { +export function GroupWidget({ widget }: { widget: Widget }) { const { relayUrls, ndk, fetcher } = useNDK(); const { status, data, hasNextPage, isFetchingNextPage, fetchNextPage } = useInfiniteQuery({ - queryKey: ['group-feeds-' + params.id], + queryKey: ['widget-' + widget.id], initialPageParam: 0, queryFn: async ({ signal, @@ -32,11 +32,12 @@ export function LocalFeedsWidget({ params }: { params: Widget }) { signal: AbortSignal; pageParam: number; }) => { + const authors = JSON.parse(widget.content); const events = await fetcher.fetchLatestEvents( relayUrls, { kinds: [NDKKind.Text, NDKKind.Repost], - authors: JSON.parse(params.content), + authors: authors, }, FETCH_LIMIT, { asOf: pageParam === 0 ? undefined : pageParam, abortSignal: signal } @@ -78,7 +79,7 @@ export function LocalFeedsWidget({ params }: { params: Widget }) { return ( - + {status === 'pending' ? (
diff --git a/src/shared/widgets/global/hashtag.tsx b/src/shared/widgets/hashtag.tsx similarity index 94% rename from src/shared/widgets/global/hashtag.tsx rename to src/shared/widgets/hashtag.tsx index 3dcae2d0..1255ab8f 100644 --- a/src/shared/widgets/global/hashtag.tsx +++ b/src/shared/widgets/hashtag.tsx @@ -14,11 +14,11 @@ import { FETCH_LIMIT } from '@stores/constants'; import { Widget } from '@utils/types'; -export function GlobalHashtagWidget({ params }: { params: Widget }) { +export function HashtagWidget({ widget }: { widget: Widget }) { const { ndk, relayUrls, fetcher } = useNDK(); const { status, data, hasNextPage, isFetchingNextPage, fetchNextPage } = useInfiniteQuery({ - queryKey: ['hashtag-' + params.title], + queryKey: ['widget-' + widget.id], initialPageParam: 0, queryFn: async ({ signal, @@ -31,7 +31,7 @@ export function GlobalHashtagWidget({ params }: { params: Widget }) { relayUrls, { kinds: [NDKKind.Text, NDKKind.Repost], - '#t': [params.content], + '#t': [widget.content], }, FETCH_LIMIT, { asOf: pageParam === 0 ? undefined : pageParam, abortSignal: signal } @@ -74,7 +74,7 @@ export function GlobalHashtagWidget({ params }: { params: Widget }) { return ( - + {status === 'pending' ? (
@@ -86,7 +86,7 @@ export function GlobalHashtagWidget({ params }: { params: Widget }) { empty feeds

- Oops, it looks like there are no events related to {params.title}. + Oops, it looks like there are no events related to {widget.content}.

You can close this widget diff --git a/src/shared/widgets/index.ts b/src/shared/widgets/index.ts index 2f2a82f0..e6499a24 100644 --- a/src/shared/widgets/index.ts +++ b/src/shared/widgets/index.ts @@ -1,17 +1,15 @@ -export * from './wrapper'; -export * from './local/feeds'; -export * from './local/user'; -export * from './local/thread'; -export * from './local/files'; -export * from './local/articles'; -export * from './global/articles'; -export * from './global/files'; -export * from './global/hashtag'; -export * from './nostrBand/trendingNotes'; -export * from './nostrBand/trendingAccounts'; -export * from './tmp/feeds'; -export * from './tmp/hashtag'; export * from './newsfeed'; export * from './notification'; -export * from './liveUpdater'; export * from './topic'; +export * from './user'; +export * from './article'; +export * from './file'; +export * from './hashtag'; +export * from './thread'; +export * from './group'; +export * from './nostrBand/trendingAccounts'; +export * from './nostrBand/trendingNotes'; +export * from './other/wrapper'; +export * from './other/liveUpdater'; +export * from './other/toggleWidgetList'; +export * from './other/widgetList'; diff --git a/src/shared/widgets/newsfeed.tsx b/src/shared/widgets/newsfeed.tsx index 1ae017a3..d3d5704f 100644 --- a/src/shared/widgets/newsfeed.tsx +++ b/src/shared/widgets/newsfeed.tsx @@ -14,8 +14,7 @@ import { UnknownNote, } from '@shared/notes'; import { TitleBar } from '@shared/titleBar'; -import { WidgetWrapper } from '@shared/widgets'; -import { LiveUpdater } from '@shared/widgets'; +import { LiveUpdater, WidgetWrapper } from '@shared/widgets'; import { FETCH_LIMIT } from '@stores/constants'; diff --git a/src/shared/widgets/nostrBand/trendingAccounts.tsx b/src/shared/widgets/nostrBand/trendingAccounts.tsx index 4a0e9d13..ecb214af 100644 --- a/src/shared/widgets/nostrBand/trendingAccounts.tsx +++ b/src/shared/widgets/nostrBand/trendingAccounts.tsx @@ -4,7 +4,10 @@ import { VList } from 'virtua'; import { LoaderIcon } from '@shared/icons'; import { TitleBar } from '@shared/titleBar'; import { WidgetWrapper } from '@shared/widgets'; -import { NostrBandUserProfile, type Profile } from '@shared/widgets/nostrBandUserProfile'; +import { + NostrBandUserProfile, + type Profile, +} from '@shared/widgets/other/nostrBandUserProfile'; import { Widget } from '@utils/types'; diff --git a/src/shared/widgets/nostrBand/trendingNotes.tsx b/src/shared/widgets/nostrBand/trendingNotes.tsx index 13bbc2c0..cee7970f 100644 --- a/src/shared/widgets/nostrBand/trendingNotes.tsx +++ b/src/shared/widgets/nostrBand/trendingNotes.tsx @@ -13,9 +13,9 @@ interface Response { notes: Array<{ event: NDKEvent }>; } -export function TrendingNotesWidget({ params }: { params: Widget }) { +export function TrendingNotesWidget({ widget }: { widget: Widget }) { const { status, data } = useQuery({ - queryKey: ['trending-notes-widget'], + queryKey: ['widget-' + widget.id], queryFn: async () => { const res = await fetch('https://api.nostr.band/v0/trending/notes'); if (!res.ok) { @@ -33,7 +33,7 @@ export function TrendingNotesWidget({ params }: { params: Widget }) { return ( - + {status === 'pending' ? (

diff --git a/src/shared/widgets/liveUpdater.tsx b/src/shared/widgets/other/liveUpdater.tsx similarity index 100% rename from src/shared/widgets/liveUpdater.tsx rename to src/shared/widgets/other/liveUpdater.tsx diff --git a/src/shared/widgets/nostrBandUserProfile.tsx b/src/shared/widgets/other/nostrBandUserProfile.tsx similarity index 100% rename from src/shared/widgets/nostrBandUserProfile.tsx rename to src/shared/widgets/other/nostrBandUserProfile.tsx diff --git a/src/app/space/components/toggle.tsx b/src/shared/widgets/other/toggleWidgetList.tsx similarity index 91% rename from src/app/space/components/toggle.tsx rename to src/shared/widgets/other/toggleWidgetList.tsx index b7657939..c657b067 100644 --- a/src/app/space/components/toggle.tsx +++ b/src/shared/widgets/other/toggleWidgetList.tsx @@ -14,7 +14,7 @@ export function ToggleWidgetList() { +
+
+
+
+ ); +} diff --git a/src/shared/widgets/wrapper.tsx b/src/shared/widgets/other/wrapper.tsx similarity index 100% rename from src/shared/widgets/wrapper.tsx rename to src/shared/widgets/other/wrapper.tsx diff --git a/src/shared/widgets/local/thread.tsx b/src/shared/widgets/thread.tsx similarity index 88% rename from src/shared/widgets/local/thread.tsx rename to src/shared/widgets/thread.tsx index 2a44f2f1..b3236135 100644 --- a/src/shared/widgets/local/thread.tsx +++ b/src/shared/widgets/thread.tsx @@ -18,8 +18,8 @@ import { WidgetWrapper } from '@shared/widgets'; import { useEvent } from '@utils/hooks/useEvent'; import { Widget } from '@utils/types'; -export function LocalThreadWidget({ params }: { params: Widget }) { - const { status, data } = useEvent(params.content); +export function ThreadWidget({ widget }: { widget: Widget }) { + const { status, data } = useEvent(widget.content); const renderKind = useCallback( (event: NDKEvent) => { @@ -39,7 +39,7 @@ export function LocalThreadWidget({ params }: { params: Widget }) { return ( - + {status === 'pending' ? (
@@ -52,7 +52,7 @@ export function LocalThreadWidget({ params }: { params: Widget }) { {renderKind(data)}
- + )} diff --git a/src/shared/widgets/tmp/feeds.tsx b/src/shared/widgets/tmp/feeds.tsx index dea8665a..9b5f0986 100644 --- a/src/shared/widgets/tmp/feeds.tsx +++ b/src/shared/widgets/tmp/feeds.tsx @@ -28,7 +28,7 @@ export function XfeedsWidget({ params }: { params: Widget }) { const submit = async () => { addWidget.mutate({ - kind: WIDGET_KIND.local.feeds, + kind: WIDGET_KIND.group, title: title || 'Group', content: JSON.stringify(groups), }); diff --git a/src/shared/widgets/tmp/hashtag.tsx b/src/shared/widgets/tmp/hashtag.tsx index 688b2029..467b3dc9 100644 --- a/src/shared/widgets/tmp/hashtag.tsx +++ b/src/shared/widgets/tmp/hashtag.tsx @@ -39,7 +39,7 @@ export function XhashtagWidget({ params }: { params: Widget }) { const onSubmit = async (data: FormValues) => { try { addWidget.mutate({ - kind: WIDGET_KIND.global.hashtag, + kind: WIDGET_KIND.hashtag, title: data.hashtag, content: data.hashtag.replace('#', ''), }); diff --git a/src/shared/widgets/topic.tsx b/src/shared/widgets/topic.tsx index fe3c1d18..80fe3e92 100644 --- a/src/shared/widgets/topic.tsx +++ b/src/shared/widgets/topic.tsx @@ -23,7 +23,7 @@ export function TopicWidget({ widget }: { widget: Widget }) { const { relayUrls, ndk, fetcher } = useNDK(); const { status, data, hasNextPage, isFetchingNextPage, fetchNextPage } = useInfiniteQuery({ - queryKey: [widget.title], + queryKey: ['widget-' + widget.id], initialPageParam: 0, queryFn: async ({ signal, diff --git a/src/shared/widgets/local/user.tsx b/src/shared/widgets/user.tsx similarity index 92% rename from src/shared/widgets/local/user.tsx rename to src/shared/widgets/user.tsx index c65beaeb..6fdebc52 100644 --- a/src/shared/widgets/local/user.tsx +++ b/src/shared/widgets/user.tsx @@ -18,17 +18,17 @@ import { WidgetWrapper } from '@shared/widgets'; import { nHoursAgo } from '@utils/date'; import { Widget } from '@utils/types'; -export function LocalUserWidget({ params }: { params: Widget }) { +export function UserWidget({ widget }: { widget: Widget }) { const { ndk } = useNDK(); const { status, data } = useQuery({ - queryKey: ['user-posts', params.content], + queryKey: ['widget-' + widget.id], queryFn: async () => { const rootIds = new Set(); const dedupQueue = new Set(); const events = await ndk.fetchEvents({ kinds: [NDKKind.Text, NDKKind.Repost], - authors: [params.content], + authors: [widget.content], since: nHoursAgo(24), }); @@ -70,10 +70,10 @@ export function LocalUserWidget({ params }: { params: Widget }) { return ( - +
- +

diff --git a/src/stores/constants.ts b/src/stores/constants.ts index d574a7fa..f3d48a2e 100644 --- a/src/stores/constants.ts +++ b/src/stores/constants.ts @@ -42,35 +42,18 @@ export const HASHTAGS = [ ]; export const WIDGET_KIND = { - local: { - network: 100, - feeds: 101, - files: 102, - articles: 103, - user: 104, - thread: 105, - follows: 106, - notification: 107, - }, - global: { - feeds: 1000, - files: 1001, - articles: 1002, - hashtag: 1003, - topic: 108, - }, - nostrBand: { - trendingAccounts: 1, - trendingNotes: 2, - }, - other: { - learnNostr: 90000, - }, - tmp: { - list: 10000, - xfeed: 10001, - xhashtag: 10002, - }, + user: 1, + thread: 2, + group: 3, + article: 4, + file: 5, + trendingNotes: 6, + trendingAccounts: 7, + topic: 8, + hashtag: 9, + notification: 9998, + newsfeed: 9999, + list: 10000, }; export const TOPICS = [