-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathlayout.tsx
More file actions
161 lines (144 loc) · 5.6 KB
/
Copy pathlayout.tsx
File metadata and controls
161 lines (144 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"use client";
import { useEffect, useMemo, useState } from "react";
import { useRouter, usePathname } from "next/navigation";
import { useAuth } from "@/hooks/use-auth";
import { NavBar } from "@/components/registration/navbar";
import { DotPattern } from "@/components/registration/dot-pattern";
import { StickyAlert } from "@/components/registration/sticky-alert";
// Single source of truth for which roles can access which dashboard segment.
// Longest matching prefix wins. Anything not matched is treated as open to all
// signed-in users (e.g. /dashboard root, which has its own role-based redirect
// below).
type Role = "admin" | "evaluator" | "user" | "frai";
const ROUTE_POLICY: Array<{ prefix: string; roles: Role[] }> = [
// Participant-only routes
{ prefix: "/dashboard/profile", roles: ["user", "frai"] },
{ prefix: "/dashboard/team", roles: ["user", "frai"] },
{ prefix: "/dashboard/discover", roles: ["user", "frai"] },
// Role-scoped workspaces
{ prefix: "/dashboard/admin", roles: ["admin"] },
{ prefix: "/dashboard/evaluator", roles: ["evaluator"] },
{ prefix: "/dashboard/frai", roles: ["frai"] },
// Shared utility pages
{ prefix: "/dashboard/resume", roles: ["user", "frai", "admin", "evaluator"] },
];
const ROLE_LANDING: Record<Role, string> = {
admin: "/dashboard/admin",
evaluator: "/dashboard/evaluator",
frai: "/dashboard/frai",
user: "/dashboard",
};
function policyFor(pathname: string) {
// Pick the longest-matching prefix (so /dashboard/team/x matches /dashboard/team).
const match = ROUTE_POLICY
.filter((p) => pathname === p.prefix || pathname.startsWith(p.prefix + "/"))
.sort((a, b) => b.prefix.length - a.prefix.length)[0];
return match;
}
export default function DashboardLayout({
children,
}: {
children: React.ReactNode;
}) {
const { user, isAuthenticated, isLoading, logout, refreshUser } = useAuth();
const router = useRouter();
const pathname = usePathname();
const [alert, setAlert] = useState<{
type: "success" | "error" | "warning" | "info";
message: string;
} | null>(null);
const [hasRefreshed, setHasRefreshed] = useState(false);
useEffect(() => {
if (!isLoading && isAuthenticated && user && !hasRefreshed) {
refreshUser()
.then(() => setHasRefreshed(true))
.catch((error) => {
console.error("Error refreshing user:", error);
setHasRefreshed(true);
});
}
}, [isLoading, isAuthenticated, user, hasRefreshed, refreshUser]);
// Compute whether the current route is forbidden for this user. Used to
// suppress the content render while the redirect below is in flight so we
// don't leak even one frame of the disallowed page (the screenshot bug).
const policy = useMemo(() => policyFor(pathname || ""), [pathname]);
const role = (user?.role as Role | undefined) ?? undefined;
const forbidden =
!!user && !!role && !!policy && !policy.roles.includes(role);
useEffect(() => {
if (isLoading) return;
if (!isAuthenticated || !user) {
router.push("/login");
return;
}
// /dashboard root: redirect privileged roles to their own workspace.
if (pathname === "/dashboard" || pathname === "/dashboard/") {
if (role && role !== "user") {
router.replace(ROLE_LANDING[role]);
return;
}
}
// Forbidden-by-policy: redirect to that role's landing page.
if (forbidden && role) {
router.replace(ROLE_LANDING[role]);
}
}, [isAuthenticated, user, isLoading, router, pathname, role, forbidden]);
const handleLogout = async () => {
await logout();
setAlert({ type: "info", message: "Logged out successfully" });
setTimeout(() => setAlert(null), 3000);
};
// Once auth has resolved and confirmed no user, the useEffect above redirects to /login.
// Render nothing in that brief gap to avoid flashing the dashboard shell.
if (!isLoading && (!isAuthenticated || !user)) {
return null;
}
return (
<div className="min-h-screen w-full flex flex-col bg-void relative">
<NavBar
isAuthLoading={isLoading}
user={
user && user.name
? {
uid: user.uid,
name: user.name,
email: user.email,
role: user.role,
profilePicture: user.profile_picture,
}
: undefined
}
onLogout={handleLogout}
onNavigate={(view) => {
if (view === "login") router.push("/login");
else if (view === "register") router.push("/register");
else if (view === "landing") router.push("/");
else if (view.startsWith("team?joinCode=")) {
const joinCode = view.split("joinCode=")[1];
router.push(`/dashboard/team?joinCode=${joinCode}`);
} else {
router.push(`/dashboard/${view}`);
}
}}
/>
<main className="relative flex-1 w-full">
<DotPattern />
<div className="relative z-10 mx-auto w-full max-w-[1100px] px-4 sm:px-6 md:px-8 py-8 sm:py-10 md:py-12">
<div className="flex flex-col gap-6 md:gap-8">
{alert && (
<StickyAlert
type={alert.type}
message={alert.message}
onClose={() => setAlert(null)}
/>
)}
{/* If the route is policy-forbidden for this user, suppress the
page content while the layout redirect is in flight. Prevents
the data fetch + content render of a page they shouldn't see. */}
{forbidden ? null : children}
</div>
</div>
</main>
</div>
);
}