waveterm/frontend/util/previewutil.ts
Sylvie Crowe ff4b71dfae
Error Popups and Context Menu Items (#1989)
This makes the following changes:
- Connects various context menu items to the error overlay on failure
- Connects read file errors to the error overlay on failure
- Consolidates context menu items for open and reveal
- Reduces duplication in context menu items
- Removes an unnecessary File Stat RPC call for the parent directory

---------

Co-authored-by: Evan Simkowitz <esimkowitz@users.noreply.github.com>
Co-authored-by: sawka <mike@commandline.dev>
2025-02-18 16:25:03 -08:00

77 lines
2.5 KiB
TypeScript

import { createBlock, getApi } from "@/app/store/global";
import { makeNativeLabel } from "./platformutil";
import { fireAndForget } from "./util";
import { formatRemoteUri } from "./waveutil";
export function addOpenMenuItems(menu: ContextMenuItem[], conn: string, finfo: FileInfo): ContextMenuItem[] {
if (!finfo) {
return menu;
}
menu.push({
type: "separator",
});
if (!conn) {
// TODO: resolve correct host path if connection is WSL
// if the entry is a directory, reveal it in the file manager, if the entry is a file, reveal its parent directory
menu.push({
label: makeNativeLabel(true),
click: () => {
getApi().openNativePath(finfo.isdir ? finfo.path : finfo.dir);
},
});
// if the entry is a file, open it in the default application
if (!finfo.isdir) {
menu.push({
label: makeNativeLabel(false),
click: () => {
getApi().openNativePath(finfo.path);
},
});
}
} else {
menu.push({
label: "Download File",
click: () => {
const remoteUri = formatRemoteUri(finfo.path, conn);
getApi().downloadFile(remoteUri);
},
});
}
menu.push({
type: "separator",
});
if (!finfo.isdir) {
menu.push({
label: "Open Preview in New Block",
click: () =>
fireAndForget(async () => {
const blockDef: BlockDef = {
meta: {
view: "preview",
file: finfo.path,
connection: conn,
},
};
await createBlock(blockDef);
}),
});
}
// TODO: improve behavior as we add more connection types
if (!conn?.startsWith("aws:")) {
menu.push({
label: "Open Terminal in New Block",
click: () => {
const termBlockDef: BlockDef = {
meta: {
controller: "shell",
view: "term",
"cmd:cwd": finfo.isdir ? finfo.path : finfo.dir,
connection: conn,
},
};
fireAndForget(() => createBlock(termBlockDef));
},
});
}
return menu;
}