import { createFileRoute } from "@tanstack/react-router";
import { useQuery } from "@tanstack/react-query";

import { DashboardShell } from "@/components/dashboard-shell";
import { requireSession } from "@/lib/protected-route";
import { trpc } from "@/utils/trpc";

export const Route = createFileRoute("/dashboard")({
  component: RouteComponent,
  beforeLoad: requireSession,
});

function RouteComponent() {
  const { session } = Route.useRouteContext();
  const me = useQuery(trpc.users.me.queryOptions());

  return (
    <DashboardShell
      userName={session.data?.user.name}
      userEmail={session.data?.user.email}
      permissions={me.data?.permissions ?? []}
    />
  );
}
