mirror of
https://github.com/wu736139669/hapi.git
synced 2026-08-05 06:24:37 +00:00
fix(web): warn before creating missing session directories (#317)
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
import { useCallback, useEffect, useState } from 'react'
|
||||
import type { ApiClient } from '@/api/client'
|
||||
|
||||
export function useMachinePathsExists(
|
||||
api: ApiClient,
|
||||
machineId: string | null,
|
||||
paths: string[]
|
||||
): {
|
||||
pathExistence: Record<string, boolean>
|
||||
checkPathsExists: (pathsToCheck: string[]) => Promise<Record<string, boolean>>
|
||||
} {
|
||||
const [pathExistence, setPathExistence] = useState<Record<string, boolean>>({})
|
||||
|
||||
useEffect(() => {
|
||||
setPathExistence({})
|
||||
}, [machineId])
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false
|
||||
|
||||
if (!machineId || paths.length === 0) {
|
||||
setPathExistence({})
|
||||
return () => {
|
||||
cancelled = true
|
||||
}
|
||||
}
|
||||
|
||||
void api.checkMachinePathsExists(machineId, paths)
|
||||
.then((result) => {
|
||||
if (cancelled) return
|
||||
setPathExistence(result.exists ?? {})
|
||||
})
|
||||
.catch(() => {
|
||||
if (cancelled) return
|
||||
setPathExistence({})
|
||||
})
|
||||
|
||||
return () => {
|
||||
cancelled = true
|
||||
}
|
||||
}, [api, machineId, paths])
|
||||
|
||||
const checkPathsExists = useCallback(async (pathsToCheck: string[]) => {
|
||||
if (!machineId || pathsToCheck.length === 0) {
|
||||
return {}
|
||||
}
|
||||
|
||||
const result = await api.checkMachinePathsExists(machineId, pathsToCheck)
|
||||
const exists = result.exists ?? {}
|
||||
setPathExistence((current) => ({ ...current, ...exists }))
|
||||
return exists
|
||||
}, [api, machineId])
|
||||
|
||||
return {
|
||||
pathExistence,
|
||||
checkPathsExists,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user