import { AddProject } from "@/components/dashboard/projects/add"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "../ui/tabs"; import { useEffect, useMemo, useState } from "react"; import { useRouter } from "next/router"; import { api } from "@/utils/api"; import type { Auth } from "@/server/api/services/auth"; import type { User } from "@/server/api/services/user"; interface TabInfo { label: string; tabLabel?: string; description: string; index: string; isShow?: ({ rol, user }: { rol?: Auth["rol"]; user?: User }) => boolean; } export type TabState = | "projects" | "monitoring" | "settings" | "traefik" | "docker"; const tabMap: Record = { projects: { label: "Projects", description: "Manage your projects", index: "/dashboard/projects", }, monitoring: { label: "Monitoring", description: "Monitor your projects", index: "/dashboard/monitoring", }, traefik: { label: "Traefik", tabLabel: "Traefik File System", description: "Manage your traefik", index: "/dashboard/traefik", isShow: ({ rol, user }) => { return Boolean(rol === "admin" || user?.canAccessToTraefikFiles); }, }, docker: { label: "Docker", description: "Manage your docker", index: "/dashboard/docker", isShow: ({ rol, user }) => { return Boolean(rol === "admin" || user?.canAccessToDocker); }, }, settings: { label: "Settings", description: "Manage your settings", index: "/dashboard/settings/server", }, }; interface Props { tab: TabState; children: React.ReactNode; } export const NavigationTabs = ({ tab, children }: Props) => { const router = useRouter(); const { data } = api.auth.get.useQuery(); const [activeTab, setActiveTab] = useState(tab); const { data: user } = api.user.byAuthId.useQuery( { authId: data?.id || "", }, { enabled: !!data?.id && data?.rol === "user", }, ); useEffect(() => { setActiveTab(tab); }, [tab]); const activeTabInfo = useMemo(() => { return tabMap[activeTab]; }, [activeTab]); return (

{activeTabInfo.label}

{activeTabInfo.description}

{tab === "projects" && (data?.rol === "admin" || user?.canCreateProjects) && }
{ setActiveTab(e as TabState); router.push(tabMap[e as TabState].index); }} > {/* className="grid w-fit grid-cols-4 bg-transparent" */}
{Object.keys(tabMap).map((key) => { const tab = tabMap[key as TabState]; if (tab.isShow && !tab.isShow?.({ rol: data?.rol, user })) { return null; } return ( {tab.tabLabel || tab.label} {key === activeTab && (
)} ); })}
{children}
); };