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">
|
<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>
|
||||||
|
|
|
||||||
|
|
@ -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()
|
||||||
|
|
|
||||||
|
|
@ -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>()
|
||||||
|
|
|
||||||
|
|
@ -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 {
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
)
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -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())
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue