|
| 1 | +import { redirect } from "next/navigation"; |
| 2 | +import { ResponsiveSearchParamsProvider } from "responsive-rsc"; |
| 3 | +import { getAuthToken } from "@/api/auth-token"; |
| 4 | +import { getProject } from "@/api/project/projects"; |
| 5 | +import type { DurationId } from "@/components/analytics/date-range-selector"; |
| 6 | +import { ResponsiveTimeFilters } from "@/components/analytics/responsive-time-filters"; |
| 7 | +import { ProjectPage } from "@/components/blocks/project-page/project-page"; |
| 8 | +import { getClientThirdwebClient } from "@/constants/thirdweb-client.client"; |
| 9 | +import { NebulaIcon } from "@/icons/NebulaIcon"; |
| 10 | +import { getFiltersFromSearchParams } from "@/lib/time"; |
| 11 | +import { loginRedirect } from "@/utils/redirects"; |
| 12 | +import { AiAnalytics } from "./chart"; |
| 13 | +import { AiSummary } from "./chart/Summary"; |
| 14 | + |
| 15 | +export default async function Page(props: { |
| 16 | + params: Promise<{ team_slug: string; project_slug: string }>; |
| 17 | + searchParams: Promise<{ |
| 18 | + from?: string; |
| 19 | + to?: string; |
| 20 | + type?: string; |
| 21 | + interval?: string; |
| 22 | + }>; |
| 23 | +}) { |
| 24 | + const [searchParams, params] = await Promise.all([ |
| 25 | + props.searchParams, |
| 26 | + props.params, |
| 27 | + ]); |
| 28 | + |
| 29 | + const { team_slug, project_slug } = params; |
| 30 | + |
| 31 | + const [project, authToken] = await Promise.all([ |
| 32 | + getProject(team_slug, project_slug), |
| 33 | + getAuthToken(), |
| 34 | + ]); |
| 35 | + |
| 36 | + if (!authToken) { |
| 37 | + loginRedirect(`/team/${team_slug}/${project_slug}/ai`); |
| 38 | + } |
| 39 | + |
| 40 | + if (!project) { |
| 41 | + redirect(`/team/${team_slug}`); |
| 42 | + } |
| 43 | + |
| 44 | + const client = getClientThirdwebClient({ |
| 45 | + jwt: authToken, |
| 46 | + teamId: project.teamId, |
| 47 | + }); |
| 48 | + |
| 49 | + const defaultRange = "last-30" as DurationId; |
| 50 | + const { range, interval } = getFiltersFromSearchParams({ |
| 51 | + defaultRange, |
| 52 | + from: searchParams.from, |
| 53 | + interval: searchParams.interval, |
| 54 | + to: searchParams.to, |
| 55 | + }); |
| 56 | + |
| 57 | + return ( |
| 58 | + <ResponsiveSearchParamsProvider value={searchParams}> |
| 59 | + <ProjectPage |
| 60 | + header={{ |
| 61 | + icon: NebulaIcon, |
| 62 | + client, |
| 63 | + title: "AI Analytics", |
| 64 | + description: "Track your AI app usage and performance", |
| 65 | + actions: null, |
| 66 | + links: [ |
| 67 | + { |
| 68 | + href: "https://portal.thirdweb.com/ai/chat", |
| 69 | + type: "docs", |
| 70 | + }, |
| 71 | + { |
| 72 | + href: "https://api.thirdweb.com/reference#tag/ai/post/ai/chat", |
| 73 | + type: "api", |
| 74 | + }, |
| 75 | + { |
| 76 | + href: "https://playground.thirdweb.com/ai/chat", |
| 77 | + type: "playground", |
| 78 | + }, |
| 79 | + ], |
| 80 | + }} |
| 81 | + > |
| 82 | + <div className="flex flex-col gap-4 md:gap-6"> |
| 83 | + <ResponsiveTimeFilters defaultRange={defaultRange} /> |
| 84 | + <AiSummary |
| 85 | + projectId={project.id} |
| 86 | + teamId={project.teamId} |
| 87 | + authToken={authToken} |
| 88 | + range={range} |
| 89 | + /> |
| 90 | + |
| 91 | + <AiAnalytics |
| 92 | + interval={interval} |
| 93 | + projectId={project.id} |
| 94 | + range={range} |
| 95 | + teamId={project.teamId} |
| 96 | + authToken={authToken} |
| 97 | + /> |
| 98 | + </div> |
| 99 | + </ProjectPage> |
| 100 | + </ResponsiveSearchParamsProvider> |
| 101 | + ); |
| 102 | +} |
0 commit comments