Skip to content
This repository was archived by the owner on Jul 30, 2025. It is now read-only.

Commit e849ae7

Browse files
myan9starpit
authored andcommitted
fix: eliminate complex nesting of NavResponse model
Fixes #4205
1 parent 5b661a6 commit e849ae7

File tree

10 files changed

+165
-271
lines changed

10 files changed

+165
-271
lines changed

packages/core/src/models/NavResponse.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import { Entity } from './entity'
18-
import { MultiModalResponse } from './mmr/types'
18+
import { Mode } from './mmr/types'
1919

2020
export interface Breadcrumb {
2121
label: string
@@ -34,7 +34,8 @@ export type NavResponse = {
3434
breadcrumbs?: Breadcrumb[]
3535
}
3636

37-
export type Menu = Record<string, MultiModalResponse>
37+
export type Menu = Label & MenuItems
38+
type MenuItems = { items: Mode[] }
3839

3940
type Label = { label: string }
4041
type Command = { command: string }

plugins/plugin-client-common/src/components/Sidecar/LeftNavSidecar.tsx

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import {
2525
NavResponse,
2626
MultiModalMode,
2727
ParsedOptions,
28-
Menu,
2928
Link,
3029
isLinkWithCommand
3130
} from '@kui-shell/core'
@@ -119,10 +118,8 @@ export default class LeftNavSidecar extends BaseSidecar<NavResponse, HistoryEntr
119118
const navigations = []
120119
// get state from each of the left navigation
121120
response.menus.forEach(menu => {
122-
Object.entries(menu).forEach(([title, mmr]) => {
123-
const state = getStateFromMMR(tab, mmr, argvNoOptions, parsedOptions)
124-
navigations.push(Object.assign({ title }, state))
125-
})
121+
const state = getStateFromMMR(tab, { modes: menu.items }, argvNoOptions, parsedOptions)
122+
navigations.push(Object.assign({ title: menu.label }, state))
126123
})
127124

128125
return {
@@ -220,10 +217,6 @@ export default class LeftNavSidecar extends BaseSidecar<NavResponse, HistoryEntr
220217
return {}
221218
}
222219

223-
private getMMRFromMenu(menu: Menu) {
224-
return Object.values(menu)[0]
225-
}
226-
227220
protected bodyContent(tabIdx: number, menuIdx = 0) {
228221
return (
229222
<React.Suspense fallback={<div />}>
@@ -232,7 +225,7 @@ export default class LeftNavSidecar extends BaseSidecar<NavResponse, HistoryEntr
232225
tab={this.state.tab}
233226
mode={this.current.allNavs[menuIdx].tabs[tabIdx]}
234227
args={{ argvNoOptions: this.state.current.argvNoOptions, parsedOptions: this.state.current.parsedOptions }}
235-
response={this.getMMRFromMenu(this.current.response.menus[menuIdx])}
228+
response={{ modes: this.current.response.menus[menuIdx].items }}
236229
/>
237230
</React.Suspense>
238231
)

plugins/plugin-client-default/config.d/about.json

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
{
22
"menus": [
33
{
4-
"Kui": {
5-
"modes": [
6-
{
7-
"mode": "about",
8-
"content": "about:content",
9-
"contentType": "text/markdown"
10-
},
11-
{ "mode": "version", "contentFrom": "version --full" }
12-
]
13-
}
4+
"label": "Kui",
5+
"items": [
6+
{
7+
"mode": "about",
8+
"content": "about:content",
9+
"contentType": "text/markdown"
10+
},
11+
{ "mode": "version", "contentFrom": "version --full" }
12+
]
1413
}
1514
],
1615
"links": [

plugins/plugin-client-test/src/lib/cmds/NavResponse.ts

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,7 @@ interface Options extends ParsedOptions {
3232
const navResponseWithoutLinks = (): NavResponse => ({
3333
apiVersion: 'kui-shell/v1',
3434
kind: 'NavResponse',
35-
menus: [
36-
{
37-
'Test Nav Without Links': {
38-
kind: 'MultiModelResponse',
39-
metadata: { name: 'test nav 2' },
40-
modes: tableMode
41-
}
42-
}
43-
]
35+
menus: [{ label: 'Test Nav Without Links', items: tableMode }]
4436
})
4537

4638
function navResponseWithBreadcrumbs(): NavResponse {
@@ -62,15 +54,7 @@ const doNav = () => (args: Arguments<Options>): NavResponse => {
6254
return {
6355
apiVersion: 'kui-shell/v1',
6456
kind: 'NavResponse',
65-
menus: [
66-
{
67-
'Test Nav 2': {
68-
kind: 'MultiModelResponse',
69-
metadata: { name: 'test nav 2' },
70-
modes: tableMode
71-
}
72-
}
73-
],
57+
menus: [{ label: 'Test Nav 2', items: tableMode }],
7458
links: [
7559
{ label: 'Home Page', href: 'http://kui.tools' },
7660
{ label: 'switch', command: 'test nav' }
@@ -80,15 +64,7 @@ const doNav = () => (args: Arguments<Options>): NavResponse => {
8064
return {
8165
apiVersion: 'kui-shell/v1',
8266
kind: 'NavResponse',
83-
menus: [
84-
{
85-
'Test Nav': {
86-
kind: 'MultiModelResponse',
87-
metadata: { name: 'test nav' },
88-
modes: tableMode
89-
}
90-
}
91-
],
67+
menus: [{ label: 'Test Nav', items: tableMode }],
9268
links: [
9369
{ label: 'Home Page', href: 'http://kui.tools' },
9470
{ label: 'switch', command: 'test nav --switch' }

plugins/plugin-core-support/about/src/about.ts

Lines changed: 15 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ import {
2424
NavResponse,
2525
isNavResponse,
2626
isLink,
27-
Menu,
28-
isMultiModalResponse,
29-
MultiModalResponse,
3027
Table,
3128
isStringWithOptionalContentType
3229
} from '@kui-shell/core'
@@ -98,22 +95,16 @@ const getAbout = async (): Promise<NavResponse> => {
9895
}
9996

10097
// inject Configure Menu
101-
const fullMenus = (_.menus as Menu[]).slice(0)
102-
fullMenus.push({
103-
[strings('Configure')]: {
104-
modes: [
105-
{
106-
mode: 'theme',
107-
contentFrom: 'themes'
108-
}
109-
]
110-
}
111-
})
98+
const fullMenus = _.menus.slice(0)
99+
const configureMenu = {
100+
label: strings('Configure'),
101+
items: [{ mode: 'theme', contentFrom: 'themes' }]
102+
}
112103

113104
return {
114105
apiVersion: _apiVersion || 'kui-shell/v1',
115106
kind: _kind || 'NavResponse',
116-
menus: fullMenus,
107+
menus: fullMenus.concat([configureMenu]),
117108
links: _['links'] || []
118109
}
119110
})
@@ -140,44 +131,19 @@ const translateModesLabel = (modesFromAbout: Mode[]) => {
140131
)
141132
}
142133

143-
// finish the MMR from modes
144-
const createMMRFromAbout = (modes: Mode[], name: string): MultiModalResponse => {
145-
const mmr = {
146-
kind: 'about',
147-
modes,
148-
metadata: { name }
149-
}
150-
151-
if (!isMultiModalResponse(mmr)) {
152-
throw new Error('Error in modes of about.json')
153-
}
154-
155-
return mmr
156-
}
157-
158134
const aboutWindow = async (): Promise<NavResponse> => {
159-
const name = await getName()
160135
const { apiVersion, kind, menus, links } = await getAbout()
161136

162-
// translated labels and fulfilled MMR details for users
163-
const translatedMenusWithMMR = menus.map(menu => {
164-
const menuTitle = Object.keys(menu)[0]
165-
const mmr = Object.values(menu)[0]
166-
167-
try {
168-
const modesFromAbout = mmr.modes
169-
const modes = translateModesLabel(modesFromAbout)
170-
const fullMMR = createMMRFromAbout(modes, name)
171-
return { [clientStrings(menuTitle)]: fullMMR }
172-
} catch (err) {
173-
console.error(err)
174-
throw new Error('Error in menus of about.json')
175-
}
137+
// translate the label of sidecar modes under each menu
138+
menus.forEach(menu => {
139+
menu.label = clientStrings(menu.label)
140+
menu.items = translateModesLabel(menu.items)
176141
})
177142

178-
const translatedLinks = links.map(link => {
143+
// translate the label of each navlink
144+
links.forEach(link => {
179145
if (isLink(link)) {
180-
return Object.assign(link, { label: clientStrings(link.label) })
146+
link.label = clientStrings(link.label)
181147
} else {
182148
console.error('error in about.json', link)
183149
throw new Error('links in about.json is not supported')
@@ -187,8 +153,8 @@ const aboutWindow = async (): Promise<NavResponse> => {
187153
return {
188154
apiVersion,
189155
kind,
190-
menus: translatedMenusWithMMR,
191-
links: translatedLinks
156+
menus,
157+
links
192158
}
193159
}
194160

plugins/plugin-kubectl/helm/src/controller/helm/status.ts

Lines changed: 47 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import Debug from 'debug'
18-
import { Arguments, ExecOptions, Menu, Registrar, i18n } from '@kui-shell/core'
18+
import { Arguments, ExecOptions, Registrar, NavResponse, i18n } from '@kui-shell/core'
1919
import { isUsage, doHelp, preprocessTable, formatTable, KubeOptions } from '@kui-shell/plugin-kubectl'
2020

2121
import doExecWithStdout from './exec'
@@ -124,62 +124,58 @@ ${notesMatch[3]}`
124124

125125
const notes = notesMatch && notesMatch[4]
126126

127-
const overviewMenu: Menu[] = [
128-
{
129-
Overview: {
130-
modes: [
131-
{
132-
mode: 'status',
133-
label: strings('status'),
134-
content: status,
135-
contentType: 'text/markdown'
136-
}
137-
]
138-
.concat(
139-
!summary
140-
? []
141-
: [
142-
{
143-
mode: 'summary',
144-
label: strings('summary'),
145-
content: summary,
146-
contentType: 'text/markdown'
147-
}
148-
]
149-
)
150-
.concat(
151-
!notes
152-
? []
153-
: [
154-
{
155-
mode: 'notes',
156-
label: strings2('Notes'),
157-
content: notes,
158-
contentType: 'text/markdown'
159-
}
160-
]
161-
)
127+
const overviewMenu = {
128+
label: 'Overview',
129+
items: [
130+
{
131+
mode: 'status',
132+
label: strings('status'),
133+
content: status,
134+
contentType: 'text/markdown'
162135
}
163-
}
164-
]
165-
166-
const resourcesMenu: Menu[] = [
167-
{
168-
Resources: {
169-
modes: resources.map(_ => ({
170-
mode: _.kind,
171-
content: _.table
172-
}))
173-
}
174-
}
175-
]
136+
]
137+
.concat(
138+
!summary
139+
? []
140+
: [
141+
{
142+
mode: 'summary',
143+
label: strings('summary'),
144+
content: summary,
145+
contentType: 'text/markdown'
146+
}
147+
]
148+
)
149+
.concat(
150+
!notes
151+
? []
152+
: [
153+
{
154+
mode: 'notes',
155+
label: strings2('Notes'),
156+
content: notes,
157+
contentType: 'text/markdown'
158+
}
159+
]
160+
)
161+
}
162+
163+
const resourcesMenu = {
164+
label: 'Resources',
165+
items: resources.map(_ => ({
166+
mode: _.kind,
167+
content: _.table
168+
}))
169+
}
176170

177-
return {
171+
const response: NavResponse = {
178172
apiVersion: 'kui-shell/v1',
179173
kind: 'NavResponse',
180174
breadcrumbs: [{ label: 'helm' }, { label: 'release', command: `helm ls` }, { label: name }],
181-
menus: overviewMenu.concat(resourcesMenu)
175+
menus: [overviewMenu, resourcesMenu]
182176
}
177+
178+
return response
183179
}
184180
}
185181

0 commit comments

Comments
 (0)