From 1206486016ba598cf5bf1248ff28da1ddc42d96d Mon Sep 17 00:00:00 2001 From: Ren Amamiya <123083837+reyamir@users.noreply.github.com> Date: Sat, 16 Sep 2023 16:06:01 +0700 Subject: [PATCH] partial support replaceable event --- src/app/notes/article.tsx | 14 +-- src/shared/widgets/other/learnNostr.tsx | 138 ++++++++---------------- src/stores/resources.ts | 68 ++++++++++++ src/utils/hooks/useEvent.ts | 15 ++- src/utils/types.d.ts | 11 ++ 5 files changed, 144 insertions(+), 102 deletions(-) create mode 100644 src/stores/resources.ts diff --git a/src/app/notes/article.tsx b/src/app/notes/article.tsx index 0ae99d31..5ca80f82 100644 --- a/src/app/notes/article.tsx +++ b/src/app/notes/article.tsx @@ -1,7 +1,7 @@ import { NDKEvent, NDKKind } from '@nostr-dev-kit/ndk'; import { writeText } from '@tauri-apps/api/clipboard'; import { nip19 } from 'nostr-tools'; -import { EventPointer } from 'nostr-tools/lib/nip19'; +import { AddressPointer, EventPointer } from 'nostr-tools/lib/nip19'; import { useRef, useState } from 'react'; import { useNavigate, useParams } from 'react-router-dom'; @@ -27,7 +27,9 @@ export function ArticleNoteScreen() { const { id } = useParams(); const { db } = useStorage(); - const { status, data } = useEvent(id); + + const naddr = id.startsWith('naddr') ? (nip19.decode(id).data as AddressPointer) : null; + const { status, data } = useEvent(id, naddr); const [isCopy, setIsCopy] = useState(false); @@ -103,15 +105,15 @@ export function ArticleNoteScreen() {
{renderKind(data)}
- - + +
)}
- - + +
diff --git a/src/shared/widgets/other/learnNostr.tsx b/src/shared/widgets/other/learnNostr.tsx index d6dbe64c..73a34e8e 100644 --- a/src/shared/widgets/other/learnNostr.tsx +++ b/src/shared/widgets/other/learnNostr.tsx @@ -1,89 +1,24 @@ -import { Link } from 'react-router-dom'; +import { useNavigate } from 'react-router-dom'; import { ArrowRightIcon } from '@shared/icons'; import { TitleBar } from '@shared/titleBar'; +import { useResources } from '@stores/resources'; + import { Widget } from '@utils/types'; -const resources = [ - { - title: 'The Basics (provide by nostr.com)', - data: [ - { - title: 'What is Nostr?', - image: '', - url: '', - }, - { - title: 'Understanding keys', - image: '', - url: '', - }, - { - title: "What's a client?", - image: '', - url: '', - }, - { - title: 'What are relays?', - image: '', - url: '', - }, - { - title: 'What is an event?', - image: '', - url: '', - }, - { - title: 'How to help Nostr?', - image: '', - url: '', - }, - ], - }, - { - title: 'Lume Tutorials', - data: [ - { - title: 'How to use widget?', - image: '', - url: '', - }, - { - title: 'How to send a post?', - image: '', - url: '', - }, - { - title: 'How to find more people?', - image: '', - url: '', - }, - { - title: 'How to edit your profile?', - image: '', - url: '', - }, - { - title: 'How to use focus mode?', - image: '', - url: '', - }, - { - title: 'Report an issue', - image: '', - url: '', - }, - { - title: 'How to support Lume', - image: '', - url: '', - }, - ], - }, -]; - export function LearnNostrWidget({ params }: { params: Widget }) { + const navigate = useNavigate(); + const readResource = useResources((state) => state.readResource); + + const resources = useResources((state) => state.resources); + const readed = useResources((state) => state.readed); + + const open = (naddr: string) => { + readResource(naddr); + navigate(`/notes/article/${naddr}`); + }; + return (
@@ -92,24 +27,37 @@ export function LearnNostrWidget({ params }: { params: Widget }) {

{resource.title}

- {resource.data.map((item, index) => ( - -
-
-
-
{item.title}
-

Unread

+ {resource.data.length ? ( + resource.data.map((item, index) => ( + - - ))} + )) + ) : ( +
+

+ More resources are coming, stay tuned. +

+
+ )}
))} diff --git a/src/stores/resources.ts b/src/stores/resources.ts new file mode 100644 index 00000000..30d69aa9 --- /dev/null +++ b/src/stores/resources.ts @@ -0,0 +1,68 @@ +import { create } from 'zustand'; +import { createJSONStorage, persist } from 'zustand/middleware'; + +import { Resources } from '@utils/types'; + +const DEFAULT_RESOURCES: Array = [ + { + title: 'The Basics (provide by nostr.com)', + data: [ + { + id: 'naddr1qqxnzd3exsurgwfnxgcnjve5qgsym7p8qvs805ny3z3vausedzzwnwk27cfe67r69nrxpqe8w0urmegrqsqqqa283wgxe0', + title: 'What is Nostr?', + image: '', + }, + { + id: 'naddr1qqxnzd3exsurgwf48qcnvdfcqgsym7p8qvs805ny3z3vausedzzwnwk27cfe67r69nrxpqe8w0urmegrqsqqqa28cnv0yt', + title: 'Understanding keys', + image: '', + }, + { + id: 'naddr1qqxnzd3exsurgwfcxgcrzwfjqgsym7p8qvs805ny3z3vausedzzwnwk27cfe67r69nrxpqe8w0urmegrqsqqqa28uccw5e', + title: "What's a client?", + image: '', + }, + { + id: 'naddr1qqxnzd3exsurgwfexqersdp5qgsym7p8qvs805ny3z3vausedzzwnwk27cfe67r69nrxpqe8w0urmegrqsqqqa28jvlesq', + title: 'What are relays?', + image: '', + }, + { + id: 'naddr1qqxnzd3exsur2vpjxserjveeqgsym7p8qvs805ny3z3vausedzzwnwk27cfe67r69nrxpqe8w0urmegrqsqqqa28rqy7mx', + title: 'What is an event?', + image: '', + }, + { + id: 'naddr1qqxnzd3exsur2vp5xsmnywpnqgsym7p8qvs805ny3z3vausedzzwnwk27cfe67r69nrxpqe8w0urmegrqsqqqa28hxwx4e', + title: 'How to help Nostr?', + image: '', + }, + ], + }, + { + title: 'Lume Tutorials', + data: [], + }, +]; + +interface ResourceState { + resources: Array; + readed: Set; + readResource: (id: string) => void; +} + +export const useResources = create()( + persist( + (set) => ({ + resources: DEFAULT_RESOURCES, + readed: new Set(), + readResource: (id: string) => { + set((state) => ({ readed: new Set(state.readed).add(id) })); + }, + }), + { + name: 'resources', + storage: createJSONStorage(() => localStorage), + } + ) +); diff --git a/src/utils/hooks/useEvent.ts b/src/utils/hooks/useEvent.ts index 1e952b19..f6dde150 100644 --- a/src/utils/hooks/useEvent.ts +++ b/src/utils/hooks/useEvent.ts @@ -1,22 +1,35 @@ import { NDKEvent } from '@nostr-dev-kit/ndk'; import { useQuery } from '@tanstack/react-query'; +import { AddressPointer } from 'nostr-tools/lib/nip19'; import { useNDK } from '@libs/ndk/provider'; import { useStorage } from '@libs/storage/provider'; import { toRawEvent } from '@utils/rawEvent'; -export function useEvent(id: string, embed?: string) { +export function useEvent(id: string, naddr?: AddressPointer, embed?: string) { const { db } = useStorage(); const { ndk } = useNDK(); const { status, data } = useQuery( ['event', id], async () => { + if (naddr) { + const rEvents = await ndk.fetchEvents({ + kinds: [naddr.kind], + '#d': [naddr.identifier], + authors: [naddr.pubkey], + }); + const rEvent = [...rEvents].slice(-1)[0]; + + return rEvent; + } + // return embed event (nostr.band api) or repost if (embed) { const event: NDKEvent = JSON.parse(embed); return event; } + // get event from db const dbEvent = await db.getEventByID(id); if (dbEvent) return dbEvent; diff --git a/src/utils/types.d.ts b/src/utils/types.d.ts index 21244e04..0bd2ff84 100644 --- a/src/utils/types.d.ts +++ b/src/utils/types.d.ts @@ -111,3 +111,14 @@ export interface NostrBuildResponse extends Response { }>; }; } + +export interface Resource { + id: string; + title: string; + image: string; +} + +export interface Resources { + title: string; + data: Array; +}