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_role: asLabelCounts(pickField(u, "by_role", "byRole", "ByRole")),
by_status: asLabelCounts(pickField(u, "by_status", "byStatus", "ByStatus")), by_status: asLabelCounts(pickField(u, "by_status", "byStatus", "ByStatus")),
by_age_group: asLabelCounts(pickField(u, "by_age_group", "byAgeGroup", "ByAgeGroup")), by_age_group: asLabelCounts(pickField(u, "by_age_group", "byAgeGroup", "ByAgeGroup")),
by_gender: asLabelCounts(pickField(u, "by_gender", "byGender", "ByGender")),
by_education_level: asLabelCounts( by_education_level: asLabelCounts(
pickField(u, ...EDUCATION_LEVEL_KEYS) ?? pickField(scope, ...EDUCATION_LEVEL_KEYS), 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 { Navigate, Route, Routes } from "react-router-dom";
import { getDefaultAppHome } from "../lib/adminAccess"; import { getDefaultAppHome } from "../lib/adminAccess";
import { getNormalizedSessionTeamRole } from "../lib/teamRole"; import { getNormalizedSessionTeamRole } from "../lib/teamRole";
import { FullPanelGuard } from "../components/access/AdminAccessGates";
import { AppLayout } from "../layouts/AppLayout"; import { AppLayout } from "../layouts/AppLayout";
import { DashboardPage } from "../pages/DashboardPage"; import { DashboardPage } from "../pages/DashboardPage";
import { AnalyticsPage } from "../pages/analytics/AnalyticsPage"; import { AnalyticsPage } from "../pages/analytics/AnalyticsPage";
@ -94,6 +95,8 @@ export function AppRoutes() {
<Route path="/account-deletion" element={<AccountDeletionPage />} /> <Route path="/account-deletion" element={<AccountDeletionPage />} />
<Route element={<AppLayout />}> <Route element={<AppLayout />}>
<Route path="/" element={<Navigate to={getDefaultAppHome(getNormalizedSessionTeamRole())} replace />} /> <Route path="/" element={<Navigate to={getDefaultAppHome(getNormalizedSessionTeamRole())} replace />} />
<Route element={<FullPanelGuard />}>
<Route path="/dashboard" element={<DashboardPage />} /> <Route path="/dashboard" element={<DashboardPage />} />
<Route path="/users" element={<UserManagementLayout />}> <Route path="/users" element={<UserManagementLayout />}>
<Route index element={<Navigate to="list" replace />} /> <Route index element={<Navigate to="list" replace />} />
@ -107,6 +110,7 @@ export function AppRoutes() {
<Route index element={<RolesListPage />} /> <Route index element={<RolesListPage />} />
<Route path="add" element={<AddRolePage />} /> <Route path="add" element={<AddRolePage />} />
</Route> </Route>
</Route>
<Route path="/content" element={<ContentManagementLayout />}> <Route path="/content" element={<ContentManagementLayout />}>
<Route index element={<Navigate to="practices" replace />} /> <Route index element={<Navigate to="practices" replace />} />
@ -323,7 +327,12 @@ export function AppRoutes() {
element={<LinkExistingPracticeFlow />} element={<LinkExistingPracticeFlow />}
/> />
<Route path="/personas" element={<PersonasPage />} />
<Route path="/admin/personas" element={<PersonasPage />} />
<Route path="/profile" element={<ProfilePage />} />
<Route path="/notifications" element={<NotificationsPage />} /> <Route path="/notifications" element={<NotificationsPage />} />
<Route element={<FullPanelGuard />}>
<Route <Route
path="/notifications/email-templates" path="/notifications/email-templates"
element={<EmailTemplatesPage />} element={<EmailTemplatesPage />}
@ -354,15 +363,13 @@ export function AppRoutes() {
<Route path="/admin/app-reviews" element={<AppReviewsPage />} /> <Route path="/admin/app-reviews" element={<AppReviewsPage />} />
<Route path="/operations/app-reviews" element={<AppReviewsPage />} /> <Route path="/operations/app-reviews" element={<AppReviewsPage />} />
<Route path="/ratings" 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="/analytics" element={<AnalyticsPage />} />
<Route path="/team" element={<TeamManagementPage />} /> <Route path="/team" element={<TeamManagementPage />} />
<Route path="/team/add" element={<AddTeamMemberPage />} /> <Route path="/team/add" element={<AddTeamMemberPage />} />
<Route path="/team/:id" element={<TeamMemberDetailPage />} /> <Route path="/team/:id" element={<TeamMemberDetailPage />} />
<Route path="/profile" element={<ProfilePage />} />
<Route path="/settings" element={<SettingsPage />} /> <Route path="/settings" element={<SettingsPage />} />
</Route>
</Route> </Route>
<Route path="*" element={<NotFoundPage />} /> <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 { hasActivityLogPermission } from "../../lib/activityLogPermissions";
import { hasExportPermission } from "../../lib/exportPermissions"; import { hasExportPermission } from "../../lib/exportPermissions";
import { SidebarNavGroup } from "./SidebarNavGroup"; import { SidebarNavGroup } from "./SidebarNavGroup";
import { import { useAdminAccess } from "../../hooks/useAdminAccess";
getNormalizedSessionTeamRole,
} from "../../lib/teamRole";
import { isNavEntryAllowedForTeamRole } from "../../lib/adminAccess"; import { isNavEntryAllowedForTeamRole } from "../../lib/adminAccess";
type NavLinkItem = { type NavLinkItem = {
@ -168,7 +166,7 @@ export function Sidebar({
loading: permissionsLoading, loading: permissionsLoading,
} = useTeamPermissions(); } = useTeamPermissions();
const [unreadCount, setUnreadCount] = useState(0); const [unreadCount, setUnreadCount] = useState(0);
const sessionTeamRole = getNormalizedSessionTeamRole(); const { teamRole: sessionTeamRole } = useAdminAccess();
useEffect(() => { useEffect(() => {
const fetchUnread = async () => { const fetchUnread = async () => {
@ -352,6 +350,15 @@ export function Sidebar({
if (entry.kind === "group") { if (entry.kind === "group") {
const isNotifications = entry.basePath === "/notifications"; 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 ( return (
<SidebarNavGroup <SidebarNavGroup
key={entry.basePath} key={entry.basePath}
@ -359,7 +366,7 @@ export function Sidebar({
icon={entry.icon} icon={entry.icon}
basePath={entry.basePath} basePath={entry.basePath}
activePaths={entry.activePaths} activePaths={entry.activePaths}
children={entry.children} children={visibleChildren}
isCollapsed={isCollapsed} isCollapsed={isCollapsed}
onNavigate={onClose} onNavigate={onClose}
trailing={ trailing={

View File

@ -7,6 +7,7 @@ import { Avatar, AvatarFallback, AvatarImage } from "../ui/avatar"
import * as DropdownMenu from "@radix-ui/react-dropdown-menu" import * as DropdownMenu from "@radix-ui/react-dropdown-menu"
import { cn } from "../../lib/utils" import { cn } from "../../lib/utils"
import { NotificationDropdown } from "./NotificationDropdown" import { NotificationDropdown } from "./NotificationDropdown"
import { FullPanelOnly } from "../access/AdminAccessGates"
type TopbarProps = { type TopbarProps = {
onSidebarToggle: () => void onSidebarToggle: () => void
@ -57,10 +58,8 @@ export function Topbar({ onSidebarToggle }: TopbarProps) {
</button> </button>
<div className="flex items-center gap-3"> <div className="flex items-center gap-3">
{/* Notifications */}
<NotificationDropdown /> <NotificationDropdown />
{/* Separator */}
<div className="h-6 w-px bg-grayScale-200" /> <div className="h-6 w-px bg-grayScale-200" />
{/* Avatar + Radix Dropdown */} {/* Avatar + Radix Dropdown */}
@ -92,6 +91,7 @@ export function Topbar({ onSidebarToggle }: TopbarProps) {
</span> </span>
Profile Profile
</DropdownMenu.Item> </DropdownMenu.Item>
<FullPanelOnly>
<DropdownMenu.Item <DropdownMenu.Item
className={cn( 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" "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> </span>
Settings Settings
</DropdownMenu.Item> </DropdownMenu.Item>
</FullPanelOnly>
<DropdownMenu.Item <DropdownMenu.Item
className={cn( 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" "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 { export function isPathAllowedForTeamRole(pathname: string, role?: string): boolean {
if (!isContentManagerPanelRole(role)) return true if (!isContentManagerPanelRole(role)) return true
if (pathname === "/notifications") return true
return CONTENT_MANAGER_PATH_PREFIXES.some( return CONTENT_MANAGER_PATH_PREFIXES.some(
(p) => pathname === p || pathname.startsWith(`${p}/`), (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 { export function getDefaultAppHome(role?: string): string {
if (isContentManagerPanelRole(role)) return "/new-content" if (isContentManagerPanelRole(role)) return "/new-content"
return "/dashboard" return "/dashboard"
@ -61,13 +67,26 @@ export function isNavEntryAllowedForTeamRole(
if (!isContentManagerPanelRole(role)) return true if (!isContentManagerPanelRole(role)) return true
if (entry.kind === "section") { 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") { if (entry.kind === "group") {
return entry.basePath === "/new-content" return entry.basePath === "/new-content" || entry.basePath === "/notifications"
} }
if (entry.kind === "link") { 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 return false
} }

View File

@ -734,6 +734,16 @@ export function AnalyticsPage() {
data={users.by_age_group ?? []} data={users.by_age_group ?? []}
total={users.total_users} 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 <BreakdownList
title="Country" title="Country"
data={users.by_country ?? []} data={users.by_country ?? []}

View File

@ -54,6 +54,7 @@ import { toast } from "sonner"
import { TablePagination } from "../../components/admin/TablePagination" import { TablePagination } from "../../components/admin/TablePagination"
import { DEFAULT_TABLE_PAGE_SIZE } from "../../lib/tablePagination" import { DEFAULT_TABLE_PAGE_SIZE } from "../../lib/tablePagination"
import { UnassignedLabel } from "../../lib/displayValue" import { UnassignedLabel } from "../../lib/displayValue"
import { FullPanelOnly } from "../../components/access/AdminAccessGates"
function NotificationItem({ function NotificationItem({
notification, notification,
@ -441,6 +442,7 @@ export function NotificationsPage() {
{/* Bulk actions */} {/* Bulk actions */}
{!loading && !error && ( {!loading && !error && (
<div className="flex w-full flex-wrap items-center gap-2 sm:w-auto sm:justify-end"> <div className="flex w-full flex-wrap items-center gap-2 sm:w-auto sm:justify-end">
<FullPanelOnly>
<Button <Button
size="sm" size="sm"
className="bg-brand-500 text-white hover:bg-brand-600" 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" /> <Mail className="mr-2 h-3.5 w-3.5" />
Send notification Send notification
</Button> </Button>
</FullPanelOnly>
{notifications.length > 0 && ( {notifications.length > 0 && (
<> <>
{globalUnread > 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 { toast } from "sonner"
import { SpinnerIcon } from "../../components/ui/spinner-icon" import { SpinnerIcon } from "../../components/ui/spinner-icon"
import { InviteTeamMemberDialog } from "./components/InviteTeamMemberDialog" import { InviteTeamMemberDialog } from "./components/InviteTeamMemberDialog"
import { canSendTeamInvitations } from "../../lib/adminAccess" import { SuperAdminOnly } from "../../components/access/AdminAccessGates"
import { getNormalizedSessionTeamRole } from "../../lib/teamRole"
import { STAFF_TEAM_ROLE_OPTIONS } from "../../lib/teamRoles" import { STAFF_TEAM_ROLE_OPTIONS } from "../../lib/teamRoles"
export function RolesListPage() { export function RolesListPage() {
const navigate = useNavigate() const navigate = useNavigate()
const showTeamInviteActions = canSendTeamInvitations(getNormalizedSessionTeamRole())
const staffRoleNames = useMemo( const staffRoleNames = useMemo(
() => new Set(STAFF_TEAM_ROLE_OPTIONS.map((o) => o.value)), () => new Set(STAFF_TEAM_ROLE_OPTIONS.map((o) => o.value)),
[], [],
@ -474,7 +472,8 @@ export function RolesListPage() {
</div> </div>
</div> </div>
{showTeamInviteActions && staffRoleNames.has(role.name.toUpperCase()) ? ( <SuperAdminOnly>
{staffRoleNames.has(role.name.toUpperCase()) ? (
<Button <Button
type="button" type="button"
variant="outline" variant="outline"
@ -487,6 +486,7 @@ export function RolesListPage() {
Invite team members Invite team members
</Button> </Button>
) : null} ) : null}
</SuperAdminOnly>
<div className="grid grid-cols-1 gap-2 sm:grid-cols-2"> <div className="grid grid-cols-1 gap-2 sm:grid-cols-2">
<Button <Button

View File

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