feat(electron): add Electron UI components and layout integration
This commit is contained in:
62
dashboard/src/hooks/useBackendConnections.ts
Normal file
62
dashboard/src/hooks/useBackendConnections.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { useCallback, useEffect, useState } from 'react'
|
||||
|
||||
import { isElectron } from '@/lib/runtime'
|
||||
import type { BackendConnection } from '@/types/electron'
|
||||
|
||||
export function useBackendConnections() {
|
||||
const [backends, setBackends] = useState<BackendConnection[]>([])
|
||||
const [activeId, setActiveId] = useState<string | null>(null)
|
||||
const [loading, setLoading] = useState(true)
|
||||
|
||||
const refresh = useCallback(async () => {
|
||||
if (!isElectron()) return
|
||||
const [list, active] = await Promise.all([
|
||||
window.electronAPI!.getBackends(),
|
||||
window.electronAPI!.getActiveBackend(),
|
||||
])
|
||||
setBackends(list)
|
||||
setActiveId(active?.id ?? null)
|
||||
setLoading(false)
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
refresh()
|
||||
}, [refresh])
|
||||
|
||||
const addBackend = useCallback(async (conn: Omit<BackendConnection, 'id'>) => {
|
||||
if (!isElectron()) return
|
||||
await window.electronAPI!.addBackend(conn)
|
||||
await refresh()
|
||||
}, [refresh])
|
||||
|
||||
const updateBackend = useCallback(async (id: string, patch: Partial<BackendConnection>) => {
|
||||
if (!isElectron()) return
|
||||
await window.electronAPI!.updateBackend(id, patch)
|
||||
await refresh()
|
||||
}, [refresh])
|
||||
|
||||
const removeBackend = useCallback(async (id: string) => {
|
||||
if (!isElectron()) return
|
||||
await window.electronAPI!.removeBackend(id)
|
||||
await refresh()
|
||||
}, [refresh])
|
||||
|
||||
const switchBackend = useCallback(async (id: string) => {
|
||||
if (!isElectron()) return
|
||||
await window.electronAPI!.setActiveBackend(id)
|
||||
setActiveId(id)
|
||||
// 重新加载页面以使用新后端
|
||||
window.location.reload()
|
||||
}, [])
|
||||
|
||||
return {
|
||||
backends,
|
||||
activeId,
|
||||
loading,
|
||||
addBackend,
|
||||
updateBackend,
|
||||
removeBackend,
|
||||
switchBackend,
|
||||
refresh
|
||||
}
|
||||
}
|
||||
30
dashboard/src/hooks/useWindowControls.ts
Normal file
30
dashboard/src/hooks/useWindowControls.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { useCallback, useEffect, useState } from 'react'
|
||||
|
||||
import { isElectron } from '@/lib/runtime'
|
||||
|
||||
export function useWindowControls() {
|
||||
const [isMaximized, setIsMaximized] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
if (!isElectron()) return
|
||||
|
||||
const api = window.electronAPI
|
||||
if (!api) return
|
||||
|
||||
api.isMaximized().then(setIsMaximized)
|
||||
|
||||
const unsubMax = api.onWindowMaximized(() => setIsMaximized(true))
|
||||
const unsubUnmax = api.onWindowUnmaximized(() => setIsMaximized(false))
|
||||
|
||||
return () => {
|
||||
unsubMax?.()
|
||||
unsubUnmax?.()
|
||||
}
|
||||
}, [])
|
||||
|
||||
const minimize = useCallback(() => window.electronAPI?.minimizeWindow(), [])
|
||||
const toggleMaximize = useCallback(() => window.electronAPI?.maximizeWindow(), [])
|
||||
const close = useCallback(() => window.electronAPI?.closeWindow(), [])
|
||||
|
||||
return { close, isMaximized, minimize, toggleMaximize }
|
||||
}
|
||||
Reference in New Issue
Block a user