Skip to content

Commit 9921bad

Browse files
Enhance Header component to conditionally render UserProfile based on auth status and improve code readability
1 parent 2521cae commit 9921bad

2 files changed

Lines changed: 34 additions & 17 deletions

File tree

src/frontend/declarations.d.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,16 @@ declare module "*.png" {
22
const value: string;
33
export default value;
44
}
5-
5+
6+
interface AppConfig {
7+
API_URL?: string;
8+
REACT_APP_MSAL_AUTH_CLIENTID?: string;
9+
REACT_APP_MSAL_AUTH_AUTHORITY?: string;
10+
REACT_APP_MSAL_REDIRECT_URL?: string;
11+
REACT_APP_MSAL_POST_REDIRECT_URL?: string;
12+
ENABLE_AUTH?: boolean;
13+
}
14+
15+
interface Window {
16+
appConfig?: AppConfig;
17+
}

src/frontend/src/components/Header/Header.tsx

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@ type HeaderProps = {
2020

2121
// Determine once whether MSAL authentication is enabled, so the hooks inside
2222
// UserProfile (which require MsalProvider in the tree) are only mounted when safe.
23+
// window.appConfig is set in main.jsx after fetching /config; falls back to
24+
// false when the config has not loaded or auth is disabled.
2325
const isAuthEnabled = (): boolean => {
24-
// window.appConfig is set in main.jsx after fetching /config
25-
// Falls back to false when the config has not loaded or auth is disabled.
26-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
27-
const cfg = (typeof window !== "undefined" ? (window as any).appConfig : null);
28-
return Boolean(cfg && cfg.ENABLE_AUTH);
26+
if (typeof window === "undefined") return false;
27+
return Boolean(window.appConfig && window.appConfig.ENABLE_AUTH);
2928
};
3029

3130
const Header: React.FC<HeaderProps> = ({ title = "Contoso", subtitle, children }) => {
31+
const authEnabled = isAuthEnabled();
32+
const hasToolbarContent = React.Children.count(children) > 0 || authEnabled;
33+
3234
return (
3335
<header
3436
style={{
@@ -68,17 +70,20 @@ const Header: React.FC<HeaderProps> = ({ title = "Contoso", subtitle, children }
6870
</Subtitle2>
6971
</div>
7072

71-
{/* HEADER TOOLBAR (rendered only if passed as a child) */}
72-
<div
73-
style={{
74-
display: "flex",
75-
alignItems: "center",
76-
gap: "8px",
77-
}}
78-
>
79-
{children}
80-
{isAuthEnabled() && <UserProfile />}
81-
</div>
73+
{/* HEADER TOOLBAR (rendered only when there is toolbar content
74+
or the auth-enabled user profile menu to display) */}
75+
{hasToolbarContent && (
76+
<div
77+
style={{
78+
display: "flex",
79+
alignItems: "center",
80+
gap: "8px",
81+
}}
82+
>
83+
{children}
84+
{authEnabled && <UserProfile />}
85+
</div>
86+
)}
8287
</header>
8388
);
8489
};

0 commit comments

Comments
 (0)