Skip to content

Commit cd93226

Browse files
committed
fix: tray profile runs menu always uses fixed default profile
1 parent 2d5bd74 commit cd93226

File tree

2 files changed

+41
-28
lines changed

2 files changed

+41
-28
lines changed

plugins/plugin-codeflare/src/tray/menus/profiles/index.ts

+24-19
Original file line numberDiff line numberDiff line change
@@ -14,44 +14,49 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { Choices, Profiles } from "madwizard"
17+
import { Profiles } from "madwizard"
1818
import { MenuItemConstructorOptions } from "electron"
1919
import { CreateWindowFunction } from "@kui-shell/core"
2020

2121
import boot from "./boot"
2222
import shutdown from "./shutdown"
23-
import submenuForRuns from "./runs"
23+
import runs from "./runs"
2424

2525
import UpdateFunction from "../../update"
2626
import { profileIcon } from "../../icons"
27-
2827
import ProfileStatusWatcher from "../../watchers/profile/status"
2928

29+
/** Memo of `ProfileStatusWatcher`, keyed by profile name */
3030
const watchers: Record<string, ProfileStatusWatcher> = {}
3131

32-
/** @return a menu for the given profile */
33-
async function submenuForOneProfile(
34-
state: Choices.ChoiceState,
32+
/** @return menu items for the status of the given `profile` */
33+
function status(profile: string, updateFunction: UpdateFunction) {
34+
if (!watchers[profile]) {
35+
watchers[profile] = new ProfileStatusWatcher(profile, updateFunction)
36+
}
37+
const watcher = watchers[profile]
38+
39+
return [{ label: "Status", enabled: false }, watcher.head, watcher.workers]
40+
}
41+
42+
/** @return a menu for the given `profile` */
43+
async function profileMenu(
44+
profile: string,
3545
createWindow: CreateWindowFunction,
3646
updateFunction: UpdateFunction
3747
): Promise<MenuItemConstructorOptions> {
38-
if (!watchers[state.profile.name]) {
39-
watchers[state.profile.name] = new ProfileStatusWatcher(state.profile.name, updateFunction)
40-
}
41-
const watcher = watchers[state.profile.name]
42-
4348
return {
44-
label: state.profile.name,
49+
label: profile,
4550
icon: profileIcon,
4651
submenu: [
47-
boot(state.profile.name, createWindow),
48-
shutdown(state.profile.name, createWindow),
52+
boot(profile, createWindow),
53+
shutdown(profile, createWindow),
54+
4955
{ type: "separator" },
50-
{ label: "Status", enabled: false },
51-
watcher.head,
52-
watcher.workers,
56+
...status(profile, updateFunction),
57+
5358
{ type: "separator" },
54-
...(await submenuForRuns(createWindow)),
59+
...(await runs(profile, createWindow)),
5560
],
5661
}
5762
}
@@ -68,6 +73,6 @@ export default async function profilesMenu(
6873
label: "Profiles",
6974
enabled: false,
7075
},
71-
...(await Promise.all(profiles.map((_) => submenuForOneProfile(_, createWindow, updateFn)))),
76+
...(await Promise.all(profiles.map((_) => profileMenu(_.profile.name, createWindow, updateFn)))),
7277
]
7378
}

plugins/plugin-codeflare/src/tray/menus/profiles/runs.ts

+17-9
Original file line numberDiff line numberDiff line change
@@ -25,36 +25,44 @@ import windowOptions from "../../window"
2525
export const RUNS_ERROR = "No runs found"
2626

2727
/** @return a new Window with a dashboard of the selected job run */
28-
function openRun(runId: string, createWindow: CreateWindowFunction) {
29-
const runsDir = Profiles.guidebookJobDataPath({})
28+
function openRun(profile: string, runId: string, createWindow: CreateWindowFunction) {
29+
const runsDir = Profiles.guidebookJobDataPath({ profile })
3030
createWindow(
3131
["codeflare", "dashboard", join(runsDir, runId)],
3232
windowOptions({ title: "CodeFlare Dashboard: " + runId })
3333
)
3434
}
3535

3636
/** @return files of the directory of job runs for a given profile */
37-
async function readRunsDir(): Promise<string[]> {
37+
async function readRunsDir(profile: string): Promise<string[]> {
3838
try {
39-
const runsDir = Profiles.guidebookJobDataPath({})
40-
return await readdir(runsDir)
39+
// TODO do a "full" read with Dirents, so that we have filesystem
40+
// timestamps, and sort, so that the `.slice(0, 10)` below pulls
41+
// out the most recent runs
42+
return await readdir(Profiles.guidebookJobDataPath({ profile }))
4143
} catch (err) {
4244
return [RUNS_ERROR]
4345
}
4446
}
4547

4648
/** @return a menu for all runs of a profile */
47-
export function runMenuItems(createWindow: CreateWindowFunction, runs: string[]): MenuItemConstructorOptions[] {
48-
return runs.map((run) => ({ label: run, click: () => openRun(run, createWindow) }))
49+
export function runMenuItems(
50+
profile: string,
51+
createWindow: CreateWindowFunction,
52+
runs: string[]
53+
): MenuItemConstructorOptions[] {
54+
return runs.slice(0, 10).map((run) => ({ label: run, click: () => openRun(profile, run, createWindow) }))
4955
}
5056

57+
/** @return menu items for the runs of the given profile */
5158
export default async function submenuForRuns(
59+
profile: string,
5260
createWindow: CreateWindowFunction
5361
): Promise<MenuItemConstructorOptions[]> {
54-
const runs = await readRunsDir()
62+
const runs = await readRunsDir(profile)
5563

5664
return [
5765
{ label: "Recent Runs", enabled: false },
58-
...(runs.length && runs[0] !== RUNS_ERROR ? runMenuItems(createWindow, runs) : [{ label: RUNS_ERROR }]),
66+
...(runs.length && runs[0] !== RUNS_ERROR ? runMenuItems(profile, createWindow, runs) : [{ label: RUNS_ERROR }]),
5967
]
6068
}

0 commit comments

Comments
 (0)