mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-01 12:06:38 +08:00
feat: add STRM repair workflow
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
import type { FormEvent } from 'react'
|
||||
import { useState } from 'react'
|
||||
import toast from 'react-hot-toast'
|
||||
|
||||
import { strmAPI, type RepairSTRMResult } from '../api/strm'
|
||||
import { currentOrigin } from './strmPageModel'
|
||||
import { apiErrorMessage, isHTTPURL } from './strmPageUtils'
|
||||
|
||||
export function useStrmRepairForm() {
|
||||
const [baseURL, setBaseURL] = useState(currentOrigin())
|
||||
const [outputDir, setOutputDir] = useState('data/strm/tree')
|
||||
const [runningMode, setRunningMode] = useState<'repair' | 'preview' | null>(null)
|
||||
const [result, setResult] = useState<RepairSTRMResult | null>(null)
|
||||
|
||||
const runRepair = async (dryRun: boolean) => {
|
||||
const trimmedBaseURL = baseURL.trim()
|
||||
if (!outputDir.trim()) {
|
||||
toast.error('请填写 STRM 输出目录')
|
||||
return
|
||||
}
|
||||
if (trimmedBaseURL && !isHTTPURL(trimmedBaseURL)) {
|
||||
toast.error('域名必须以 http:// 或 https:// 开头')
|
||||
return
|
||||
}
|
||||
setRunningMode(dryRun ? 'preview' : 'repair')
|
||||
try {
|
||||
const next = await strmAPI.repair({
|
||||
output_dir: outputDir.trim(),
|
||||
base_url: trimmedBaseURL.replace(/\/+$/, '') || undefined,
|
||||
dry_run: dryRun,
|
||||
})
|
||||
setResult(next)
|
||||
if (dryRun) {
|
||||
toast.success(`预检完成:可修复 ${next.previewed ?? 0} 个 · 跳过 ${next.skipped}`)
|
||||
} else {
|
||||
toast.success(`修复完成:修复 ${next.repaired} 个 · 跳过 ${next.skipped}`)
|
||||
}
|
||||
} catch (err: unknown) {
|
||||
toast.error(apiErrorMessage(err, dryRun ? 'STRM 修复预检失败' : 'STRM 修复失败'))
|
||||
} finally {
|
||||
setRunningMode(null)
|
||||
}
|
||||
}
|
||||
|
||||
const onRepair = async (event: FormEvent) => {
|
||||
event.preventDefault()
|
||||
await runRepair(false)
|
||||
}
|
||||
|
||||
const onPreview = async () => {
|
||||
await runRepair(true)
|
||||
}
|
||||
|
||||
return {
|
||||
baseURL,
|
||||
outputDir,
|
||||
repairing: runningMode !== null,
|
||||
result,
|
||||
runningMode,
|
||||
onPreview,
|
||||
onRepair,
|
||||
setBaseURL,
|
||||
setOutputDir,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user