refactor(status-page): simplify renderer lookup, extract route prefix, memoize wrapper style
Four small follow-ups from a code review pass: 1. Drop the redundant `?? THEMED_RENDERERS[DEFAULT_STATUS_PAGE_THEME]` fallback in Status/index.tsx — the map is now a total Record, so the first lookup cannot return undefined. 2. Extract the literal "/status/public" into PUBLIC_STATUS_PAGE_PREFIX in Types/StatusPage.ts and reuse from the three runtime call sites (Status/index.tsx, HeaderStatusPageControls.tsx, StatusPagesTable.tsx). The Routes/index.tsx route definition keeps its string literal — the route table is otherwise all literals and the prefix only matters when it's interpolated. 3. Delete two narrating comments above the public/admin branches in Status/index.tsx — the JSX is self-describing. 4. Move the inline `style` object construction in StatusPageThemeProvider into the existing useMemo so the wrapper div receives a stable style reference. The previous spread defeated the surrounding memoization on every render.
This commit is contained in:
1 parent
ed3645c3d0
commit
bab65770ce
5 files changed
+22
-21
No files matched your search
@@ -8,7 +8,7 @@ import { Settings, ExternalLink } from "lucide-react";
|
|||||||
import { useTheme } from "@mui/material";
|
import { useTheme } from "@mui/material";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import type { StatusPage } from "@/Types/StatusPage";
|
import { PUBLIC_STATUS_PAGE_PREFIX, type StatusPage } from "@/Types/StatusPage";
|
||||||
|
|
||||||
interface HeaderStatusPageControlsProps {
|
interface HeaderStatusPageControlsProps {
|
||||||
isAdmin: boolean;
|
isAdmin: boolean;
|
||||||
@@ -50,7 +50,7 @@ export const HeaderStatusPageControls = ({
|
|||||||
<Typography
|
<Typography
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
window.open(
|
window.open(
|
||||||
`/status/public/${statusPage.url}`,
|
`${PUBLIC_STATUS_PAGE_PREFIX}/${statusPage.url}`,
|
||||||
"_blank",
|
"_blank",
|
||||||
"noopener,noreferrer"
|
"noopener,noreferrer"
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import { useGet } from "@/Hooks/UseApi";
|
|||||||
import type { Monitor } from "@/Types/Monitor";
|
import type { Monitor } from "@/Types/Monitor";
|
||||||
import {
|
import {
|
||||||
DEFAULT_STATUS_PAGE_THEME,
|
DEFAULT_STATUS_PAGE_THEME,
|
||||||
|
PUBLIC_STATUS_PAGE_PREFIX,
|
||||||
type StatusPage,
|
type StatusPage,
|
||||||
type StatusPageResponse,
|
type StatusPageResponse,
|
||||||
type StatusPageTheme,
|
type StatusPageTheme,
|
||||||
@@ -44,7 +45,7 @@ const StatusPageView = () => {
|
|||||||
const { url } = useParams();
|
const { url } = useParams();
|
||||||
const isAdmin = useIsAdmin();
|
const isAdmin = useIsAdmin();
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
const isPublic = location.pathname.startsWith("/status/public");
|
const isPublic = location.pathname.startsWith(PUBLIC_STATUS_PAGE_PREFIX);
|
||||||
|
|
||||||
const apiUrl = url ? `/status-page/${url}?type=uptime&type=infrastructure` : null;
|
const apiUrl = url ? `/status-page/${url}?type=uptime&type=infrastructure` : null;
|
||||||
|
|
||||||
@@ -88,9 +89,7 @@ const StatusPageView = () => {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const ThemedRenderer =
|
const ThemedRenderer = THEMED_RENDERERS[statusPage.theme ?? DEFAULT_STATUS_PAGE_THEME];
|
||||||
THEMED_RENDERERS[statusPage.theme ?? DEFAULT_STATUS_PAGE_THEME] ??
|
|
||||||
THEMED_RENDERERS[DEFAULT_STATUS_PAGE_THEME];
|
|
||||||
const themedRenderer = (
|
const themedRenderer = (
|
||||||
<ThemedRenderer
|
<ThemedRenderer
|
||||||
statusPage={statusPage}
|
statusPage={statusPage}
|
||||||
@@ -98,7 +97,6 @@ const StatusPageView = () => {
|
|||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
|
||||||
// Public route: render directly on the viewport, themed background covers everything.
|
|
||||||
if (isPublic) {
|
if (isPublic) {
|
||||||
return (
|
return (
|
||||||
<StatusPageThemeProvider
|
<StatusPageThemeProvider
|
||||||
@@ -111,8 +109,7 @@ const StatusPageView = () => {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Admin preview: wrap in a mock browser frame inside the admin shell.
|
const publicUrl = `${window.location.origin}${PUBLIC_STATUS_PAGE_PREFIX}/${statusPage.url}`;
|
||||||
const publicUrl = `${window.location.origin}/status/public/${statusPage.url}`;
|
|
||||||
return (
|
return (
|
||||||
<BasePage
|
<BasePage
|
||||||
loading={isLoading}
|
loading={isLoading}
|
||||||
|
|||||||
@@ -105,7 +105,17 @@ export const StatusPageThemeProvider = ({
|
|||||||
resolvedThemeMode === "auto" ? systemMode : resolvedThemeMode;
|
resolvedThemeMode === "auto" ? systemMode : resolvedThemeMode;
|
||||||
const tokens = themeTokens[resolvedTheme][resolvedMode];
|
const tokens = themeTokens[resolvedTheme][resolvedMode];
|
||||||
|
|
||||||
const style = useMemo(() => toCssVars(tokens) as React.CSSProperties, [tokens]);
|
const style = useMemo<React.CSSProperties>(
|
||||||
|
() =>
|
||||||
|
transparent
|
||||||
|
? (toCssVars(tokens) as React.CSSProperties)
|
||||||
|
: {
|
||||||
|
...(toCssVars(tokens) as React.CSSProperties),
|
||||||
|
background: tokens.bg,
|
||||||
|
minHeight: "100vh",
|
||||||
|
},
|
||||||
|
[tokens, transparent]
|
||||||
|
);
|
||||||
|
|
||||||
const value = useMemo(
|
const value = useMemo(
|
||||||
() => ({ theme: resolvedTheme, mode: resolvedMode, tokens }),
|
() => ({ theme: resolvedTheme, mode: resolvedMode, tokens }),
|
||||||
@@ -118,15 +128,7 @@ export const StatusPageThemeProvider = ({
|
|||||||
data-status-theme={resolvedTheme}
|
data-status-theme={resolvedTheme}
|
||||||
data-status-mode={resolvedMode}
|
data-status-mode={resolvedMode}
|
||||||
data-status-transparent={transparent ? "" : undefined}
|
data-status-transparent={transparent ? "" : undefined}
|
||||||
style={
|
style={style}
|
||||||
transparent
|
|
||||||
? (style as React.CSSProperties)
|
|
||||||
: {
|
|
||||||
...style,
|
|
||||||
background: tokens.bg,
|
|
||||||
minHeight: "100vh",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
>
|
>
|
||||||
{children}
|
{children}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import { useTranslation } from "react-i18next";
|
|||||||
import { useTheme } from "@mui/material/styles";
|
import { useTheme } from "@mui/material/styles";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
|
|
||||||
import type { StatusPage } from "@/Types/StatusPage";
|
import { PUBLIC_STATUS_PAGE_PREFIX, type StatusPage } from "@/Types/StatusPage";
|
||||||
|
|
||||||
interface StatusPagesTableProps {
|
interface StatusPagesTableProps {
|
||||||
data: StatusPage[];
|
data: StatusPage[];
|
||||||
@@ -50,7 +50,7 @@ export const StatusPagesTable = ({
|
|||||||
const handleUrlClick = (e: React.MouseEvent, row: StatusPage) => {
|
const handleUrlClick = (e: React.MouseEvent, row: StatusPage) => {
|
||||||
if (row.isPublished) {
|
if (row.isPublished) {
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
const url = `/status/public/${row.url}`;
|
const url = `${PUBLIC_STATUS_PAGE_PREFIX}/${row.url}`;
|
||||||
window.open(url, "_blank", "noopener,noreferrer");
|
window.open(url, "_blank", "noopener,noreferrer");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -29,6 +29,8 @@ export const STATUS_PAGE_THEME_MODES = ["auto", "light", "dark"] as const;
|
|||||||
export type StatusPageThemeMode = (typeof STATUS_PAGE_THEME_MODES)[number];
|
export type StatusPageThemeMode = (typeof STATUS_PAGE_THEME_MODES)[number];
|
||||||
export const DEFAULT_STATUS_PAGE_THEME_MODE: StatusPageThemeMode = "auto";
|
export const DEFAULT_STATUS_PAGE_THEME_MODE: StatusPageThemeMode = "auto";
|
||||||
|
|
||||||
|
export const PUBLIC_STATUS_PAGE_PREFIX = "/status/public";
|
||||||
|
|
||||||
export interface StatusPage {
|
export interface StatusPage {
|
||||||
id: string;
|
id: string;
|
||||||
userId: string;
|
userId: string;
|
||||||
|
|||||||
Reference in new issue
Block a user