This commit is contained in:
padreug 2025-02-15 02:34:19 +01:00
parent 8df44506c0
commit 2bbb9ae938
10 changed files with 47 additions and 9 deletions

View file

@ -1,12 +1,9 @@
<script setup lang="ts"> <script setup lang="ts">
import { useTheme } from '@/components/theme-provider'
import { computed } from 'vue' import { computed } from 'vue'
import { useRoute } from 'vue-router' import { useRoute } from 'vue-router'
import Navbar from '@/components/layout/Navbar.vue' import Navbar from '@/components/layout/Navbar.vue'
import Footer from '@/components/layout/Footer.vue' import Footer from '@/components/layout/Footer.vue'
// Initialize theme
useTheme()
const route = useRoute() const route = useRoute()
const showFooter = computed(() => route.path !== '/support') const showFooter = computed(() => route.path !== '/support')
</script> </script>

View file

@ -1,3 +1,7 @@
import { computed } from 'vue'
import { useMessageStore } from '@/stores/messages'
import { useNostrStore } from '@/stores/nostr'
export function useChat(pubkey: string) { export function useChat(pubkey: string) {
const messageStore = useMessageStore() const messageStore = useMessageStore()
const nostrStore = useNostrStore() const nostrStore = useNostrStore()

View file

@ -1,3 +1,5 @@
import type { DirectMessage } from '@/types/nostr'
export class MessageManager { export class MessageManager {
private messages = new Map<string, DirectMessage[]>() private messages = new Map<string, DirectMessage[]>()
private processedIds = new Set<string>() private processedIds = new Set<string>()

View file

@ -1,3 +1,5 @@
import type { DirectMessage } from '@/types/nostr'
export class MessageStorage { export class MessageStorage {
static saveMessages(pubkey: string, messages: DirectMessage[]) { static saveMessages(pubkey: string, messages: DirectMessage[]) {
try { try {

View file

@ -1,3 +1,5 @@
import type { NostrEvent } from '@/types/nostr'
export class SubscriptionManager { export class SubscriptionManager {
private currentSubs: any[] = [] private currentSubs: any[] = []
private isActive = false private isActive = false

View file

@ -4,3 +4,12 @@ import { twMerge } from 'tailwind-merge'
export function cn(...inputs: ClassValue[]) { export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs)) return twMerge(clsx(inputs))
} }
export async function withTimeout<T>(promise: Promise<T>, timeoutMs: number = 10000): Promise<T> {
return Promise.race([
promise,
new Promise<T>((_, reject) =>
setTimeout(() => reject(new Error('Operation timed out')), timeoutMs)
)
])
}

View file

@ -1,3 +1,6 @@
import { withTimeout } from '@/lib/utils'
import type { NostrEvent } from '@/types/nostr'
// Create a new WebSocket manager class // Create a new WebSocket manager class
export class NostrWebSocketManager { export class NostrWebSocketManager {
private relayPool: any[] = [] private relayPool: any[] = []
@ -41,4 +44,19 @@ export class NostrWebSocketManager {
get isConnected() { get isConnected() {
return this.relayPool.length > 0 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
}
}
} }

View file

@ -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 // Separate message handling into its own store
export const useMessageStore = defineStore('messages', () => { export const useMessageStore = defineStore('messages', () => {
const messages = ref<Map<string, DirectMessage[]>>(new Map()) const messages = ref<Map<string, DirectMessage[]>>(new Map())

View file

@ -102,11 +102,11 @@ export const useNostrStore = defineStore('nostr', () => {
// Load stored messages and IDs on initialization // Load stored messages and IDs on initialization
const initializeFromStorage = () => { const initializeFromStorage = () => {
try { try {
const storedMessages = JSON.parse(localStorage.getItem('nostr_messages') || '[]') const messageMap = new Map<string, DirectMessage[]>(
const messageMap = new Map(storedMessages) JSON.parse(localStorage.getItem('nostr_messages') || '[]')
)
// Initialize processedMessageIds from stored messages messageMap.forEach((msgs: DirectMessage[]) => {
messageMap.forEach(msgs => {
msgs.forEach(msg => { msgs.forEach(msg => {
processedMessageIds.value.add(msg.id) processedMessageIds.value.add(msg.id)
}) })

View file

@ -1,9 +1,9 @@
export interface NostrEvent { export interface NostrEvent {
kind: number kind: number
pubkey: string pubkey: string
created_at: number
tags: string[][]
content: string content: string
tags: string[][]
created_at: number
id: string id: string
sig: string sig: string
} }