This commit is contained in:
reya
2023-12-07 11:50:25 +07:00
parent a42a2788ea
commit 95124e5ded
28 changed files with 1547 additions and 301 deletions
+3 -11
View File
@@ -3,26 +3,18 @@ import { useQueryClient } from '@tanstack/react-query';
import { useNavigate } from 'react-router-dom';
import { toast } from 'sonner';
import { useNDK } from '@libs/ndk/provider';
import { useStorage } from '@libs/storage/provider';
import { useArk } from '@libs/ark';
export function Logout() {
const { db } = useStorage();
const { ndk } = useNDK();
const { ark } = useArk();
const navigate = useNavigate();
const queryClient = useQueryClient();
const logout = async () => {
try {
ndk.signer = null;
// remove private key
await db.secureRemove(db.account.pubkey);
await db.secureRemove(`${db.account.pubkey}-nsecbunker`);
// logout
await db.accountLogout();
await ark.logout();
// clear cache
queryClient.clear();
+8 -7
View File
@@ -2,7 +2,7 @@ import { NDKEvent, NDKKind, NostrEvent } from '@nostr-dev-kit/ndk';
import { useQuery } from '@tanstack/react-query';
import { memo } from 'react';
import { useNDK } from '@libs/ndk/provider';
import { useArk } from '@libs/ark';
import {
MemoizedArticleKind,
@@ -14,24 +14,25 @@ import {
import { User } from '@shared/user';
export function Repost({ event }: { event: NDKEvent }) {
const { ndk } = useNDK();
const { ark } = useArk();
const { status, data: repostEvent } = useQuery({
queryKey: ['repost', event.id],
queryFn: async () => {
try {
let event: NDKEvent = undefined;
if (event.content.length > 50) {
const embed = JSON.parse(event.content) as NostrEvent;
const embedEvent = new NDKEvent(ndk, embed);
return embedEvent;
event = ark.createNDKEvent({ event: embed });
}
const id = event.tags.find((el) => el[0] === 'e')[1];
if (!id) throw new Error('Failed to get repost event id');
const ndkEvent = await ndk.fetchEvent(id);
if (!ndkEvent) return Promise.reject(new Error('Failed to get repost event'));
event = await ark.getEventById({ id });
return ndkEvent;
if (!event) return Promise.reject(new Error('Failed to get repost event'));
return event;
} catch {
throw new Error('Failed to get repost event');
}
+13 -19
View File
@@ -1,16 +1,13 @@
import { NDKEvent, NDKFilter, NDKKind, NDKSubscription } from '@nostr-dev-kit/ndk';
import { NDKEvent, NDKKind, NDKSubscription } from '@nostr-dev-kit/ndk';
import { QueryStatus, useQueryClient } from '@tanstack/react-query';
import { useEffect, useState } from 'react';
import { useNDK } from '@libs/ndk/provider';
import { useStorage } from '@libs/storage/provider';
import { useArk } from '@libs/ark';
import { ChevronUpIcon } from '@shared/icons';
export function LiveUpdater({ status }: { status: QueryStatus }) {
const { db } = useStorage();
const { ndk } = useNDK();
const { ark } = useArk();
const [events, setEvents] = useState<NDKEvent[]>([]);
const queryClient = useQueryClient();
@@ -30,19 +27,16 @@ export function LiveUpdater({ status }: { status: QueryStatus }) {
useEffect(() => {
let sub: NDKSubscription = undefined;
if (status === 'success' && db.account && db.account?.follows?.length > 0) {
queryClient.fetchQuery({ queryKey: ['notification'] });
const filter: NDKFilter = {
kinds: [NDKKind.Text, NDKKind.Repost],
authors: db.account.contacts,
since: Math.floor(Date.now() / 1000),
};
sub = ndk.subscribe(filter, { closeOnEose: false, groupable: false });
sub.addListener('event', (event: NDKEvent) =>
setEvents((prev) => [...prev, event])
);
if (status === 'success' && ark.account && ark.account?.contacts?.length > 0) {
sub = ark.subscribe({
filter: {
kinds: [NDKKind.Text, NDKKind.Repost],
authors: ark.account.contacts,
since: Math.floor(Date.now() / 1000),
},
closeOnEose: false,
cb: (event: NDKEvent) => setEvents((prev) => [...prev, event]),
});
}
return () => {
@@ -1,10 +1,8 @@
import { NDKUser } from '@nostr-dev-kit/ndk';
import { useEffect, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { toast } from 'sonner';
import { useNDK } from '@libs/ndk/provider';
import { useStorage } from '@libs/storage/provider';
import { useArk } from '@libs/ark';
import { FollowIcon } from '@shared/icons';
@@ -16,9 +14,7 @@ export interface Profile {
}
export function NostrBandUserProfile({ data }: { data: Profile }) {
const { db } = useStorage();
const { ndk } = useNDK();
const { ark } = useArk();
const [followed, setFollowed] = useState(false);
const navigate = useNavigate();
@@ -26,12 +22,10 @@ export function NostrBandUserProfile({ data }: { data: Profile }) {
const follow = async (pubkey: string) => {
try {
if (!ndk.signer) return navigate('/new/privkey');
if (!ark.readyToSign) 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);
const add = ark.createContact({ pubkey });
if (!add) {
toast.success('You already follow this user');
@@ -44,7 +38,7 @@ export function NostrBandUserProfile({ data }: { data: Profile }) {
};
useEffect(() => {
if (db.account.contacts.includes(data.pubkey)) {
if (ark.account.contacts.includes(data.pubkey)) {
setFollowed(true);
}
}, []);