integratedhub/islands/WelcomeBar/navigationConfig.ts
2026-02-14 02:20:19 +00:00

180 lines
4.7 KiB
TypeScript

import { NavigationGroup } from "./welcome-types.ts";
/**
* Default navigation groups configuration
*/
export const defaultNavigationGroups: NavigationGroup[] = [
{
id: "auth",
label: "Authentication",
items: [
{
path: "/auth/login",
label: "Sign In",
requiresAuth: false,
isForm: true,
formAction: "/auth/login",
formMethod: "get",
},
{
path: "/auth/logout",
label: "Logout",
requiresAuth: true,
isForm: true,
formAction: "/auth/logout",
formMethod: "post",
onClick: () => {
localStorage.setItem("lastClickedButton", "logout");
},
},
],
},
{
id: "financial-tools",
label: "Financial Tools",
items: [
{
path: `/calendar/financial-calendar/${new Date().getFullYear()}/${
(new Date().getMonth() + 1).toString().padStart(2, "0")
}?`,
label: "Financial Calendar",
requiresAuth: true,
},
{
path: "/calendar/every-income",
label: "Every Income",
requiresAuth: true,
},
{
path: "/calendar/budget-obligations",
label: "Budget Obligations",
requiresAuth: true,
},
],
},
{
id: "language",
label: "Language & Learning",
items: [
{
path: "/language/dashboard",
label: "Dashboard",
requiresAuth: true,
},
// {
// path: "/language/set-quiz",
// label: "Set Quiz",
// requiresAuth: true,
// },
{
path: "/language/lists",
label: "Lists",
requiresAuth: true,
},
{
path: "/language/arrange",
label: "Arrange",
requiresAuth: true,
},
],
},
{
id: "general",
label: "General",
items: [
{
path: "/",
label: "Home",
requiresAuth: false,
},
{
path: "/user-profile",
label: "Profile",
requiresAuth: true,
},
{
path: "#help",
label: "Help",
requiresAuth: false,
disabled: true,
},
{
path: "#about",
label: "About",
requiresAuth: false,
disabled: true,
},
],
},
];
/**
* Get navigation groups filtered by authentication status and current route
*
* When on /language/* routes, only show auth + language-specific navigation,
* excluding the current route from the language items.
*
* When on /calendar/* routes, only show auth + financial-tools navigation,
* excluding the current route from the financial-tools items.
*/
export function getNavigationGroups(
isLoggedIn: boolean,
currentRoute: string,
customGroups?: NavigationGroup[],
): NavigationGroup[] {
const groups = customGroups || defaultNavigationGroups;
const isLanguageRoute = currentRoute.startsWith("/language/");
const isCalendarRoute = currentRoute.startsWith("/calendar/");
return groups
.filter((group) => {
// On language routes, only show auth and language groups
if (isLanguageRoute) {
return group.id === "auth" || group.id === "language";
}
// On calendar routes, only show auth and financial-tools groups
if (isCalendarRoute) {
return group.id === "auth" || group.id === "financial-tools";
}
return true;
})
.map((group) => ({
...group,
items: group.items.filter((item) => {
// Filter out items that require authentication if user is not logged in
if (item.requiresAuth && !isLoggedIn) {
return false;
}
// Show login only when logged out, logout only when logged in
if (group.id === "auth") {
if (item.path === "/auth/login" && isLoggedIn) {
return false;
}
if (item.path === "/auth/logout" && !isLoggedIn) {
return false;
}
}
// On language routes, exclude the current route from language items
if (
isLanguageRoute && group.id === "language" &&
item.path === currentRoute
) {
return false;
}
// On calendar routes, exclude the current route from financial-tools items
if (isCalendarRoute && group.id === "financial-tools") {
// Check if current route matches or starts with the item path (for dynamic routes)
const itemBasePath = item.path.split("?")[0]; // Remove query params
if (
currentRoute === itemBasePath ||
currentRoute.startsWith(itemBasePath)
) {
return false;
}
}
return true;
}),
}))
.filter((group) => group.items.length > 0);
}