2026-03-08 17:03:21 +08:00
|
|
|
import React, { useState, useEffect, useCallback } from 'react'
|
2026-03-09 17:27:47 +08:00
|
|
|
import { Folder, FileText, ChevronLeft, RefreshCw, HardDrive } from 'lucide-react'
|
2026-03-08 01:34:54 +08:00
|
|
|
import { clsx } from 'clsx'
|
|
|
|
|
import type { FileItem } from '@/lib/api'
|
2026-03-09 17:27:47 +08:00
|
|
|
import { fetchSystemFiles, fetchSystemDrives } from '../../api'
|
2026-03-08 01:34:54 +08:00
|
|
|
|
|
|
|
|
interface LocalFilePanelProps {
|
|
|
|
|
selectedFile: FileItem | null
|
|
|
|
|
onSelect: (file: FileItem | null) => void
|
|
|
|
|
onUpload: () => void
|
|
|
|
|
disabled?: boolean
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const LocalFilePanel: React.FC<LocalFilePanelProps> = ({
|
|
|
|
|
selectedFile,
|
|
|
|
|
onSelect,
|
|
|
|
|
onUpload,
|
|
|
|
|
disabled,
|
|
|
|
|
}) => {
|
2026-03-08 17:03:21 +08:00
|
|
|
const [currentPath, setCurrentPath] = useState('')
|
|
|
|
|
const [files, setFiles] = useState<FileItem[]>([])
|
|
|
|
|
const [loading, setLoading] = useState(false)
|
|
|
|
|
const [pathHistory, setPathHistory] = useState<string[]>([''])
|
2026-03-09 17:27:47 +08:00
|
|
|
const [isAtDrives, setIsAtDrives] = useState(true)
|
2026-03-08 01:34:54 +08:00
|
|
|
|
2026-03-08 17:03:21 +08:00
|
|
|
const loadFiles = useCallback(async (systemPath: string) => {
|
2026-03-08 01:34:54 +08:00
|
|
|
setLoading(true)
|
|
|
|
|
try {
|
|
|
|
|
const items = await fetchSystemFiles(systemPath)
|
|
|
|
|
setFiles(items)
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error('Failed to load local files:', err)
|
|
|
|
|
} finally {
|
|
|
|
|
setLoading(false)
|
|
|
|
|
}
|
|
|
|
|
}, [])
|
|
|
|
|
|
2026-03-09 17:27:47 +08:00
|
|
|
const loadDrives = useCallback(async () => {
|
|
|
|
|
setLoading(true)
|
|
|
|
|
try {
|
|
|
|
|
const items = await fetchSystemDrives()
|
|
|
|
|
setFiles(items)
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error('Failed to load drives:', err)
|
|
|
|
|
} finally {
|
|
|
|
|
setLoading(false)
|
|
|
|
|
}
|
|
|
|
|
}, [])
|
|
|
|
|
|
2026-03-08 17:03:21 +08:00
|
|
|
useEffect(() => {
|
2026-03-09 17:27:47 +08:00
|
|
|
if (isAtDrives) {
|
|
|
|
|
loadDrives()
|
|
|
|
|
} else {
|
|
|
|
|
loadFiles(currentPath)
|
|
|
|
|
}
|
|
|
|
|
}, [currentPath, isAtDrives, loadFiles, loadDrives])
|
2026-03-08 01:34:54 +08:00
|
|
|
|
|
|
|
|
const handleGoBack = () => {
|
|
|
|
|
if (pathHistory.length > 1) {
|
|
|
|
|
const newHistory = [...pathHistory]
|
|
|
|
|
newHistory.pop()
|
|
|
|
|
const prevPath = newHistory[newHistory.length - 1]
|
|
|
|
|
setPathHistory(newHistory)
|
|
|
|
|
setCurrentPath(prevPath)
|
2026-03-09 17:27:47 +08:00
|
|
|
if (prevPath === '') {
|
|
|
|
|
setIsAtDrives(true)
|
|
|
|
|
}
|
2026-03-08 01:34:54 +08:00
|
|
|
onSelect(null)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-08 17:03:21 +08:00
|
|
|
const handleGoInto = (file: FileItem) => {
|
2026-03-09 17:27:47 +08:00
|
|
|
setIsAtDrives(false)
|
2026-03-08 17:03:21 +08:00
|
|
|
setPathHistory([...pathHistory, file.path])
|
|
|
|
|
setCurrentPath(file.path)
|
2026-03-08 01:34:54 +08:00
|
|
|
onSelect(null)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handleRefresh = () => {
|
2026-03-09 17:27:47 +08:00
|
|
|
if (isAtDrives) {
|
|
|
|
|
loadDrives()
|
|
|
|
|
} else {
|
|
|
|
|
loadFiles(currentPath)
|
|
|
|
|
}
|
2026-03-08 01:34:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const getDisplayPath = () => {
|
2026-03-09 17:27:47 +08:00
|
|
|
if (isAtDrives) return '本地磁盘'
|
2026-03-08 01:34:54 +08:00
|
|
|
const parts = currentPath.split(/[/\\]/)
|
|
|
|
|
return parts[parts.length - 1] || currentPath
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2026-03-08 17:03:21 +08:00
|
|
|
<div className="flex flex-col h-full border border-gray-200 dark:border-gray-700 overflow-hidden">
|
|
|
|
|
<div className="h-12 flex items-center justify-between px-4 bg-gray-50 dark:bg-gray-800 border-b border-gray-200 dark:border-gray-700">
|
2026-03-08 01:34:54 +08:00
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
<button
|
|
|
|
|
onClick={handleGoBack}
|
|
|
|
|
disabled={pathHistory.length <= 1}
|
|
|
|
|
className="p-1 rounded hover:bg-gray-200 dark:hover:bg-gray-700 disabled:opacity-30 disabled:cursor-not-allowed"
|
|
|
|
|
title="返回上级"
|
|
|
|
|
>
|
2026-03-08 17:03:21 +08:00
|
|
|
<ChevronLeft size={18} />
|
2026-03-08 01:34:54 +08:00
|
|
|
</button>
|
2026-03-08 17:03:21 +08:00
|
|
|
<span className="text-base font-medium text-gray-700 dark:text-gray-300 truncate max-w-[180px]">
|
2026-03-08 01:34:54 +08:00
|
|
|
{getDisplayPath()}
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
<button
|
|
|
|
|
onClick={handleRefresh}
|
|
|
|
|
disabled={loading}
|
2026-03-08 17:03:21 +08:00
|
|
|
className="p-1.5 rounded hover:bg-gray-200 dark:hover:bg-gray-700"
|
2026-03-08 01:34:54 +08:00
|
|
|
title="刷新"
|
|
|
|
|
>
|
2026-03-08 17:03:21 +08:00
|
|
|
<RefreshCw size={16} className={clsx(loading && 'animate-spin')} />
|
2026-03-08 01:34:54 +08:00
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
|
2026-03-08 17:03:21 +08:00
|
|
|
<div className="flex-1 overflow-y-auto p-2" onClick={(e) => {
|
|
|
|
|
if ((e.target as HTMLElement).classList.contains('space-y-1') || (e.target as HTMLElement).classList.contains('p-2')) {
|
|
|
|
|
onSelect(null)
|
|
|
|
|
}
|
|
|
|
|
}}>
|
2026-03-08 01:34:54 +08:00
|
|
|
{loading ? (
|
|
|
|
|
<div className="flex items-center justify-center h-full text-gray-400">
|
|
|
|
|
加载中...
|
|
|
|
|
</div>
|
|
|
|
|
) : files.length === 0 ? (
|
|
|
|
|
<div className="flex items-center justify-center h-full text-gray-400">
|
|
|
|
|
空文件夹
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<div className="space-y-1">
|
|
|
|
|
{files.map((file) => (
|
|
|
|
|
<div
|
|
|
|
|
key={file.path}
|
2026-03-08 17:03:21 +08:00
|
|
|
onClick={() => onSelect(selectedFile?.path === file.path ? null : file)}
|
2026-03-08 01:34:54 +08:00
|
|
|
onDoubleClick={() => file.type === 'dir' && handleGoInto(file)}
|
|
|
|
|
className={clsx(
|
2026-03-08 17:03:21 +08:00
|
|
|
'flex items-center gap-2 px-2 py-1.5 rounded-md cursor-pointer transition-colors text-sm',
|
2026-03-08 01:34:54 +08:00
|
|
|
selectedFile?.path === file.path
|
2026-03-08 17:03:21 +08:00
|
|
|
? 'bg-gray-100 text-gray-800 dark:bg-gray-700/60 dark:text-gray-200'
|
|
|
|
|
: 'hover:bg-gray-100 dark:hover:bg-gray-700/50 text-gray-700 dark:text-gray-200'
|
2026-03-08 01:34:54 +08:00
|
|
|
)}
|
|
|
|
|
>
|
2026-03-09 17:27:47 +08:00
|
|
|
{file.type === 'dir' || file.path.match(/^[A-Z]:\\?$/i) ? (
|
|
|
|
|
file.path.match(/^[A-Z]:\\?$/i) ? (
|
|
|
|
|
<HardDrive size={16} className="text-blue-500 dark:text-blue-400 shrink-0" />
|
|
|
|
|
) : (
|
|
|
|
|
<Folder size={16} className="text-gray-500 dark:text-gray-400 shrink-0" />
|
|
|
|
|
)
|
2026-03-08 01:34:54 +08:00
|
|
|
) : (
|
2026-03-08 17:03:21 +08:00
|
|
|
<FileText size={16} className="text-gray-500 dark:text-gray-400 shrink-0" />
|
2026-03-08 01:34:54 +08:00
|
|
|
)}
|
2026-03-08 17:03:21 +08:00
|
|
|
<span className="flex-1 truncate">
|
2026-03-08 01:34:54 +08:00
|
|
|
{file.name}
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="p-3 border-t border-gray-200 dark:border-gray-700 bg-gray-50 dark:bg-gray-800">
|
|
|
|
|
<button
|
|
|
|
|
onClick={onUpload}
|
|
|
|
|
disabled={!selectedFile || disabled}
|
2026-03-08 17:03:21 +08:00
|
|
|
className="w-full flex items-center justify-center gap-2 px-4 py-2 bg-gray-600 hover:bg-gray-700 disabled:bg-gray-400 dark:disabled:bg-gray-700 dark:disabled:text-gray-500 text-white rounded-lg transition-colors"
|
2026-03-08 01:34:54 +08:00
|
|
|
>
|
2026-03-08 17:03:21 +08:00
|
|
|
<span>↑</span>
|
2026-03-08 01:34:54 +08:00
|
|
|
上传
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|