import { useCallback, useEffect, useState } from "react"; import { RotateCcw, Trash2 } from "lucide-react"; import { useTranslation } from "@termix/plugin-sdk/frontend"; import { toast } from "sonner"; import { Button, Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "@termix/plugin-sdk/ui"; import { emptySSHTrash, getSSHTrash, permanentlyDeleteSSHTrashItem, restoreSSHTrashItem, updateSSHTrashRetention, type TrashItem, } from "./api/ssh-file-operations-api"; export function FileManagerTrashDialog({ open, onOpenChange, sessionId, onChanged, }: { open: boolean; onOpenChange: (open: boolean) => void; sessionId: string | null; onChanged: () => void; }) { const { t } = useTranslation(); const [items, setItems] = useState([]); const [retentionDays, setRetentionDays] = useState(7); const [canManageRetention, setCanManageRetention] = useState(false); const [loading, setLoading] = useState(false); const load = useCallback(async () => { if (!sessionId) return; setLoading(true); try { const result = await getSSHTrash(sessionId); setItems(result.items); setRetentionDays(result.retentionDays); setCanManageRetention(result.canManageRetention); } catch { toast.error(t("fileManager.trashLoadFailed")); } finally { setLoading(false); } }, [sessionId, t]); useEffect(() => { if (open) void load(); }, [load, open]); async function restore(item: TrashItem) { if (!sessionId) return; try { await restoreSSHTrashItem(sessionId, item.id); setItems((current) => current.filter((entry) => entry.id !== item.id)); onChanged(); toast.success(t("fileManager.trashRestored", { name: item.name })); } catch { toast.error(t("fileManager.trashRestoreFailed")); } } async function remove(item: TrashItem) { if (!sessionId) return; if ( !window.confirm(t("fileManager.trashDeleteConfirm", { name: item.name })) ) return; try { await permanentlyDeleteSSHTrashItem(sessionId, item.id); setItems((current) => current.filter((entry) => entry.id !== item.id)); toast.success( t("fileManager.trashPermanentlyDeleted", { name: item.name }), ); } catch { toast.error(t("fileManager.trashDeleteFailed")); } } async function empty() { if (!sessionId || items.length === 0) return; if (!window.confirm(t("fileManager.trashEmptyConfirm"))) return; try { await emptySSHTrash(sessionId); setItems([]); toast.success(t("fileManager.trashEmptied")); } catch { toast.error(t("fileManager.trashDeleteFailed")); } } return ( {t("fileManager.trash")} {t("fileManager.trashDescription", { days: retentionDays })}{" "} {t("hosts.docsLink")} {canManageRetention && (
{t("fileManager.trashRetention")} setRetentionDays(Number(event.target.value))} className="h-8 w-20 border border-border bg-background px-2" />
)}
{loading ? (
{t("common.loading")}
) : items.length === 0 ? (
{t("fileManager.trashEmpty")}
) : ( items.map((item) => (
{item.name}
{item.originalPath}
{new Date(item.deletedAt).toLocaleString()}
)) )}
); }