web: ssr redirects
This commit is contained in:
@@ -13,7 +13,9 @@ import {
|
|||||||
} from '@/components/ui/dropdown-menu';
|
} from '@/components/ui/dropdown-menu';
|
||||||
import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group';
|
import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group';
|
||||||
import { useOrganizationParams } from '@/hooks/useOrganizationParams';
|
import { useOrganizationParams } from '@/hooks/useOrganizationParams';
|
||||||
|
import { db } from '@/server/db';
|
||||||
import { createServerSideProps } from '@/server/getServerSideProps';
|
import { createServerSideProps } from '@/server/getServerSideProps';
|
||||||
|
import { getDashboardBySlug } from '@/server/services/dashboard.service';
|
||||||
import type { IChartRange } from '@/types';
|
import type { IChartRange } from '@/types';
|
||||||
import { api, handleError } from '@/utils/api';
|
import { api, handleError } from '@/utils/api';
|
||||||
import { cn } from '@/utils/cn';
|
import { cn } from '@/utils/cn';
|
||||||
@@ -22,7 +24,27 @@ import { getRangeLabel } from '@/utils/getRangeLabel';
|
|||||||
import { ChevronRight, MoreHorizontal, Trash } from 'lucide-react';
|
import { ChevronRight, MoreHorizontal, Trash } from 'lucide-react';
|
||||||
import Link from 'next/link';
|
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() {
|
export default function Dashboard() {
|
||||||
const params = useOrganizationParams();
|
const params = useOrganizationParams();
|
||||||
|
|||||||
@@ -3,14 +3,30 @@ import { Container } from '@/components/Container';
|
|||||||
import { MainLayout } from '@/components/layouts/MainLayout';
|
import { MainLayout } from '@/components/layouts/MainLayout';
|
||||||
import { PageTitle } from '@/components/PageTitle';
|
import { PageTitle } from '@/components/PageTitle';
|
||||||
import { useOrganizationParams } from '@/hooks/useOrganizationParams';
|
import { useOrganizationParams } from '@/hooks/useOrganizationParams';
|
||||||
import { useRefetchActive } from '@/hooks/useRefetchActive';
|
|
||||||
import { pushModal } from '@/modals';
|
import { pushModal } from '@/modals';
|
||||||
|
import { db } from '@/server/db';
|
||||||
import { createServerSideProps } from '@/server/getServerSideProps';
|
import { createServerSideProps } from '@/server/getServerSideProps';
|
||||||
import { api, handleError } from '@/utils/api';
|
import { api, handleError } from '@/utils/api';
|
||||||
import { Pencil, Plus, Trash } from 'lucide-react';
|
import { Pencil, Plus, Trash } from 'lucide-react';
|
||||||
import Link from 'next/link';
|
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() {
|
export default function Home() {
|
||||||
const params = useOrganizationParams();
|
const params = useOrganizationParams();
|
||||||
|
|||||||
@@ -1,22 +1,9 @@
|
|||||||
import { useEffect } from 'react';
|
|
||||||
import { MainLayout } from '@/components/layouts/MainLayout';
|
import { MainLayout } from '@/components/layouts/MainLayout';
|
||||||
import { createServerSideProps } from '@/server/getServerSideProps';
|
import { createServerSideProps } from '@/server/getServerSideProps';
|
||||||
import { api } from '@/utils/api';
|
|
||||||
import { useRouter } from 'next/router';
|
|
||||||
|
|
||||||
export const getServerSideProps = createServerSideProps();
|
export const getServerSideProps = createServerSideProps();
|
||||||
|
|
||||||
export default function Home() {
|
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 (
|
return (
|
||||||
<MainLayout>
|
<MainLayout>
|
||||||
<div />
|
<div />
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ export function createServerSideProps(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (context.params?.organization) {
|
if (context.params?.organization) {
|
||||||
const organization = await db.user.findFirst({
|
const user = await db.user.findFirst({
|
||||||
where: {
|
where: {
|
||||||
id: session.user.id,
|
id: session.user.id,
|
||||||
organization: {
|
organization: {
|
||||||
@@ -30,11 +30,35 @@ export function createServerSideProps(
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!organization) {
|
if (!user) {
|
||||||
return {
|
return {
|
||||||
notFound: true,
|
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'
|
const res = await (typeof cb === 'function'
|
||||||
|
|||||||
Reference in New Issue
Block a user