perf: improve system settings page

pull/3445/head
Ryan Wang 2022-09-10 23:24:58 +08:00
parent c278e85d6c
commit 9668c429f1
2 changed files with 33 additions and 31 deletions

View File

@ -1,20 +1,16 @@
<script lang="ts" setup> <script lang="ts" setup>
// core libs // core libs
import { computed, inject, onMounted, ref } from "vue"; import { computed, ref } from "vue";
// components // components
import { VButton } from "@halo-dev/components"; import { VButton } from "@halo-dev/components";
// types
import type { Ref } from "vue";
// hooks // hooks
import { useSettingForm } from "@halo-dev/admin-shared"; import { useSettingForm } from "@halo-dev/admin-shared";
import { useRouteParams } from "@vueuse/router";
const group = inject<Ref<string | undefined>>("activeTab"); const group = useRouteParams<string>("group");
const settingName = ref("system");
const configMapName = ref("system");
const { const {
settings, settings,
configMapFormData, configMapFormData,
@ -22,7 +18,7 @@ const {
handleFetchConfigMap, handleFetchConfigMap,
handleFetchSettings, handleFetchSettings,
handleSaveConfigMap, handleSaveConfigMap,
} = useSettingForm(settingName, configMapName); } = useSettingForm(ref("system"), ref("system"));
const formSchema = computed(() => { const formSchema = computed(() => {
if (!settings?.value?.spec) { if (!settings?.value?.spec) {
@ -32,10 +28,8 @@ const formSchema = computed(() => {
?.formSchema; ?.formSchema;
}); });
onMounted(async () => { await handleFetchSettings();
await handleFetchSettings(); await handleFetchConfigMap();
await handleFetchConfigMap();
});
</script> </script>
<template> <template>
<div class="bg-white p-4 sm:px-6"> <div class="bg-white p-4 sm:px-6">

View File

@ -1,7 +1,7 @@
<script lang="ts" setup> <script lang="ts" setup>
// core libs // core libs
import { onMounted, type Ref } from "vue"; import { nextTick, onMounted } from "vue";
import { provide, ref, watch } from "vue"; import { ref, watch } from "vue";
import { useRoute, useRouter } from "vue-router"; import { useRoute, useRouter } from "vue-router";
// types // types
@ -28,28 +28,25 @@ interface SettingTab {
const tabs = ref<SettingTab[]>([] as SettingTab[]); const tabs = ref<SettingTab[]>([] as SettingTab[]);
const activeTab = ref(""); const activeTab = ref("");
const settingName = ref("system");
const configMapName = ref("system");
const { settings, handleFetchSettings } = useSettingForm( const { settings, handleFetchSettings } = useSettingForm(
settingName, ref("system"),
configMapName ref("system")
); );
provide<Ref<string | undefined>>("activeTab", activeTab);
const route = useRoute(); const route = useRoute();
const router = useRouter(); const router = useRouter();
const handleTabChange = (id: string) => { const handleTabChange = (id: string) => {
const tab = tabs.value.find((item) => item.id === id); const tab = tabs.value.find((item) => item.id === id);
if (tab) { if (tab) {
activeTab.value = tab.id;
router.push(tab.route); router.push(tab.route);
} }
}; };
onMounted(async () => { onMounted(async () => {
await handleFetchSettings(); await handleFetchSettings();
if (settings.value && settings.value.spec) { if (settings.value && settings.value.spec) {
tabs.value = settings.value.spec.map((item: FormKitSettingSpec) => { tabs.value = settings.value.spec.map((item: FormKitSettingSpec) => {
return { return {
@ -63,14 +60,17 @@ onMounted(async () => {
}, },
}; };
}); });
onTabChange(route.name as string);
} }
await nextTick();
handleTriggerTabChange();
}); });
const onTabChange = (routeName: string) => { const handleTriggerTabChange = () => {
const tab = tabs.value.find((tab) => { const tab = tabs.value.find((tab) => {
return ( return (
tab.route.name === routeName && tab.route.name === route.name &&
tab.route.params?.group === route.params.group tab.route.params?.group === route.params.group
); );
}); });
@ -83,12 +83,9 @@ const onTabChange = (routeName: string) => {
activeTab.value = tabs.value[0].id; activeTab.value = tabs.value[0].id;
}; };
watch( watch([() => route.name, () => route.params], async () => {
() => route.name, handleTriggerTabChange();
async (newRouteName) => { });
onTabChange(newRouteName as string);
}
);
</script> </script>
<template> <template>
<BasicLayout> <BasicLayout>
@ -111,7 +108,18 @@ watch(
</template> </template>
</VCard> </VCard>
<div> <div>
<RouterView :key="activeTab" /> <RouterView :key="activeTab" v-slot="{ Component }">
<template v-if="Component">
<Suspense>
<component :is="Component"></component>
<template #fallback>
<div class="flex h-32 w-full justify-center bg-white">
<span class="text-sm text-gray-600">加载中...</span>
</div>
</template>
</Suspense>
</template>
</RouterView>
</div> </div>
</div> </div>
</BasicLayout> </BasicLayout>