Skip to content

Commit 902c34c

Browse files
refactor: improve apps.ts readbility (#17741)
Apply PR comments from #17724
1 parent 2bdd035 commit 902c34c

File tree

4 files changed

+27
-25
lines changed

4 files changed

+27
-25
lines changed

site/migrate-icons.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Look for all the @mui/icons-material icons below and replace them accordinlying with the Lucide icon:
2+
3+
MUI | Lucide
4+
TaskAlt | CircleCheckBigIcon
5+
InfoOutlined | InfoIcon
6+
ErrorOutline | CircleAlertIcon
7+
8+
You should update the imports and usage.

site/src/modules/apps/apps.ts

+16-22
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,11 @@ export const getAppHref = (
8383
: app.url;
8484
}
8585

86-
// The backend redirects if the trailing slash isn't included, so we add it
87-
// here to avoid extra roundtrips.
88-
let href = `${path}/@${workspace.owner_name}/${workspace.name}.${
89-
agent.name
90-
}/apps/${encodeURIComponent(app.slug)}/`;
91-
9286
if (app.command) {
9387
// Terminal links are relative. The terminal page knows how
9488
// to select the correct workspace proxy for the websocket
9589
// connection.
96-
href = `/@${workspace.owner_name}/${workspace.name}.${
90+
return `/@${workspace.owner_name}/${workspace.name}.${
9791
agent.name
9892
}/terminal?command=${encodeURIComponent(app.command)}`;
9993
}
@@ -102,23 +96,14 @@ export const getAppHref = (
10296
const baseUrl = `${window.location.protocol}//${host.replace(/\*/g, app.subdomain_name)}`;
10397
const url = new URL(baseUrl);
10498
url.pathname = "/";
105-
href = url.toString();
106-
}
107-
108-
return href;
109-
};
110-
111-
export const needsSessionToken = (app: WorkspaceApp) => {
112-
if (!isExternalApp(app)) {
113-
return false;
99+
return url.toString();
114100
}
115101

116-
// HTTP links should never need the session token, since Cookies
117-
// handle sharing it when you access the Coder Dashboard. We should
118-
// never be forwarding the bare session token to other domains!
119-
const isHttp = app.url.startsWith("http");
120-
const requiresSessionToken = app.url.includes(SESSION_TOKEN_PLACEHOLDER);
121-
return requiresSessionToken && !isHttp;
102+
// The backend redirects if the trailing slash isn't included, so we add it
103+
// here to avoid extra roundtrips.
104+
return `${path}/@${workspace.owner_name}/${workspace.name}.${
105+
agent.name
106+
}/apps/${encodeURIComponent(app.slug)}/`;
122107
};
123108

124109
type ExternalWorkspaceApp = WorkspaceApp & {
@@ -131,3 +116,12 @@ export const isExternalApp = (
131116
): app is ExternalWorkspaceApp => {
132117
return app.external && app.url !== undefined;
133118
};
119+
120+
export const needsSessionToken = (app: ExternalWorkspaceApp) => {
121+
// HTTP links should never need the session token, since Cookies
122+
// handle sharing it when you access the Coder Dashboard. We should
123+
// never be forwarding the bare session token to other domains!
124+
const isHttp = app.url.startsWith("http");
125+
const requiresSessionToken = app.url.includes(SESSION_TOKEN_PLACEHOLDER);
126+
return requiresSessionToken && !isHttp;
127+
};

site/src/modules/apps/useAppLink.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export const useAppLink = (
3434
const href = getAppHref(app, {
3535
agent,
3636
workspace,
37-
token: apiKeyResponse?.key ?? "",
37+
token: apiKeyResponse?.key,
3838
path: proxy.preferredPathAppURL,
3939
host: proxy.preferredWildcardHostname,
4040
});

site/src/modules/resources/AppLink/AppLink.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
TooltipTrigger,
1010
} from "components/Tooltip/Tooltip";
1111
import { useProxy } from "contexts/ProxyContext";
12-
import { needsSessionToken } from "modules/apps/apps";
12+
import { isExternalApp, needsSessionToken } from "modules/apps/apps";
1313
import { useAppLink } from "modules/apps/useAppLink";
1414
import { type FC, useState } from "react";
1515
import { AgentButton } from "../AgentButton";
@@ -65,7 +65,7 @@ export const AppLink: FC<AppLinkProps> = ({ app, workspace, agent }) => {
6565
"Your admin has not configured subdomain application access";
6666
}
6767

68-
if (needsSessionToken(app) && !link.hasToken) {
68+
if (isExternalApp(app) && needsSessionToken(app) && !link.hasToken) {
6969
canClick = false;
7070
}
7171

0 commit comments

Comments
 (0)