forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

nav-items.ts477 lines, 20.4 KiBHistoryRawDownload
1import type { Component } from "solid-js";
2import type { Role } from "@/api/client";
3import {
4 IconArchive,
5 IconBook,
6 IconBriefcase,
7 IconCalendarBlank,
8 IconCalendarCheck,
9 IconCalendarDays,
10 IconCalendarStar,
11 IconChalkboardTeacher,
12 IconChart,
13 IconChartPie,
14 IconClipboardCheck,
15 IconClockUser,
16 IconEdit,
17 IconExam,
18 IconHandshake,
19 IconHelpCircle,
20 IconHome,
21 IconHomework,
22 IconListChecks,
23 IconMessage,
24 IconNote,
25 IconPackage,
26 IconReportAnalytics,
27 IconScan,
28 IconSchool,
29 IconSettings,
30 IconShieldCheck,
31 IconSparkles,
32 IconTarget,
33 IconTimer,
34 IconUserCog,
35 IconUsers,
36 IconUtensils,
37 IconWaveform,
38} from "@/components/ui/icons";
39import type { MessageKey } from "@/i18n/messages";
40import { hasExactRole, roleInRange } from "@/lib/roles";
41
42export type NavItem = {
43 id: string;
44 to: string;
45 labelKey: MessageKey;
46 Icon: Component<{ class?: string }>;
47 minRole?: Role;
48 maxRole?: Role;
49 exactRole?: Role;
50 exact?: boolean;
51 /**
52 * Search params the entry links with (`/questions?status=pending`). It is
53 * active only while the URL carries them, and then wins over a plain entry
54 * for the same path.
55 */
56 search?: Record<string, string>;
57 /** Backend module nest behind this entry; hidden when the school did not buy it. */
58 module?: string;
59 /** No backend yet — routes to the shared placeholder and carries a "yakında" badge. */
60 soon?: boolean;
61 /** Opens an existing shell surface instead of navigating (the Çelebi panel,
62 * the account settings dialog) — both already live elsewhere in the shell. */
63 action?: "celebi" | "profile";
64};
65
66export type NavGroup = {
67 id: string;
68 labelKey: MessageKey;
69 Icon: Component<{ class?: string }>;
70 items: NavItem[];
71 /** Starts folded in the sidebar until the viewer opens it (the "yakında" shelf). */
72 defaultFolded?: boolean;
73};
74
75export const HOME_ITEM: NavItem = {
76 id: "today",
77 to: "/",
78 labelKey: "nav.today",
79 Icon: IconHome,
80 exact: true,
81};
82
83const AI_STUDIO_ITEM: NavItem = {
84 id: "ai-studio",
85 to: "/ai/studio",
86 labelKey: "aiHub.tab.studio",
87 Icon: IconWaveform,
88 module: "course_notes",
89};
90
91const AI_STUDY_ITEM: NavItem = {
92 id: "ai-study",
93 to: "/ai/study",
94 labelKey: "aiHub.tab.study",
95 Icon: IconBook,
96};
97
98const AI_INSIGHTS_ITEM: NavItem = {
99 id: "ai-insights",
100 to: "/ai/insights",
101 labelKey: "aiHub.tab.insights",
102 Icon: IconChart,
103};
104
105const AI_CELEBI_ITEM: NavItem = {
106 id: "ai-celebi",
107 to: "",
108 labelKey: "nav.celebi",
109 Icon: IconSparkles,
110 action: "celebi",
111 module: "chatbot",
112};
113
114const AI_GROUP: NavGroup = {
115 id: "ai",
116 labelKey: "nav.group.ai",
117 Icon: IconSparkles,
118 items: [AI_STUDIO_ITEM, AI_STUDY_ITEM, AI_INSIGHTS_ITEM, AI_CELEBI_ITEM],
119};
120
121// A teacher's student analysis is the same board, listed under Tracking as
122// "Öğrenci analizi" — so their AI group leaves it out instead of listing it twice.
123const TEACHER_AI_GROUP: NavGroup = {
124 id: "ai",
125 labelKey: "nav.group.ai",
126 Icon: IconSparkles,
127 items: [AI_STUDIO_ITEM, AI_STUDY_ITEM, AI_CELEBI_ITEM],
128};
129
130const PARENT_AI_GROUP: NavGroup = {
131 id: "ai",
132 labelKey: "nav.group.ai",
133 Icon: IconSparkles,
134 items: [AI_INSIGHTS_ITEM],
135};
136
137function settingsAction(id: string): NavItem {
138 return { id, to: "", labelKey: "nav.settings", Icon: IconSettings, action: "profile" };
139}
140
141// Every role's tree is short, labelled sections that stay open (Fintables
142// style) instead of a few accordions hiding 20 entries each. Entries without a
143// backend yet all move to one trailing "Yakında" section, folded by default, so
144// they stay visible without crowding the live pages.
145function soonGroup(items: NavItem[]): NavGroup {
146 return { id: "soon", labelKey: "nav.soon", Icon: IconTarget, items, defaultFolded: true };
147}
148
149// Admin and manager share one tree — manager just qualifies for fewer of its
150// items, via the same minRole gates the rest of this file already uses.
151const ADMIN_GROUPS: NavGroup[] = [
152 AI_GROUP,
153 {
154 id: "school",
155 labelKey: "nav.group.school",
156 Icon: IconSchool,
157 items: [
158 { id: "students-roster", to: "/management/students", labelKey: "nav.studentsRoster", Icon: IconUsers, minRole: "manager" },
159 { id: "teachers-roster", to: "/management/teachers", labelKey: "nav.teachersRoster", Icon: IconChalkboardTeacher, minRole: "manager" },
160 { id: "class-groups", to: "/management/classes", labelKey: "nav.classGroups", Icon: IconSchool, minRole: "teacher", module: "classes" },
161 { id: "academic-years", to: "/management/academic-years", labelKey: "nav.academicYears", Icon: IconCalendarBlank, minRole: "manager" },
162 { id: "terms", to: "/management/terms", labelKey: "nav.terms", Icon: IconCalendarCheck, minRole: "manager" },
163 { id: "schedule", to: "/calendar", labelKey: "nav.schedule", Icon: IconCalendarDays, minRole: "manager" },
164 ],
165 },
166 {
167 id: "teaching",
168 labelKey: "nav.group.teaching",
169 Icon: IconBook,
170 items: [
171 { id: "courses", to: "/courses", labelKey: "nav.classes", Icon: IconBook, module: "courses" },
172 { id: "homework", to: "/homework", labelKey: "nav.homework", Icon: IconHomework, module: "homework" },
173 { id: "exams", to: "/exams", labelKey: "nav.exams", Icon: IconExam, minRole: "teacher", module: "exams" },
174 { id: "question-bank", to: "/question-bank", labelKey: "nav.questionBank", Icon: IconArchive, minRole: "teacher", module: "bank_questions" },
175 { id: "questions", to: "/questions", labelKey: "nav.questions", Icon: IconHelpCircle, module: "questions" },
176 { id: "notes", to: "/notes", labelKey: "nav.notes", Icon: IconNote, module: "notes" },
177 { id: "whiteboards", to: "/whiteboards", labelKey: "nav.whiteboards", Icon: IconEdit, module: "boards" },
178 ],
179 },
180 {
181 id: "tracking",
182 labelKey: "nav.group.tracking",
183 Icon: IconClipboardCheck,
184 items: [
185 { id: "student-attendance", to: "/management/student-attendance", labelKey: "nav.studentAttendance", Icon: IconClipboardCheck, minRole: "teacher", module: "attendance" },
186 { id: "student-marks", to: "/management/student-marks", labelKey: "nav.studentMarks", Icon: IconChart, minRole: "teacher", module: "marks" },
187 { id: "student-pomodoros", to: "/management/pomodoros", labelKey: "nav.studentPomodoro", Icon: IconTimer, minRole: "teacher", module: "pomodoro" },
188 ],
189 },
190 {
191 id: "services",
192 labelKey: "nav.group.services",
193 Icon: IconCalendarDays,
194 items: [
195 { id: "events", to: "/events", labelKey: "nav.events", Icon: IconCalendarStar, module: "events" },
196 { id: "appointments", to: "/appointments", labelKey: "nav.appointments", Icon: IconHandshake, module: "appointments" },
197 { id: "school-meals", to: "/meals", labelKey: "nav.schoolMeals", Icon: IconUtensils, minRole: "manager", module: "meals" },
198 ],
199 },
200 {
201 id: "institution",
202 labelKey: "nav.group.institution",
203 Icon: IconUserCog,
204 items: [
205 { id: "payments-collection", to: "/management/payments", labelKey: "nav.payments", Icon: IconReportAnalytics, minRole: "manager", module: "payments" },
206 { id: "staff-work", to: "/management/staff-work", labelKey: "nav.staffWork", Icon: IconBriefcase, minRole: "manager", module: "work" },
207 { id: "work", to: "/work", labelKey: "nav.work", Icon: IconClockUser, minRole: "teacher", module: "work" },
208 { id: "users", to: "/admin/users", labelKey: "nav.users", Icon: IconUserCog, minRole: "admin" },
209 { id: "license-modules", to: "/management/modules", labelKey: "nav.licenseModules", Icon: IconPackage, minRole: "admin" },
210 { id: "settings", to: "/management/settings", labelKey: "settings.title", Icon: IconSettings, minRole: "manager" },
211 ],
212 },
213 soonGroup([
214 { id: "mock-exams", to: "/coming-soon/deneme-sinavlari", labelKey: "nav.mockExams", Icon: IconTarget, soon: true, minRole: "manager" },
215 { id: "optical-reading", to: "/coming-soon/optik-okuma", labelKey: "nav.opticalReading", Icon: IconScan, soon: true, minRole: "manager" },
216 { id: "reports", to: "/coming-soon/raporlar", labelKey: "nav.reports", Icon: IconChartPie, soon: true, minRole: "manager" },
217 { id: "data-protection", to: "/coming-soon/kvkk-denetim", labelKey: "nav.dataProtection", Icon: IconShieldCheck, soon: true, minRole: "admin" },
218 ]),
219];
220
221const TEACHER_GROUPS: NavGroup[] = [
222 TEACHER_AI_GROUP,
223 {
224 id: "my-classroom",
225 labelKey: "nav.group.myClassroom",
226 Icon: IconSchool,
227 items: [
228 { id: "my-classes", to: "/management/classes", labelKey: "nav.myClasses", Icon: IconSchool, module: "classes" },
229 { id: "my-schedule", to: "/calendar", labelKey: "nav.mySchedule", Icon: IconCalendarDays },
230 { id: "courses", to: "/courses", labelKey: "nav.classes", Icon: IconBook, module: "courses" },
231 ],
232 },
233 {
234 id: "tracking",
235 labelKey: "nav.group.tracking",
236 Icon: IconClipboardCheck,
237 items: [
238 { id: "student-attendance", to: "/management/student-attendance", labelKey: "nav.studentAttendance", Icon: IconClipboardCheck, module: "attendance" },
239 { id: "student-marks", to: "/management/student-marks", labelKey: "nav.studentMarks", Icon: IconChart, module: "marks" },
240 { id: "student-pomodoros", to: "/management/pomodoros", labelKey: "nav.studentPomodoro", Icon: IconTimer, module: "pomodoro" },
241 { id: "student-analysis", to: "/ai/insights", labelKey: "nav.studentAnalysis", Icon: IconChart },
242 ],
243 },
244 {
245 id: "teaching",
246 labelKey: "nav.group.teaching",
247 Icon: IconBook,
248 items: [
249 { id: "homework", to: "/homework", labelKey: "nav.homework", Icon: IconHomework, module: "homework" },
250 { id: "exams", to: "/exams", labelKey: "nav.exams", Icon: IconExam, module: "exams" },
251 { id: "question-bank", to: "/question-bank", labelKey: "nav.questionBank", Icon: IconArchive, module: "bank_questions" },
252 { id: "question-generation", to: "/ai/question-generation", labelKey: "nav.questionGeneration", Icon: IconSparkles },
253 { id: "questions", to: "/questions", labelKey: "nav.questions", Icon: IconHelpCircle, module: "questions" },
254 // The pool's own pending tab: for a teacher, `?status=pending` is the
255 // approval queue (`POST /questions/{id}/approve`).
256 { id: "pending-approvals", to: "/questions", search: { status: "pending" }, labelKey: "nav.pendingApprovals", Icon: IconClipboardCheck, module: "questions" },
257 { id: "notes", to: "/notes", labelKey: "nav.notes", Icon: IconNote, module: "notes" },
258 { id: "whiteboards", to: "/whiteboards", labelKey: "nav.whiteboards", Icon: IconEdit, module: "boards" },
259 ],
260 },
261 {
262 id: "other",
263 labelKey: "nav.group.other",
264 Icon: IconMessage,
265 items: [
266 { id: "parent-communication", to: "/messages", labelKey: "nav.parentCommunication", Icon: IconMessage, module: "messages" },
267 { id: "appointments", to: "/appointments", labelKey: "nav.appointments", Icon: IconHandshake, module: "appointments" },
268 { id: "events", to: "/events", labelKey: "nav.events", Icon: IconCalendarStar, module: "events" },
269 { id: "meals", to: "/meals", labelKey: "nav.meals", Icon: IconUtensils, module: "meals" },
270 { id: "work", to: "/work", labelKey: "nav.work", Icon: IconClockUser, minRole: "teacher", module: "work" },
271 settingsAction("settings-teacher"),
272 ],
273 },
274];
275
276const STUDENT_GROUPS: NavGroup[] = [
277 AI_GROUP,
278 {
279 id: "study",
280 labelKey: "nav.group.study",
281 Icon: IconSchool,
282 items: [
283 { id: "topic-mastery", to: "/marks", labelKey: "nav.topicMastery", Icon: IconChart, exactRole: "student", module: "marks" },
284 { id: "exam-results", to: "/exams", labelKey: "nav.examResults", Icon: IconExam, module: "exams" },
285 { id: "my-homework", to: "/homework", labelKey: "nav.myHomework", Icon: IconHomework, module: "homework" },
286 { id: "pomodoro", to: "/pomodoro", labelKey: "nav.pomodoro", Icon: IconTimer, exactRole: "student", module: "pomodoro" },
287 ],
288 },
289 {
290 id: "teaching",
291 labelKey: "nav.group.teaching",
292 Icon: IconBook,
293 items: [
294 { id: "courses", to: "/courses", labelKey: "nav.classes", Icon: IconBook, module: "courses" },
295 { id: "notes", to: "/notes", labelKey: "nav.notes", Icon: IconNote, module: "notes" },
296 { id: "questions", to: "/questions", labelKey: "nav.questions", Icon: IconHelpCircle, module: "questions" },
297 { id: "whiteboards", to: "/whiteboards", labelKey: "nav.whiteboards", Icon: IconEdit, module: "boards" },
298 ],
299 },
300 {
301 id: "other",
302 labelKey: "nav.group.other",
303 Icon: IconCalendarDays,
304 items: [
305 { id: "calendar", to: "/calendar", labelKey: "nav.mySchedule", Icon: IconCalendarDays },
306 { id: "events", to: "/events", labelKey: "nav.events", Icon: IconCalendarStar, module: "events" },
307 { id: "messages", to: "/messages", labelKey: "nav.messages", Icon: IconMessage, module: "messages" },
308 { id: "appointments", to: "/appointments", labelKey: "nav.appointments", Icon: IconHandshake, module: "appointments" },
309 { id: "meals", to: "/meals", labelKey: "nav.meals", Icon: IconUtensils, module: "meals" },
310 settingsAction("settings-student"),
311 ],
312 },
313 soonGroup([
314 { id: "study-plan", to: "/coming-soon/calisma-programim", labelKey: "nav.studyPlan", Icon: IconListChecks, soon: true },
315 ]),
316];
317
318const PARENT_GROUPS: NavGroup[] = [
319 PARENT_AI_GROUP,
320 {
321 id: "my-student",
322 labelKey: "nav.group.myStudent",
323 Icon: IconUsers,
324 items: [
325 { id: "progress-report", to: "/students", labelKey: "nav.progressReport", Icon: IconReportAnalytics, exactRole: "parent" },
326 { id: "absence", to: "/students/attendance", labelKey: "nav.absence", Icon: IconClipboardCheck, exactRole: "parent", module: "attendance" },
327 { id: "child-exam-results", to: "/students/exams", labelKey: "nav.childExamResults", Icon: IconExam, exactRole: "parent", module: "marks" },
328 { id: "child-study-plan", to: "/students/study", labelKey: "nav.childStudyPlan", Icon: IconListChecks, exactRole: "parent", module: "pomodoro" },
329 ],
330 },
331 {
332 id: "institution",
333 labelKey: "nav.group.institution",
334 Icon: IconUserCog,
335 items: [
336 { id: "payment-statement", to: "/payments", labelKey: "nav.payments", Icon: IconReportAnalytics, exactRole: "parent", module: "payments" },
337 { id: "appointments", to: "/appointments", labelKey: "nav.appointmentsAndCommunication", Icon: IconHandshake, exactRole: "parent", module: "appointments" },
338 { id: "messages", to: "/messages", labelKey: "nav.messages", Icon: IconMessage, module: "messages" },
339 { id: "calendar", to: "/calendar", labelKey: "nav.mySchedule", Icon: IconCalendarDays },
340 { id: "events", to: "/events", labelKey: "nav.events", Icon: IconCalendarStar, module: "events" },
341 { id: "meals", to: "/meals", labelKey: "nav.meals", Icon: IconUtensils, module: "meals" },
342 settingsAction("settings-parent"),
343 ],
344 },
345];
346
347const GROUPS_BY_ROLE: Record<Role, NavGroup[]> = {
348 student: STUDENT_GROUPS,
349 teacher: TEACHER_GROUPS,
350 manager: ADMIN_GROUPS,
351 admin: ADMIN_GROUPS,
352 parent: PARENT_GROUPS,
353};
354
355// The mobile bottom tab bar: Home plus two of the role's busiest pages, then
356// the bar's own Search and Menu buttons. Entries are picked by id from the
357// role's sidebar tree, so a tab always carries the same icon and label as its
358// sidebar row.
359const PRIMARY_IDS_BY_ROLE: Record<Role, string[]> = {
360 student: ["my-homework", "exam-results"],
361 teacher: ["my-classes", "student-attendance"],
362 manager: ["students-roster", "class-groups"],
363 admin: ["students-roster", "class-groups"],
364 parent: ["progress-report", "appointments"],
365};
366
367function primaryItemsFor(role: Role): NavItem[] {
368 const all = GROUPS_BY_ROLE[role].flatMap((group) => group.items);
369 const picked = PRIMARY_IDS_BY_ROLE[role]
370 .map((id) => all.find((item) => item.id === id))
371 .filter((item): item is NavItem => item !== undefined);
372 return [HOME_ITEM, ...picked];
373}
374
375function itemVisible(item: NavItem, role: Role | undefined) {
376 if (!role) return false;
377 if (item.exactRole) return hasExactRole(role, item.exactRole);
378 if (item.minRole || item.maxRole) return roleInRange(role, item.minRole, item.maxRole);
379 return true;
380}
381
382/**
383 * The school's entitlement gate. Null/undefined means "unknown" (still
384 * loading, logged out, or the lookup failed) and shows everything — a modules
385 * outage must never blank the shell; the backend still 403s the nest itself.
386 */
387export function moduleVisible(item: NavItem, enabled: readonly string[] | null | undefined): boolean {
388 if (enabled == null) return true;
389 if (item.module == null) return true;
390 return enabled.includes(item.module);
391}
392
393export function primaryNavItems(role: Role | undefined, enabled?: readonly string[] | null): NavItem[] {
394 if (!role) return [];
395 return primaryItemsFor(role).filter((item) => itemVisible(item, role) && moduleVisible(item, enabled));
396}
397
398/** The full, role-specific group tree — desktop sidebar and the mobile menu
399 * sheet both render this directly; it mirrors the Figma sidebar as-is, with
400 * no items pulled out for a separate "primary" section. */
401export function visibleNavGroups(role: Role | undefined, enabled?: readonly string[] | null): NavGroup[] {
402 if (!role) return [];
403 return GROUPS_BY_ROLE[role]
404 .map((group) => ({
405 ...group,
406 items: group.items.filter((item) => itemVisible(item, role) && moduleVisible(item, enabled)),
407 }))
408 .filter((group) => group.items.length > 0);
409}
410
411export function visibleNavItems(role: Role | undefined, enabled?: readonly string[] | null): NavItem[] {
412 const items = [HOME_ITEM, ...primaryNavItems(role, enabled), ...visibleNavGroups(role, enabled).flatMap((group) => group.items)];
413 return items.filter((item, index) => items.findIndex((candidate) => candidate.id === item.id && candidate.to === item.to) === index);
414}
415
416export function pathActive(pathname: string, to: string, exact?: boolean) {
417 if (!to) return false;
418 if (exact) return pathname === to;
419 return pathname === to || pathname.startsWith(`${to}/`);
420}
421
422export function primaryPathActive(pathname: string, item: NavItem) {
423 return pathActive(pathname, item.to, item.exact);
424}
425
426function searchMatches(item: NavItem, search: Record<string, unknown> | undefined) {
427 if (!item.search) return true;
428 return Object.entries(item.search).every(([key, value]) => search?.[key] === value);
429}
430
431export function routeNavItem(pathname: string, role: Role | undefined, search?: Record<string, unknown>): NavItem | undefined {
432 return [...primaryNavItems(role), ...visibleNavItems(role)]
433 .filter((item) => pathActive(pathname, item.to, item.exact) && searchMatches(item, search))
434 .sort((a, b) => b.to.length - a.to.length || Number(!!b.search) - Number(!!a.search))[0];
435}
436
437// Routes that are reachable but carry no sidebar entry — the account menu, a
438// course-kind view, a deep link. Without these the shell's title falls back to
439// the raw pathname and the header reads "/profile/me".
440const UNLISTED_ROUTE_LABELS: { prefix: string; labelKey: MessageKey }[] = [
441 // Longest prefix first — /profile/me is the viewer's own, every other
442 // /profile/{id} belongs to somebody else and must not read "My profile".
443 { prefix: "/profile/me", labelKey: "profile.myProfile" },
444 { prefix: "/profile", labelKey: "profile.title" },
445 { prefix: "/guide", labelKey: "nav.guide" },
446 { prefix: "/attendance", labelKey: "nav.attendance" },
447 { prefix: "/studies", labelKey: "courses.kind.study" },
448 { prefix: "/clubs", labelKey: "courses.kind.club" },
449 { prefix: "/courses", labelKey: "nav.classes" },
450 { prefix: "/notes", labelKey: "nav.notes" },
451 { prefix: "/questions", labelKey: "nav.questions" },
452 { prefix: "/events", labelKey: "nav.events" },
453 { prefix: "/meals", labelKey: "nav.meals" },
454];
455
456/**
457 * The label the shell header shows for a route: its nav entry when it has one,
458 * otherwise an explicit fallback. Returns undefined only for routes rendered
459 * without the shell (login, register, the exam room).
460 */
461export function routeLabelKey(pathname: string, role: Role | undefined): MessageKey | undefined {
462 const item = routeNavItem(pathname, role);
463 if (item) return item.labelKey;
464
465 const unlisted = UNLISTED_ROUTE_LABELS.find(
466 (entry) => pathname === entry.prefix || pathname.startsWith(`${entry.prefix}/`),
467 );
468 if (unlisted) return unlisted.labelKey;
469
470 // A page still has a name when the viewer may not open it — the shell title
471 // must not go blank on the "no access" screen — so fall back to the nav entry
472 // for any role, not just this one.
473 const allItems = [HOME_ITEM, ...Object.values(GROUPS_BY_ROLE).flatMap((groups) => groups.flatMap((group) => group.items))];
474 return allItems
475 .filter((entry) => pathActive(pathname, entry.to, entry.exact))
476 .sort((a, b) => b.to.length - a.to.length)[0]?.labelKey;
477}