Eve/src/utils/getCCNList.ts
Danny Morabito 740921132a
feat: improve community creation and joining flow
- Add support for joining existing communities via invite codes.
- Refactor createCCN to handle both creating new communities and joining existing ones.
- Fix relay status polling to ensure continuous updates while running.
2025-04-24 15:26:50 +02:00

33 lines
1 KiB
TypeScript

import { nip19 } from '@nostr/tools';
import type { NPub } from '@nostr/tools/nip19';
export function getCCNList(): Promise<{ name: string; pubkey: string }[]> {
return new Promise((resolve, reject) => {
const ws = new WebSocket('ws://localhost:6942');
ws.onopen = () => {
ws.send(JSON.stringify(['CCN', 'LIST']));
};
ws.onmessage = (event) => {
const data = JSON.parse(event.data);
if (data[0] !== 'OK' || data[1] !== 'CCN LIST' || data[2] !== true) return;
resolve(JSON.parse(data[3]));
};
ws.onerror = (event) => {
reject(event);
};
});
}
export function getActiveCCNNpub() {
const selectedCCN = localStorage.getItem('selectedCCN');
if (!selectedCCN) return null;
const { pubkey } = JSON.parse(selectedCCN);
return nip19.npubEncode(pubkey);
}
export async function getCCNName(npub: NPub) {
const ccnList = await getCCNList();
const npubToPubkey = nip19.decode<'npub'>(npub);
const ccn = ccnList.find((ccn) => ccn.pubkey === npubToPubkey.data);
return ccn?.name;
}