Skip to content

Commit

Permalink
refactor: optimize the subnet operations in the Node interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jamebal committed Aug 29, 2024
1 parent 4247e16 commit a65ba8d
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 184 deletions.
3 changes: 2 additions & 1 deletion src/service/api/node.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getRequestInstance } from '../http/instances'
import type { User } from './user'
import type { RouteData } from '@/service'

const request = getRequestInstance()

Expand All @@ -13,7 +14,7 @@ export interface NodeData {
name: string
user: User
ipAddresses: Array<string>
prefixList: Array<string>
routes: Array<RouteData>
online: boolean
givenName: string
validTags: Array<string>
Expand Down
21 changes: 6 additions & 15 deletions src/views/node/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import NodeDetails from '@/views/node/nodeDetails.vue'
import NodeActions from '@/views/node/nodeActions.vue'
import { useAppStore } from '@/store'
import { showBackfillipsDialog } from '@/views/node/backfillIpsDialog'
import NodeSubNetDetails from "@/views/node/nodeSubNetDetails.vue";
const dialog = useDialog()
Expand Down Expand Up @@ -82,20 +83,11 @@ const columns = computed((): DataTableColumns<NodeData> => [
h('span', rowData.givenName),
h('br'),
h('span', { style: { color: 'var(--test-color-fringe)' } }, rowData.name),
rowData.prefixList && rowData.prefixList.length > 0
? h(NTag, {
show: false,
size: 'small',
style: {
marginLeft: '10px',
},
type: 'info',
bordered: true,
}, {
default: () => t('app.subnets'),
rowData.routes && rowData.routes.length > 0
? h(NodeSubNetDetails, {
nodeData: rowData,
})
: '',
// rowData.prefixList ? h('span', { style: { marginLeft: '14px', color: 'var(--test-color-fringe)' } }, rowData.prefixList.join('\n')) : null,
],
)
},
Expand All @@ -122,7 +114,6 @@ const columns = computed((): DataTableColumns<NodeData> => [
default: () => row.ipAddresses[0],
}),
h('br'),
// row.prefixList ? h('span', { style: { marginLeft: '14px', color: 'var(--test-color-fringe)' } }, row.prefixList.join('\n')) : null,
],
)
},
Expand Down Expand Up @@ -195,10 +186,10 @@ const columns = computed((): DataTableColumns<NodeData> => [
function addRoutePrefixToNodes() {
nodeList.value.forEach((node) => {
node.prefixList = []
node.routes = []
routeList.value.forEach((route) => {
if (route.node.id === node.id) {
node.prefixList.push(route.prefix)
node.routes.push(route)
}
})
})
Expand Down
33 changes: 33 additions & 0 deletions src/views/node/nodeSubNetDetails.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<script setup lang="ts">
import type { NodeData } from '@/service/api/node'
import RouteTable from '@/views/route/routeTable.vue'
const props = defineProps(
{
nodeData: {
type: Object as PropType<NodeData>,
required: true,
},
},
)
const routeCount = props.nodeData.routes?.length
const enableCount = props.nodeData.routes?.filter(route => route?.enabled)?.length
const { t } = useI18n()
</script>

<template>
<n-popover trigger="click" placement="top">
<template #trigger>
<n-tag type="info" size="small" style="margin-left: 10px;">
{{ `${t('app.subnets')} ${enableCount}/${routeCount}` }}
</n-tag>
</template>
<RouteTable :routes="nodeData.routes" hide-node-name />
</n-popover>
</template>

<style scoped>
</style>
170 changes: 2 additions & 168 deletions src/views/route/index.vue
Original file line number Diff line number Diff line change
@@ -1,175 +1,9 @@
<script setup lang="ts">
import { onMounted } from 'vue'
import type { DataTableColumns } from 'naive-ui'
import { NButton, NTag } from 'naive-ui'
import { useI18n } from 'vue-i18n'
import { useAppStore } from '@/store'
import type { RouteData } from '@/service'
import { disableRoute, enableRoute, fetchRouteList } from '@/service'
import { showDeleteRouteDialog } from '@/views/route/deleteRouteDialog'
const { t } = useI18n()
const routeList = ref<RouteData[]>([])
const appStore = useAppStore()
watch(() => appStore.message, (newMessage) => {
if (newMessage?.event === 'refreshRouteList') {
renderRouteList()
}
})
const dialog = useDialog()
const enableBtnLoading = ref(false)
function enableOrDisableRoute(id: string) {
const route = routeList.value.find(item => item.id === id)
if (!route) {
return
}
enableBtnLoading.value = true
if (route.enabled) {
disableRoute(id).then((res) => {
enableBtnLoading.value = false
if (!res.isSuccess) {
return
}
window.$message.success(`${t('common.disable')} ${t('common.success')}`)
renderRouteList()
})
}
else {
enableRoute(id).then((res) => {
enableBtnLoading.value = false
if (!res.isSuccess) {
return
}
window.$message.success(`${t('common.enable')} ${t('common.success')}`)
renderRouteList()
})
}
}
const columns = computed((): DataTableColumns<RouteData> => [
{
title: 'id',
key: 'id',
align: 'center',
},
{
title: t('app.node'),
key: 'node.givenName',
},
{
title: t('app.prefix'),
key: 'prefix',
},
{
title: t('app.advertised'),
key: 'advertised',
render(rowData) {
return h(NTag, {
style: {
marginRight: '6px',
},
type: rowData.advertised ? 'info' : 'default',
bordered: true,
}, {
default: () => t(`common.${rowData.advertised ? 'yes' : 'no'}`),
})
},
},
{
title: t('app.enabled'),
key: 'enabled',
render(rowData) {
return h(NTag, {
style: {
marginRight: '6px',
},
type: rowData.enabled ? 'info' : 'default',
bordered: true,
}, {
default: () => t(`common.${rowData.enabled ? 'enable' : 'disable'}`),
})
},
},
{
title: t('app.isPrimary'),
key: 'isPrimary',
render(rowData) {
return h(NTag, {
style: {
marginRight: '6px',
},
type: rowData.isPrimary ? 'info' : 'default',
bordered: true,
}, {
default: () => t(`common.${rowData.isPrimary ? 'yes' : 'no'}`),
})
},
},
{
title: t('app.action'),
key: 'actions',
align: 'center',
width: '180px',
render(rowData) {
return h(
'div',
{ style: { display: 'flex', justifyContent: 'space-evenly' } },
[
h(NButton, {
secondary: true,
size: 'small',
loading: enableBtnLoading.value,
type: rowData.enabled ? 'warning' : 'info',
onClick() {
enableOrDisableRoute(rowData.id)
},
}, {
default: () => t(`common.${rowData.enabled ? 'disable' : 'enable'}`),
}),
h(NButton, {
secondary: true,
size: 'small',
type: 'error',
onClick() {
showDeleteRouteDialog(dialog, t, rowData.id, rowData.prefix)
},
}, {
default: () => t('common.delete'),
}),
],
)
},
},
])
function renderRouteList() {
fetchRouteList().then((res) => {
if (!res.isSuccess) {
return
}
routeList.value = res.data.routes
})
}
onMounted(() => {
renderRouteList()
})
import RouteTable from '@/views/route/routeTable.vue'
</script>

<template>
<n-space vertical>
<n-data-table
striped
:columns="columns"
:data="routeList"
/>
</n-space>
<RouteTable />
</template>

<style scoped></style>
Loading

0 comments on commit a65ba8d

Please sign in to comment.