wraith/frontend/stores/connection.store.ts
Vantz Stockwell b93fe016ed feat: frontend — auth flow, connection manager UI, host tree
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-12 17:11:02 -04:00

99 lines
2.4 KiB
TypeScript

import { defineStore } from 'pinia'
import { useAuthStore } from './auth.store'
interface Host {
id: number
name: string
hostname: string
port: number
protocol: 'ssh' | 'rdp'
groupId: number | null
credentialId: number | null
tags: string[]
notes: string | null
color: string | null
lastConnectedAt: string | null
group: { id: number; name: string } | null
}
interface HostGroup {
id: number
name: string
parentId: number | null
children: HostGroup[]
hosts: Host[]
}
export const useConnectionStore = defineStore('connections', {
state: () => ({
hosts: [] as Host[],
groups: [] as HostGroup[],
search: '',
loading: false,
}),
actions: {
headers() {
const auth = useAuthStore()
return { Authorization: `Bearer ${auth.token}` }
},
async fetchHosts() {
this.loading = true
try {
this.hosts = await $fetch('/api/hosts', { headers: this.headers() })
} finally {
this.loading = false
}
},
async fetchTree() {
this.groups = await $fetch('/api/groups/tree', { headers: this.headers() })
},
async createHost(data: Partial<Host>) {
const host = await $fetch<Host>('/api/hosts', {
method: 'POST',
body: data,
headers: this.headers(),
})
await this.fetchHosts()
return host
},
async updateHost(id: number, data: Partial<Host>) {
await $fetch(`/api/hosts/${id}`, {
method: 'PUT',
body: data,
headers: this.headers(),
})
await this.fetchHosts()
},
async deleteHost(id: number) {
await $fetch(`/api/hosts/${id}`, {
method: 'DELETE',
headers: this.headers(),
})
await this.fetchHosts()
},
async createGroup(data: { name: string; parentId?: number }) {
await $fetch('/api/groups', {
method: 'POST',
body: data,
headers: this.headers(),
})
await this.fetchTree()
},
async updateGroup(id: number, data: { name?: string; parentId?: number }) {
await $fetch(`/api/groups/${id}`, {
method: 'PUT',
body: data,
headers: this.headers(),
})
await this.fetchTree()
},
async deleteGroup(id: number) {
await $fetch(`/api/groups/${id}`, {
method: 'DELETE',
headers: this.headers(),
})
await this.fetchTree()
},
},
})