mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-04 16:49:56 +08:00
148 lines
4.8 KiB
TypeScript
148 lines
4.8 KiB
TypeScript
'use client'
|
|
|
|
import { ArrowLeft, FileText, Folder, RefreshCw } from 'lucide-react'
|
|
import { useEffect, useState } from 'react'
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
import { listImaFiles, type ImaFileEntry, type ImaFilesPage } from '@/features/knowledge/api/files'
|
|
|
|
const buttonClass =
|
|
'inline-flex items-center gap-2 rounded-md border border-[var(--border)] px-3 py-1.5 text-sm hover:bg-[var(--muted)] disabled:opacity-50'
|
|
|
|
function mergeEntries(previous: ImaFileEntry[], next: ImaFileEntry[]) {
|
|
return [...new Map([...previous, ...next].map(entry => [entry.id, entry])).values()]
|
|
}
|
|
|
|
export default function ImaFilesTab({ kbName, title }: { kbName: string; title: string }) {
|
|
const { t } = useTranslation()
|
|
const [path, setPath] = useState<ImaFileEntry[]>([])
|
|
const [revision, setRevision] = useState(0)
|
|
const folderId = path.at(-1)?.id ?? ''
|
|
|
|
return (
|
|
<div className="flex h-full min-h-0 flex-col gap-3 overflow-y-auto p-4">
|
|
<div className="flex flex-wrap items-center gap-2">
|
|
{path.length > 0 && (
|
|
<button type="button" className={buttonClass} onClick={() => setPath(path.slice(0, -1))}>
|
|
<ArrowLeft aria-hidden="true" className="h-4 w-4" />
|
|
{t('Back')}
|
|
</button>
|
|
)}
|
|
<p className="min-w-0 flex-1 break-words text-sm font-medium">
|
|
{[title, ...path.map(folder => folder.name)].join(' / ')}
|
|
</p>
|
|
<button
|
|
type="button"
|
|
className={buttonClass}
|
|
onClick={() => setRevision(value => value + 1)}
|
|
>
|
|
<RefreshCw aria-hidden="true" className="h-4 w-4" />
|
|
{t('Refresh')}
|
|
</button>
|
|
</div>
|
|
<ImaFolderContents
|
|
key={`${folderId}:${revision}`}
|
|
kbName={kbName}
|
|
folderId={folderId}
|
|
onOpen={folder => setPath([...path, folder])}
|
|
/>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
function ImaFolderContents({
|
|
kbName,
|
|
folderId,
|
|
onOpen,
|
|
}: {
|
|
kbName: string
|
|
folderId: string
|
|
onOpen: (folder: ImaFileEntry) => void
|
|
}) {
|
|
const { t } = useTranslation()
|
|
const [page, setPage] = useState<ImaFilesPage | null>(null)
|
|
const [error, setError] = useState('')
|
|
const [loadingMore, setLoadingMore] = useState(false)
|
|
|
|
useEffect(() => {
|
|
const controller = new AbortController()
|
|
void listImaFiles(kbName, folderId, '', controller.signal)
|
|
.then(setPage)
|
|
.catch((cause: unknown) => {
|
|
if (!controller.signal.aborted)
|
|
setError(cause instanceof Error ? cause.message : String(cause))
|
|
})
|
|
return () => controller.abort()
|
|
}, [kbName, folderId])
|
|
|
|
async function loadMore() {
|
|
if (!page?.next_cursor || loadingMore) return
|
|
setLoadingMore(true)
|
|
setError('')
|
|
try {
|
|
const next = await listImaFiles(kbName, folderId, page.next_cursor)
|
|
setPage({
|
|
folders: mergeEntries(page.folders, next.folders),
|
|
files: mergeEntries(page.files, next.files),
|
|
next_cursor: next.next_cursor,
|
|
})
|
|
} catch (cause) {
|
|
setError(cause instanceof Error ? cause.message : String(cause))
|
|
} finally {
|
|
setLoadingMore(false)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="space-y-3">
|
|
{error && (
|
|
<p role="alert" className="text-sm text-[var(--destructive)]">
|
|
{error}
|
|
</p>
|
|
)}
|
|
{!page && !error && (
|
|
<p role="status" className="text-sm">
|
|
{t('Loading')}
|
|
</p>
|
|
)}
|
|
{page && (
|
|
<>
|
|
{!page.files.length && !page.folders.length && !page.next_cursor && (
|
|
<p className="text-sm text-[var(--muted-foreground)]">{t('This folder is empty')}</p>
|
|
)}
|
|
<ul className="divide-y divide-[var(--border)]">
|
|
{page.folders.map(folder => (
|
|
<li key={`folder:${folder.id}`}>
|
|
<button
|
|
type="button"
|
|
onClick={() => onOpen(folder)}
|
|
className="flex w-full items-center gap-2 rounded-md px-2 py-2 text-left text-sm hover:bg-[var(--muted)]"
|
|
>
|
|
<Folder aria-hidden="true" className="h-4 w-4 shrink-0" />
|
|
<span className="break-all">{folder.name}</span>
|
|
</button>
|
|
</li>
|
|
))}
|
|
{page.files.map(file => (
|
|
<li key={`file:${file.id}`} className="flex items-center gap-2 px-2 py-2 text-sm">
|
|
<FileText aria-hidden="true" className="h-4 w-4 shrink-0" />
|
|
<span className="break-all">{file.name}</span>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
{page.next_cursor && (
|
|
<button
|
|
type="button"
|
|
className={buttonClass}
|
|
disabled={loadingMore}
|
|
onClick={() => void loadMore()}
|
|
>
|
|
{t(loadingMore ? 'Loading' : 'Load more')}
|
|
</button>
|
|
)}
|
|
</>
|
|
)}
|
|
</div>
|
|
)
|
|
}
|