Files
Investidor-V2/resources/js/components/portal-grouped-menu.tsx
T
nala c13f250b87
tests / ci (push) Canceled after 0s
versao compilada
2026-10-15 17:37:00 +02:00

245 lines
11 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { ProfileAccess } from '@/types/profile-access';
import { Link, router, usePage } from '@inertiajs/react';
import { LogOut } from 'lucide-react';
import { logout } from '@/routes';
import { useEffect, useId, useRef, useState } from 'react';
import type { OpportunityManagementAccess } from '@/types/opportunity-management';
import {
portalNavigation,
currentNavigationGroup,
currentNavigationItem,
type PortalNavItem,
} from '@/lib/portal-navigation';
export function PortalGroupedMenu() {
const { props, url } = usePage<{
auth: {
user: { name?: string; email?: string } | null;
canManagePortal: boolean;
};
existingBusinessEnabled?: boolean;
businessReviewUrl?: string | null;
educationEnabled?: boolean;
aiEnabled?: boolean;
legacyContentEnabled?: boolean;
portalOperator?: string | null;
profileAccess?: ProfileAccess | null;
opportunityManagement?: OpportunityManagementAccess | null;
}>();
const groups = portalNavigation({
authenticated: !!props.auth?.user,
manager: !!props.auth?.canManagePortal,
businesses: props.existingBusinessEnabled,
businessReviewUrl: props.businessReviewUrl,
education: props.educationEnabled,
ai: props.aiEnabled,
legacyContent: props.legacyContentEnabled,
opportunityManagement: props.opportunityManagement,
profileAccess: props.profileAccess,
});
const current = currentNavigationGroup(groups, url);
const currentItem = currentNavigationItem(groups, url);
const [open, setOpen] = useState<string | null>(null);
const [branch, setBranch] = useState<string | null>(null);
const [offset, setOffset] = useState(0);
const [loggingOut, setLoggingOut] = useState(false);
const root = useRef<HTMLElement>(null);
const id = useId();
useEffect(() => {
const position = () => {
const button = open
? document.getElementById(`${id}-${open}-button`)
: null;
if (!button || window.innerWidth < 640) {
setOffset(0);
return;
}
const rect = button.getBoundingClientRect();
const width = Math.min(288, window.innerWidth - 32);
setOffset(
Math.max(
16,
Math.min(rect.left, window.innerWidth - width - 16),
) - rect.left,
);
};
position();
window.addEventListener('resize', position);
return () => window.removeEventListener('resize', position);
}, [open, id]);
useEffect(() => {
const outside = (event: PointerEvent) => {
if (
event.target instanceof Node &&
!root.current?.contains(event.target)
) {
setOpen(null);
setBranch(null);
}
};
document.addEventListener('pointerdown', outside);
return () => document.removeEventListener('pointerdown', outside);
}, []);
const close = () => {
setOpen(null);
setBranch(null);
};
const items = (children: PortalNavItem[]) =>
children.map((item) => (
<li key={item.id}>
{item.children ? (
<>
<button
type="button"
id={`${id}-${item.id}-button`}
className="flex w-full items-center justify-between rounded-lg px-3 py-2 text-left font-semibold hover:bg-emerald-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-emerald-700"
aria-expanded={branch === item.id}
aria-controls={`${id}-${item.id}`}
onClick={() =>
setBranch(branch === item.id ? null : item.id)
}
>
{item.label}
<span aria-hidden="true">
{branch === item.id ? '−' : '+'}
</span>
</button>
{branch === item.id && (
<ul
id={`${id}-${item.id}`}
className="ml-3 space-y-1 border-l-2 border-emerald-200 pl-2"
>
{items(item.children)}
</ul>
)}
</>
) : (
<Link
href={item.href!}
onClick={close}
aria-current={
currentItem === item.id ? 'page' : undefined
}
className="block rounded-lg px-3 py-2 hover:bg-emerald-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-emerald-700"
>
{item.label}
</Link>
)}
</li>
));
return (
<header
ref={root}
className="relative z-40 border-b border-slate-200 bg-white text-slate-900"
onKeyDown={(event) => {
if (event.key !== 'Escape') return;
event.preventDefault();
if (branch) {
document.getElementById(`${id}-${branch}-button`)?.focus();
setBranch(null);
} else if (open) {
document.getElementById(`${id}-${open}-button`)?.focus();
close();
}
}}
>
<div className="mx-auto max-w-7xl px-4 py-4 sm:px-6">
<div className="mb-3 flex flex-wrap items-center justify-between gap-2">
<Link
href="/"
className="text-xl font-extrabold tracking-tight text-emerald-900"
>
InvesteMoz
</Link>
<p className="max-w-2xl text-xs leading-relaxed text-slate-600">
Iniciativa privada e independente de apoio ao
investimento. Orientação e acompanhamento internos;
decisões oficiais cabem às instituições competentes.
{props.portalOperator && (
<> Operador: {props.portalOperator}.</>
)}
</p>
</div>
{props.auth?.user && (
<nav
aria-label="Sessão da conta"
className="mb-3 flex flex-wrap items-center justify-between gap-3 rounded-lg bg-slate-50 px-3 py-2"
>
<p className="min-w-0 text-sm [overflow-wrap:anywhere] break-words text-slate-700">
Sessão:{' '}
{props.auth.user.name || props.auth.user.email}
</p>
<Link
href={logout()}
as="button"
type="button"
disabled={loggingOut}
onClick={() => {
close();
router.flushAll();
}}
onStart={() => setLoggingOut(true)}
onFinish={() => setLoggingOut(false)}
data-test="portal-logout-button"
className="inline-flex min-h-11 shrink-0 items-center gap-2 rounded-lg border border-slate-400 bg-white px-3 py-2 text-sm font-semibold text-slate-900 hover:bg-slate-100 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-emerald-700 disabled:cursor-wait disabled:opacity-60"
>
<LogOut aria-hidden="true" className="size-4" />
{loggingOut
? 'A terminar sessão…'
: 'Terminar sessão'}
</Link>
</nav>
)}
<nav aria-label="Áreas do portal">
<ul className="flex flex-wrap gap-2">
{groups.map((group) => (
<li
key={group.id}
className="relative w-full sm:w-auto"
>
<button
id={`${id}-${group.id}-button`}
type="button"
className={`flex min-h-11 w-full items-center justify-between gap-2 rounded-lg border px-3 py-2 text-left text-sm font-semibold focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-emerald-700 ${current === group.id ? 'border-emerald-700 bg-emerald-50 text-emerald-950' : 'border-slate-200 hover:bg-slate-50'}`}
aria-expanded={open === group.id}
aria-controls={`${id}-${group.id}`}
data-current-group={
current === group.id || undefined
}
onClick={() => {
setOpen(
open === group.id ? null : group.id,
);
setBranch(null);
}}
>
{group.label}
<span aria-hidden="true">
{open === group.id ? '▴' : '▾'}
</span>
{current === group.id && (
<span className="sr-only">
Grupo actual
</span>
)}
</button>
{open === group.id && (
<ul
id={`${id}-${group.id}`}
style={{
transform: `translateX(${offset}px)`,
}}
className="mt-2 max-h-[min(70vh,32rem)] w-full space-y-1 overflow-y-auto rounded-xl border border-slate-200 bg-white p-2 text-sm shadow-xl sm:absolute sm:top-full sm:left-0 sm:z-40 sm:w-72 sm:max-w-[calc(100vw-2rem)]"
>
{items(group.children ?? [])}
</ul>
)}
</li>
))}
</ul>
</nav>
</div>
</header>
);
}