[fix] powershell black screens and try to solve update re-start not working
This commit is contained in:
@@ -3,10 +3,8 @@ import { Card, CardBody, CardHeader, CardIcon, CardTool } from "@/components/win
|
||||
import { ToolButton } from "@/components/window/ToolButton"
|
||||
import { Chip, Skeleton } from "@heroui/react"
|
||||
import { Refresh, SettingConfig } from "@icon-park/react"
|
||||
// import { version } from "@tauri-apps/plugin-os"
|
||||
import { type AllSystemInfo, allSysInfo } from "tauri-plugin-system-info-api"
|
||||
import { invoke } from "@tauri-apps/api/core"
|
||||
import useSWR, { useSWRConfig } from "swr"
|
||||
import { useHardwareStore } from "@/store/hardware"
|
||||
import { useEffect, useState } from "react"
|
||||
|
||||
export default function Page() {
|
||||
return (
|
||||
@@ -36,135 +34,47 @@ export default function Page() {
|
||||
}
|
||||
|
||||
function HardwareInfo() {
|
||||
const { mutate } = useSWRConfig()
|
||||
const { refreshHardwareInfo } = useHardwareStore()
|
||||
const [isRefreshing, setIsRefreshing] = useState(false)
|
||||
|
||||
return (
|
||||
<ToolButton
|
||||
onClick={() => {
|
||||
// 使用 SWR 的 mutate 来刷新数据
|
||||
mutate("/api/hardware-info")
|
||||
onClick={async () => {
|
||||
setIsRefreshing(true)
|
||||
try {
|
||||
await refreshHardwareInfo()
|
||||
} finally {
|
||||
setIsRefreshing(false)
|
||||
}
|
||||
}}
|
||||
disabled={isRefreshing}
|
||||
>
|
||||
<Refresh /> 刷新
|
||||
</ToolButton>
|
||||
)
|
||||
}
|
||||
|
||||
interface ComputerInfo {
|
||||
OsName?: string
|
||||
OSDisplayVersion?: string
|
||||
BiosSMBIOSBIOSVersion?: string
|
||||
CsManufacturer?: string
|
||||
CsName?: string
|
||||
ReleaseId?: string
|
||||
}
|
||||
|
||||
interface GpuInfo {
|
||||
vendor: string
|
||||
model: string
|
||||
family: string
|
||||
device_id: string
|
||||
total_vram: number
|
||||
used_vram: number
|
||||
load_pct: number
|
||||
temperature: number
|
||||
}
|
||||
|
||||
interface MemoryInfo {
|
||||
capacity?: number // 容量(字节)
|
||||
manufacturer?: string
|
||||
speed?: number // MHz,实际频率 ConfiguredClockSpeed
|
||||
default_speed?: number // MHz,默认频率 Speed(如果存在)
|
||||
}
|
||||
|
||||
interface MonitorInfo {
|
||||
name?: string
|
||||
refresh_rate?: number // Hz
|
||||
resolution_width?: number
|
||||
resolution_height?: number
|
||||
}
|
||||
|
||||
interface MotherboardInfo {
|
||||
manufacturer?: string // 制造商
|
||||
model?: string // 型号
|
||||
version?: string
|
||||
}
|
||||
|
||||
interface HardwareData {
|
||||
allSysData: AllSystemInfo
|
||||
computerInfo: ComputerInfo
|
||||
gpuInfo: GpuInfo | null
|
||||
memoryInfo: MemoryInfo[]
|
||||
monitorInfo: MonitorInfo[]
|
||||
motherboardInfo: MotherboardInfo | null
|
||||
}
|
||||
|
||||
// 硬件信息 fetcher
|
||||
const hardwareInfoFetcher = async (): Promise<HardwareData> => {
|
||||
// 并行获取系统信息、PowerShell 信息、GPU 信息、内存信息、显示器信息和主板信息
|
||||
const [sys, computerInfoData, gpuInfoData, memoryInfoData, monitorInfoData, motherboardInfoData] =
|
||||
await Promise.all([
|
||||
allSysInfo(),
|
||||
invoke<ComputerInfo>("get_computer_info").catch((error) => {
|
||||
console.error("获取 PowerShell 信息失败:", error)
|
||||
return {} as ComputerInfo
|
||||
}),
|
||||
invoke<GpuInfo | null>("get_gpu_info").catch((error) => {
|
||||
console.error("获取 GPU 信息失败:", error)
|
||||
return null
|
||||
}),
|
||||
invoke<MemoryInfo[]>("get_memory_info").catch((error) => {
|
||||
console.error("获取内存信息失败:", error)
|
||||
return [] as MemoryInfo[]
|
||||
}),
|
||||
invoke<MonitorInfo[]>("get_monitor_info").catch((error) => {
|
||||
console.error("获取显示器信息失败:", error)
|
||||
return [] as MonitorInfo[]
|
||||
}),
|
||||
invoke<MotherboardInfo>("get_motherboard_info").catch((error) => {
|
||||
console.error("获取主板信息失败:", error)
|
||||
return { manufacturer: undefined, model: undefined, version: undefined } as MotherboardInfo
|
||||
}),
|
||||
])
|
||||
|
||||
console.log("系统信息:", sys)
|
||||
console.log("PowerShell 信息:", computerInfoData)
|
||||
console.log("GPU 信息:", gpuInfoData)
|
||||
console.log("内存信息:", memoryInfoData)
|
||||
console.log("显示器信息:", monitorInfoData)
|
||||
console.log("主板信息:", motherboardInfoData)
|
||||
|
||||
if (sys?.cpus) {
|
||||
console.log("CPU数据:", sys.cpus)
|
||||
console.log("第一个CPU:", sys.cpus[0])
|
||||
if (sys.cpus[0]) {
|
||||
console.log("CPU字段:", Object.keys(sys.cpus[0]))
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
allSysData: sys,
|
||||
computerInfo: computerInfoData,
|
||||
gpuInfo: gpuInfoData,
|
||||
memoryInfo: memoryInfoData,
|
||||
monitorInfo: monitorInfoData,
|
||||
motherboardInfo: motherboardInfoData,
|
||||
}
|
||||
}
|
||||
|
||||
function HardwareInfoContent() {
|
||||
const { data, isLoading, isValidating } = useSWR<HardwareData>("/api/hardware-info", hardwareInfoFetcher, {
|
||||
revalidateOnFocus: false,
|
||||
revalidateOnReconnect: false,
|
||||
dedupingInterval: 5 * 60 * 1000, // 5分钟内相同请求去重
|
||||
})
|
||||
const { state, fetchHardwareInfo } = useHardwareStore()
|
||||
const [isLoading, setIsLoading] = useState(!state.allSysData)
|
||||
|
||||
const allSysData = data?.allSysData
|
||||
const computerInfo = data?.computerInfo || {}
|
||||
const gpuInfo = data?.gpuInfo
|
||||
const memoryInfo = data?.memoryInfo || []
|
||||
const monitorInfo = data?.monitorInfo || []
|
||||
const motherboardInfo = data?.motherboardInfo
|
||||
useEffect(() => {
|
||||
// 如果数据不存在,则加载数据
|
||||
if (!state.allSysData) {
|
||||
setIsLoading(true)
|
||||
void fetchHardwareInfo().finally(() => {
|
||||
setIsLoading(false)
|
||||
})
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, []) // 只在组件挂载时执行一次,state 是响应式的,不需要作为依赖
|
||||
|
||||
const allSysData = state.allSysData
|
||||
const computerInfo = state.computerInfo || {}
|
||||
const gpuInfo = state.gpuInfo
|
||||
const memoryInfo = state.memoryInfo || []
|
||||
const monitorInfo = state.monitorInfo || []
|
||||
const motherboardInfo = state.motherboardInfo
|
||||
|
||||
const formatBytes = (bytes?: number) => {
|
||||
if (!bytes) return "未知"
|
||||
@@ -243,8 +153,8 @@ function HardwareInfoContent() {
|
||||
})()
|
||||
: null
|
||||
|
||||
// 如果正在加载或正在验证(包括刷新),显示 Skeleton 骨架屏
|
||||
if (isLoading || isValidating) {
|
||||
// 如果正在加载,显示 Skeleton 骨架屏
|
||||
if (isLoading || !allSysData) {
|
||||
return (
|
||||
<div className="flex flex-col gap-4">
|
||||
{/* 系统信息 Skeleton */}
|
||||
|
||||
Reference in New Issue
Block a user