11<script setup lang="ts">
22import { getList } from ' ./api/list'
3- import { ref , reactive , computed , ComputedRef , watch } from ' vue'
3+ import { ref , watch , computed , ComputedRef , onUnmounted } from ' vue'
44import { useRouter } from ' vue-router'
55import { useASMeta } from ' ./state/meta'
66
@@ -35,17 +35,14 @@ class bgp {
3535
3636class ospf {
3737 asn: number
38- name! : ComputedRef <string >
38+ name: ComputedRef <string > = computed (
39+ () => ASMeta .value ?.metadata ?.[this .asn + ' ' ]?.display || ` AS ${this .asn } ` ,
40+ )
3941 constructor (asn : number ) {
4042 this .asn = asn
4143 }
42- async init() {
43- this .name = computed (
44- () => ASMeta .value ?.metadata ?.[this .asn + ' ' ]?.display || ' ' ,
45- )
46- }
4744 display() {
48- return this .name ? ` ${ this . name } Network` : ` AS ${ this . asn } `
45+ return this .name + ' Network'
4946 }
5047 path() {
5148 return ` /ospf/${this .asn } `
@@ -57,47 +54,57 @@ interface graph {
5754 path(): string
5855}
5956
60- const graph_list = reactive ([] as Array <graph >)
61-
62- const sort_list = () =>
63- [... graph_list ].sort ((a , b ) => {
64- if (a instanceof ospf && b instanceof bgp ) return 1
65- if (b instanceof bgp && a instanceof ospf ) return - 1
66- return a .display ().localeCompare (b .display ())
67- })
68-
69- watch ([graph_list ], sort_list )
57+ const graph_list = ref ([] as Array <graph >)
7058
7159async function load_list() {
7260 const list = await getList ()
7361
62+ if (list .length === 0 ) {
63+ alert (' no data' )
64+ return
65+ }
66+
67+ const local_list = [] as Array <graph >
7468 list .forEach ((graph ) => {
7569 switch (graph .type ) {
7670 case ' bgp' :
77- graph_list .push (new bgp ())
71+ local_list .push (new bgp ())
7872 break
7973 case ' ospf' : {
80- const gr = new ospf (graph .asn )
81- gr .init ()
82- graph_list .push (gr )
74+ local_list .push (new ospf (graph .asn ))
8375 break
8476 }
8577 }
8678 })
8779
88- sort_list ()
80+ local_list .sort ((a , b ) => {
81+ if (a instanceof ospf && b instanceof bgp ) return 1
82+ if (a instanceof bgp && b instanceof ospf ) return - 1
83+ return a .display ().localeCompare (b .display ())
84+ })
8985
86+ graph_list .value = local_list
9087 if (router .currentRoute .value .path === ' /' ) {
91- router .push (graph_list [0 ].path ())
92- }
93-
94- if (graph_list .length === 0 ) {
95- alert (' no data' )
88+ router .push (local_list [0 ].path ())
9689 }
9790 dataReady .value = true
9891}
9992
93+ watch (
94+ () => ASMeta .value ,
95+ () =>
96+ (graph_list .value = [... graph_list .value ].sort ((a , b ) => {
97+ if (a instanceof ospf && b instanceof bgp ) return 1
98+ if (a instanceof bgp && b instanceof ospf ) return - 1
99+ return a .display ().localeCompare (b .display ())
100+ })),
101+ )
102+
100103load_list ()
104+ const refresh = setInterval (() => load_list (), 60 * 1000 )
105+ onUnmounted (() => {
106+ clearInterval (refresh )
107+ })
101108 </script >
102109<template >
103110 <div class =" aside" >
@@ -119,7 +126,7 @@ load_list()
119126 <el-menu-item
120127 class =" menu-item"
121128 v-for =" graph in graph_list"
122- :key =" graph.path"
129+ :key =" graph.path() "
123130 :index =" graph.path()"
124131 >
125132 <span >{{ graph.display() }}</span >
0 commit comments