-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathApp.ts
More file actions
314 lines (273 loc) · 9.78 KB
/
App.ts
File metadata and controls
314 lines (273 loc) · 9.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import {
App,
applyDocumentTheme,
applyHostStyleVariables,
applyHostFonts,
} from "@modelcontextprotocol/ext-apps";
import type { CallToolResult } from "@modelcontextprotocol/ext-apps";
import { parseDiff } from "./diff-parser";
import { renderDiff, setViewMode, getViewMode } from "./diff-renderer";
import { renderPRDetails } from "./pr-details-renderer";
import "./styles.css";
type Tab = "details" | "diff";
let app: App | null = null;
let activeTab: Tab = "details";
// Stored params for making subsequent tool calls when switching tabs
let prOwner = "";
let prRepo = "";
let prPullNumber = 0;
// Cache fetched data to avoid re-fetching on tab switch
let cachedDetails: Record<string, unknown> | null = null;
let cachedDiff: string | null = null;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function handleHostContextChanged(ctx: any): void {
if (ctx.theme) {
applyDocumentTheme(ctx.theme);
}
if (ctx.styles?.variables) {
applyHostStyleVariables(ctx.styles.variables);
}
if (ctx.styles?.css?.fonts) {
applyHostFonts(ctx.styles.css.fonts);
}
// Apply safe area insets
if (ctx.safeAreaInsets) {
document.body.style.paddingTop = `${ctx.safeAreaInsets.top}px`;
document.body.style.paddingBottom = `${ctx.safeAreaInsets.bottom}px`;
document.body.style.paddingLeft = `${ctx.safeAreaInsets.left}px`;
document.body.style.paddingRight = `${ctx.safeAreaInsets.right}px`;
}
// Update fullscreen button visibility and state
const fullscreenBtn = document.getElementById("fullscreen-btn");
if (fullscreenBtn) {
if (ctx.availableDisplayModes) {
const canFullscreen = ctx.availableDisplayModes.includes("fullscreen");
fullscreenBtn.style.display = canFullscreen ? "flex" : "none";
}
if (ctx.displayMode) {
const isFullscreen = ctx.displayMode === "fullscreen";
fullscreenBtn.textContent = isFullscreen ? "✕" : "⛶";
fullscreenBtn.title = isFullscreen ? "Exit fullscreen" : "Fullscreen";
document.body.classList.toggle("fullscreen", isFullscreen);
}
}
}
async function toggleFullscreen(): Promise<void> {
if (!app) return;
const ctx = app.getHostContext();
const currentMode = ctx?.displayMode || "inline";
const newMode = currentMode === "fullscreen" ? "inline" : "fullscreen";
if (ctx?.availableDisplayModes?.includes(newMode)) {
await app.requestDisplayMode({ mode: newMode });
}
}
function toggleViewMode(): void {
const currentMode = getViewMode();
const newMode = currentMode === "unified" ? "split" : "unified";
setViewMode(newMode);
updateViewModeButton();
}
function updateViewModeButton(): void {
const btn = document.getElementById("view-mode-btn");
if (btn) {
const mode = getViewMode();
btn.textContent = mode === "unified" ? "Split" : "Unified";
btn.title = mode === "unified" ? "Switch to split view" : "Switch to unified view";
}
}
function updateTitle(owner: string, repo: string, pullNumber: number): void {
const title = document.getElementById("title");
if (title) {
const prUrl = `https://github.com/${owner}/${repo}/pull/${pullNumber}`;
title.innerHTML = `<span class="pr-link">${escapeHtml(owner)}/${escapeHtml(repo)} #${pullNumber}</span>`;
const link = title.querySelector(".pr-link");
if (link) {
link.addEventListener("click", () => {
app?.openLink({ url: prUrl });
});
}
}
}
function escapeHtml(text: string): string {
const div = document.createElement("div");
div.textContent = text;
return div.innerHTML;
}
function switchTab(tab: Tab): void {
if (tab === activeTab) return;
activeTab = tab;
// Update tab bar
document.querySelectorAll(".tab").forEach((el) => {
el.classList.toggle("active", (el as HTMLElement).dataset.tab === tab);
});
// Toggle content visibility
const contentArea = document.getElementById("content-area");
const diffContainer = document.getElementById("diff-container");
const viewModeBtn = document.getElementById("view-mode-btn");
if (contentArea) contentArea.style.display = tab === "details" ? "block" : "none";
if (diffContainer) diffContainer.style.display = tab === "diff" ? "flex" : "none";
if (viewModeBtn) viewModeBtn.style.display = tab === "diff" ? "flex" : "none";
// Fetch data if not cached
if (tab === "diff" && cachedDiff === null) {
fetchDiff();
} else if (tab === "details" && cachedDetails === null) {
fetchDetails();
}
}
function parseToolResultText(result: CallToolResult): string | null {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const content = result.content as any[];
if (!content || content.length === 0) return null;
const textBlock = content.find((c) => c.type === "text");
return textBlock?.text ?? null;
}
async function fetchDiff(): Promise<void> {
if (!app || !prOwner || !prRepo || !prPullNumber) return;
const diffContainer = document.getElementById("diff-container");
if (diffContainer) diffContainer.innerHTML = '<div class="loading">Loading diff...</div>';
const result = await app.callTool("pull_request_read", {
method: "get_diff",
owner: prOwner,
repo: prRepo,
pullNumber: prPullNumber,
});
const text = parseToolResultText(result);
if (text) {
cachedDiff = text;
const parsed = parseDiff(text);
renderDiff(parsed);
}
}
async function fetchDetails(): Promise<void> {
if (!app || !prOwner || !prRepo || !prPullNumber) return;
const contentArea = document.getElementById("content-area");
if (contentArea) contentArea.innerHTML = '<div class="loading">Loading details...</div>';
const result = await app.callTool("pull_request_read", {
method: "get",
owner: prOwner,
repo: prRepo,
pullNumber: prPullNumber,
});
const text = parseToolResultText(result);
if (text) {
try {
cachedDetails = JSON.parse(text);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
renderPRDetails(cachedDetails as any);
} catch {
if (contentArea) contentArea.innerHTML = `<div class="empty-state">Failed to parse PR details</div>`;
}
}
}
function handleInitialResult(result: CallToolResult, method: string): void {
const text = parseToolResultText(result);
if (!text) return;
if (method === "get_diff") {
cachedDiff = text;
const parsed = parseDiff(text);
renderDiff(parsed);
switchTab("diff");
} else if (method === "get") {
try {
cachedDetails = JSON.parse(text);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
renderPRDetails(cachedDetails as any);
switchTab("details");
} catch {
// fall through
}
}
}
function init(): void {
app = new App({ name: "github-mcp-server-pr-read", version: "1.0.0" });
// Handle tool input to extract params and determine initial tab
app.ontoolinput = (input: Record<string, unknown>) => {
const owner = input.owner as string;
const repo = input.repo as string;
const pullNumber = input.pullNumber as number;
const method = (input.method as string) || "get";
if (owner) prOwner = owner;
if (repo) prRepo = repo;
if (pullNumber) prPullNumber = pullNumber;
if (prOwner && prRepo && prPullNumber) {
updateTitle(prOwner, prRepo, prPullNumber);
}
// Set initial tab based on method
if (method === "get_diff") {
switchTab("diff");
} else {
switchTab("details");
}
};
// Handle tool results
app.ontoolresult = (result: CallToolResult) => {
// Determine which method this result is for based on active tab / cached state
// If we don't have either cached, this is the initial result
if (cachedDetails === null && cachedDiff === null) {
// Peek at the content to determine the type
const text = parseToolResultText(result);
if (text) {
// If it looks like a unified diff, it's a diff result
if (text.startsWith("diff --git") || text.includes("\n---\n")) {
cachedDiff = text;
const parsed = parseDiff(text);
renderDiff(parsed);
if (activeTab === "diff") {
// Already on diff tab, just render
}
} else {
// Try to parse as JSON (PR details)
try {
cachedDetails = JSON.parse(text);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
renderPRDetails(cachedDetails as any);
} catch {
// Unknown format - show as text
const contentArea = document.getElementById("content-area");
if (contentArea) contentArea.textContent = text;
}
}
}
}
};
// Handle streaming partial input for progressive diff rendering
app.ontoolinputpartial = (input: Record<string, unknown>) => {
const diff = input.diff as string | undefined;
if (diff && activeTab === "diff") {
const parsed = parseDiff(diff);
renderDiff(parsed);
}
};
// Handle host context changes (theme, etc.)
app.onhostcontextchanged = handleHostContextChanged;
// Set up tab bar
document.querySelectorAll(".tab").forEach((tab) => {
tab.addEventListener("click", () => {
const tabName = (tab as HTMLElement).dataset.tab as Tab;
if (tabName) switchTab(tabName);
});
});
// Set up view mode toggle button
const viewModeBtn = document.getElementById("view-mode-btn");
if (viewModeBtn) {
viewModeBtn.addEventListener("click", toggleViewMode);
}
// Set up fullscreen button
const fullscreenBtn = document.getElementById("fullscreen-btn");
if (fullscreenBtn) {
fullscreenBtn.addEventListener("click", toggleFullscreen);
}
// Connect to host
app.connect().then(() => {
const ctx = app?.getHostContext();
if (ctx) {
handleHostContextChanged(ctx);
}
});
}
// Initialize when DOM is ready
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", init);
} else {
init();
}