Skip to content

Commit 10cc4bd

Browse files
authored
dashboard: fix and improve build (#986)
* fix deps not built before building dashboard Signed-off-by: Teo Koon Peng <[email protected]> * cleanup old config; make adminTab and customTab optional Signed-off-by: Teo Koon Peng <[email protected]> * allow authenticator to be tree shaked; rudimetry way to have runtime args Signed-off-by: Teo Koon Peng <[email protected]> * allow custom tabs and admin tags to be tree shaken Signed-off-by: Teo Koon Peng <[email protected]> * actually tree shake out admin tab Signed-off-by: Teo Koon Peng <[email protected]> * put all build time config in separate object Signed-off-by: Teo Koon Peng <[email protected]> * fallback to test config Signed-off-by: Teo Koon Peng <[email protected]> * add base url to build config Signed-off-by: Teo Koon Peng <[email protected]> * add __init__.py so older setuptools sees ros_pydantic Signed-off-by: Teo Koon Peng <[email protected]> * use header logo in login page Signed-off-by: Teo Koon Peng <[email protected]> --------- Signed-off-by: Teo Koon Peng <[email protected]>
1 parent b4c2b06 commit 10cc4bd

File tree

13 files changed

+204
-79
lines changed

13 files changed

+204
-79
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
"prettier --write"
2828
],
2929
"**/*.py": [
30-
".venv/bin/isort",
31-
".venv/bin/black"
30+
".venv/bin/pipenv run isort",
31+
".venv/bin/pipenv run black"
3232
]
3333
},
3434
"overrides": {

packages/api-server/api_server/models/ros_pydantic/__init__.py

Whitespace-only changes.

packages/dashboard/app-config.d.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
export interface RobotResource {
2+
icon?: string;
3+
scale?: number;
4+
}
5+
6+
export interface FleetResource {
7+
default: RobotResource;
8+
}
9+
10+
export interface LogoResource {
11+
header: string;
12+
}
13+
14+
export interface Resources {
15+
fleets: Record<string, FleetResource>;
16+
logos: LogoResource;
17+
}
18+
19+
export interface TaskResource {
20+
taskDefinitionId: string;
21+
displayName?: string;
22+
}
23+
24+
export interface StubAuthConfig {
25+
provider: 'stub';
26+
config: null;
27+
}
28+
29+
export interface KeycloakAuthConfig {
30+
provider: 'keycloak';
31+
config: {
32+
url: string;
33+
realm: string;
34+
clientId: string;
35+
};
36+
}
37+
38+
export interface AppConfigInput {
39+
rmfServerUrl: string;
40+
trajectoryServerUrl: string;
41+
auth: KeycloakAuthConfig | StubAuthConfig;
42+
helpLink: string;
43+
reportIssue: string;
44+
pickupZones: string[];
45+
defaultZoom: number;
46+
defaultRobotZoom: number;
47+
attributionPrefix: string;
48+
defaultMapLevel: string;
49+
allowedTasks: TaskResource[];
50+
resources: Record<string, Resources> & Record<'default', Resources>;
51+
customTabs?: boolean;
52+
adminTab?: boolean;
53+
// FIXME(koonpeng): this is used for very specific tasks, should be removed when mission
54+
// system is implemented.
55+
cartIds: string[];
56+
}

packages/dashboard/app-config.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
{
22
"rmfServerUrl": "http://localhost:8000",
33
"trajectoryServerUrl": "ws://localhost:8006",
4-
"auth": {
5-
"provider": "stub"
6-
},
4+
"authConfig": {},
75
"helpLink": "https://osrf.github.io/ros2multirobotbook/rmf-core.html",
86
"reportIssue": "https://github.com/open-rmf/rmf-web/issues",
97
"pickupZones": [],
@@ -33,7 +31,11 @@
3331
}
3432
}
3533
},
36-
"customTabs": false,
37-
"adminTab": false,
38-
"cartIds": []
34+
"cartIds": [],
35+
"buildConfig": {
36+
"baseUrl": "/",
37+
"authProvider": "stub",
38+
"customTabs": false,
39+
"adminTab": false
40+
}
3941
}

