Skip to content

Commit 918f702

Browse files
committed
refactor: streamline ActionButton URL handling
- Renamed getUrl to getCodeUrl for clarity. - Simplified URL generation logic for terminal and code targets. - Consolidated embedding logic by directly using ExcalidrawElementFactory for creating embeddable elements. - Improved error handling for URL determination in both embedding and opening actions. - Enhanced debug logging for better traceability of actions.
1 parent 477928d commit 918f702

File tree

1 file changed

+45
-42
lines changed

1 file changed

+45
-42
lines changed

src/frontend/src/pad/buttons/ActionButton.tsx

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -280,35 +280,16 @@ const ActionButton: React.FC<ActionButtonProps> = ({
280280
};
281281

282282

283-
const getUrl = () => {
283+
const getCodeUrl = () => {
284284
if (!workspaceState) {
285-
if (selectedTarget === 'terminal') {
286-
return 'https://terminal.example.dev';
287-
} else {
288-
return 'https://vscode.example.dev';
289-
}
285+
return '';
290286
}
291287

292-
if (selectedTarget === 'terminal') {
293-
return `${workspaceState.base_url}/@${workspaceState.username}/${workspaceState.workspace_id}.${workspaceState.agent}/terminal`;
294-
} else {
295-
return `${workspaceState.base_url}/@${workspaceState.username}/${workspaceState.workspace_id}.${workspaceState.agent}/apps/code-server`;
296-
}
288+
return `${workspaceState.base_url}/@${workspaceState.username}/${workspaceState.workspace_id}.${workspaceState.agent}/apps/code-server`;
297289
};
298290

299291
// Placement logic has been moved to ExcalidrawElementFactory.placeInScene
300292

301-
const createEmbeddableElement = (link: string, buttonElement: HTMLElement | null = null) => {
302-
return ExcalidrawElementFactory.createEmbeddableElement({
303-
link,
304-
width: 600,
305-
height: 400,
306-
strokeColor: "#1e1e1e",
307-
backgroundColor: "#ffffff",
308-
roughness: 1
309-
});
310-
};
311-
312293

313294
const executeAction = () => {
314295
capture('action_button_clicked', {
@@ -324,36 +305,60 @@ const ActionButton: React.FC<ActionButtonProps> = ({
324305
return;
325306
}
326307

327-
const baseUrl = getUrl();
308+
// Determine the link to use
309+
let link: string;
328310

329-
if (!baseUrl) {
330-
console.error('Could not determine URL for embedding');
331-
return;
311+
if (selectedTarget === 'terminal') {
312+
// For terminal, use the !terminal embed link
313+
link = '!terminal';
314+
} else {
315+
// For code, use the code URL
316+
const codeUrl = getCodeUrl();
317+
if (!codeUrl) {
318+
console.error('Could not determine URL for embedding');
319+
return;
320+
}
321+
link = codeUrl;
332322
}
333323

334-
// Create element with our factory
335-
const buttonElement = wrapperRef.current;
336-
const newElement = createEmbeddableElement(baseUrl, buttonElement);
324+
// Create element directly with ExcalidrawElementFactory
325+
const newElement = ExcalidrawElementFactory.createEmbeddableElement({
326+
link,
327+
width: 600,
328+
height: 400,
329+
});
337330

338-
// Place the element in the scene using our new placement logic
331+
// Place the element in the scene
339332
ExcalidrawElementFactory.placeInScene(newElement, excalidrawAPI, {
340333
mode: PlacementMode.NEAR_VIEWPORT_CENTER,
341334
bufferPercentage: 10,
342335
scrollToView: true
343336
});
344337

345-
console.debug(`[pad.ws] Embedded ${selectedTarget} at URL: ${baseUrl}`);
338+
console.debug(`[pad.ws] Embedded ${selectedTarget}`);
346339

347340
} else if (selectedAction === 'open-tab') {
348-
const baseUrl = getUrl();
349-
if (!baseUrl) {
350-
console.error('Could not determine URL for opening in tab');
351-
return;
341+
if (selectedTarget === 'terminal') {
342+
// For terminal, open the terminal URL in a new tab
343+
if (!workspaceState) {
344+
console.error('Workspace state not available for opening terminal in tab');
345+
return;
346+
}
347+
348+
const terminalUrl = `${workspaceState.base_url}/@${workspaceState.username}/${workspaceState.workspace_id}.${workspaceState.agent}/terminal`;
349+
console.debug(`[pad.ws] Opening terminal in new tab: ${terminalUrl}`);
350+
window.open(terminalUrl, '_blank');
351+
} else {
352+
// For code, open the code URL in a new tab
353+
const codeUrl = getCodeUrl();
354+
if (!codeUrl) {
355+
console.error('Could not determine URL for opening in tab');
356+
return;
357+
}
358+
359+
console.debug(`[pad.ws] Opening ${selectedTarget} in new tab: ${codeUrl}`);
360+
window.open(codeUrl, '_blank');
352361
}
353-
354-
console.debug(`[pad.ws] Opening ${selectedTarget} in new tab from ${baseUrl}`);
355-
window.open(baseUrl, '_blank');
356-
357362
} else if (selectedAction === 'magnet') {
358363
if (!workspaceState) {
359364
console.error('Workspace state not available for magnet link');
@@ -365,14 +370,12 @@ const ActionButton: React.FC<ActionButtonProps> = ({
365370
const url = workspaceState.base_url;
366371
const agent = workspaceState.agent;
367372

368-
let magnetLink = '';
369-
370373
if (selectedTarget === 'terminal') {
371374
console.error('Terminal magnet links are not supported');
372375
return;
373376
} else if (selectedTarget === 'code') {
374377
const prefix = selectedCodeVariant === 'cursor' ? 'cursor' : 'vscode';
375-
magnetLink = `${prefix}://coder.coder-remote/open?owner=${owner}&workspace=${workspace}&url=${url}&token=&openRecent=true&agent=${agent}`;
378+
const magnetLink = `${prefix}://coder.coder-remote/open?owner=${owner}&workspace=${workspace}&url=${url}&token=&openRecent=true&agent=${agent}`;
376379
console.debug(`[pad.ws] Opening ${selectedCodeVariant} desktop app with magnet link: ${magnetLink}`);
377380
window.open(magnetLink, '_blank');
378381
}

0 commit comments

Comments
 (0)