archy/neode-ui/src/stores/server.ts

117 lines
3.2 KiB
TypeScript
Raw Normal View History

// Server store — computed server state and RPC action proxies
import { defineStore } from 'pinia'
import { computed, ref } from 'vue'
import { rpcClient } from '../api/rpc-client'
import { useSyncStore } from './sync'
import type { InstallProgress } from '../views/marketplace/marketplaceData'
export const useServerStore = defineStore('server', () => {
const sync = useSyncStore()
// Global install tracking — persists across navigation
const installingApps = ref<Map<string, InstallProgress>>(new Map())
function setInstallProgress(appId: string, progress: Partial<InstallProgress> & { id: string; title: string }) {
const existing = installingApps.value.get(appId)
installingApps.value.set(appId, {
status: 'downloading',
progress: 0,
message: 'Preparing...',
attempt: 0,
...existing,
...progress,
})
}
function clearInstallProgress(appId: string) {
installingApps.value.delete(appId)
}
function isInstalling(appId: string): boolean {
return installingApps.value.has(appId)
}
// Computed — derived from sync store's data
const serverName = computed(() => sync.serverInfo?.name || 'Archipelago')
const isRestarting = computed(() => sync.serverInfo?.['status-info']?.restarting || false)
const isShuttingDown = computed(() => sync.serverInfo?.['status-info']?.['shutting-down'] || false)
const isOffline = computed(() => !sync.isConnected || isRestarting.value || isShuttingDown.value)
// Package actions
async function installPackage(id: string, marketplaceUrl: string, version: string): Promise<string> {
return rpcClient.installPackage(id, marketplaceUrl, version)
}
async function uninstallPackage(id: string): Promise<void> {
return rpcClient.uninstallPackage(id)
}
async function startPackage(id: string): Promise<void> {
return rpcClient.startPackage(id)
}
async function stopPackage(id: string): Promise<void> {
return rpcClient.stopPackage(id)
}
async function restartPackage(id: string): Promise<void> {
return rpcClient.restartPackage(id)
}
// Server actions
async function updateServer(marketplaceUrl: string): Promise<'updating' | 'no-updates'> {
return rpcClient.updateServer(marketplaceUrl)
}
async function restartServer(): Promise<void> {
return rpcClient.restartServer()
}
async function shutdownServer(): Promise<void> {
return rpcClient.shutdownServer()
}
async function getMetrics(): Promise<Record<string, unknown>> {
return rpcClient.getMetrics()
}
// Marketplace actions
async function getMarketplace(url: string): Promise<Record<string, unknown>> {
return rpcClient.getMarketplace(url)
}
function updateServerName(name: string) {
if (sync.data?.['server-info']) {
sync.data['server-info'].name = name
}
}
return {
// Computed
serverName,
isRestarting,
isShuttingDown,
isOffline,
// Install tracking (global, persists across navigation)
installingApps,
setInstallProgress,
clearInstallProgress,
isInstalling,
// Actions
installPackage,
uninstallPackage,
startPackage,
stopPackage,
restartPackage,
updateServer,
restartServer,
shutdownServer,
getMetrics,
getMarketplace,
updateServerName,
}
})