Skip to content

Commit d9791e6

Browse files
committed
Fix community loading for hex and npub keys
1 parent ffa78ff commit d9791e6

1 file changed

Lines changed: 25 additions & 60 deletions

File tree

src/components/Communities.astro

Lines changed: 25 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -51,76 +51,39 @@ const clientConfig = JSON.stringify({
5151
const CONFIG = JSON.parse(clientConfig);
5252
const { communities: COMMUNITIES, relay: RELAY, socialIcons: SOCIAL_ICONS, translations: T } = CONFIG;
5353

54-
interface NostrEvent {
55-
id: string;
56-
pubkey: string;
57-
kind: number;
58-
tags: string[][];
59-
content: string;
60-
sig: string;
61-
created_at: number;
62-
}
63-
64-
interface CommunityData {
65-
pubkey: string;
66-
relayPubkey?: string;
67-
region: string;
68-
social: { type: string; url: string }[];
69-
website?: string;
70-
// From kind 0
71-
name?: string;
72-
about?: string;
73-
picture?: string;
74-
metaWebsite?: string;
75-
// From kind 38385
76-
currencies?: string[];
77-
minAmount?: string;
78-
maxAmount?: string;
79-
fee?: string;
80-
}
81-
82-
function escapeHtml(str: string): string {
54+
function escapeHtml(str) {
8355
const div = document.createElement('div');
8456
div.appendChild(document.createTextNode(str));
8557
return div.innerHTML;
8658
}
8759

88-
function isValidUrl(str: string): boolean {
60+
function isValidUrl(str) {
8961
try {
9062
const url = new URL(str);
9163
return url.protocol === 'https:' || url.protocol === 'http:';
9264
} catch { return false; }
9365
}
9466

95-
function formatSats(sats: string): string {
67+
function formatSats(sats) {
9668
const num = parseInt(sats, 10);
9769
if (isNaN(num)) return '';
9870
if (num >= 1_000_000) return (num / 1_000_000).toFixed(1).replace(/\.0$/, '') + 'M';
9971
if (num >= 1_000) return (num / 1_000).toFixed(0) + 'K';
10072
return num.toLocaleString();
10173
}
10274

103-
function isHexPubkey(value: string): boolean {
75+
function isHexPubkey(value) {
10476
return /^[0-9a-f]{64}$/i.test(value);
10577
}
10678

107-
function hexToBytes(hex: string): Uint8Array {
108-
const normalized = hex.toLowerCase();
109-
const bytes = new Uint8Array(normalized.length / 2);
110-
for (let i = 0; i < normalized.length; i += 2) {
111-
bytes[i / 2] = parseInt(normalized.slice(i, i + 2), 16);
112-
}
113-
return bytes;
114-
}
115-
116-
function bytesToHex(bytes: Uint8Array): string {
79+
function bytesToHex(bytes) {
11780
return Array.from(bytes, byte => byte.toString(16).padStart(2, '0')).join('');
11881
}
11982

120-
function convertBits(data: number[], fromBits: number, toBits: number, pad: boolean): number[] | null {
83+
function convertBits(data, fromBits, toBits, pad) {
12184
let acc = 0;
12285
let bits = 0;
123-
const result: number[] = [];
86+
const result = [];
12487
const maxV = (1 << toBits) - 1;
12588
const maxAcc = (1 << (fromBits + toBits - 1)) - 1;
12689

@@ -143,7 +106,7 @@ const clientConfig = JSON.stringify({
143106
return result;
144107
}
145108

146-
function decodeNpub(value: string): string | null {
109+
function decodeNpub(value) {
147110
const normalized = value.trim().toLowerCase();
148111
if (!normalized.startsWith('npub1')) return null;
149112

@@ -170,13 +133,13 @@ const clientConfig = JSON.stringify({
170133
return isHexPubkey(hex) ? hex : null;
171134
}
172135

173-
function normalizePubkey(value: string): string | null {
136+
function normalizePubkey(value) {
174137
const normalized = value.trim();
175138
if (isHexPubkey(normalized)) return normalized.toLowerCase();
176139
return decodeNpub(normalized);
177140
}
178141

179-
function renderCard(c: CommunityData): string {
142+
function renderCard(c) {
180143
const name = escapeHtml(c.name || c.region);
181144
const desc = c.about ? escapeHtml(c.about) : '';
182145
const picture = c.picture && isValidUrl(c.picture) ? c.picture : '';
@@ -241,7 +204,7 @@ const clientConfig = JSON.stringify({
241204
`;
242205
}
243206

244-
function renderCopyToast(): void {
207+
function renderCopyToast() {
245208
let toast = document.getElementById('copy-toast');
246209
if (!toast) {
247210
toast = document.createElement('div');
@@ -251,11 +214,13 @@ const clientConfig = JSON.stringify({
251214
document.body.appendChild(toast);
252215
}
253216
toast.classList.add('show');
254-
setTimeout(() => toast!.classList.remove('show'), 2000);
217+
setTimeout(() => {
218+
if (toast) toast.classList.remove('show');
219+
}, 2000);
255220
}
256221

257-
async function fetchFromRelay(pubkeys: string[]): Promise<Map<string, { kind0?: NostrEvent; kind38385?: NostrEvent }>> {
258-
const results = new Map<string, { kind0?: NostrEvent; kind38385?: NostrEvent }>();
222+
async function fetchFromRelay(pubkeys) {
223+
const results = new Map();
259224
pubkeys.forEach(pk => results.set(pk, {}));
260225

261226
return new Promise((resolve) => {
@@ -264,7 +229,7 @@ const clientConfig = JSON.stringify({
264229
resolve(results);
265230
}, 10000);
266231

267-
let ws: WebSocket;
232+
let ws;
268233
try {
269234
ws = new WebSocket(RELAY);
270235
} catch {
@@ -289,7 +254,7 @@ const clientConfig = JSON.stringify({
289254
try {
290255
const data = JSON.parse(msg.data);
291256
if (data[0] === 'EVENT' && data[1] === subId) {
292-
const event: NostrEvent = data[2];
257+
const event = data[2];
293258
const entry = results.get(event.pubkey);
294259
if (!entry) return;
295260

@@ -318,7 +283,7 @@ const clientConfig = JSON.stringify({
318283
});
319284
}
320285

321-
function getTag(event: NostrEvent, name: string): string | undefined {
286+
function getTag(event, name) {
322287
const tag = event.tags.find(t => t[0] === name);
323288
return tag ? tag[1] : undefined;
324289
}
@@ -332,14 +297,14 @@ const clientConfig = JSON.stringify({
332297
const relayPubkey = normalizePubkey(c.pubkey);
333298
return relayPubkey ? { ...c, relayPubkey } : null;
334299
})
335-
.filter((community): community is CommunityData => community !== null);
300+
.filter(community => community !== null);
336301

337-
const pubkeys = [...new Set(normalizedCommunities.map(c => c.relayPubkey!))];
302+
const pubkeys = [...new Set(normalizedCommunities.map(c => c.relayPubkey))];
338303
const nostrData = await fetchFromRelay(pubkeys);
339304

340-
const cards: CommunityData[] = normalizedCommunities.map(c => {
341-
const data = nostrData.get(c.relayPubkey!);
342-
const result: CommunityData = { ...c };
305+
const cards = normalizedCommunities.map(c => {
306+
const data = nostrData.get(c.relayPubkey);
307+
const result = { ...c };
343308

344309
if (data?.kind0) {
345310
try {
@@ -369,7 +334,7 @@ const clientConfig = JSON.stringify({
369334
// Attach copy handlers
370335
grid.querySelectorAll('.copy-pubkey').forEach(btn => {
371336
btn.addEventListener('click', async () => {
372-
const pubkey = (btn as HTMLElement).dataset.pubkey;
337+
const pubkey = btn.dataset.pubkey;
373338
if (!pubkey) return;
374339
try {
375340
await navigator.clipboard.writeText(pubkey);

0 commit comments

Comments
 (0)