Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Formalize 'route walkers' a little more #3221

Merged
merged 3 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions packages/kuma-gui/src/app/application/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { services as x } from '@/app/x'
import type { ServiceDefinition } from '@/services/utils'
import { token, createInjections, constant } from '@/services/utils'
import type { Component } from 'vue'
import type { RouteRecordRaw } from 'vue-router'
export * from './services/can'
export { runInDebug } from './utilities'
export { defineSources } from './services/data-source'
Expand Down Expand Up @@ -83,6 +84,45 @@ const $ = {
i18n: token<ReturnType<typeof I18n>>('i18n'),
enUs: token('i18n.locale.enUs'),
}

const addModule = (item: RouteRecordRaw, parent?: RouteRecordRaw) => {
item.meta = {
...(item.meta ?? {}),
}
if (typeof parent?.meta?.module !== 'undefined') {
item.meta.module = parent.meta.module
}
}
const addPath = (item: RouteRecordRaw, parent?: RouteRecordRaw) => {
item.meta = {
...(item.meta ?? {}),
}
if (typeof parent?.meta?.path !== 'undefined') {
const path = String(parent.meta.path) ?? ''
item.meta.path = `${path}${path.length > 0 ? '.' : ''}${String(item.name)}`
}
}
const addRouteName = (item: RouteRecordRaw) => {
if (typeof item.name === 'undefined') {
return
}
const props = ((props) => {
switch (true) {
case typeof props === 'function':
return props
case typeof props === 'undefined':
return () => ({})
default:
return () => props
}
})(item.props)
item.props = (...args) => {
return {
...props(...args),
routeName: item.name,
}
}
}
export const services = (app: Record<string, Token>): ServiceDefinition[] => {
return [

Expand Down Expand Up @@ -116,6 +156,18 @@ export const services = (app: Record<string, Token>): ServiceDefinition[] => {
app.routes,
],
}],
[token('application.routes.walkers'), {
service: () => {
return [
addModule,
addPath,
addRouteName,
]
},
labels: [
app.routeWalkers,
],
}],
[token('application.locales'), {
service: () => locales,
labels: [
Expand Down
3 changes: 0 additions & 3 deletions packages/kuma-gui/src/app/data-planes/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ export const routes = () => {
{
path: ':dataPlane',
name: `${fullPrefix}data-plane-summary-view`,
props: () => ({
routeName: `${fullPrefix}data-plane-summary-view`,
}),
component: () => import('@/app/data-planes/views/DataPlaneSummaryView.vue'),
},
]
Expand Down
9 changes: 0 additions & 9 deletions packages/kuma-gui/src/app/subscriptions/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,16 @@ export const routes = (prefix: string): RouteRecordRaw[] => {
path: 'subscription/:subscription',
name: `${prefix}-subscription-summary-view`,
redirect: { name: `${prefix}-subscription-summary-overview-view` },
props: () => ({
routeName: `${prefix}-subscription-summary-view`,
}),
component: () => import('@/app/subscriptions/views/SubscriptionSummaryView.vue'),
children: [
{
path: 'overview',
name: `${prefix}-subscription-summary-overview-view`,
props: () => ({
routeName: `${prefix}-subscription-summary-overview-view`,
}),
component: () => import('@/app/subscriptions/views/SubscriptionSummaryOverviewView.vue'),
},
{
path: 'config',
name: `${prefix}-subscription-summary-config-view`,
props: () => ({
routeName: `${prefix}-subscription-summary-config-view`,
}),
component: () => import('@/app/subscriptions/views/SubscriptionSummaryConfigView.vue'),
},
],
Expand Down
39 changes: 12 additions & 27 deletions packages/kuma-gui/src/app/vue/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,40 +25,20 @@ const $ = {
routes: token<RouteRecordRaw[]>('vue.routes'),
routesLabel: token<RouteRecordRaw[]>('vue.routes.label'),
navigationGuards: token<NavigationGuard[]>('vue.routes.navigation.guards'),
routeWalkers: token<NavigationGuard[]>('vue.routes.walkers'),
}

// @TODO at some point we should expose this as an extendable thing
// similar to navigationGuards. We'd then do this specific functionality in
// `@app/application` as it relates to RouteView
const addModule = (item: RouteRecordRaw, parent?: RouteRecordRaw) => {
item.meta = {
...(item.meta ?? {}),
}
if (typeof parent?.meta?.module !== 'undefined') {
item.meta.module = parent.meta.module
}
}
const addPath = (item: RouteRecordRaw, parent?: RouteRecordRaw) => {
item.meta = {
...(item.meta ?? {}),
}
if (typeof parent?.meta?.path !== 'undefined') {
const path = String(parent.meta.path) ?? ''
item.meta.path = `${path}${path.length > 0 ? '.' : ''}${String(item.name)}`
}
}
function walkRoutes(routes: RouteRecordRaw[], parent?: RouteRecordRaw) {
type RouteWalker = (item: RouteRecordRaw, parent?: RouteRecordRaw) => void
function walkRoutes(walker: RouteWalker, routes: RouteRecordRaw[], parent?: RouteRecordRaw) {
routes.forEach((item) => {
// this is very specific to app/application
addModule(item, parent)
addPath(item, parent)
//
walker(item, parent)
if (typeof item.children !== 'undefined') {
walkRoutes(item.children, item)
walkRoutes(walker, item.children, item)
}
})
return routes
}

export const services = (app: Record<string, Token>): ServiceDefinition[] => {
return [
[$.app, {
Expand Down Expand Up @@ -90,10 +70,14 @@ export const services = (app: Record<string, Token>): ServiceDefinition[] => {
env: (str: 'KUMA_BASE_PATH') => string,
routes: RouteRecordRaw[],
guards: NavigationGuard[],
walkers: RouteWalker[],
) => {

const router = createRouter({
history: createWebHistory(env('KUMA_BASE_PATH')),
routes: walkRoutes([
routes: walkRoutes((item, parent) => {
walkers.forEach(walker => walker(item, parent))
}, [
{
path: '/',
name: 'app',
Expand All @@ -117,6 +101,7 @@ export const services = (app: Record<string, Token>): ServiceDefinition[] => {
app.env,
$.routes,
$.navigationGuards,
$.routeWalkers,
],
}],

Expand Down
Loading