Skip to content

Commit 6f01317

Browse files
feat: Add composable useDefaultSelect
1 parent 842ab8e commit 6f01317

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

packages/client/src/composables/select.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Ref } from 'vue'
22
import { ref } from 'vue'
3+
import type { ComponentTreeNode } from '@vue/devtools-kit'
34

45
export function createSelectContext(id: string) {
56
const selected = ref<string>('')
@@ -44,3 +45,30 @@ export function useSelectWithContext(groupId: string, id: string, onSelect?: (id
4445
toggleSelected,
4546
}
4647
}
48+
49+
export function useDefaultSelect() {
50+
const router = useRouter()
51+
const route = useRoute()
52+
53+
function saveParamId(id: string) {
54+
router.push({
55+
params: {
56+
id,
57+
},
58+
})
59+
}
60+
61+
function getValidNodeId(treeNode: { id: string }[]) {
62+
return treeNode.some(({ id: treeNodeId }) => route.params.id === treeNodeId) && (route.params.id as string)
63+
}
64+
65+
function getValidNestedNodeId(treeNode: ComponentTreeNode[]) {
66+
return treeNode.some(({ id: treeNodeId, children }) => getValidNodeId(children) || treeNodeId === route.params.id) && (route.params.id as string)
67+
}
68+
69+
return {
70+
saveParamId,
71+
getValidNodeId,
72+
getValidNestedNodeId,
73+
}
74+
}

packages/client/src/main.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { isInChromePanel, isInElectron, isInIframe } from '@vue/devtools-shared'
66
import { Bridge, HandShakeServer, createDevToolsVuePlugin, initDevToolsSeparateWindow, initDevToolsSeparateWindowBridge, initViteClientHotContext, setupDevToolsBridge } from '@vue/devtools-core'
77

88
import { createApp } from 'vue'
9-
import { createMemoryHistory, createRouter } from 'vue-router'
9+
import { type RouteRecordRaw, createMemoryHistory, createRouter } from 'vue-router'
1010
import App from './App.vue'
1111
import Components from '~/pages/components.vue'
1212
import Overview from '~/pages/overview.vue'
@@ -25,14 +25,14 @@ import WaitForConnection from '~/components/WaitForConnection.vue'
2525
import 'uno.css'
2626
import '~/assets/styles/main.css'
2727

28-
const routes = [
28+
const routes: RouteRecordRaw[] = [
2929
{ path: '/', component: Index },
3030
{ path: '/overview', component: Overview },
31-
{ path: '/components', component: Components },
32-
{ path: '/pinia', component: PiniaPage },
33-
{ path: '/router', component: RouterPage },
31+
{ path: '/components/:id?', component: Components },
32+
{ path: '/pinia/:id?', component: PiniaPage },
33+
{ path: '/router/:id?', component: RouterPage },
3434
{ path: '/i18n', component: I18nPage },
35-
{ path: '/timeline', component: Timeline },
35+
{ path: '/timeline/:id?', component: Timeline },
3636
{ path: '/pages', component: Pages },
3737
{ path: '/assets', component: Assets },
3838
{ path: '/graph', component: Graph },

0 commit comments

Comments
 (0)