refactor: better breadcrumbs #258

This commit is contained in:
Jacky 2024-07-24 19:16:48 +08:00
parent b9c7d1d001
commit 532d6e83c5
No known key found for this signature in database
GPG key ID: 215C21B10DF38B4D
7 changed files with 35 additions and 16 deletions

View file

@ -1,30 +1,41 @@
<script setup lang="ts"> <script setup lang="ts">
import { useRoute } from 'vue-router'
interface bread { interface bread {
name: () => string name: string
translatedName: () => string
path: string path: string
hasChildren?: boolean
} }
const name = ref() const name = ref()
const route = useRoute() const route = useRoute()
const router = useRouter()
const breadList = computed(() => { const breadList = computed(() => {
const _breadList: bread[] = [] const result: bread[] = []
name.value = route.meta.name name.value = route.meta.name
route.matched.forEach(item => { route.matched.forEach(item => {
// item.name !== 'index' && this.breadList.push(item) if (item.meta?.lastRouteName) {
_breadList.push({ const lastRoute = router.resolve({ name: item.meta.lastRouteName })
name: item.meta.name as never as () => string,
result.push({
name: lastRoute.name as string,
translatedName: lastRoute.meta.name as never as () => string,
path: lastRoute.path,
})
}
result.push({
name: item.name as string,
translatedName: item.meta.name as never as () => string,
path: item.path, path: item.path,
hasChildren: item.children?.length > 0,
}) })
}) })
return _breadList return result
}) })
</script> </script>
<template> <template>
@ -34,12 +45,13 @@ const breadList = computed(() => {
:key="item.name" :key="item.name"
> >
<RouterLink <RouterLink
v-if="item.name !== name && index !== 1" v-if="index === 0 || !item.hasChildren && index !== breadList.length - 1"
:to="{ path: item.path === '' ? '/' : item.path }" :to="{ path: item.path === '' ? '/' : item.path }"
> >
{{ item.name() }} {{ item.translatedName() }}
</RouterLink> </RouterLink>
<span v-else>{{ item.name() }}</span> <span v-else-if="item.hasChildren">{{ item.translatedName() }}</span>
<span v-else>{{ item.translatedName() }}</span>
</ABreadcrumbItem> </ABreadcrumbItem>
</ABreadcrumb> </ABreadcrumb>
</template> </template>

View file

@ -4,7 +4,7 @@ import type { Environment } from '@/api/environment'
import environment from '@/api/environment' import environment from '@/api/environment'
const props = defineProps<{ const props = defineProps<{
target: number[] target?: number[]
map?: Record<number, string> map?: Record<number, string>
hiddenLocal?: boolean hiddenLocal?: boolean
}>() }>()
@ -37,7 +37,7 @@ const value = computed({
}, },
set(v) { set(v) {
if (typeof props.map === 'object') { if (typeof props.map === 'object') {
v.forEach(id => { v?.forEach(id => {
if (id !== 0) if (id !== 0)
emit('update:map', { ...props.map, [id]: data_map.value[id].name }) emit('update:map', { ...props.map, [id]: data_map.value[id].name })
}) })

View file

@ -1,5 +1,4 @@
<script setup lang="ts"> <script setup lang="ts">
import { useRoute } from 'vue-router'
import Breadcrumb from '@/components/Breadcrumb/Breadcrumb.vue' import Breadcrumb from '@/components/Breadcrumb/Breadcrumb.vue'
const route = useRoute() const route = useRoute()
@ -9,7 +8,7 @@ const display = computed(() => {
}) })
const name = computed(() => { const name = computed(() => {
return (route.meta.name as never as () => string)() return (route.meta.name as () => string)()
}) })
</script> </script>

View file

@ -62,6 +62,7 @@ export const routes: RouteRecordRaw[] = [
component: () => import('@/views/domain/DomainAdd.vue'), component: () => import('@/views/domain/DomainAdd.vue'),
meta: { meta: {
name: () => $gettext('Add Site'), name: () => $gettext('Add Site'),
lastRouteName: 'Sites List',
}, },
}, { }, {
path: ':name', path: ':name',
@ -70,6 +71,7 @@ export const routes: RouteRecordRaw[] = [
meta: { meta: {
name: () => $gettext('Edit Site'), name: () => $gettext('Edit Site'),
hiddenInSidebar: true, hiddenInSidebar: true,
lastRouteName: 'Sites List',
}, },
}], }],
}, },
@ -89,6 +91,7 @@ export const routes: RouteRecordRaw[] = [
meta: { meta: {
name: () => $gettext('Edit Stream'), name: () => $gettext('Edit Stream'),
hiddenInSidebar: true, hiddenInSidebar: true,
lastRouteName: 'Manage Streams',
}, },
}, },
{ {
@ -143,6 +146,7 @@ export const routes: RouteRecordRaw[] = [
meta: { meta: {
name: () => $gettext('Modify Certificate'), name: () => $gettext('Modify Certificate'),
hiddenInSidebar: true, hiddenInSidebar: true,
lastRouteName: 'Certificates List',
}, },
}, },
{ {
@ -152,6 +156,7 @@ export const routes: RouteRecordRaw[] = [
meta: { meta: {
name: () => $gettext('Import Certificate'), name: () => $gettext('Import Certificate'),
hiddenInSidebar: true, hiddenInSidebar: true,
lastRouteName: 'Certificates List',
}, },
}, },
{ {

View file

@ -16,5 +16,6 @@ declare module 'vue-router' {
noAuth?: boolean noAuth?: boolean
status_code?: number status_code?: number
error?: () => string error?: () => string
lastRouteName?: string
} }
} }

View file

@ -181,6 +181,7 @@ function handleAddStream() {
<AModal <AModal
v-model:open="showAddStream" v-model:open="showAddStream"
:title="$gettext('Add Stream')" :title="$gettext('Add Stream')"
:mask="false"
@ok="handleAddStream" @ok="handleAddStream"
> >
<AForm layout="vertical"> <AForm layout="vertical">

View file

@ -27,6 +27,7 @@ const columns: Column[] = [{
}, },
}, },
hiddenInTable: true, hiddenInTable: true,
hiddenInDetail: true,
}, { }, {
title: () => $gettext('2FA'), title: () => $gettext('2FA'),
dataIndex: 'enabled_2fa', dataIndex: 'enabled_2fa',