2022-08-12 08:20:21 +00:00
|
|
|
<script lang="ts" setup>
|
|
|
|
import {
|
2022-10-18 01:58:09 +00:00
|
|
|
Dialog,
|
2024-05-21 07:10:44 +00:00
|
|
|
Toast,
|
2022-08-12 08:20:21 +00:00
|
|
|
VButton,
|
|
|
|
VCard,
|
2024-05-21 07:10:44 +00:00
|
|
|
VDropdownItem,
|
2022-08-22 08:49:07 +00:00
|
|
|
VEmpty,
|
2022-09-14 03:17:15 +00:00
|
|
|
VEntity,
|
|
|
|
VEntityField,
|
2022-11-24 03:46:10 +00:00
|
|
|
VLoading,
|
2024-05-21 07:10:44 +00:00
|
|
|
VSpace,
|
|
|
|
VStatusDot,
|
|
|
|
VTag,
|
2022-08-12 08:20:21 +00:00
|
|
|
} from "@halo-dev/components";
|
|
|
|
import MenuEditingModal from "./MenuEditingModal.vue";
|
2023-04-16 15:12:13 +00:00
|
|
|
import { onMounted, ref } from "vue";
|
2022-08-12 08:20:21 +00:00
|
|
|
import type { Menu } from "@halo-dev/api-client";
|
2024-06-25 04:31:44 +00:00
|
|
|
import { coreApiClient } from "@halo-dev/api-client";
|
2022-08-12 08:20:21 +00:00
|
|
|
import { useRouteQuery } from "@vueuse/router";
|
2022-09-30 09:48:19 +00:00
|
|
|
import { usePermission } from "@/utils/permission";
|
2023-03-23 08:54:33 +00:00
|
|
|
import { useI18n } from "vue-i18n";
|
2023-04-16 15:12:13 +00:00
|
|
|
import { useQuery } from "@tanstack/vue-query";
|
2022-09-30 09:48:19 +00:00
|
|
|
|
|
|
|
const { currentUserHasPermission } = usePermission();
|
2023-03-23 08:54:33 +00:00
|
|
|
const { t } = useI18n();
|
2022-08-12 08:20:21 +00:00
|
|
|
|
2022-08-15 09:56:13 +00:00
|
|
|
const props = withDefaults(
|
|
|
|
defineProps<{
|
2023-04-16 15:12:13 +00:00
|
|
|
selectedMenu?: Menu;
|
2022-08-15 09:56:13 +00:00
|
|
|
}>(),
|
|
|
|
{
|
2023-04-16 15:12:13 +00:00
|
|
|
selectedMenu: undefined,
|
2022-08-15 09:56:13 +00:00
|
|
|
}
|
|
|
|
);
|
2022-08-12 08:20:21 +00:00
|
|
|
|
2022-08-15 12:31:51 +00:00
|
|
|
const emit = defineEmits<{
|
|
|
|
(event: "update:selectedMenu", menu: Menu): void;
|
|
|
|
}>();
|
2022-08-12 08:20:21 +00:00
|
|
|
|
2022-09-30 09:48:19 +00:00
|
|
|
const selectedMenuToUpdate = ref<Menu>();
|
2022-08-12 08:20:21 +00:00
|
|
|
const menuEditingModal = ref<boolean>(false);
|
|
|
|
|
2023-04-16 15:12:13 +00:00
|
|
|
const {
|
|
|
|
data: menus,
|
|
|
|
isLoading,
|
|
|
|
refetch,
|
|
|
|
} = useQuery<Menu[]>({
|
|
|
|
queryKey: ["menus"],
|
|
|
|
queryFn: async () => {
|
2024-06-25 04:31:44 +00:00
|
|
|
const { data } = await coreApiClient.menu.listMenu({
|
2023-04-16 15:12:13 +00:00
|
|
|
page: 0,
|
|
|
|
size: 0,
|
|
|
|
});
|
|
|
|
return data.items;
|
|
|
|
},
|
|
|
|
onSuccess(data) {
|
2022-08-12 08:20:21 +00:00
|
|
|
if (props.selectedMenu) {
|
2023-04-16 15:12:13 +00:00
|
|
|
const updatedMenu = data?.find(
|
2022-08-12 08:20:21 +00:00
|
|
|
(menu) => menu.metadata.name === props.selectedMenu?.metadata.name
|
|
|
|
);
|
|
|
|
if (updatedMenu) {
|
|
|
|
emit("update:selectedMenu", updatedMenu);
|
|
|
|
}
|
|
|
|
}
|
2023-04-16 15:12:13 +00:00
|
|
|
},
|
|
|
|
refetchInterval(data) {
|
|
|
|
const deletingMenus = data?.filter(
|
2022-10-24 14:10:10 +00:00
|
|
|
(menu) => !!menu.metadata.deletionTimestamp
|
|
|
|
);
|
2023-07-19 03:44:12 +00:00
|
|
|
return deletingMenus?.length ? 1000 : false;
|
2023-04-16 15:12:13 +00:00
|
|
|
},
|
2022-10-24 14:10:10 +00:00
|
|
|
});
|
|
|
|
|
2022-08-12 08:20:21 +00:00
|
|
|
const menuQuery = useRouteQuery("menu");
|
|
|
|
const handleSelect = (menu: Menu) => {
|
|
|
|
emit("update:selectedMenu", menu);
|
|
|
|
menuQuery.value = menu.metadata.name;
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleDeleteMenu = async (menu: Menu) => {
|
2022-10-18 01:58:09 +00:00
|
|
|
Dialog.warning({
|
2023-03-23 08:54:33 +00:00
|
|
|
title: t("core.menu.operations.delete_menu.title"),
|
|
|
|
description: t("core.menu.operations.delete_menu.description"),
|
2022-08-12 08:20:21 +00:00
|
|
|
confirmType: "danger",
|
2023-03-23 08:54:33 +00:00
|
|
|
confirmText: t("core.common.buttons.confirm"),
|
|
|
|
cancelText: t("core.common.buttons.cancel"),
|
2022-08-12 08:20:21 +00:00
|
|
|
onConfirm: async () => {
|
|
|
|
try {
|
2024-06-25 04:31:44 +00:00
|
|
|
await coreApiClient.menu.deleteMenu({
|
refactor: method parameters of api client (halo-dev/console#605)
<!-- Thanks for sending a pull request! Here are some tips for you:
1. 如果这是你的第一次,请阅读我们的贡献指南:<https://github.com/halo-dev/halo/blob/master/CONTRIBUTING.md>。
1. If this is your first time, please read our contributor guidelines: <https://github.com/halo-dev/halo/blob/master/CONTRIBUTING.md>.
2. 请根据你解决问题的类型为 Pull Request 添加合适的标签。
2. Please label this pull request according to what type of issue you are addressing, especially if this is a release targeted pull request.
3. 请确保你已经添加并运行了适当的测试。
3. Ensure you have added or ran the appropriate tests for your PR.
-->
#### What type of PR is this?
/kind improvement
/milestone 2.0
<!--
添加其中一个类别:
Add one of the following kinds:
/kind bug
/kind cleanup
/kind documentation
/kind feature
/kind optimization
适当添加其中一个或多个类别(可选):
Optionally add one or more of the following kinds if applicable:
/kind api-change
/kind deprecation
/kind failing-test
/kind flake
/kind regression
-->
#### What this PR does / why we need it:
修改 api-client 的请求参数结构,改为所有参数由一个对象包裹,而不是将各个参数作为方法的参数,防止因为后端参数结构发生改变,或者生成 api-client 时参数顺序发生改变导致请求异常。如:
```diff
await apiClient.extension.storage.group.updatestorageHaloRunV1alpha1Group(
- formState.value.metadata.name,
- formState.value
+ {
+ name: formState.value.metadata.name,
+ group: formState.value,
+ }
);
```
#### Which issue(s) this PR fixes:
<!--
PR 合并时自动关闭 issue。
Automatically closes linked issue when PR is merged.
用法:`Fixes #<issue 号>`,或者 `Fixes (粘贴 issue 完整链接)`
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
-->
None
#### Screenshots:
<!--
如果此 PR 有 UI 的改动,最好截图说明这个 PR 的改动。
If there are UI changes to this PR, it is best to take a screenshot to illustrate the changes to this PR.
eg.
Before:

After:

-->
None
#### Special notes for your reviewer:
/cc @halo-dev/sig-halo-admin
#### Does this PR introduce a user-facing change?
<!--
如果当前 Pull Request 的修改不会造成用户侧的任何变更,在 `release-note` 代码块儿中填写 `NONE`。
否则请填写用户侧能够理解的 Release Note。如果当前 Pull Request 包含破坏性更新(Break Change),
Release Note 需要以 `action required` 开头。
If no, just write "NONE" in the release-note block below.
If yes, a release note is required:
Enter your extended release note in the block below. If the PR requires additional action from users switching to the new release, include the string "action required".
-->
```release-note
None
```
2022-09-06 02:26:11 +00:00
|
|
|
name: menu.metadata.name,
|
|
|
|
});
|
2022-08-12 08:20:21 +00:00
|
|
|
|
2023-04-16 15:12:13 +00:00
|
|
|
const deleteItemsPromises = menu.spec.menuItems?.map((item) =>
|
2024-06-25 04:31:44 +00:00
|
|
|
coreApiClient.menuItem.deleteMenuItem({
|
2023-04-16 15:12:13 +00:00
|
|
|
name: item,
|
|
|
|
})
|
2022-08-12 08:20:21 +00:00
|
|
|
);
|
|
|
|
|
2023-04-16 15:12:13 +00:00
|
|
|
if (!deleteItemsPromises) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-12 08:20:21 +00:00
|
|
|
await Promise.all(deleteItemsPromises);
|
2022-12-20 11:04:29 +00:00
|
|
|
|
2023-03-23 08:54:33 +00:00
|
|
|
Toast.success(t("core.common.toast.delete_success"));
|
2022-08-12 08:20:21 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error("Failed to delete menu", e);
|
|
|
|
} finally {
|
2023-04-16 15:12:13 +00:00
|
|
|
await refetch();
|
2022-08-12 08:20:21 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-09-30 09:48:19 +00:00
|
|
|
const handleOpenEditingModal = (menu?: Menu) => {
|
2022-08-12 08:20:21 +00:00
|
|
|
selectedMenuToUpdate.value = menu;
|
|
|
|
menuEditingModal.value = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
onMounted(async () => {
|
2023-04-16 15:12:13 +00:00
|
|
|
await refetch();
|
2022-08-12 08:20:21 +00:00
|
|
|
|
|
|
|
if (menuQuery.value) {
|
2023-04-16 15:12:13 +00:00
|
|
|
const menu = menus.value?.find((m) => m.metadata.name === menuQuery.value);
|
2022-08-12 08:20:21 +00:00
|
|
|
if (menu) {
|
|
|
|
handleSelect(menu);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-16 15:12:13 +00:00
|
|
|
if (menus.value?.length) {
|
2022-08-12 08:20:21 +00:00
|
|
|
handleSelect(menus.value[0]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-11-10 02:34:09 +00:00
|
|
|
// primary menu
|
2023-04-16 15:12:13 +00:00
|
|
|
const { data: primaryMenuName, refetch: refetchPrimaryMenuName } = useQuery({
|
|
|
|
queryKey: ["primary-menu-name"],
|
|
|
|
queryFn: async () => {
|
2024-06-25 04:31:44 +00:00
|
|
|
const { data } = await coreApiClient.configMap.getConfigMap({
|
2023-04-16 15:12:13 +00:00
|
|
|
name: "system",
|
|
|
|
});
|
2022-11-10 02:34:09 +00:00
|
|
|
|
2023-04-16 15:12:13 +00:00
|
|
|
if (!data.data?.menu) {
|
|
|
|
return "";
|
|
|
|
}
|
2022-11-10 02:34:09 +00:00
|
|
|
|
2023-04-16 15:12:13 +00:00
|
|
|
const menuConfig = JSON.parse(data.data.menu);
|
2022-11-10 02:34:09 +00:00
|
|
|
|
2023-04-16 15:12:13 +00:00
|
|
|
return menuConfig.primary;
|
|
|
|
},
|
|
|
|
});
|
2022-11-10 02:34:09 +00:00
|
|
|
|
|
|
|
const handleSetPrimaryMenu = async (menu: Menu) => {
|
2024-06-25 04:31:44 +00:00
|
|
|
const { data: systemConfigMap } = await coreApiClient.configMap.getConfigMap({
|
|
|
|
name: "system",
|
|
|
|
});
|
2022-11-10 02:34:09 +00:00
|
|
|
|
|
|
|
if (systemConfigMap.data) {
|
|
|
|
const menuConfigToUpdate = JSON.parse(systemConfigMap.data?.menu || "{}");
|
|
|
|
menuConfigToUpdate.primary = menu.metadata.name;
|
|
|
|
systemConfigMap.data["menu"] = JSON.stringify(menuConfigToUpdate);
|
|
|
|
|
2024-06-25 04:31:44 +00:00
|
|
|
await coreApiClient.configMap.updateConfigMap({
|
2022-11-10 02:34:09 +00:00
|
|
|
name: "system",
|
|
|
|
configMap: systemConfigMap,
|
|
|
|
});
|
|
|
|
}
|
2023-04-16 15:12:13 +00:00
|
|
|
await refetchPrimaryMenuName();
|
2022-12-20 11:04:29 +00:00
|
|
|
|
2023-03-23 08:54:33 +00:00
|
|
|
Toast.success(t("core.menu.operations.set_primary.toast_success"));
|
2022-11-10 02:34:09 +00:00
|
|
|
};
|
2022-08-12 08:20:21 +00:00
|
|
|
</script>
|
|
|
|
<template>
|
|
|
|
<MenuEditingModal
|
2024-05-21 07:10:44 +00:00
|
|
|
v-if="menuEditingModal"
|
2022-08-12 08:20:21 +00:00
|
|
|
:menu="selectedMenuToUpdate"
|
2024-05-21 07:10:44 +00:00
|
|
|
@close="menuEditingModal = false"
|
2022-09-04 09:25:31 +00:00
|
|
|
@created="handleSelect"
|
2022-08-12 08:20:21 +00:00
|
|
|
/>
|
2023-03-23 08:54:33 +00:00
|
|
|
<VCard :body-class="['!p-0']" :title="$t('core.menu.title')">
|
2023-04-16 15:12:13 +00:00
|
|
|
<VLoading v-if="isLoading" />
|
|
|
|
<Transition v-else-if="!menus?.length" appear name="fade">
|
2023-03-23 08:54:33 +00:00
|
|
|
<VEmpty
|
|
|
|
:message="$t('core.menu.empty.message')"
|
|
|
|
:title="$t('core.menu.empty.title')"
|
|
|
|
>
|
2022-11-24 03:46:10 +00:00
|
|
|
<template #actions>
|
|
|
|
<VSpace>
|
2023-04-16 15:12:13 +00:00
|
|
|
<VButton size="sm" @click="refetch()">
|
2023-03-23 08:54:33 +00:00
|
|
|
{{ $t("core.common.buttons.refresh") }}
|
|
|
|
</VButton>
|
2022-11-24 03:46:10 +00:00
|
|
|
</VSpace>
|
|
|
|
</template>
|
|
|
|
</VEmpty>
|
|
|
|
</Transition>
|
|
|
|
<Transition v-else appear name="fade">
|
|
|
|
<ul class="box-border h-full w-full divide-y divide-gray-100" role="list">
|
|
|
|
<li
|
|
|
|
v-for="(menu, index) in menus"
|
|
|
|
:key="index"
|
|
|
|
@click="handleSelect(menu)"
|
2022-08-12 08:20:21 +00:00
|
|
|
>
|
2022-11-24 03:46:10 +00:00
|
|
|
<VEntity
|
|
|
|
:is-selected="selectedMenu?.metadata.name === menu.metadata.name"
|
2022-09-30 09:48:19 +00:00
|
|
|
>
|
2022-11-24 03:46:10 +00:00
|
|
|
<template #start>
|
|
|
|
<VEntityField
|
|
|
|
:title="menu.spec?.displayName"
|
2023-03-23 08:54:33 +00:00
|
|
|
:description="
|
|
|
|
$t('core.menu.list.fields.items_count', {
|
|
|
|
count: menu.spec.menuItems?.length || 0,
|
|
|
|
})
|
|
|
|
"
|
2022-11-24 03:46:10 +00:00
|
|
|
>
|
|
|
|
<template v-if="menu.metadata.name === primaryMenuName" #extra>
|
2023-03-23 08:54:33 +00:00
|
|
|
<VTag>
|
|
|
|
{{ $t("core.menu.list.fields.primary") }}
|
|
|
|
</VTag>
|
2022-11-24 03:46:10 +00:00
|
|
|
</template>
|
|
|
|
</VEntityField>
|
|
|
|
</template>
|
|
|
|
<template #end>
|
|
|
|
<VEntityField v-if="menu.metadata.deletionTimestamp">
|
|
|
|
<template #description>
|
2023-03-23 08:54:33 +00:00
|
|
|
<VStatusDot
|
|
|
|
v-tooltip="$t('core.common.status.deleting')"
|
|
|
|
state="warning"
|
|
|
|
animate
|
|
|
|
/>
|
2022-11-24 03:46:10 +00:00
|
|
|
</template>
|
|
|
|
</VEntityField>
|
|
|
|
</template>
|
|
|
|
<template
|
|
|
|
v-if="currentUserHasPermission(['system:menus:manage'])"
|
|
|
|
#dropdownItems
|
2022-09-13 04:08:12 +00:00
|
|
|
>
|
2023-03-27 08:06:13 +00:00
|
|
|
<VDropdownItem @click="handleSetPrimaryMenu(menu)">
|
2023-03-23 08:54:33 +00:00
|
|
|
{{ $t("core.menu.operations.set_primary.button") }}
|
2023-03-27 08:06:13 +00:00
|
|
|
</VDropdownItem>
|
|
|
|
<VDropdownItem @click="handleOpenEditingModal(menu)">
|
2023-03-23 08:54:33 +00:00
|
|
|
{{ $t("core.common.buttons.edit") }}
|
2023-03-27 08:06:13 +00:00
|
|
|
</VDropdownItem>
|
|
|
|
<VDropdownItem type="danger" @click="handleDeleteMenu(menu)">
|
2023-03-23 08:54:33 +00:00
|
|
|
{{ $t("core.common.buttons.delete") }}
|
2023-03-27 08:06:13 +00:00
|
|
|
</VDropdownItem>
|
2022-11-24 03:46:10 +00:00
|
|
|
</template>
|
|
|
|
</VEntity>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</Transition>
|
2022-09-30 11:03:38 +00:00
|
|
|
<template v-if="currentUserHasPermission(['system:menus:manage'])" #footer>
|
2022-09-30 09:48:19 +00:00
|
|
|
<VButton block type="secondary" @click="handleOpenEditingModal()">
|
2023-03-23 08:54:33 +00:00
|
|
|
{{ $t("core.common.buttons.new") }}
|
2022-08-12 08:20:21 +00:00
|
|
|
</VButton>
|
|
|
|
</template>
|
|
|
|
</VCard>
|
|
|
|
</template>
|