File size: 2,861 Bytes
4d70170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import type { RouteLocationNormalized, RouteRecordRaw } from 'vue-router'
import { createRouter, createWebHashHistory } from 'vue-router'
import { BuiltinTabs, getStorage, setStorage } from '@vue-devtools/shared-utils'
import ComponentsInspector from './features/components/ComponentsInspector.vue'
import CustomInspector from './features/inspector/custom/CustomInspector.vue'
import Timeline from './features/timeline/Timeline.vue'
import Plugins from './features/plugin/Plugins.vue'
import PluginHome from './features/plugin/PluginHome.vue'
import PluginDetails from './features/plugin/PluginDetails.vue'
import GlobalSettings from './features/settings/GlobalSettings.vue'

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    redirect: {
      // An error will be thrown if param id is not provided, provide a default value to avoid this.
      // Ref: https://github.com/vuejs/router/issues/845
      path: '/app/init/inspector/components',
    },
  },
  {
    path: '/app/:appId',
    children: [
      {
        path: 'inspector',
        name: 'inspector',
        children: [
          {
            path: 'components/:componentId?',
            name: 'inspector-components',
            component: ComponentsInspector,
            meta: {
              tab: BuiltinTabs.COMPONENTS,
            },
          },
          {
            path: 'custom/:inspectorId',
            name: 'custom-inspector',
            component: CustomInspector,
            meta: {
              tab: (route: RouteLocationNormalized) => `custom-inspector:${route.params.inspectorId}`,
            },
          },
        ],
      },
      {
        path: 'timeline',
        name: 'timeline',
        component: Timeline,
        meta: {
          tab: BuiltinTabs.TIMELINE,
        },
      },
      {
        path: 'plugins',
        component: Plugins,
        meta: {
          match: 'plugins',
          tab: BuiltinTabs.PLUGINS,
        },
        children: [
          {
            path: '',
            name: 'plugins',
            component: PluginHome,
          },
          {
            path: ':pluginId',
            name: 'plugin-details',
            component: PluginDetails,
            props: true,
          },
        ],
      },
      {
        path: 'settings',
        name: 'global-settings',
        component: GlobalSettings,
        meta: {
          tab: BuiltinTabs.SETTINGS,
        },
      },
    ],
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/',
  },
]

const STORAGE_ROUTE = 'route'

export function createRouterInstance() {
  const router = createRouter({
    history: createWebHashHistory('/'),
    routes,
  })

  const previousRoute = getStorage(STORAGE_ROUTE)
  if (previousRoute) {
    router.push(previousRoute)
  }

  router.afterEach((to) => {
    setStorage(STORAGE_ROUTE, to.fullPath)
  })

  return router
}