Skip to content

Commit

Permalink
✨ 新增实例删除选项
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyomotoi committed Dec 26, 2023
1 parent 8d07fb8 commit 4a9df08
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 21 deletions.
6 changes: 5 additions & 1 deletion frontend/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,14 @@ class API extends CustomAPI {
});
}

async deleteProject(projectID: string): Promise<GenericResponse<string>> {
async deleteProject(
projectID: string,
deleteFully: boolean,
): Promise<GenericResponse<string>> {
return await this.request.delete("/project/delete", {
params: {
project_id: projectID,
delete_fully: deleteFully,
},
});
}
Expand Down
60 changes: 46 additions & 14 deletions frontend/src/components/HomePage/ProjectAction.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import api from "@/api";
import type { AxiosError } from "axios";
const oprateLock = ref(false);
const deleteProjectModal = ref<HTMLDialogElement>();
const oLock = () => {
oprateLock.value = true;
Expand Down Expand Up @@ -90,14 +91,15 @@ const restartProject = async () => {
oUnlock();
};
const deleteProject = async () => {
const deleteProject = async (deleteFully: boolean) => {
oLock();
const project = store().choiceProject;
await api
.deleteProject(project.project_id)
.deleteProject(project.project_id, deleteFully)
.then(async () => {
await getProjectList();
store().choiceProject = Object();
deleteProjectModal.value?.close();
oUnlock();
})
.catch((error: AxiosError) => {
Expand All @@ -114,6 +116,43 @@ const deleteProject = async () => {
</script>

<template>
<dialog ref="deleteProjectModal" class="modal">
<div class="modal-box rounded-lg">
<h2 class="font-bold text-lg">实例操作</h2>
<div class="py-4 text-sm">
<div>清除 - 仅删除其在 WebUI 上的信息</div>
<div>销毁 - 连同实例源代码一同删除</div>
<p class="font-bold py-4">注意:该操作不可逆</p>
</div>
<div class="flex gap-4 justify-end">
<label class="swap">
<input type="checkbox" />

<div class="swap-off btn h-10 min-h-0 rounded-lg" @click="deleteProject(false)">
清除实例信息
</div>
<div class="swap-on btn btn-primary h-10 min-h-0 rounded-lg text-white">
确定吗
</div>
</label>

<label class="swap">
<input type="checkbox" />

<div class="swap-off btn h-10 min-h-0 rounded-lg" @click="deleteProject(true)">
销毁整个实例
</div>
<div class="swap-on btn btn-primary h-10 min-h-0 rounded-lg text-white">
确定吗
</div>
</label>
</div>
</div>
<form method="dialog" class="modal-backdrop">
<button>close</button>
</form>
</dialog>

<div class="!mt-0 !mb-0 sm:m-8 md:m-12 lg:m-16 xl:m-20 2xl:m-28">
<Transition>
<div
Expand Down Expand Up @@ -156,22 +195,15 @@ const deleteProject = async () => {
<span class="material-symbols-outlined"> restart_alt </span>
</button>

<label
<button
:class="{
'swap btn btn-sm rounded hover:bg-[hsl(var(--bc)/0.1)] transition-all ease-in': true,
'btn btn-sm rounded hover:bg-[hsl(var(--bc)/0.1)] transition-all ease-in': true,
'btn-disabled': store().choiceProject.is_running || oprateLock,
}"
@click="deleteProjectModal?.showModal()"
>
<input type="checkbox" />
<span
title="销毁实例"
class="swap-off material-symbols-outlined"
@click="deleteProject()"
>
delete
</span>
<span class="swap-on material-symbols-outlined"> check </span>
</label>
<span class="swap-off material-symbols-outlined"> delete </span>
</button>
</div>
</div>
</div>
Expand Down
14 changes: 8 additions & 6 deletions nb_cli_plugin_webui/app/project/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,20 @@ async def get_project_profile(

@router.delete("/delete", response_model=GenericResponse[str])
async def delete_project(
delete_fully: bool = False,
project: NoneBotProjectManager = Depends(get_nonebot_project_manager),
) -> GenericResponse[str]:
"""
- 删除 NoneBot 实例
"""
data = project.read()
try:
shutil.rmtree(data.project_dir)
except OSError as err:
log.error(f"Delete nonebot project failed: {err}")
log.exception(err)
raise ProjectDeleteFailed()
if delete_fully:
try:
shutil.rmtree(data.project_dir)
except OSError as err:
log.error(f"Delete nonebot project failed: {err}")
log.exception(err)
raise ProjectDeleteFailed()
project.remove_project()
return GenericResponse(detail="success")

Expand Down

0 comments on commit 4a9df08

Please sign in to comment.