diff --git a/src/App.vue b/src/App.vue index 2af6772..dfbadfa 100644 --- a/src/App.vue +++ b/src/App.vue @@ -1,12 +1,9 @@ diff --git a/src/composables/useChat.ts b/src/composables/useChat.ts index 2284122..4b1ed25 100644 --- a/src/composables/useChat.ts +++ b/src/composables/useChat.ts @@ -1,3 +1,7 @@ +import { computed } from 'vue' +import { useMessageStore } from '@/stores/messages' +import { useNostrStore } from '@/stores/nostr' + export function useChat(pubkey: string) { const messageStore = useMessageStore() const nostrStore = useNostrStore() diff --git a/src/lib/messages.ts b/src/lib/messages.ts index 3e14351..c8938e0 100644 --- a/src/lib/messages.ts +++ b/src/lib/messages.ts @@ -1,3 +1,5 @@ +import type { DirectMessage } from '@/types/nostr' + export class MessageManager { private messages = new Map() private processedIds = new Set() diff --git a/src/lib/storage.ts b/src/lib/storage.ts index 5c9bf40..d3cb79c 100644 --- a/src/lib/storage.ts +++ b/src/lib/storage.ts @@ -1,3 +1,5 @@ +import type { DirectMessage } from '@/types/nostr' + export class MessageStorage { static saveMessages(pubkey: string, messages: DirectMessage[]) { try { diff --git a/src/lib/subscriptions.ts b/src/lib/subscriptions.ts index a8c7149..a9e51c5 100644 --- a/src/lib/subscriptions.ts +++ b/src/lib/subscriptions.ts @@ -1,3 +1,5 @@ +import type { NostrEvent } from '@/types/nostr' + export class SubscriptionManager { private currentSubs: any[] = [] private isActive = false diff --git a/src/lib/utils.ts b/src/lib/utils.ts index d32b0fe..2617904 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -4,3 +4,12 @@ import { twMerge } from 'tailwind-merge' export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)) } + +export async function withTimeout(promise: Promise, timeoutMs: number = 10000): Promise { + return Promise.race([ + promise, + new Promise((_, reject) => + setTimeout(() => reject(new Error('Operation timed out')), timeoutMs) + ) + ]) +} diff --git a/src/lib/websocket.ts b/src/lib/websocket.ts index e19e644..9806882 100644 --- a/src/lib/websocket.ts +++ b/src/lib/websocket.ts @@ -1,3 +1,6 @@ +import { withTimeout } from '@/lib/utils' +import type { NostrEvent } from '@/types/nostr' + // Create a new WebSocket manager class export class NostrWebSocketManager { private relayPool: any[] = [] @@ -41,4 +44,19 @@ export class NostrWebSocketManager { get isConnected() { return this.relayPool.length > 0 } + + private async publishToRelay(event: NostrEvent, url: string) { + const relay = window.NostrTools.relayInit(url) + try { + await relay.connect() + return new Promise((resolve, reject) => { + const pub = relay.publish(event) + pub.on('ok', () => resolve(true)) + pub.on('failed', reject) + }) + } catch (err) { + console.error(`Failed to publish to ${url}:`, err) + return false + } + } } \ No newline at end of file diff --git a/src/stores/messages.ts b/src/stores/messages.ts index 83e4d6d..eeb6d66 100644 --- a/src/stores/messages.ts +++ b/src/stores/messages.ts @@ -1,3 +1,7 @@ +import { defineStore } from 'pinia' +import { ref } from 'vue' +import type { DirectMessage } from '@/types/nostr' + // Separate message handling into its own store export const useMessageStore = defineStore('messages', () => { const messages = ref>(new Map()) diff --git a/src/stores/nostr.ts b/src/stores/nostr.ts index 9678329..026dca8 100644 --- a/src/stores/nostr.ts +++ b/src/stores/nostr.ts @@ -102,11 +102,11 @@ export const useNostrStore = defineStore('nostr', () => { // Load stored messages and IDs on initialization const initializeFromStorage = () => { try { - const storedMessages = JSON.parse(localStorage.getItem('nostr_messages') || '[]') - const messageMap = new Map(storedMessages) + const messageMap = new Map( + JSON.parse(localStorage.getItem('nostr_messages') || '[]') + ) - // Initialize processedMessageIds from stored messages - messageMap.forEach(msgs => { + messageMap.forEach((msgs: DirectMessage[]) => { msgs.forEach(msg => { processedMessageIds.value.add(msg.id) }) diff --git a/src/types/nostr.ts b/src/types/nostr.ts index 453445d..b34905c 100644 --- a/src/types/nostr.ts +++ b/src/types/nostr.ts @@ -1,9 +1,9 @@ export interface NostrEvent { kind: number pubkey: string - created_at: number - tags: string[][] content: string + tags: string[][] + created_at: number id: string sig: string }