Skip to content
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
63 changes: 62 additions & 1 deletion apps/desktop/src/lib/trpc/routers/external/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,40 @@
import { spawn } from "node:child_process";
import path from "node:path";
import { shell } from "electron";
import { clipboard, shell } from "electron";
import { db } from "main/lib/db";
import { EXTERNAL_APPS, type ExternalApp } from "main/lib/db/schemas";
import { z } from "zod";
import { publicProcedure, router } from "../..";

const ExternalAppSchema = z.enum(EXTERNAL_APPS);

/**
* Get the command and args to open a path in the specified app
*/
const getAppCommand = (
app: ExternalApp,
targetPath: string,
): { command: string; args: string[] } | null => {
switch (app) {
case "finder":
return null; // Handled specially with shell.showItemInFolder
case "vscode":
return { command: "code", args: [targetPath] };
case "cursor":
return { command: "cursor", args: [targetPath] };
case "xcode":
return { command: "open", args: ["-a", "Xcode", targetPath] };
case "iterm":
return { command: "open", args: ["-a", "iTerm", targetPath] };
case "warp":
return { command: "open", args: ["-a", "Warp", targetPath] };
case "terminal":
return { command: "open", args: ["-a", "Terminal", targetPath] };
default:
return null;
}
};

/**
* Spawns a process and waits for it to complete
* @throws Error if the process exits with non-zero code or fails to spawn
Expand Down Expand Up @@ -45,6 +76,36 @@ export const createExternalRouter = () => {
shell.showItemInFolder(input);
}),

openInApp: publicProcedure
.input(
z.object({
path: z.string(),
app: ExternalAppSchema,
}),
)
.mutation(async ({ input }) => {
// Save last used app to DB
await db.update((data) => {
data.settings.lastUsedApp = input.app;
});

if (input.app === "finder") {
shell.showItemInFolder(input.path);
return;
}

const cmd = getAppCommand(input.app, input.path);
if (!cmd) {
throw new Error(`Unknown app: ${input.app}`);
}

await spawnAsync(cmd.command, cmd.args);
}),

copyPath: publicProcedure.input(z.string()).mutation(async ({ input }) => {
clipboard.writeText(input);
}),

openFileInEditor: publicProcedure
.input(
z.object({
Expand Down
2 changes: 2 additions & 0 deletions apps/desktop/src/lib/trpc/routers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { router } from "..";
import { createExternalRouter } from "./external";
import { createNotificationsRouter } from "./notifications";
import { createProjectsRouter } from "./projects";
import { createSettingsRouter } from "./settings";
import { createTerminalRouter } from "./terminal";
import { createWindowRouter } from "./window";
import { createWorkspacesRouter } from "./workspaces";
Expand All @@ -19,6 +20,7 @@ export const createAppRouter = (window: BrowserWindow) => {
terminal: createTerminalRouter(),
notifications: createNotificationsRouter(),
external: createExternalRouter(),
settings: createSettingsRouter(),
});
};

Expand Down
10 changes: 10 additions & 0 deletions apps/desktop/src/lib/trpc/routers/settings/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { db } from "main/lib/db";
import { publicProcedure, router } from "../..";

export const createSettingsRouter = () => {
return router({
getLastUsedApp: publicProcedure.query(() => {
return db.data.settings.lastUsedApp ?? "cursor";
}),
});
};
5 changes: 4 additions & 1 deletion apps/desktop/src/lib/trpc/routers/workspaces/workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,10 @@ export const createWorkspacesRouter = () => {
);
}

return workspace;
return {
...workspace,
worktreePath: getWorktreePath(workspace.worktreeId) ?? "",
};
}),

update: publicProcedure
Expand Down
13 changes: 13 additions & 0 deletions apps/desktop/src/main/lib/db/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,21 @@ export interface Tab {
updatedAt: number;
}

export const EXTERNAL_APPS = [
"finder",
"vscode",
"cursor",
"xcode",
"iterm",
"warp",
"terminal",
] as const;

export type ExternalApp = (typeof EXTERNAL_APPS)[number];

export interface Settings {
lastActiveWorkspaceId?: string;
lastUsedApp?: ExternalApp;
}

export interface Database {
Expand Down
20 changes: 1 addition & 19 deletions apps/desktop/src/main/lib/menu.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { app, type BrowserWindow, dialog, Menu } from "electron";
import { app, type BrowserWindow, Menu } from "electron";

export function createApplicationMenu(mainWindow: BrowserWindow) {
const template: Electron.MenuItemConstructorOptions[] = [
Expand All @@ -11,24 +11,6 @@ export function createApplicationMenu(mainWindow: BrowserWindow) {
click: async () => {},
},
{ type: "separator" },
{
label: "Open Repository...",
accelerator: "CmdOrCtrl+O",
click: async () => {
const result = await dialog.showOpenDialog(mainWindow, {
properties: ["openDirectory"],
title: "Select Repository",
});

if (result.canceled || result.filePaths.length === 0) {
return;
}

const _repoPath = result.filePaths[0];
console.log("[Menu] Event sent");
},
},
{ type: "separator" },
{ role: "quit" },
],
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { trpc } from "renderer/lib/trpc";
import { WorkspaceHeader } from "../WorkspaceView/WorkspaceHeader";
import { SettingsButton } from "./SettingsButton";
import { SidebarControl } from "./SidebarControl";
import { WindowControls } from "./WindowControls";
import { WorkspacesTabs } from "./WorkspaceTabs";

export function TopBar() {
const { data: platform } = trpc.window.getPlatform.useQuery();
const { data: activeWorkspace } = trpc.workspaces.getActive.useQuery();
const isMac = platform === "darwin";
return (
<div className="drag gap-2 h-12 w-full flex items-center justify-between bg-background">
Expand All @@ -21,6 +23,7 @@ export function TopBar() {
<WorkspacesTabs />
</div>
<div className="flex items-center gap-2 h-full pr-4">
<WorkspaceHeader worktreePath={activeWorkspace?.worktreePath} />
<SettingsButton />
{!isMac && <WindowControls />}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Kbd, KbdGroup } from "@superset/ui/kbd";
import { HiMiniCommandLine } from "react-icons/hi2";
import { formatKeysForDisplay, HOTKEYS } from "shared/hotkeys";

const shortcuts = [HOTKEYS.NEW_TERMINAL, HOTKEYS.SPLIT_HORIZONTAL];
const shortcuts = [HOTKEYS.NEW_TERMINAL, HOTKEYS.OPEN_IN_APP];

export function EmptyTabView() {
return (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import { Button } from "@superset/ui/button";
import { ButtonGroup } from "@superset/ui/button-group";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@superset/ui/dropdown-menu";
import type { ExternalApp } from "main/lib/db/schemas";
import { useState } from "react";
import { HiChevronDown } from "react-icons/hi2";
import { LuCopy } from "react-icons/lu";
import { trpc } from "renderer/lib/trpc";

import cursorIcon from "./assets/cursor.svg";
import finderIcon from "./assets/finder.png";
import itermIcon from "./assets/iterm.png";
import terminalIcon from "./assets/terminal.png";
import vscodeIcon from "./assets/vscode.svg";
import warpIcon from "./assets/warp.png";
import xcodeIcon from "./assets/xcode.svg";

interface AppOption {
id: ExternalApp;
label: string;
icon: string;
}

const APP_OPTIONS: AppOption[] = [
{ id: "finder", label: "Finder", icon: finderIcon },
{ id: "cursor", label: "Cursor", icon: cursorIcon },
{ id: "vscode", label: "VS Code", icon: vscodeIcon },
{ id: "xcode", label: "Xcode", icon: xcodeIcon },
{ id: "iterm", label: "iTerm", icon: itermIcon },
{ id: "warp", label: "Warp", icon: warpIcon },
{ id: "terminal", label: "Terminal", icon: terminalIcon },
];

const getAppOption = (id: ExternalApp) =>
APP_OPTIONS.find((app) => app.id === id) ?? APP_OPTIONS[1];

interface WorkspaceHeaderProps {
worktreePath: string | undefined;
}

export function WorkspaceHeader({ worktreePath }: WorkspaceHeaderProps) {
const [isOpen, setIsOpen] = useState(false);
const utils = trpc.useUtils();

const { data: lastUsedApp = "cursor" } =
trpc.settings.getLastUsedApp.useQuery();

const openInApp = trpc.external.openInApp.useMutation({
onSuccess: () => utils.settings.getLastUsedApp.invalidate(),
});
const copyPath = trpc.external.copyPath.useMutation();

const folderName = worktreePath
? worktreePath.split("/").filter(Boolean).pop() || worktreePath
: null;
const currentApp = getAppOption(lastUsedApp);

const handleOpenIn = (app: ExternalApp) => {
if (!worktreePath) return;
openInApp.mutate({ path: worktreePath, app });
setIsOpen(false);
};

const handleCopyPath = () => {
if (!worktreePath) return;
copyPath.mutate(worktreePath);
setIsOpen(false);
};

const handleOpenLastUsed = () => {
if (!worktreePath) return;
openInApp.mutate({ path: worktreePath, app: lastUsedApp });
};

return (
<div className="no-drag flex items-center">
<ButtonGroup>
{folderName && (
<Button
variant="outline"
size="sm"
className="gap-1.5"
onClick={handleOpenLastUsed}
title={`Open in ${currentApp.label} (⌘O)`}
>
<img
src={currentApp.icon}
alt=""
className="size-4 object-contain"
/>
<span className="font-medium">/{folderName}</span>
</Button>
)}
<DropdownMenu open={isOpen} onOpenChange={setIsOpen}>
<DropdownMenuTrigger asChild>
<Button
variant="outline"
size="sm"
className="gap-1"
disabled={!worktreePath}
>
<span>Open</span>
<HiChevronDown className="size-3" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end" className="w-48">
{APP_OPTIONS.map((app) => (
<DropdownMenuItem
key={app.id}
onClick={() => handleOpenIn(app.id)}
className="flex items-center justify-between"
>
<div className="flex items-center gap-2">
<img
src={app.icon}
alt={app.label}
className="size-4 object-contain"
/>
<span>{app.label}</span>
</div>
{app.id === lastUsedApp && (
<span className="text-xs text-muted-foreground">⌘O</span>
)}
</DropdownMenuItem>
))}
<DropdownMenuSeparator />
<DropdownMenuItem
onClick={handleCopyPath}
className="flex items-center justify-between"
>
<div className="flex items-center gap-2">
<LuCopy className="size-4" />
<span>Copy path</span>
</div>
<span className="text-xs text-muted-foreground">⌘⇧C</span>
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</ButtonGroup>
</div>
);
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { WorkspaceHeader } from "./WorkspaceHeader";
Loading