diff --git a/.gitignore b/.gitignore index 6c48782f9a..9e14e91791 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,4 @@ apps/web/src/components/__screenshots__ __screenshots__/ .tanstack squashfs-root/ +.vercel diff --git a/apps/web/src/hostedPairing.test.ts b/apps/web/src/hostedPairing.test.ts index f347ca46ab..ef2b2eb527 100644 --- a/apps/web/src/hostedPairing.test.ts +++ b/apps/web/src/hostedPairing.test.ts @@ -3,6 +3,7 @@ import { afterEach, describe, expect, it, vi } from "vitest"; import { buildHostedPairingUrl, hasHostedPairingRequest, + isHostedStaticApp, readHostedPairingRequest, } from "./hostedPairing"; @@ -49,4 +50,17 @@ describe("hostedPairing", () => { false, ); }); + + it("detects the hosted static app only when no backend URL is configured", () => { + vi.stubEnv("VITE_HOSTED_APP_URL", "https://preview.t3.codes"); + vi.stubEnv("VITE_HTTP_URL", ""); + vi.stubEnv("VITE_WS_URL", ""); + + expect(isHostedStaticApp(new URL("https://preview.t3.codes/"))).toBe(true); + expect(isHostedStaticApp(new URL("https://preview.t3.codes/pair"))).toBe(true); + expect(isHostedStaticApp(new URL("https://backend.example.com/"))).toBe(false); + + vi.stubEnv("VITE_HTTP_URL", "https://backend.example.com"); + expect(isHostedStaticApp(new URL("https://preview.t3.codes/"))).toBe(false); + }); }); diff --git a/apps/web/src/hostedPairing.ts b/apps/web/src/hostedPairing.ts index 9855c01be2..a44cfa7083 100644 --- a/apps/web/src/hostedPairing.ts +++ b/apps/web/src/hostedPairing.ts @@ -12,6 +12,27 @@ function configuredHostedAppUrl(): string { return import.meta.env.VITE_HOSTED_APP_URL?.trim() || DEFAULT_HOSTED_APP_URL; } +function configuredBackendUrl(): string { + return import.meta.env.VITE_HTTP_URL?.trim() || import.meta.env.VITE_WS_URL?.trim() || ""; +} + +function originFromUrl(value: string): string | null { + try { + return new URL(value).origin; + } catch { + return null; + } +} + +export function isHostedStaticApp(url: URL = new URL(window.location.href)): boolean { + if (configuredBackendUrl()) { + return false; + } + + const hostedOrigin = originFromUrl(configuredHostedAppUrl()); + return hostedOrigin !== null && url.origin === hostedOrigin; +} + export function readHostedPairingRequest(url: URL = new URL(window.location.href)) { const host = url.searchParams.get("host")?.trim() ?? ""; const token = getPairingTokenFromUrl(url)?.trim() ?? ""; diff --git a/apps/web/src/routes/__root.tsx b/apps/web/src/routes/__root.tsx index fb33c6ae23..df62be29df 100644 --- a/apps/web/src/routes/__root.tsx +++ b/apps/web/src/routes/__root.tsx @@ -58,7 +58,7 @@ import { resolveInitialServerAuthGateState, updatePrimaryEnvironmentDescriptor, } from "../environments/primary"; -import { hasHostedPairingRequest } from "../hostedPairing"; +import { hasHostedPairingRequest, isHostedStaticApp } from "../hostedPairing"; export const Route = createRootRouteWithContext<{ queryClient: QueryClient; @@ -72,6 +72,15 @@ export const Route = createRootRouteWithContext<{ }; } + if (isHostedStaticApp(new URL(window.location.href))) { + await waitForSavedEnvironmentRegistryHydration(); + return { + authGateState: { + status: "hosted-static", + } as const, + }; + } + try { const [, authGateState] = await Promise.all([ ensurePrimaryEnvironmentReady(), @@ -103,6 +112,7 @@ export const Route = createRootRouteWithContext<{ function RootRouteView() { const pathname = useLocation({ select: (location) => location.pathname }); const { authGateState } = Route.useRouteContext(); + const primaryEnvironmentAuthenticated = authGateState.status === "authenticated"; useEffect(() => { const frame = window.requestAnimationFrame(() => { @@ -120,23 +130,30 @@ function RootRouteView() { if (authGateState.status !== "authenticated" && authGateState.status !== "hosted-static") { return ; } + + const appShell = ( + + + + + + ); + return ( - - + {primaryEnvironmentAuthenticated ? : null} + {primaryEnvironmentAuthenticated ? : null} - - - - - - - - - - + {primaryEnvironmentAuthenticated ? : null} + {primaryEnvironmentAuthenticated ? : null} + {primaryEnvironmentAuthenticated ? : null} + {primaryEnvironmentAuthenticated ? ( + {appShell} + ) : ( + appShell + )} ); diff --git a/apps/web/src/routes/_chat.index.tsx b/apps/web/src/routes/_chat.index.tsx index 43f06177a7..98a125bdfe 100644 --- a/apps/web/src/routes/_chat.index.tsx +++ b/apps/web/src/routes/_chat.index.tsx @@ -1,11 +1,66 @@ import { createFileRoute } from "@tanstack/react-router"; +import { LinkIcon, PlusIcon } from "lucide-react"; import { NoActiveThreadState } from "../components/NoActiveThreadState"; +import { Button } from "../components/ui/button"; +import { Empty, EmptyDescription, EmptyHeader, EmptyTitle } from "../components/ui/empty"; +import { SidebarInset, SidebarTrigger } from "../components/ui/sidebar"; +import { useSavedEnvironmentRegistryStore } from "../environments/runtime"; +import { APP_DISPLAY_NAME } from "~/branding"; function ChatIndexRouteView() { + const { authGateState } = Route.useRouteContext(); + const savedEnvironmentCount = useSavedEnvironmentRegistryStore( + (state) => Object.keys(state.byId).length, + ); + + if (authGateState.status === "hosted-static" && savedEnvironmentCount === 0) { + return ; + } + return ; } export const Route = createFileRoute("/_chat/")({ component: ChatIndexRouteView, }); + +function HostedStaticOnboardingState() { + return ( + + + + + + + {APP_DISPLAY_NAME} + + + + + + + + + + + + Connect an environment to get started + + + Open a pairing link from your T3 Code desktop app or add a reachable backend + manually. Your saved environments stay in this browser. + + + } size="sm"> + + Add environment + + + + + + + + ); +} diff --git a/apps/web/src/vite-env.d.ts b/apps/web/src/vite-env.d.ts index 1d7d41db1b..99c5a65d85 100644 --- a/apps/web/src/vite-env.d.ts +++ b/apps/web/src/vite-env.d.ts @@ -3,6 +3,9 @@ import type { DesktopBridge, LocalApi } from "@t3tools/contracts"; interface ImportMetaEnv { + readonly VITE_HTTP_URL: string; + readonly VITE_WS_URL: string; + readonly VITE_HOSTED_APP_URL: string; readonly APP_VERSION: string; } diff --git a/apps/web/vercel.json b/apps/web/vercel.json new file mode 100644 index 0000000000..1323cdac34 --- /dev/null +++ b/apps/web/vercel.json @@ -0,0 +1,8 @@ +{ + "rewrites": [ + { + "source": "/(.*)", + "destination": "/index.html" + } + ] +} diff --git a/apps/web/vite.config.ts b/apps/web/vite.config.ts index 091cb454b1..c4d44064b8 100644 --- a/apps/web/vite.config.ts +++ b/apps/web/vite.config.ts @@ -9,7 +9,15 @@ const port = Number(process.env.PORT ?? 5733); const host = process.env.HOST?.trim() || "localhost"; const configuredHttpUrl = process.env.VITE_HTTP_URL?.trim(); const configuredWsUrl = process.env.VITE_WS_URL?.trim(); -const configuredHostedAppUrl = process.env.VITE_HOSTED_APP_URL?.trim(); +const configuredHostedAppUrl = (() => { + if (process.env.VERCEL_ENV === "production" && process.env.VERCEL_PROJECT_PRODUCTION_URL) { + return `https://${process.env.VERCEL_PROJECT_PRODUCTION_URL}`; + } + if (process.env.VERCEL_URL) { + return `https://${process.env.VERCEL_URL}`; + } + return process.env.VITE_HOSTED_APP_URL?.trim(); +})(); const sourcemapEnv = process.env.T3CODE_WEB_SOURCEMAP?.trim().toLowerCase(); const buildSourcemap =