diff --git a/apps/web/src/pages/[organization]/[project]/[dashboard].tsx b/apps/web/src/pages/[organization]/[project]/[dashboard].tsx index f60be8c2..ed3e4546 100644 --- a/apps/web/src/pages/[organization]/[project]/[dashboard].tsx +++ b/apps/web/src/pages/[organization]/[project]/[dashboard].tsx @@ -13,7 +13,9 @@ import { } from '@/components/ui/dropdown-menu'; import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group'; import { useOrganizationParams } from '@/hooks/useOrganizationParams'; +import { db } from '@/server/db'; import { createServerSideProps } from '@/server/getServerSideProps'; +import { getDashboardBySlug } from '@/server/services/dashboard.service'; import type { IChartRange } from '@/types'; import { api, handleError } from '@/utils/api'; import { cn } from '@/utils/cn'; @@ -22,7 +24,27 @@ import { getRangeLabel } from '@/utils/getRangeLabel'; import { ChevronRight, MoreHorizontal, Trash } from 'lucide-react'; import Link from 'next/link'; -export const getServerSideProps = createServerSideProps(); +export const getServerSideProps = createServerSideProps(async (context) => { + const projectSlug = context.params?.project as string; + const dashboardSlug = context.params?.dashboard as string; + try { + await db.dashboard.findFirstOrThrow({ + select: { + id: true, + }, + where: { + slug: dashboardSlug, + project: { + slug: projectSlug, + }, + }, + }); + } catch (error) { + return { + notFound: true, + }; + } +}); export default function Dashboard() { const params = useOrganizationParams(); diff --git a/apps/web/src/pages/[organization]/[project]/index.tsx b/apps/web/src/pages/[organization]/[project]/index.tsx index ecccd59f..7381b829 100644 --- a/apps/web/src/pages/[organization]/[project]/index.tsx +++ b/apps/web/src/pages/[organization]/[project]/index.tsx @@ -3,14 +3,30 @@ import { Container } from '@/components/Container'; import { MainLayout } from '@/components/layouts/MainLayout'; import { PageTitle } from '@/components/PageTitle'; import { useOrganizationParams } from '@/hooks/useOrganizationParams'; -import { useRefetchActive } from '@/hooks/useRefetchActive'; import { pushModal } from '@/modals'; +import { db } from '@/server/db'; import { createServerSideProps } from '@/server/getServerSideProps'; import { api, handleError } from '@/utils/api'; import { Pencil, Plus, Trash } from 'lucide-react'; import Link from 'next/link'; -export const getServerSideProps = createServerSideProps(); +export const getServerSideProps = createServerSideProps(async (context) => { + const projectSlug = context.params?.project as string; + try { + await db.project.findFirstOrThrow({ + select: { + id: true, + }, + where: { + slug: projectSlug, + }, + }); + } catch (error) { + return { + notFound: true, + }; + } +}); export default function Home() { const params = useOrganizationParams(); diff --git a/apps/web/src/pages/index.tsx b/apps/web/src/pages/index.tsx index 2b3b4967..3b6bc812 100644 --- a/apps/web/src/pages/index.tsx +++ b/apps/web/src/pages/index.tsx @@ -1,22 +1,9 @@ -import { useEffect } from 'react'; import { MainLayout } from '@/components/layouts/MainLayout'; import { createServerSideProps } from '@/server/getServerSideProps'; -import { api } from '@/utils/api'; -import { useRouter } from 'next/router'; export const getServerSideProps = createServerSideProps(); export default function Home() { - const router = useRouter(); - const query = api.organization.first.useQuery(); - const organization = query.data ?? null; - - useEffect(() => { - if (organization) { - router.replace(`/${organization.slug}`); - } - }, [organization, router]); - return (
diff --git a/apps/web/src/server/getServerSideProps.ts b/apps/web/src/server/getServerSideProps.ts index f4559b72..002e9e94 100644 --- a/apps/web/src/server/getServerSideProps.ts +++ b/apps/web/src/server/getServerSideProps.ts @@ -21,7 +21,7 @@ export function createServerSideProps( } if (context.params?.organization) { - const organization = await db.user.findFirst({ + const user = await db.user.findFirst({ where: { id: session.user.id, organization: { @@ -30,11 +30,35 @@ export function createServerSideProps( }, }); - if (!organization) { + if (!user) { return { notFound: true, }; } + } else { + const user = await db.user.findFirst({ + where: { + id: session.user.id, + }, + include: { + organization: true, + }, + }); + + if (!user) { + return { + notFound: true, + }; + } + + if (user.organization) { + return { + redirect: { + destination: `/${user.organization.slug}`, + permanent: false, + }, + }; + } } const res = await (typeof cb === 'function'