wip: add relay discover to onboarding

This commit is contained in:
Ren Amamiya
2023-08-08 18:52:46 +07:00
parent 9c7b58ee99
commit e6c6793f6e
20 changed files with 974 additions and 535 deletions

View File

@@ -1,30 +1,38 @@
// source: https://github.com/nostr-dev-kit/ndk-react/
import NDK, { NDKCacheAdapter } from '@nostr-dev-kit/ndk';
import NDK from '@nostr-dev-kit/ndk';
import { ndkAdapter } from '@nostr-fetch/adapter-ndk';
import { NostrFetcher, normalizeRelayUrlSet } from 'nostr-fetch';
import { NostrFetcher } from 'nostr-fetch';
import { useEffect, useState } from 'react';
import TauriAdapter from '@libs/ndk/cache';
import { getSetting } from '@libs/storage';
import { getExplicitRelayUrls } from '@libs/storage';
const setting = await getSetting('relays');
const relays = normalizeRelayUrlSet(JSON.parse(setting));
import { FULL_RELAYS } from '@stores/constants';
export const NDKInstance = () => {
const [ndk, setNDK] = useState<NDK | undefined>(undefined);
const [relayUrls, setRelayUrls] = useState<string[]>(relays);
const [relayUrls, setRelayUrls] = useState<string[]>([]);
const [fetcher, setFetcher] = useState<NostrFetcher>(undefined);
const [cacheAdapter] = useState(new TauriAdapter());
useEffect(() => {
const cacheAdapter = new TauriAdapter();
loadNdk(relays, cacheAdapter);
loadNdk();
return () => {
cacheAdapter.save();
};
}, [relays]);
}, []);
async function loadNdk() {
let explicitRelayUrls: string[];
const explicitRelayUrlsFromDB = await getExplicitRelayUrls();
if (explicitRelayUrlsFromDB) {
explicitRelayUrls = explicitRelayUrlsFromDB;
} else {
explicitRelayUrls = FULL_RELAYS;
}
async function loadNdk(explicitRelayUrls: string[], cacheAdapter: NDKCacheAdapter) {
const ndkInstance = new NDK({ explicitRelayUrls, cacheAdapter });
try {

View File

@@ -9,7 +9,7 @@ interface NDKContext {
ndk: NDK;
relayUrls: string[];
fetcher: NostrFetcher;
loadNdk: (_: string[]) => void;
loadNdk: () => void;
}
const NDKContext = createContext<NDKContext>({

View File

@@ -3,7 +3,15 @@ import destr from 'destr';
import { parser } from '@utils/parser';
import { getParentID } from '@utils/transform';
import { Account, Block, Chats, LumeEvent, Profile, Settings } from '@utils/types';
import {
Account,
Block,
Chats,
LumeEvent,
Profile,
Relays,
Settings,
} from '@utils/types';
let db: null | Database = null;
@@ -507,3 +515,39 @@ export async function removePrivkey() {
`UPDATE accounts SET privkey = "privkey is stored in secure storage" WHERE id = "${activeAccount.id}";`
);
}
// get relays
export async function getRelays() {
const db = await connect();
const activeAccount = await getActiveAccount();
return (await db.select(
`SELECT * FROM relays WHERE account_id = "${activeAccount.id}";`
)) as Relays[];
}
// get relays
export async function getExplicitRelayUrls() {
const db = await connect();
const activeAccount = await getActiveAccount();
const result: Relays[] = await db.select(
`SELECT * FROM relays WHERE account_id = "${activeAccount.id}";`
);
if (result.length > 0) return result.map((el) => el.relay);
return null;
}
// create relay
export async function createRelay(relay: string, purpose?: string) {
const db = await connect();
const activeAccount = await getActiveAccount();
return await db.execute(
'INSERT OR IGNORE INTO blocks (account_id, relay, purpose) VALUES (?, ?, ?);',
[activeAccount.id, relay, purpose || '']
);
}
// remove relay
export async function removeRelay(relay: string) {
const db = await connect();
return await db.execute(`DELETE FROM relays WHERE relay = "${relay}";`);
}