wip: clean up & refactor

This commit is contained in:
Ren Amamiya
2023-08-16 20:52:09 +07:00
parent c05bb54976
commit ab61bfb2cd
79 changed files with 183 additions and 2618 deletions

View File

@@ -1,31 +0,0 @@
import { useQuery } from '@tanstack/react-query';
import { useNDK } from '@libs/ndk/provider';
import { useStorage } from '@libs/storage/provider';
export function useAccount() {
const { db } = useStorage();
const { ndk } = useNDK();
const { status, data: account } = useQuery(
['account'],
async () => {
const account = await db.getActiveAccount();
console.log('account: ', account);
if (account?.pubkey) {
const user = ndk.getUser({ hexpubkey: account?.pubkey });
await user.fetchProfile();
return { ...account, ...user.profile };
}
return account;
},
{
enabled: !!ndk,
staleTime: Infinity,
refetchOnMount: false,
refetchOnWindowFocus: false,
refetchOnReconnect: false,
}
);
return { status, account };
}

View File

@@ -16,7 +16,7 @@ export function useEvent(id: string, embed?: string) {
return embed as unknown as LumeEvent;
} else {
const event = (await ndk.fetchEvent(id)) as LumeEvent;
if (!event) return null;
if (!event) throw new Error('event not found');
if (event.kind === 1) event['content'] = parser(event) as unknown as string;
return event as LumeEvent;
}

View File

@@ -6,7 +6,6 @@ import {
NDKSubscription,
NDKUser,
} from '@nostr-dev-kit/ndk';
import { destr } from 'destr';
import { LRUCache } from 'lru-cache';
import { nip19 } from 'nostr-tools';
import { useMemo } from 'react';
@@ -17,6 +16,14 @@ import { useStorage } from '@libs/storage/provider';
import { useStronghold } from '@stores/stronghold';
import { nHoursAgo } from '@utils/date';
import { LumeEvent } from '@utils/types';
interface NotesResponse {
status: string;
data: LumeEvent[];
nextCursor?: number;
message?: string;
}
export function useNostr() {
const { ndk } = useNDK();
@@ -37,6 +44,8 @@ export function useNostr() {
callback: (event: NDKEvent) => void,
closeOnEose?: boolean
) => {
if (!ndk) throw new Error('NDK instance not found');
const subEvent = ndk.subscribe(filter, { closeOnEose: closeOnEose ?? true });
subManager.set(JSON.stringify(filter), subEvent);
@@ -78,17 +87,22 @@ export function useNostr() {
}
};
const fetchNotes = async (since: number) => {
const fetchNotes = async (since: number): Promise<NotesResponse> => {
try {
if (!ndk) return { status: 'failed', message: 'NDK instance not found' };
if (!ndk) return { status: 'failed', data: [], message: 'NDK instance not found' };
console.log('fetch all events since: ', since);
const events = await ndk.fetchEvents({
kinds: [1],
authors: db.account.network ?? db.account.follows,
since: nHoursAgo(since),
});
return { status: 'ok', data: [...events], nextCursor: since * 2 };
const sorted = [...events].sort(
(a, b) => b.created_at - a.created_at
) as unknown as LumeEvent[];
return { status: 'ok', data: sorted, nextCursor: since * 2 };
} catch (e) {
console.error('failed get notes, error: ', e);
return { status: 'failed', data: [], message: e };
@@ -122,14 +136,6 @@ export function useNostr() {
};
const createZap = async (event: NDKEvent, amount: number, message?: string) => {
// @ts-expect-error, LumeEvent to NDKEvent
event.id = event.event_id;
// @ts-expect-error, LumeEvent to NDKEvent
if (typeof event.content !== 'string') event.content = event.content.original;
if (typeof event.tags === 'string') event.tags = destr(event.tags);
if (!privkey) throw new Error('Private key not found');
if (!ndk.signer) {
@@ -137,7 +143,7 @@ export function useNostr() {
ndk.signer = signer;
}
// @ts-expect-error, LumeEvent to NDKEvent
// @ts-expect-error, NostrEvent to NDKEvent
const ndkEvent = new NDKEvent(ndk, event);
const res = await ndkEvent.zap(amount, message ?? 'zap from lume');

View File

@@ -4,7 +4,7 @@ import { invoke } from '@tauri-apps/api/tauri';
import { Opengraph } from '@utils/types';
export function useOpenGraph(url: string) {
const { status, data, error, isFetching } = useQuery(
const { status, data, error } = useQuery(
['preview', url],
async () => {
const res: Opengraph = await invoke('opengraph', { url });
@@ -25,6 +25,5 @@ export function useOpenGraph(url: string) {
status,
data,
error,
isFetching,
};
}

View File

@@ -1,38 +0,0 @@
import { BaseDirectory, appConfigDir } from '@tauri-apps/api/path';
import { removeFile } from '@tauri-apps/plugin-fs';
import { Stronghold } from '@tauri-apps/plugin-stronghold';
const dir = await appConfigDir();
export function useSecureStorage() {
async function getClient(stronghold: Stronghold) {
try {
return await stronghold.loadClient('lume');
} catch {
return await stronghold.createClient('lume');
}
}
const save = async (key: string, value: string, password: string) => {
const stronghold = await Stronghold.load(`${dir}/lume.stronghold`, password);
const client = await getClient(stronghold);
const store = client.getStore();
await store.insert(key, Array.from(new TextEncoder().encode(value)));
return await stronghold.save();
};
const load = async (key: string, password: string) => {
const stronghold = await Stronghold.load(`${dir}/lume.stronghold`, password);
const client = await getClient(stronghold);
const store = client.getStore();
const value = await store.get(key);
const decoded = new TextDecoder().decode(new Uint8Array(value));
return decoded;
};
const reset = async () => {
return await removeFile('lume.stronghold', { dir: BaseDirectory.AppConfig });
};
return { save, load, reset };
}