games_app/prediqo_web/src/shared/components/PageHeader.tsx
2026-07-01 16:02:58 +02:00

150 lines
3.8 KiB
TypeScript

import RefreshRoundedIcon from '@mui/icons-material/RefreshRounded';
import { Box, IconButton, Stack, Typography } from '@mui/material';
import type { ReactNode } from 'react';
export type PageHeaderProps = {
icon?: ReactNode;
title: ReactNode;
subtitle?: ReactNode;
/** Control before the page icon (e.g. back button). */
leading?: ReactNode;
/** Right-side actions (refresh, export, etc.). */
actions?: ReactNode;
mb?: number;
};
function renderTitle(title: ReactNode) {
if (typeof title === 'string') {
return (
<Typography
variant="h4"
sx={{ fontWeight: 900, fontSize: { xs: '1.5rem', sm: '2.125rem' }, letterSpacing: 0.2 }}
>
{title}
</Typography>
);
}
return title;
}
function renderSubtitle(subtitle: ReactNode) {
if (typeof subtitle === 'string') {
return (
<Typography variant="body2" color="text.secondary">
{subtitle}
</Typography>
);
}
return subtitle;
}
/**
* Page title row — actions stay on the title row (top-right), subtitle on a second line.
* Matches the Referral program layout on narrow screens.
*/
export function PageHeader({ icon, title, subtitle, leading, actions, mb = 2 }: PageHeaderProps) {
const hasSubtitle = subtitle != null;
const columns = [
leading ? 'auto' : null,
icon ? 'auto' : null,
'minmax(0, 1fr)',
actions != null ? 'auto' : null,
]
.filter(Boolean)
.join(' ');
let col = 1;
const leadingCol = leading ? col++ : 0;
const iconCol = icon ? col++ : 0;
const textCol = col++;
const actionsCol = actions != null ? col : 0;
return (
<Box
sx={{
display: 'grid',
gridTemplateColumns: columns,
gridTemplateRows: hasSubtitle ? 'auto auto' : 'auto',
columnGap: 1.5,
rowGap: 0.5,
alignItems: 'start',
mb,
}}
>
{leading && (
<Box sx={{ gridColumn: leadingCol, gridRow: hasSubtitle ? '1 / -1' : 1, alignSelf: 'start' }}>
{leading}
</Box>
)}
{icon && (
<Box
sx={{
gridColumn: iconCol,
gridRow: hasSubtitle ? '1 / -1' : 1,
alignSelf: 'start',
pt: 0.25,
lineHeight: 0,
}}
>
{icon}
</Box>
)}
<Box sx={{ gridColumn: textCol, gridRow: 1, minWidth: 0, alignSelf: 'center' }}>
{renderTitle(title)}
</Box>
{hasSubtitle && (
<Box sx={{ gridColumn: textCol, gridRow: 2, minWidth: 0 }}>
{renderSubtitle(subtitle)}
</Box>
)}
{actions != null && (
<Box sx={{ gridColumn: actionsCol, gridRow: 1, alignSelf: 'start' }}>
<Stack direction="row" spacing={0.5} useFlexGap sx={{ flexShrink: 0, alignItems: 'center' }}>
{actions}
</Stack>
</Box>
)}
</Box>
);
}
type RefreshIconButtonProps = {
onClick: () => void;
disabled?: boolean;
label?: string;
};
export function RefreshIconButton({ onClick, disabled, label = 'Refresh' }: RefreshIconButtonProps) {
return (
<IconButton aria-label={label} onClick={onClick} disabled={disabled} sx={{ flexShrink: 0 }}>
<RefreshRoundedIcon />
</IconButton>
);
}
type PageSectionHeaderProps = {
title: string;
actions?: ReactNode;
mb?: number;
};
/** Section title inside a card — title and actions stay on one row. */
export function PageSectionHeader({ title, actions, mb = 2 }: PageSectionHeaderProps) {
return (
<Box
sx={{
display: 'grid',
gridTemplateColumns: 'minmax(0, 1fr) auto',
columnGap: 1,
alignItems: 'center',
mb,
}}
>
<Typography variant="h6" sx={{ fontWeight: 800, minWidth: 0 }}>
{title}
</Typography>
{actions != null && <Box sx={{ flexShrink: 0 }}>{actions}</Box>}
</Box>
);
}