refactor
This commit is contained in:
parent
8df44506c0
commit
2bbb9ae938
10 changed files with 47 additions and 9 deletions
|
|
@ -1,12 +1,9 @@
|
|||
<script setup lang="ts">
|
||||
import { useTheme } from '@/components/theme-provider'
|
||||
import { computed } from 'vue'
|
||||
import { useRoute } from 'vue-router'
|
||||
import Navbar from '@/components/layout/Navbar.vue'
|
||||
import Footer from '@/components/layout/Footer.vue'
|
||||
|
||||
// Initialize theme
|
||||
useTheme()
|
||||
const route = useRoute()
|
||||
const showFooter = computed(() => route.path !== '/support')
|
||||
</script>
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
import type { DirectMessage } from '@/types/nostr'
|
||||
|
||||
export class MessageManager {
|
||||
private messages = new Map<string, DirectMessage[]>()
|
||||
private processedIds = new Set<string>()
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
import type { DirectMessage } from '@/types/nostr'
|
||||
|
||||
export class MessageStorage {
|
||||
static saveMessages(pubkey: string, messages: DirectMessage[]) {
|
||||
try {
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
import type { NostrEvent } from '@/types/nostr'
|
||||
|
||||
export class SubscriptionManager {
|
||||
private currentSubs: any[] = []
|
||||
private isActive = false
|
||||
|
|
|
|||
|
|
@ -4,3 +4,12 @@ import { twMerge } from 'tailwind-merge'
|
|||
export function cn(...inputs: ClassValue[]) {
|
||||
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)
|
||||
)
|
||||
])
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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<Map<string, DirectMessage[]>>(new Map())
|
||||
|
|
|
|||
|
|
@ -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<string, DirectMessage[]>(
|
||||
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)
|
||||
})
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue