From b0687d27f6dca0446716b694300e46a292eedab8 Mon Sep 17 00:00:00 2001 From: an-lee Date: Mon, 11 Nov 2024 20:11:01 +0800 Subject: [PATCH] Fix document delete (#1176) --- enjoy/src/i18n/en.json | 2 ++ enjoy/src/i18n/zh-CN.json | 2 ++ enjoy/src/main/db/models/document.ts | 7 ++++ .../components/documents/document-card.tsx | 36 ++++++++++++++++--- .../documents/document-text-renderer.tsx | 2 -- .../documents/documents-segment.tsx | 3 ++ enjoy/src/renderer/pages/documents.tsx | 8 ++++- 7 files changed, 52 insertions(+), 8 deletions(-) diff --git a/enjoy/src/i18n/en.json b/enjoy/src/i18n/en.json index 48b1ffc9..4c828e7c 100644 --- a/enjoy/src/i18n/en.json +++ b/enjoy/src/i18n/en.json @@ -902,6 +902,8 @@ "bulkDeleteAborted": "Bulk delete aborted", "notFound": "Not found", "saved": "Saved", + "deleteDocumentConfirm": "Are you sure to delete this document?", + "documentDeletedSuccessfully": "Document deleted successfully", "autoTranslate": "Auto translate", "autoNextSpeech": "Auto speech", "failedToLoadLink": "Failed to load link", diff --git a/enjoy/src/i18n/zh-CN.json b/enjoy/src/i18n/zh-CN.json index e623f06e..43a8db7e 100644 --- a/enjoy/src/i18n/zh-CN.json +++ b/enjoy/src/i18n/zh-CN.json @@ -902,6 +902,8 @@ "bulkDeleteAborted": "批量删除已中止", "notFound": "未找到", "saved": "已保存", + "deleteDocumentConfirm": "您确定要删除此文档吗?", + "documentDeletedSuccessfully": "文档删除成功", "autoTranslate": "自动翻译", "autoNextSpeech": "连续朗读", "failedToLoadLink": "加载链接失败", diff --git a/enjoy/src/main/db/models/document.ts b/enjoy/src/main/db/models/document.ts index 0033c6ee..370fab76 100644 --- a/enjoy/src/main/db/models/document.ts +++ b/enjoy/src/main/db/models/document.ts @@ -198,6 +198,13 @@ export class Document extends Model { }); } + @AfterDestroy + static removeLocalFile(document: Document) { + if (document.filePath) { + fs.removeSync(document.filePath); + } + } + @AfterDestroy static async destroyRemote(document: Document) { const webApi = new Client({ diff --git a/enjoy/src/renderer/components/documents/document-card.tsx b/enjoy/src/renderer/components/documents/document-card.tsx index ff1fe604..a1601064 100644 --- a/enjoy/src/renderer/components/documents/document-card.tsx +++ b/enjoy/src/renderer/components/documents/document-card.tsx @@ -12,17 +12,23 @@ import { DropdownMenuContent, DropdownMenuItem, AlertDialogFooter, + AlertDialogCancel, + AlertDialogAction, + toast, } from "@renderer/components/ui"; import { MoreVerticalIcon, TrashIcon } from "lucide-react"; import { t } from "i18next"; -import { useState } from "react"; +import { useContext, useState } from "react"; +import { AppSettingsProviderContext } from "@renderer/context"; export const DocumentCard = (props: { document: DocumentEType; className?: string; + onDelete?: () => void; }) => { - const { document, className } = props; + const { document, className, onDelete } = props; const [deleting, setDeleting] = useState(false); + const { EnjoyApp } = useContext(AppSettingsProviderContext); const handleDelete = (event: React.MouseEvent) => { event.stopPropagation(); setDeleting(true); @@ -90,12 +96,32 @@ export const DocumentCard = (props: { {t("delete")} - {t("deleteConfirm")} + {t("deleteDocumentConfirm")} - - + {t("cancel")} + + + diff --git a/enjoy/src/renderer/components/documents/document-text-renderer.tsx b/enjoy/src/renderer/components/documents/document-text-renderer.tsx index 83238e63..fbede692 100644 --- a/enjoy/src/renderer/components/documents/document-text-renderer.tsx +++ b/enjoy/src/renderer/components/documents/document-text-renderer.tsx @@ -18,9 +18,7 @@ export const DocumentTextRenderer = () => { const fetchContent = async () => { const res = await fetch(document.src); - console.log("res", res); const text = await res.text(); - console.log("text", text); setContent(text); }; diff --git a/enjoy/src/renderer/components/documents/documents-segment.tsx b/enjoy/src/renderer/components/documents/documents-segment.tsx index 8737da06..64ca993c 100644 --- a/enjoy/src/renderer/components/documents/documents-segment.tsx +++ b/enjoy/src/renderer/components/documents/documents-segment.tsx @@ -45,6 +45,9 @@ export const DocumentsSegment = () => { key={document.id} document={document} className="w-48" + onDelete={() => + setDocuments(documents.filter((d) => d.id !== document.id)) + } /> ))} diff --git a/enjoy/src/renderer/pages/documents.tsx b/enjoy/src/renderer/pages/documents.tsx index 024890d4..2a6548e1 100644 --- a/enjoy/src/renderer/pages/documents.tsx +++ b/enjoy/src/renderer/pages/documents.tsx @@ -58,7 +58,13 @@ export default () => { ) : (
{documents.map((document) => ( - + + setDocuments(documents.filter((d) => d.id !== document.id)) + } + /> ))}
)}