Map gender analytics from API and show gender and education breakdowns.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Yared Yemane 2026-08-05 13:09:09 +03:00
parent feea83a588
commit ef170e7fbd
12 changed files with 130 additions and 25 deletions

View File

@ -139,6 +139,7 @@ function normalizeDashboardUsers(raw: unknown, root?: Record<string, unknown>):
by_role: asLabelCounts(pickField(u, "by_role", "byRole", "ByRole")),
by_status: asLabelCounts(pickField(u, "by_status", "byStatus", "ByStatus")),
by_age_group: asLabelCounts(pickField(u, "by_age_group", "byAgeGroup", "ByAgeGroup")),
by_gender: asLabelCounts(pickField(u, "by_gender", "byGender", "ByGender")),
by_education_level: asLabelCounts(
pickField(u, ...EDUCATION_LEVEL_KEYS) ?? pickField(scope, ...EDUCATION_LEVEL_KEYS),
),

View File

@ -1,6 +1,7 @@
import { Navigate, Route, Routes } from "react-router-dom";
import { getDefaultAppHome } from "../lib/adminAccess";
import { getNormalizedSessionTeamRole } from "../lib/teamRole";
import { FullPanelGuard } from "../components/access/AdminAccessGates";
import { AppLayout } from "../layouts/AppLayout";
import { DashboardPage } from "../pages/DashboardPage";
import { AnalyticsPage } from "../pages/analytics/AnalyticsPage";
@ -94,6 +95,8 @@ export function AppRoutes() {
<Route path="/account-deletion" element={<AccountDeletionPage />} />
<Route element={<AppLayout />}>
<Route path="/" element={<Navigate to={getDefaultAppHome(getNormalizedSessionTeamRole())} replace />} />
<Route element={<FullPanelGuard />}>
<Route path="/dashboard" element={<DashboardPage />} />
<Route path="/users" element={<UserManagementLayout />}>
<Route index element={<Navigate to="list" replace />} />
@ -107,6 +110,7 @@ export function AppRoutes() {
<Route index element={<RolesListPage />} />
<Route path="add" element={<AddRolePage />} />
</Route>
</Route>
<Route path="/content" element={<ContentManagementLayout />}>
<Route index element={<Navigate to="practices" replace />} />
@ -323,7 +327,12 @@ export function AppRoutes() {
element={<LinkExistingPracticeFlow />}
/>
<Route path="/personas" element={<PersonasPage />} />
<Route path="/admin/personas" element={<PersonasPage />} />
<Route path="/profile" element={<ProfilePage />} />
<Route path="/notifications" element={<NotificationsPage />} />
<Route element={<FullPanelGuard />}>
<Route
path="/notifications/email-templates"
element={<EmailTemplatesPage />}
@ -354,15 +363,13 @@ export function AppRoutes() {
<Route path="/admin/app-reviews" element={<AppReviewsPage />} />
<Route path="/operations/app-reviews" element={<AppReviewsPage />} />
<Route path="/ratings" element={<AppReviewsPage />} />
<Route path="/personas" element={<PersonasPage />} />
<Route path="/admin/personas" element={<PersonasPage />} />
<Route path="/analytics" element={<AnalyticsPage />} />
<Route path="/team" element={<TeamManagementPage />} />
<Route path="/team/add" element={<AddTeamMemberPage />} />
<Route path="/team/:id" element={<TeamMemberDetailPage />} />
<Route path="/profile" element={<ProfilePage />} />
<Route path="/settings" element={<SettingsPage />} />
</Route>
</Route>
<Route path="*" element={<NotFoundPage />} />

View File

@ -0,0 +1,31 @@
import type { ReactNode } from "react"
import { Navigate, Outlet } from "react-router-dom"
import { useAdminAccess } from "../../hooks/useAdminAccess"
type GateProps = {
children: ReactNode
fallback?: ReactNode
}
/** Renders children only for Admin and Super Admin (not Content Manager). */
export function FullPanelOnly({ children, fallback = null }: GateProps) {
const { hasFullPanel } = useAdminAccess()
if (!hasFullPanel) return <>{fallback}</>
return <>{children}</>
}
/** Renders children only for Super Admin (e.g. team email invites). */
export function SuperAdminOnly({ children, fallback = null }: GateProps) {
const { canInviteTeam } = useAdminAccess()
if (!canInviteTeam) return <>{fallback}</>
return <>{children}</>
}
/** Route layout: redirects Content Manager to their home route. */
export function FullPanelGuard() {
const { hasFullPanel, defaultHome } = useAdminAccess()
if (!hasFullPanel) {
return <Navigate to={defaultHome} replace />
}
return <Outlet />
}

View File

@ -28,9 +28,7 @@ import { hasPersonaPermission } from "../../lib/personasPermissions";
import { hasActivityLogPermission } from "../../lib/activityLogPermissions";
import { hasExportPermission } from "../../lib/exportPermissions";
import { SidebarNavGroup } from "./SidebarNavGroup";
import {
getNormalizedSessionTeamRole,
} from "../../lib/teamRole";
import { useAdminAccess } from "../../hooks/useAdminAccess";
import { isNavEntryAllowedForTeamRole } from "../../lib/adminAccess";
type NavLinkItem = {
@ -168,7 +166,7 @@ export function Sidebar({
loading: permissionsLoading,
} = useTeamPermissions();
const [unreadCount, setUnreadCount] = useState(0);
const sessionTeamRole = getNormalizedSessionTeamRole();
const { teamRole: sessionTeamRole } = useAdminAccess();
useEffect(() => {
const fetchUnread = async () => {
@ -352,6 +350,15 @@ export function Sidebar({
if (entry.kind === "group") {
const isNotifications = entry.basePath === "/notifications";
const visibleChildren = entry.children.filter((child) =>
isNavEntryAllowedForTeamRole(
{ kind: "link", label: child.label, to: child.to },
sessionTeamRole,
),
);
if (visibleChildren.length === 0) {
return null;
}
return (
<SidebarNavGroup
key={entry.basePath}
@ -359,7 +366,7 @@ export function Sidebar({
icon={entry.icon}
basePath={entry.basePath}
activePaths={entry.activePaths}
children={entry.children}
children={visibleChildren}
isCollapsed={isCollapsed}
onNavigate={onClose}
trailing={

View File

@ -7,6 +7,7 @@ import { Avatar, AvatarFallback, AvatarImage } from "../ui/avatar"
import * as DropdownMenu from "@radix-ui/react-dropdown-menu"
import { cn } from "../../lib/utils"
import { NotificationDropdown } from "./NotificationDropdown"
import { FullPanelOnly } from "../access/AdminAccessGates"
type TopbarProps = {
onSidebarToggle: () => void
@ -57,10 +58,8 @@ export function Topbar({ onSidebarToggle }: TopbarProps) {
</button>
<div className="flex items-center gap-3">
{/* Notifications */}
<NotificationDropdown />
{/* Separator */}
<div className="h-6 w-px bg-grayScale-200" />
{/* Avatar + Radix Dropdown */}
@ -92,6 +91,7 @@ export function Topbar({ onSidebarToggle }: TopbarProps) {
</span>
Profile
</DropdownMenu.Item>
<FullPanelOnly>
<DropdownMenu.Item
className={cn(
"group flex cursor-pointer items-center gap-3 rounded-lg px-3 py-2.5 text-sm font-medium text-grayScale-600 hover:bg-grayScale-100 hover:text-brand-600"
@ -103,6 +103,7 @@ export function Topbar({ onSidebarToggle }: TopbarProps) {
</span>
Settings
</DropdownMenu.Item>
</FullPanelOnly>
<DropdownMenu.Item
className={cn(
"group flex cursor-pointer items-center gap-3 rounded-lg px-3 py-2.5 text-sm font-medium text-grayScale-600 hover:bg-grayScale-100 hover:text-brand-600"

View File

@ -0,0 +1,26 @@
import { useMemo } from "react"
import {
canSendTeamInvitations,
getDefaultAppHome,
hasFullAdminPanelAccess,
isContentManagerPanelRole,
isPathAllowedForTeamRole,
} from "../lib/adminAccess"
import { getNormalizedSessionTeamRole } from "../lib/teamRole"
export function useAdminAccess() {
const teamRole = getNormalizedSessionTeamRole()
return useMemo(
() => ({
teamRole,
isContentManager: isContentManagerPanelRole(teamRole),
hasFullPanel: hasFullAdminPanelAccess(teamRole),
canInviteTeam: canSendTeamInvitations(teamRole),
defaultHome: getDefaultAppHome(teamRole),
isPathAllowed: (pathname: string) =>
isPathAllowedForTeamRole(pathname, teamRole),
}),
[teamRole],
)
}

View File

@ -34,11 +34,17 @@ export function canSendTeamInvitations(role?: string): boolean {
export function isPathAllowedForTeamRole(pathname: string, role?: string): boolean {
if (!isContentManagerPanelRole(role)) return true
if (pathname === "/notifications") return true
return CONTENT_MANAGER_PATH_PREFIXES.some(
(p) => pathname === p || pathname.startsWith(`${p}/`),
)
}
/** Content managers may open the personal notification inbox only (not admin broadcast tools). */
export function isContentManagerNotificationInboxPath(pathname: string): boolean {
return pathname === "/notifications"
}
export function getDefaultAppHome(role?: string): string {
if (isContentManagerPanelRole(role)) return "/new-content"
return "/dashboard"
@ -61,13 +67,26 @@ export function isNavEntryAllowedForTeamRole(
if (!isContentManagerPanelRole(role)) return true
if (entry.kind === "section") {
return entry.label === "Learning content" || entry.label === "Account"
return (
entry.label === "Learning content" ||
entry.label === "Account" ||
entry.label === "Communications"
)
}
if (entry.kind === "group") {
return entry.basePath === "/new-content"
return entry.basePath === "/new-content" || entry.basePath === "/notifications"
}
if (entry.kind === "link") {
return entry.to === "/personas" || entry.to === "/profile"
const to = entry.to ?? ""
if (to === "/personas" || to === "/profile") return true
if (to === "/notifications") return true
if (to.startsWith("/notifications/")) return false
return (
to === "/new-content" ||
to.startsWith("/new-content/") ||
to === "/content" ||
to.startsWith("/content/")
)
}
return false
}

View File

@ -734,6 +734,16 @@ export function AnalyticsPage() {
data={users.by_age_group ?? []}
total={users.total_users}
/>
<BreakdownList
title="Gender"
data={users.by_gender ?? []}
total={users.total_users}
/>
<BreakdownList
title="Education level"
data={users.by_education_level ?? []}
total={users.total_users}
/>
<BreakdownList
title="Country"
data={users.by_country ?? []}

View File

@ -54,6 +54,7 @@ import { toast } from "sonner"
import { TablePagination } from "../../components/admin/TablePagination"
import { DEFAULT_TABLE_PAGE_SIZE } from "../../lib/tablePagination"
import { UnassignedLabel } from "../../lib/displayValue"
import { FullPanelOnly } from "../../components/access/AdminAccessGates"
function NotificationItem({
notification,
@ -441,6 +442,7 @@ export function NotificationsPage() {
{/* Bulk actions */}
{!loading && !error && (
<div className="flex w-full flex-wrap items-center gap-2 sm:w-auto sm:justify-end">
<FullPanelOnly>
<Button
size="sm"
className="bg-brand-500 text-white hover:bg-brand-600"
@ -449,6 +451,7 @@ export function NotificationsPage() {
<Mail className="mr-2 h-3.5 w-3.5" />
Send notification
</Button>
</FullPanelOnly>
{notifications.length > 0 && (
<>
{globalUnread > 0 ? (

View File

@ -43,13 +43,11 @@ import { DEFAULT_TABLE_PAGE_SIZE, TABLE_PAGE_SIZE_OPTIONS } from "../../lib/tabl
import { toast } from "sonner"
import { SpinnerIcon } from "../../components/ui/spinner-icon"
import { InviteTeamMemberDialog } from "./components/InviteTeamMemberDialog"
import { canSendTeamInvitations } from "../../lib/adminAccess"
import { getNormalizedSessionTeamRole } from "../../lib/teamRole"
import { SuperAdminOnly } from "../../components/access/AdminAccessGates"
import { STAFF_TEAM_ROLE_OPTIONS } from "../../lib/teamRoles"
export function RolesListPage() {
const navigate = useNavigate()
const showTeamInviteActions = canSendTeamInvitations(getNormalizedSessionTeamRole())
const staffRoleNames = useMemo(
() => new Set(STAFF_TEAM_ROLE_OPTIONS.map((o) => o.value)),
[],
@ -474,7 +472,8 @@ export function RolesListPage() {
</div>
</div>
{showTeamInviteActions && staffRoleNames.has(role.name.toUpperCase()) ? (
<SuperAdminOnly>
{staffRoleNames.has(role.name.toUpperCase()) ? (
<Button
type="button"
variant="outline"
@ -487,6 +486,7 @@ export function RolesListPage() {
Invite team members
</Button>
) : null}
</SuperAdminOnly>
<div className="grid grid-cols-1 gap-2 sm:grid-cols-2">
<Button

View File

@ -11,6 +11,7 @@ import {
} from "lucide-react";
import { AdminFiltersPanel } from "../../components/filters/AdminFiltersPanel";
import { ExportCsvButton } from "../../components/export/ExportCsvButton";
import { FullPanelOnly, SuperAdminOnly } from "../../components/access/AdminAccessGates";
import { Button } from "../../components/ui/button";
import { Input } from "../../components/ui/input";
import {
@ -33,8 +34,6 @@ import { InviteTeamMemberDialog } from "../role-management/components/InviteTeam
import { EXPORT_PERMISSIONS, EXPORT_ROUTES } from "../../lib/csv-export";
import { teamMemberExportQuery } from "../../lib/csvExportFilters";
import { UnassignedLabel } from "../../lib/displayValue"
import { canSendTeamInvitations } from "../../lib/adminAccess";
import { getNormalizedSessionTeamRole } from "../../lib/teamRole";
function formatDate(dateStr: string): string {
return new Date(dateStr).toLocaleDateString("en-US", {
@ -245,8 +244,6 @@ export function TeamManagementPage() {
[roleFilter, statusFilter, search],
);
const showInviteActions = canSendTeamInvitations(getNormalizedSessionTeamRole());
return (
<div className="space-y-6">
<div className="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
@ -257,6 +254,7 @@ export function TeamManagementPage() {
</p>
</div>
<div className="flex w-full flex-col gap-2 sm:w-auto sm:flex-row">
<FullPanelOnly>
<ExportCsvButton
permission={EXPORT_PERMISSIONS.teamMembers}
exportPath={EXPORT_ROUTES.teamMembers}
@ -264,7 +262,8 @@ export function TeamManagementPage() {
disabled={loading}
className="w-full sm:w-auto"
/>
{showInviteActions ? (
</FullPanelOnly>
<SuperAdminOnly>
<Button
className="bg-brand-600 hover:bg-brand-500 text-white w-full sm:w-auto"
onClick={() => setInviteOpen(true)}
@ -272,7 +271,7 @@ export function TeamManagementPage() {
<Plus className="h-4 w-4" />
Add Team Member
</Button>
) : null}
</SuperAdminOnly>
</div>
</div>
@ -503,13 +502,13 @@ export function TeamManagementPage() {
</div>
{/* Status Update Confirmation Modal */}
{showInviteActions ? (
<SuperAdminOnly>
<InviteTeamMemberDialog
open={inviteOpen}
onOpenChange={setInviteOpen}
onInvited={() => void fetchMembers()}
/>
) : null}
</SuperAdminOnly>
{confirmDialog && (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/40 backdrop-blur-sm">

View File

@ -26,6 +26,7 @@ export interface DashboardUsers {
by_role: LabelCount[]
by_status: LabelCount[]
by_age_group: LabelCount[]
by_gender: LabelCount[]
by_education_level: LabelCount[]
by_occupation: LabelCount[]
by_learning_goal: LabelCount[]