Enable safe external PR link opening in desktop app
- add desktop IPC handler to validate and open http(s) URLs via Electron shell - expose `shell.openExternal` in shared NativeApi contracts and web bridge - route Git PR opening and native API reads through the new `useNativeApi` hook Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
9accd1ffb7
commit
c643de8ba7
11 files changed
+80
-36
No files matched your search
@@ -5,7 +5,7 @@ import net from "node:net";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
|
||||
import { app, BrowserWindow, dialog, ipcMain, Menu } from "electron";
|
||||
import { app, BrowserWindow, dialog, ipcMain, Menu, shell } from "electron";
|
||||
|
||||
import { fixPath } from "./fixPath";
|
||||
|
||||
@@ -13,6 +13,7 @@ fixPath();
|
||||
|
||||
const PICK_FOLDER_CHANNEL = "desktop:pick-folder";
|
||||
const CONTEXT_MENU_CHANNEL = "desktop:context-menu";
|
||||
const OPEN_EXTERNAL_CHANNEL = "desktop:open-external";
|
||||
const ROOT_DIR = path.resolve(__dirname, "../../..");
|
||||
const BACKEND_ENTRY = path.join(ROOT_DIR, "apps/server/dist/index.mjs");
|
||||
const WEB_ENTRY = path.join(ROOT_DIR, "apps/web/dist/index.html");
|
||||
@@ -159,6 +160,31 @@ function registerIpcHandlers(): void {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
ipcMain.removeHandler(OPEN_EXTERNAL_CHANNEL);
|
||||
ipcMain.handle(OPEN_EXTERNAL_CHANNEL, async (_event, rawUrl: unknown) => {
|
||||
if (typeof rawUrl !== "string" || rawUrl.length === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
let parsedUrl: URL;
|
||||
try {
|
||||
parsedUrl = new URL(rawUrl);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (parsedUrl.protocol !== "https:" && parsedUrl.protocol !== "http:") {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
await shell.openExternal(parsedUrl.toString());
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function createWindow(): BrowserWindow {
|
||||
|
||||
@@ -2,6 +2,7 @@ import { contextBridge, ipcRenderer } from "electron";
|
||||
|
||||
const PICK_FOLDER_CHANNEL = "desktop:pick-folder";
|
||||
const CONTEXT_MENU_CHANNEL = "desktop:context-menu";
|
||||
const OPEN_EXTERNAL_CHANNEL = "desktop:open-external";
|
||||
const wsUrl = process.env.T3CODE_DESKTOP_WS_URL ?? null;
|
||||
|
||||
contextBridge.exposeInMainWorld("desktopBridge", {
|
||||
@@ -9,4 +10,5 @@ contextBridge.exposeInMainWorld("desktopBridge", {
|
||||
pickFolder: () => ipcRenderer.invoke(PICK_FOLDER_CHANNEL) as Promise<string | null>,
|
||||
showContextMenu: (items: { id: string; label: string }[]) =>
|
||||
ipcRenderer.invoke(CONTEXT_MENU_CHANNEL, items) as Promise<string | null>,
|
||||
openExternal: (url: string) => ipcRenderer.invoke(OPEN_EXTERNAL_CHANNEL, url) as Promise<boolean>,
|
||||
});
|
||||
@@ -1,18 +1,18 @@
|
||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||
import { useEffect, useMemo, useRef } from "react";
|
||||
import { useEffect, useRef } from "react";
|
||||
|
||||
import ChatView from "./components/ChatView";
|
||||
import DiffPanel from "./components/DiffPanel";
|
||||
import Sidebar from "./components/Sidebar";
|
||||
import { isElectron } from "./env";
|
||||
import { DEFAULT_MODEL } from "./model-logic";
|
||||
import { readNativeApi } from "./session-logic";
|
||||
import { StoreProvider, useStore } from "./store";
|
||||
import { DEFAULT_THREAD_TERMINAL_HEIGHT } from "./types";
|
||||
import { onServerWelcome } from "./wsNativeApi";
|
||||
import { useNativeApi } from "./hooks/useNativeApi";
|
||||
|
||||
function EventRouter() {
|
||||
const api = useMemo(() => readNativeApi(), []);
|
||||
const api = useNativeApi();
|
||||
const { dispatch } = useStore();
|
||||
const activeAssistantItemRef = useRef<string | null>(null);
|
||||
|
||||
@@ -109,7 +109,7 @@ function AutoProjectBootstrap() {
|
||||
}
|
||||
|
||||
function DesktopProjectBootstrap() {
|
||||
const api = useMemo(() => readNativeApi(), []);
|
||||
const api = useNativeApi();
|
||||
const { dispatch } = useStore();
|
||||
const bootstrappedRef = useRef(false);
|
||||
|
||||
@@ -159,7 +159,7 @@ function DesktopProjectBootstrap() {
|
||||
}
|
||||
|
||||
function Layout() {
|
||||
const api = useMemo(() => readNativeApi(), []);
|
||||
const api = useNativeApi();
|
||||
const { state } = useStore();
|
||||
|
||||
if (!api) {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import type { GitBranch } from "@t3tools/contracts";
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { useEffect, useMemo, useRef, useState } from "react";
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
|
||||
import { readNativeApi } from "../session-logic";
|
||||
import { useStore } from "../store";
|
||||
import { useNativeApi } from "../hooks/useNativeApi";
|
||||
|
||||
interface BranchToolbarProps {
|
||||
envMode: "local" | "worktree";
|
||||
@@ -13,7 +13,7 @@ interface BranchToolbarProps {
|
||||
|
||||
export default function BranchToolbar({ envMode, onEnvModeChange, envLocked }: BranchToolbarProps) {
|
||||
const { state, dispatch } = useStore();
|
||||
const api = useMemo(() => readNativeApi(), []);
|
||||
const api = useNativeApi();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const [isBranchMenuOpen, setIsBranchMenuOpen] = useState(false);
|
||||
@@ -254,7 +254,11 @@ export default function BranchToolbar({ envMode, onEnvModeChange, envLocked }: B
|
||||
<span className="truncate">{branch.name}</span>
|
||||
{(branch.current || branch.isDefault || hasSecondaryWorktree) && (
|
||||
<span className="shrink-0 text-[10px] text-muted-foreground/45">
|
||||
{branch.current ? "current" : hasSecondaryWorktree ? "worktree" : "default"}
|
||||
{branch.current
|
||||
? "current"
|
||||
: hasSecondaryWorktree
|
||||
? "worktree"
|
||||
: "default"}
|
||||
</span>
|
||||
)}
|
||||
</button>
|
||||
|
||||
@@ -31,7 +31,6 @@ import {
|
||||
formatDuration,
|
||||
formatElapsed,
|
||||
formatTimestamp,
|
||||
readNativeApi,
|
||||
} from "../session-logic";
|
||||
import { useStore } from "../store";
|
||||
import BranchToolbar from "./BranchToolbar";
|
||||
@@ -39,6 +38,7 @@ import GitActionsControl from "./GitActionsControl";
|
||||
import { isTerminalToggleShortcut } from "../terminal-shortcuts";
|
||||
import ChatMarkdown from "./ChatMarkdown";
|
||||
import ThreadTerminalDrawer from "./ThreadTerminalDrawer";
|
||||
import { useNativeApi } from "../hooks/useNativeApi";
|
||||
|
||||
function formatMessageMeta(createdAt: string, duration: string | null): string {
|
||||
if (!duration) return formatTimestamp(createdAt);
|
||||
@@ -133,7 +133,7 @@ function derivePendingApprovals(events: ProviderEvent[]): PendingApprovalCard[]
|
||||
|
||||
export default function ChatView() {
|
||||
const { state, dispatch } = useStore();
|
||||
const api = useMemo(() => readNativeApi(), []);
|
||||
const api = useNativeApi();
|
||||
const [prompt, setPrompt] = useState("");
|
||||
const [isSending, setIsSending] = useState(false);
|
||||
const [isConnecting, setIsConnecting] = useState(false);
|
||||
@@ -694,9 +694,7 @@ export default function ChatView() {
|
||||
</div>
|
||||
)}
|
||||
{/* Git actions */}
|
||||
{activeProject && (
|
||||
<GitActionsControl api={api} gitCwd={gitCwd} />
|
||||
)}
|
||||
{activeProject && <GitActionsControl api={api} gitCwd={gitCwd} />}
|
||||
{/* Diff toggle */}
|
||||
<button
|
||||
type="button"
|
||||
|
||||
@@ -151,18 +151,6 @@ function buildGitActionMenuItems(
|
||||
gitStatus.hasUpstream &&
|
||||
gitStatus.behindCount === 0;
|
||||
|
||||
if (!hasChanges && !hasAhead && hasOpenPr) {
|
||||
return [
|
||||
{
|
||||
id: "pr",
|
||||
label: "Open PR",
|
||||
disabled: !canOpenPr,
|
||||
icon: "pr",
|
||||
kind: "open_pr",
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
{
|
||||
id: "commit",
|
||||
@@ -344,6 +332,10 @@ export default function GitActionsControl({ api, gitCwd }: GitActionsControlProp
|
||||
const openExistingPr = useCallback(() => {
|
||||
setIsGitMenuOpen(false);
|
||||
setGitActionError(null);
|
||||
if (!api) {
|
||||
setGitActionError("Link opening is unavailable.");
|
||||
return;
|
||||
}
|
||||
|
||||
const prUrl = gitStatus?.openPr?.url ?? null;
|
||||
if (!prUrl) {
|
||||
@@ -351,11 +343,12 @@ export default function GitActionsControl({ api, gitCwd }: GitActionsControlProp
|
||||
return;
|
||||
}
|
||||
|
||||
const popup = window.open(prUrl, "_blank", "noopener,noreferrer");
|
||||
if (!popup) {
|
||||
setGitActionError("Unable to open PR. Allow popups and try again.");
|
||||
}
|
||||
}, [gitStatus?.openPr?.url]);
|
||||
void api.shell.openExternal(prUrl).catch((error) => {
|
||||
setGitActionError(
|
||||
error instanceof Error ? error.message : "Unable to open PR link.",
|
||||
);
|
||||
});
|
||||
}, [api, gitStatus?.openPr?.url]);
|
||||
|
||||
const runGitAction = useCallback(async () => {
|
||||
if (!api || !gitCwd || !isGitModalOpen) return;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { MonitorIcon, MoonIcon, SunIcon } from "lucide-react";
|
||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { isElectron } from "../env";
|
||||
import { useTheme } from "../hooks/useTheme";
|
||||
import { DEFAULT_MODEL } from "../model-logic";
|
||||
import { readNativeApi } from "../session-logic";
|
||||
import { useStore } from "../store";
|
||||
import { DEFAULT_THREAD_TERMINAL_HEIGHT, type Project } from "../types";
|
||||
import { useNativeApi } from "../hooks/useNativeApi";
|
||||
|
||||
const THEME_CYCLE = { system: "light", light: "dark", dark: "system" } as const;
|
||||
function formatRelativeTime(iso: string): string {
|
||||
@@ -35,7 +35,7 @@ function threadStatusLabel(
|
||||
|
||||
export default function Sidebar() {
|
||||
const { state, dispatch } = useStore();
|
||||
const api = useMemo(() => readNativeApi(), []);
|
||||
const api = useNativeApi();
|
||||
const { theme, setTheme } = useTheme();
|
||||
const [addingProject, setAddingProject] = useState(false);
|
||||
const [newCwd, setNewCwd] = useState("");
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
import { useMemo } from "react";
|
||||
import { readNativeApi } from "../session-logic";
|
||||
|
||||
export function useNativeApi() {
|
||||
return useMemo(() => readNativeApi(), []);
|
||||
}
|
||||
Vendored
+1
@@ -6,6 +6,7 @@ interface DesktopBridge {
|
||||
getWsUrl: () => string | null;
|
||||
pickFolder: () => Promise<string | null>;
|
||||
showContextMenu: (items: readonly { id: string; label: string }[]) => Promise<string | null>;
|
||||
openExternal: (url: string) => Promise<boolean>;
|
||||
}
|
||||
|
||||
declare global {
|
||||
|
||||
@@ -96,6 +96,20 @@ export function createWsNativeApi(): NativeApi {
|
||||
shell: {
|
||||
openInEditor: (cwd, editor) =>
|
||||
transport.request(WS_METHODS.shellOpenInEditor, { cwd, editor }),
|
||||
openExternal: async (url) => {
|
||||
if (window.desktopBridge) {
|
||||
const opened = await window.desktopBridge.openExternal(url);
|
||||
if (!opened) {
|
||||
throw new Error("Unable to open link.");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const popup = window.open(url, "_blank", "noopener,noreferrer");
|
||||
if (!popup) {
|
||||
throw new Error("Unable to open link. Allow popups and try again.");
|
||||
}
|
||||
},
|
||||
},
|
||||
git: {
|
||||
status: (input) => transport.request(WS_METHODS.gitStatus, input),
|
||||
|
||||
@@ -89,6 +89,7 @@ export interface NativeApi {
|
||||
};
|
||||
shell: {
|
||||
openInEditor: (cwd: string, editor: EditorId) => Promise<void>;
|
||||
openExternal: (url: string) => Promise<void>;
|
||||
};
|
||||
git: {
|
||||
// Existing branch/worktree API
|
||||
@@ -111,4 +112,3 @@ export interface NativeApi {
|
||||
) => Promise<T | null>;
|
||||
};
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user