Skip to content

Commit

Permalink
feat: Add composable useDefaultSelect
Browse files Browse the repository at this point in the history
  • Loading branch information
Azurewarth0920 committed Apr 1, 2024
1 parent 842ab8e commit 6f01317
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
28 changes: 28 additions & 0 deletions packages/client/src/composables/select.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Ref } from 'vue'
import { ref } from 'vue'
import type { ComponentTreeNode } from '@vue/devtools-kit'

export function createSelectContext(id: string) {
const selected = ref<string>('')
Expand Down Expand Up @@ -44,3 +45,30 @@ export function useSelectWithContext(groupId: string, id: string, onSelect?: (id
toggleSelected,
}
}

export function useDefaultSelect() {
const router = useRouter()
const route = useRoute()

function saveParamId(id: string) {
router.push({
params: {
id,
},
})
}

function getValidNodeId(treeNode: { id: string }[]) {
return treeNode.some(({ id: treeNodeId }) => route.params.id === treeNodeId) && (route.params.id as string)
}

function getValidNestedNodeId(treeNode: ComponentTreeNode[]) {
return treeNode.some(({ id: treeNodeId, children }) => getValidNodeId(children) || treeNodeId === route.params.id) && (route.params.id as string)
}

return {
saveParamId,
getValidNodeId,
getValidNestedNodeId,
}
}
12 changes: 6 additions & 6 deletions packages/client/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { isInChromePanel, isInElectron, isInIframe } from '@vue/devtools-shared'
import { Bridge, HandShakeServer, createDevToolsVuePlugin, initDevToolsSeparateWindow, initDevToolsSeparateWindowBridge, initViteClientHotContext, setupDevToolsBridge } from '@vue/devtools-core'

import { createApp } from 'vue'
import { createMemoryHistory, createRouter } from 'vue-router'
import { type RouteRecordRaw, createMemoryHistory, createRouter } from 'vue-router'
import App from './App.vue'
import Components from '~/pages/components.vue'
import Overview from '~/pages/overview.vue'
Expand All @@ -25,14 +25,14 @@ import WaitForConnection from '~/components/WaitForConnection.vue'
import 'uno.css'
import '~/assets/styles/main.css'

const routes = [
const routes: RouteRecordRaw[] = [
{ path: '/', component: Index },
{ path: '/overview', component: Overview },
{ path: '/components', component: Components },
{ path: '/pinia', component: PiniaPage },
{ path: '/router', component: RouterPage },
{ path: '/components/:id?', component: Components },
{ path: '/pinia/:id?', component: PiniaPage },
{ path: '/router/:id?', component: RouterPage },
{ path: '/i18n', component: I18nPage },
{ path: '/timeline', component: Timeline },
{ path: '/timeline/:id?', component: Timeline },
{ path: '/pages', component: Pages },
{ path: '/assets', component: Assets },
{ path: '/graph', component: Graph },
Expand Down

0 comments on commit 6f01317

Please sign in to comment.