packages/dashboard/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!DOCTYPE html>
1+
<!doctype html>
22
<html lang="en">
33
<head>
44
<meta charset="utf-8" />

packages/dashboard/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"type": "module",
55
"private": true,
66
"scripts": {
7-
"build": "vite build",
7+
"build": "pnpm run --filter {.}^... build && vite build",
88
"build-storybook": "storybook build",
99
"lint": "tsc --build && eslint --max-warnings 0 src",
1010
"start": "concurrently npm:start:rmf-server npm:start:react",

packages/dashboard/src/app-config.ts

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import { getDefaultTaskDefinition, TaskDefinition } from 'react-components';
33

4-
import appConfigJson from '../app-config.json';
4+
import testConfig from '../app-config.json';
55
import { Authenticator, KeycloakAuthenticator, StubAuthenticator } from './auth';
66
import { BasePath } from './util/url';
77

@@ -28,27 +28,18 @@ export interface TaskResource {
2828
displayName?: string;
2929
}
3030

31-
export interface StubAuthConfig {
32-
provider: 'stub';
33-
}
31+
export interface StubAuthConfig {}
3432

3533
export interface KeycloakAuthConfig {
36-
provider: 'keycloak';
37-
config: {
38-
url: string;
39-
realm: string;
40-
clientId: string;
41-
};
42-
}
43-
44-
export interface AuthConfig {
45-
provider: string;
34+
url: string;
35+
realm: string;
36+
clientId: string;
4637
}
4738

48-
export interface AppConfig {
39+
export interface RuntimeConfig {
4940
rmfServerUrl: string;
5041
trajectoryServerUrl: string;
51-
auth: KeycloakAuthConfig | StubAuthConfig;
42+
authConfig: KeycloakAuthConfig | StubAuthConfig;
5243
helpLink: string;
5344
reportIssue: string;
5445
pickupZones: string[];
@@ -58,32 +49,48 @@ export interface AppConfig {
5849
defaultMapLevel: string;
5950
allowedTasks: TaskResource[];
6051
resources: Record<string, Resources> & Record<'default', Resources>;
61-
customTabs: boolean;
62-
adminTab: boolean;
6352
// FIXME(koonpeng): this is used for very specific tasks, should be removed when mission
6453
// system is implemented.
6554
cartIds: string[];
6655
}
6756

68-
// we specifically don't export app config to force consumers to use the context.
69-
const appConfig: AppConfig = appConfigJson as AppConfig;
57+
// these will be injected as defines and potentially be tree shaken out
58+
export interface BuildConfig {
59+
baseUrl: string;
60+
authProvider: 'keycloak' | 'stub';
61+
customTabs?: boolean;
62+
adminTab?: boolean;
63+
}
64+
65+
export interface AppConfig extends RuntimeConfig {
66+
buildConfig: BuildConfig;
67+
}
68+
69+
declare const APP_CONFIG: AppConfig;
70+
71+
const appConfig: AppConfig = (() => {
72+
if (import.meta.env.PROD) {
73+
return APP_CONFIG;
74+
} else {
75+
// globals cannot be injected in tests so we need a fallback, this should be
76+
// removed by terser in prod builds.
77+
return testConfig as AppConfig;
78+
}
79+
})();
7080

7181
export const AppConfigContext = React.createContext(appConfig);
7282

7383
const authenticator: Authenticator = (() => {
74-
switch (appConfig.auth.provider) {
75-
case 'keycloak':
76-
if (!import.meta.env.VITE_KEYCLOAK_CONFIG) {
77-
throw new Error('missing VITE_KEYCLOAK_CONFIG');
78-
}
79-
return new KeycloakAuthenticator(
80-
appConfig.auth.config,
81-
`${window.location.origin}${BasePath}/silent-check-sso.html`,
82-
);
83-
case 'stub':
84-
return new StubAuthenticator();
85-
default:
86-
throw new Error('unknown auth provider');
84+
// must use if statement instead of switch for vite tree shaking to work
85+
if (APP_CONFIG_AUTH_PROVIDER === 'keycloak') {
86+
return new KeycloakAuthenticator(
87+
APP_CONFIG.authConfig as KeycloakAuthConfig,
88+
`${window.location.origin}${BasePath}/silent-check-sso.html`,
89+
);
90+
} else if (APP_CONFIG_AUTH_PROVIDER === 'stub') {
91+
return new StubAuthenticator();
92+
} else {
93+
throw new Error('unknown auth provider');
8794
}
8895
})();
8996

packages/dashboard/src/components/app.tsx

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -96,32 +96,38 @@ export default function App(): JSX.Element | null {
9696
}
9797
/>
9898

99-
<Route
100-
path={CustomRoute1}
101-
element={
102-
<PrivateRoute unauthorizedComponent={loginRedirect} user={user}>
103-
<ManagedWorkspace key="custom1" workspaceId="custom1" />
104-
</PrivateRoute>
105-
}
106-
/>
99+
{APP_CONFIG_ENABLE_CUSTOM_TABS && (
100+
<Route
101+
path={CustomRoute1}
102+
element={
103+
<PrivateRoute unauthorizedComponent={loginRedirect} user={user}>
104+
<ManagedWorkspace key="custom1" workspaceId="custom1" />
105+
</PrivateRoute>
106+
}
107+
/>
108+
)}
107109

108-
<Route
109-
path={CustomRoute2}
110-
element={
111-
<PrivateRoute unauthorizedComponent={loginRedirect} user={user}>
112-
<ManagedWorkspace key="custom2" workspaceId="custom2" />
113-
</PrivateRoute>
114-
}
115-
/>
110+
{APP_CONFIG_ENABLE_CUSTOM_TABS && (
111+
<Route
112+
path={CustomRoute2}
113+
element={
114+
<PrivateRoute unauthorizedComponent={loginRedirect} user={user}>
115+
<ManagedWorkspace key="custom2" workspaceId="custom2" />
116+
</PrivateRoute>
117+
}
118+
/>
119+
)}
116120

117-
<Route
118-
path={AdminRoute}
119-
element={
120-
<PrivateRoute unauthorizedComponent={loginRedirect} user={user}>
121-
<AdminRouter />
122-
</PrivateRoute>
123-
}
124-
/>
121+
{APP_CONFIG_ENABLE_ADMIN_TAB && (
122+
<Route
123+
path={AdminRoute}
124+
element={
125+
<PrivateRoute unauthorizedComponent={loginRedirect} user={user}>
126+
<AdminRouter />
127+
</PrivateRoute>
128+
}
129+
/>
130+
)}
125131
</Routes>
126132
</AppBase>
127133
</RmfApp>
@@ -132,7 +138,7 @@ export default function App(): JSX.Element | null {
132138
element={
133139
<LoginPage
134140
title={'Dashboard'}
135-
logo="assets/defaultLogo.png"
141+
logo={resources.logos.header}
136142
onLoginClick={() =>
137143
authenticator.login(`${window.location.origin}${DashboardRoute}`)
138144
}

packages/dashboard/src/components/appbar.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ export const AppBar = React.memo(({ extraToolbarItems }: AppBarProps): React.Rea
344344
aria-label="Tasks"
345345
onTabClick={() => navigate(TasksRoute)}
346346
/>
347-
{appConfig.customTabs && (
347+
{APP_CONFIG_ENABLE_CUSTOM_TABS && (
348348
<>
349349
<StyledAppBarTab
350350
label="Custom 1"
@@ -360,7 +360,7 @@ export const AppBar = React.memo(({ extraToolbarItems }: AppBarProps): React.Rea
360360
/>
361361
</>
362362
)}
363-
{appConfig.adminTab && profile?.user.is_admin && (
363+
{APP_CONFIG_ENABLE_ADMIN_TAB && profile?.user.is_admin && (
364364
<StyledAppBarTab
365365
label="Admin"
366366
value="admin"

packages/dashboard/src/vite-env.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
/// <reference types="vite/client" />
2+
3+
declare const APP_CONFIG_BASE_URL: string;
4+
declare const APP_CONFIG_AUTH_PROVIDER: string;
5+
declare const APP_CONFIG_ENABLE_CUSTOM_TABS: boolean;
6+
declare const APP_CONFIG_ENABLE_ADMIN_TAB: boolean;

0 commit comments

Comments
 (0)