'use client' import { useState } from 'react' import { Copy, FolderOpen } from 'lucide-react' import { useTranslation } from 'react-i18next' import { apiFetch } from '@/lib/api' import { formatBytes } from '@/lib/doc-attachments' import { ConfirmDialog } from '@/components/ui/ConfirmDialog' interface RenderStorage { file_path: string file_bytes: number cache_path?: string cache_bytes?: number cache_files?: number fingerprint: string can_reveal: boolean } async function request(path: string, init?: RequestInit) { const response = await apiFetch(path, init) const body = await response.json() if (!response.ok) throw new Error(body.detail || response.statusText) return body } export function OutputFileActions({ url, kind }: { url: string; kind: 'render' | 'generated' }) { const render = kind === 'render' const [removed, setRemoved] = useState(false) const { t } = useTranslation() const [open, setOpen] = useState(false) const [storage, setStorage] = useState(null) const [busy, setBusy] = useState(false) const [confirm, setConfirm] = useState(false) const [error, setError] = useState('') const [message, setMessage] = useState('') // 只对当前应用发布的工作区文件提供管理操作。 const asset = URL.canParse(url, 'http://workspace.local') ? new URL(url, 'http://workspace.local') : null const path = asset?.pathname ?? '' const endpoint = (action: string) => `${path}/${action}${asset?.search ?? ''}` const supported = url.startsWith('/files/') && /^\/files\/workspace-items\/ws_[0-9a-f]{32}\/wsi_[0-9a-f]{32}$/.test(path) if (!supported) return null async function load(): Promise { setBusy(true) setError('') try { const result = await request(endpoint(render ? 'render-storage' : 'file-storage')) setStorage(result) return result } catch (cause) { setError(cause instanceof Error ? t(cause.message) : t('Unable to load file storage.')) return null } finally { setBusy(false) } } async function clean() { if (!storage) return setBusy(true) setError('') try { const result = await request(endpoint(render ? 'clean-render-cache' : 'remove-generated-output'), { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ fingerprint: storage.fingerprint }), }) if (render) { setStorage({ ...storage, ...result }) setMessage(t('Freed {{size}}. Final output is kept.', { size: formatBytes(result.freed_bytes) })) } else { setRemoved(true) setMessage(t('Workspace file removed. The chat attachment is still available.')) } } catch (cause) { setError(cause instanceof Error ? t(cause.message) : t('Unable to clean render files.')) } finally { setConfirm(false) setBusy(false) } } const buttonClass = 'rounded-md border border-[var(--border)] px-2 py-1 text-[12px] hover:bg-[var(--background)] disabled:opacity-40' return (
{open ? (
{busy && !storage ?

{t('Loading file storage…')}

: null} {error ? (

{error}

) : null} {message ?

{message}

: null} {storage ? ( <>
{storage.can_reveal && !removed ? ( ) : null}
{render &&

{t('Render files: {{count}} files · {{size}}', { count: storage.cache_files ?? 0, size: formatBytes(storage.cache_bytes ?? 0), })}

}

{render ? t('Cleanup keeps the final output, source code, and chat playback.') : t('Removing the workspace copy keeps the chat attachment available.')}

{!removed && } ) : !busy ? ( ) : null}
) : null} setConfirm(false)} onConfirm={() => void clean()} > {render ? <>

{t('Remove {{count}} render files and free {{size}}?', { count: storage?.cache_files ?? 0, size: formatBytes(storage?.cache_bytes ?? 0), })}

{t('Cleanup keeps the final output, source code, and chat playback.')}

:

{t('Remove the original generated file from the workspace? The chat attachment stays available.')}

}
) }