From 2bfa1db81670474d511ada80ff0bee0e547117da Mon Sep 17 00:00:00 2001 From: reya Date: Mon, 27 Nov 2023 09:48:51 +0700 Subject: [PATCH] update --- src/app/relays/components/relayList.tsx | 14 ++-- src/app/users/components/profile.tsx | 48 ++++++------ src/libs/ndk/instance.ts | 76 ++++++++++--------- src/libs/storage/instance.ts | 2 +- .../widgets/other/nostrBandUserProfile.tsx | 59 ++++---------- 5 files changed, 92 insertions(+), 107 deletions(-) diff --git a/src/app/relays/components/relayList.tsx b/src/app/relays/components/relayList.tsx index cc77ef51..860ce1e7 100644 --- a/src/app/relays/components/relayList.tsx +++ b/src/app/relays/components/relayList.tsx @@ -1,7 +1,7 @@ import { useQuery, useQueryClient } from '@tanstack/react-query'; -import { message } from '@tauri-apps/plugin-dialog'; import { normalizeRelayUrl } from 'nostr-fetch'; import { useNavigate } from 'react-router-dom'; +import { toast } from 'sonner'; import { VList } from 'virtua'; import { useStorage } from '@libs/storage/provider'; @@ -37,10 +37,14 @@ export function RelayList() { const url = normalizeRelayUrl(relayUrl); const res = await db.createRelay(url); - if (!res) await message("You're aldready connected to this relay"); - queryClient.invalidateQueries({ - queryKey: ['user-relay'], - }); + if (res) { + toast.info('Connected. You need to restart app to take effect'); + queryClient.invalidateQueries({ + queryKey: ['user-relay'], + }); + } else { + toast.warning("You're aldready connected to this relay"); + } }; return ( diff --git a/src/app/users/components/profile.tsx b/src/app/users/components/profile.tsx index a1e2e21b..f3d46d08 100644 --- a/src/app/users/components/profile.tsx +++ b/src/app/users/components/profile.tsx @@ -26,44 +26,48 @@ export function UserProfile({ pubkey }: { pubkey: string }) { const svgURI = 'data:image/svg+xml;utf8,' + encodeURIComponent(minidenticon(pubkey, 90, 50)); - const follow = async (pubkey: string) => { + const follow = async () => { try { + if (!ndk.signer) return navigate('/new/privkey'); + setFollowed(true); + const user = ndk.getUser({ pubkey: db.account.pubkey }); const contacts = await user.follows(); const add = await user.follow(new NDKUser({ pubkey: pubkey }), contacts); - if (add) { - setFollowed(true); - } else { - toast('You already follow this user'); + if (!add) { + toast.success('You already follow this user'); + setFollowed(false); } - } catch (error) { - console.log(error); + } catch (e) { + toast.error(e); + setFollowed(false); } }; - const unfollow = async (pubkey: string) => { + const unfollow = async () => { try { if (!ndk.signer) return navigate('/new/privkey'); + setFollowed(false); const user = ndk.getUser({ pubkey: db.account.pubkey }); const contacts = await user.follows(); contacts.delete(new NDKUser({ pubkey: pubkey })); - let list: string[][]; - contacts.forEach((el) => list.push(['p', el.pubkey, el.relayUrls?.[0] || '', ''])); - + const list = [...contacts].map((item) => [ + 'p', + item.pubkey, + item.relayUrls?.[0] || '', + '', + ]); const event = new NDKEvent(ndk); event.content = ''; event.kind = NDKKind.Contacts; event.tags = list; - const publishedRelays = await event.publish(); - if (publishedRelays) { - setFollowed(false); - } - } catch (error) { - console.log(error); + await event.publish(); + } catch (e) { + toast.error(e); } }; @@ -139,23 +143,23 @@ export function UserProfile({ pubkey }: { pubkey: string }) { {followed ? ( ) : ( )} Message diff --git a/src/libs/ndk/instance.ts b/src/libs/ndk/instance.ts index 172ce7bc..e00953b0 100644 --- a/src/libs/ndk/instance.ts +++ b/src/libs/ndk/instance.ts @@ -1,7 +1,8 @@ import NDK, { NDKNip46Signer, NDKPrivateKeySigner } from '@nostr-dev-kit/ndk'; import { ndkAdapter } from '@nostr-fetch/adapter-ndk'; -import { message } from '@tauri-apps/plugin-dialog'; +import { ask } from '@tauri-apps/plugin-dialog'; import { fetch } from '@tauri-apps/plugin-http'; +import { relaunch } from '@tauri-apps/plugin-process'; import { NostrFetcher } from 'nostr-fetch'; import { useEffect, useMemo, useState } from 'react'; import { toast } from 'sonner'; @@ -78,50 +79,57 @@ export const NDKInstance = () => { } async function initNDK() { - const outboxSetting = await db.getSettingValue('outbox'); - const bunkerSetting = await db.getSettingValue('nsecbunker'); - const signer = await getSigner(!!parseInt(bunkerSetting)); - const explicitRelayUrls = await getExplicitRelays(); - - const tauriAdapter = new NDKCacheAdapterTauri(db); - const instance = new NDK({ - explicitRelayUrls, - cacheAdapter: tauriAdapter, - outboxRelayUrls: ['wss://purplepag.es'], - blacklistRelayUrls: [], - enableOutboxModel: !!parseInt(outboxSetting), - }); - instance.signer = signer; - try { + const outboxSetting = await db.getSettingValue('outbox'); + const bunkerSetting = await db.getSettingValue('nsecbunker'); + const signer = await getSigner(!!parseInt(bunkerSetting)); + const explicitRelayUrls = await getExplicitRelays(); + + const tauriAdapter = new NDKCacheAdapterTauri(db); + const instance = new NDK({ + explicitRelayUrls, + cacheAdapter: tauriAdapter, + outboxRelayUrls: ['wss://purplepag.es'], + blacklistRelayUrls: [], + enableOutboxModel: !!parseInt(outboxSetting), + }); + instance.signer = signer; + // connect - await instance.connect(2000); + await instance.connect(); // update account's metadata if (db.account) { const user = instance.getUser({ pubkey: db.account.pubkey }); - const follows = [...(await user.follows())].map((user) => user.pubkey); - const relayList = await user.relayList(); + if (user) { + const follows = [...(await user.follows())].map((user) => user.pubkey); + const relayList = await user.relayList(); - // update user's follows - await db.updateAccount('follows', JSON.stringify(follows)); + // update user's follows + await db.updateAccount('follows', JSON.stringify(follows)); - // update user's relay list - if (relayList) { - for (const relay of relayList.relays) { - await db.createRelay(relay); - } + if (relayList) + // update user's relays + for (const relay of relayList.relays) { + await db.createRelay(relay); + } } } - } catch (error) { - await message(`NDK instance init failed: ${error}`, { - title: 'Lume', - type: 'error', - }); - } - setNDK(instance); - setRelayUrls(explicitRelayUrls); + setNDK(instance); + setRelayUrls(explicitRelayUrls); + } catch (e) { + const yes = await ask( + `Something wrong, Lume is not working as expected, do you want to relaunch app?`, + { + title: 'Lume', + type: 'error', + okLabel: 'Yes', + } + ); + + if (yes) relaunch(); + } } useEffect(() => { diff --git a/src/libs/storage/instance.ts b/src/libs/storage/instance.ts index 200b02a3..c3d3e5e2 100644 --- a/src/libs/storage/instance.ts +++ b/src/libs/storage/instance.ts @@ -438,7 +438,7 @@ export class LumeStorage { [relay, this.account.id] ); - if (!existRelays.length) return; + if (existRelays.length) return; return await this.db.execute( 'INSERT OR IGNORE INTO relays (account_id, relay, purpose) VALUES ($1, $2, $3);', diff --git a/src/shared/widgets/other/nostrBandUserProfile.tsx b/src/shared/widgets/other/nostrBandUserProfile.tsx index 064f3c11..24d93dff 100644 --- a/src/shared/widgets/other/nostrBandUserProfile.tsx +++ b/src/shared/widgets/other/nostrBandUserProfile.tsx @@ -1,4 +1,4 @@ -import { NDKEvent, NDKKind, NDKUser } from '@nostr-dev-kit/ndk'; +import { NDKUser } from '@nostr-dev-kit/ndk'; import { useEffect, useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { toast } from 'sonner'; @@ -6,7 +6,7 @@ import { toast } from 'sonner'; import { useNDK } from '@libs/ndk/provider'; import { useStorage } from '@libs/storage/provider'; -import { FollowIcon, UnfollowIcon } from '@shared/icons'; +import { FollowIcon } from '@shared/icons'; import { shortenKey } from '@utils/shortenKey'; @@ -16,53 +16,30 @@ export interface Profile { } export function NostrBandUserProfile({ data }: { data: Profile }) { - const embedProfile = data.profile ? JSON.parse(data.profile.content) : null; - const profile = embedProfile; - const { db } = useStorage(); const { ndk } = useNDK(); const [followed, setFollowed] = useState(false); const navigate = useNavigate(); + const profile = data.profile ? JSON.parse(data.profile.content) : null; + const follow = async (pubkey: string) => { try { + if (!ndk.signer) return navigate('/new/privkey'); + setFollowed(true); + const user = ndk.getUser({ pubkey: db.account.pubkey }); const contacts = await user.follows(); const add = await user.follow(new NDKUser({ pubkey: pubkey }), contacts); - if (add) { - setFollowed(true); - } else { - toast('You already follow this user'); - } - } catch (error) { - console.log(error); - } - }; - - const unfollow = async (pubkey: string) => { - try { - if (!ndk.signer) return navigate('/new/privkey'); - - const user = ndk.getUser({ pubkey: db.account.pubkey }); - const contacts = await user.follows(); - contacts.delete(new NDKUser({ pubkey: pubkey })); - - let list: string[][]; - contacts.forEach((el) => list.push(['p', el.pubkey, el.relayUrls?.[0] || '', ''])); - - const event = new NDKEvent(ndk); - event.content = ''; - event.kind = NDKKind.Contacts; - event.tags = list; - - const publishedRelays = await event.publish(); - if (publishedRelays) { + if (!add) { + toast.success('You already follow this user'); setFollowed(false); } - } catch (error) { - console.log(error); + } catch (e) { + toast.error(e); + setFollowed(false); } }; @@ -100,15 +77,7 @@ export function NostrBandUserProfile({ data }: { data: Profile }) {
- {followed ? ( - - ) : ( + {!followed ? ( - )} + ) : null}