Skip to content

Commit 0823450

Browse files
author
chenbo
committed
Fix desktop preload bridge
1 parent dde6ec4 commit 0823450

5 files changed

Lines changed: 34 additions & 10 deletions

File tree

apps/desktop/src/main/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ async function createWindow(): Promise<void> {
3636
minHeight: 620,
3737
title: "OpenCode++ Desktop",
3838
webPreferences: {
39-
preload: path.join(__dirname, "preload.js"),
39+
preload: path.join(__dirname, "preload.cjs"),
4040
contextIsolation: true,
4141
nodeIntegration: false
4242
}

apps/desktop/src/renderer/src/App.tsx

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ function App() {
1717
const [logs, setLogs] = useState<LogEntry[]>([]);
1818
const nextLogId = useRef(1);
1919
const logEndRef = useRef<HTMLSpanElement | null>(null);
20+
const bridge = window.openCodePlusPlus;
2021

2122
useEffect(() => {
22-
const offOutput = window.openCodePlusPlus.onTaskOutput((event) => {
23+
if (!bridge) return;
24+
const offOutput = bridge.onTaskOutput((event) => {
2325
appendLog(event.stream, event.text);
2426
});
25-
const offExit = window.openCodePlusPlus.onTaskExit((event) => {
27+
const offExit = bridge.onTaskExit((event) => {
2628
setRunning(false);
2729
setReportPath(event.reportPath);
2830
appendLog("system", `\nTask exited with ${event.code === null ? `signal ${event.signal ?? "unknown"}` : `code ${event.code}`}\n`);
@@ -32,7 +34,7 @@ function App() {
3234
offOutput();
3335
offExit();
3436
};
35-
}, []);
37+
}, [bridge]);
3638

3739
useEffect(() => {
3840
logEndRef.current?.scrollIntoView({ behavior: "smooth", block: "end" });
@@ -43,22 +45,39 @@ function App() {
4345
return `opencode-plusplus.cmd oc run "${task.trim()}" --repo "${repo}" --max-loops 2`;
4446
}, [repo, task]);
4547

48+
if (!bridge) {
49+
return (
50+
<main className="shell">
51+
<section className="header">
52+
<div>
53+
<p className="eyebrow">OpenCode++ Desktop MVP</p>
54+
<h1>Desktop bridge failed to load</h1>
55+
</div>
56+
<div className="status">Error</div>
57+
</section>
58+
<section className="controls">
59+
<p className="error-text">The Electron preload bridge is unavailable. Rebuild the desktop app and restart it from `apps/desktop`.</p>
60+
</section>
61+
</main>
62+
);
63+
}
64+
4665
function appendLog(stream: LogEntry["stream"], text: string): void {
4766
setLogs((current) => [...current, { id: nextLogId.current++, stream, text }]);
4867
}
4968

5069
async function selectRepo(): Promise<void> {
51-
const selected = await window.openCodePlusPlus.selectRepo();
70+
const selected = await bridge.selectRepo();
5271
if (!selected) return;
5372
setRepo(selected);
54-
setReportPath(await window.openCodePlusPlus.getLatestReport(selected));
73+
setReportPath(await bridge.getLatestReport(selected));
5574
setStatus("Repository selected.");
5675
}
5776

5877
async function runTask(): Promise<void> {
5978
setLogs([]);
6079
setReportPath(undefined);
61-
const result = await window.openCodePlusPlus.runTask({ repo, task });
80+
const result = await bridge.runTask({ repo, task });
6281
if (result.error) {
6382
appendLog("stderr", `${result.error}\n`);
6483
setStatus(result.error);
@@ -70,15 +89,15 @@ function App() {
7089
}
7190

7291
async function stopTask(): Promise<void> {
73-
const result = await window.openCodePlusPlus.stopTask();
92+
const result = await bridge.stopTask();
7493
if (result.stopped) {
7594
appendLog("system", "\nStop requested.\n");
7695
setStatus("Stop requested.");
7796
}
7897
}
7998

8099
async function openReport(): Promise<void> {
81-
const result = await window.openCodePlusPlus.openLatestReport(repo);
100+
const result = await bridge.openLatestReport(repo);
82101
if (result.opened) {
83102
setReportPath(result.path);
84103
setStatus("Report opened.");

apps/desktop/src/renderer/src/styles.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,8 @@ pre {
204204
.system {
205205
color: #fde68a;
206206
}
207+
208+
.error-text {
209+
color: #7f1d1d;
210+
margin: 0;
211+
}

apps/desktop/tsconfig.main.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
"outDir": "dist/main",
66
"noEmit": false
77
},
8-
"include": ["src/main/**/*.ts"]
8+
"include": ["src/main/**/*.ts", "src/main/**/*.cts"]
99
}

0 commit comments

Comments
 (0)