import type { Metadata } from "next"; import Link from "next/link"; import { BookOpen } from "lucide-react"; import { COUNTRIES, PLAN_LIMITS, normalizePlan } from "@fetcha/core"; import { Button } from "@/components/ui/button"; import { PageHeader } from "@/components/ui/page-header"; import { Playground } from "@/components/playground/playground"; import { FORMATS, HTTP_METHODS, NETWORKS, type BuilderState } from "@/components/playground/types"; import { internalApi } from "@/lib/api"; import { getReplayRequest, listPlaygroundKeys } from "@/lib/queries/playground"; import { getWorkspace } from "@/lib/session"; import { API_PUBLIC_URL } from "@/lib/utils"; export const dynamic = "force-dynamic"; export const metadata: Metadata = { title: "Playground" }; type SearchParams = Promise<{ url?: string | string[]; request?: string | string[] }>; function first(v: string | string[] | undefined): string | undefined { return Array.isArray(v) ? v[0] : v; } export default async function PlaygroundPage({ searchParams }: { searchParams: SearchParams }) { const [sp, ws] = await Promise.all([searchParams, getWorkspace()]); const limits = PLAN_LIMITS[normalizePlan(ws.organization.plan)]; const replayId = first(sp.request)?.trim(); const prefillUrl = first(sp.url)?.trim(); const [keys, ready, replay] = await Promise.all([ listPlaygroundKeys(ws.project.id, ws.organization.id), internalApi.ready().catch(() => ({ status: "unknown", checks: {}, available_networks: [] as string[] })), replayId ? getReplayRequest(ws.project.id, replayId).catch(() => null) : Promise.resolve(null), ]); let initial: Partial | null = null; if (replay) { initial = { url: replay.url, method: (HTTP_METHODS as readonly string[]).includes(replay.method) ? (replay.method as BuilderState["method"]) : "GET", network: (NETWORKS as readonly string[]).includes(replay.requestedNetwork) ? (replay.requestedNetwork as BuilderState["network"]) : "auto", country: replay.country ?? "", region: replay.region ?? "", city: replay.city ?? "", format: (FORMATS as readonly string[]).includes(replay.format) ? (replay.format as BuilderState["format"]) : "html", }; } else if (prefillUrl) { initial = { url: prefillUrl.slice(0, 8192) }; } const projectDefaults: Partial = { country: ws.project.defaultCountry ?? "", network: (NETWORKS as readonly string[]).includes(ws.project.defaultNetwork) ? (ws.project.defaultNetwork as BuilderState["network"]) : "auto", }; const countries = Object.entries(COUNTRIES) .map(([code, name]) => ({ code, name })) .sort((a, b) => a.name.localeCompare(b.name)); return ( <> API reference } /> {replayId && !replay ?

Request {replayId} was not found in this project, so nothing was prefilled.

: null} ); }