|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState } from "react"; |
| 4 | +import Link from "next/link"; |
| 5 | +import { QueryClientProvider, QueryClient } from "@tanstack/react-query"; |
| 6 | +import { StackProvider } from "@btst/stack/context"; |
| 7 | +import { ChatLayout } from "@btst/stack/plugins/ai-chat/client"; |
| 8 | +import { PageAIContextProvider } from "@btst/stack/plugins/ai-chat/client/context"; |
| 9 | +import type { AiChatPluginOverrides } from "@btst/stack/plugins/ai-chat/client"; |
| 10 | + |
| 11 | +type PluginOverrides = { |
| 12 | + "ai-chat": AiChatPluginOverrides; |
| 13 | +}; |
| 14 | + |
| 15 | +const hasApiKey = |
| 16 | + typeof process !== "undefined" && !!process.env.NEXT_PUBLIC_HAS_OPENAI_KEY; |
| 17 | + |
| 18 | +const getBaseURL = () => |
| 19 | + typeof window !== "undefined" |
| 20 | + ? window.location.origin |
| 21 | + : "http://localhost:3000"; |
| 22 | + |
| 23 | +export default function WidgetPage() { |
| 24 | + const [queryClient] = useState(() => new QueryClient()); |
| 25 | + const baseURL = getBaseURL(); |
| 26 | + |
| 27 | + return ( |
| 28 | + <QueryClientProvider client={queryClient}> |
| 29 | + <PageAIContextProvider> |
| 30 | + <StackProvider<PluginOverrides> |
| 31 | + basePath="" |
| 32 | + overrides={{ |
| 33 | + "ai-chat": { |
| 34 | + apiBaseURL: baseURL, |
| 35 | + apiBasePath: "/api/data", |
| 36 | + mode: "public", |
| 37 | + navigate: () => {}, |
| 38 | + chatSuggestions: [ |
| 39 | + "What plugins does BTST offer?", |
| 40 | + "How do I install @btst/stack?", |
| 41 | + "Tell me about the blog plugin", |
| 42 | + ], |
| 43 | + }, |
| 44 | + }} |
| 45 | + > |
| 46 | + <div className="min-h-screen flex flex-col bg-background"> |
| 47 | + {/* Nav */} |
| 48 | + <nav className="border-b bg-background sticky top-0 z-50"> |
| 49 | + <div className="max-w-5xl mx-auto px-4 h-14 flex items-center justify-between"> |
| 50 | + <Link |
| 51 | + href="/" |
| 52 | + className="font-semibold text-lg hover:opacity-75 transition-opacity" |
| 53 | + > |
| 54 | + BTST AI Chat Demo |
| 55 | + </Link> |
| 56 | + <div className="flex items-center gap-4 text-sm"> |
| 57 | + <Link |
| 58 | + href="/pages/chat" |
| 59 | + className="text-muted-foreground hover:text-foreground transition-colors" |
| 60 | + > |
| 61 | + Chat |
| 62 | + </Link> |
| 63 | + <Link |
| 64 | + href="/widget" |
| 65 | + className="text-foreground font-medium transition-colors" |
| 66 | + > |
| 67 | + Widget |
| 68 | + </Link> |
| 69 | + <a |
| 70 | + href="/api/data/reference" |
| 71 | + className="text-muted-foreground hover:text-foreground transition-colors" |
| 72 | + > |
| 73 | + API Docs |
| 74 | + </a> |
| 75 | + <Link |
| 76 | + href="/pages/route-docs" |
| 77 | + className="text-muted-foreground hover:text-foreground transition-colors" |
| 78 | + > |
| 79 | + Route Docs |
| 80 | + </Link> |
| 81 | + </div> |
| 82 | + </div> |
| 83 | + </nav> |
| 84 | + |
| 85 | + {!hasApiKey && ( |
| 86 | + <div className="bg-amber-50 border-b border-amber-200 px-4 py-2 text-center text-sm text-amber-800"> |
| 87 | + Add <code className="font-mono">OPENAI_API_KEY</code> to{" "} |
| 88 | + <code className="font-mono">.env.local</code> to enable AI chat. |
| 89 | + </div> |
| 90 | + )} |
| 91 | + |
| 92 | + {/* Page content */} |
| 93 | + <main className="flex-1 max-w-5xl mx-auto w-full px-4 py-12 space-y-12"> |
| 94 | + <div className="space-y-2"> |
| 95 | + <h1 className="text-3xl font-bold tracking-tight"> |
| 96 | + Widget Mode |
| 97 | + </h1> |
| 98 | + <p className="text-muted-foreground max-w-xl"> |
| 99 | + The AI Chat plugin can be embedded as a floating widget on any |
| 100 | + page. Click the button in the bottom-right corner to open it. |
| 101 | + </p> |
| 102 | + </div> |
| 103 | + |
| 104 | + <div className="grid gap-6 sm:grid-cols-2"> |
| 105 | + <div className="rounded-xl border p-6 space-y-2"> |
| 106 | + <h2 className="font-semibold">Compact & embeddable</h2> |
| 107 | + <p className="text-sm text-muted-foreground"> |
| 108 | + The widget renders as a fixed-position panel triggered by a |
| 109 | + floating button. It does not affect the surrounding page |
| 110 | + layout. |
| 111 | + </p> |
| 112 | + </div> |
| 113 | + <div className="rounded-xl border p-6 space-y-2"> |
| 114 | + <h2 className="font-semibold">Configurable size</h2> |
| 115 | + <p className="text-sm text-muted-foreground"> |
| 116 | + Pass <code className="font-mono text-xs">widgetHeight</code>{" "} |
| 117 | + and <code className="font-mono text-xs">widgetWidth</code>{" "} |
| 118 | + props to control the panel dimensions. |
| 119 | + </p> |
| 120 | + </div> |
| 121 | + <div className="rounded-xl border p-6 space-y-2"> |
| 122 | + <h2 className="font-semibold">Page-aware context</h2> |
| 123 | + <p className="text-sm text-muted-foreground"> |
| 124 | + Wrap your layout with{" "} |
| 125 | + <code className="font-mono text-xs"> |
| 126 | + PageAIContextProvider |
| 127 | + </code>{" "} |
| 128 | + and call{" "} |
| 129 | + <code className="font-mono text-xs"> |
| 130 | + useRegisterPageAIContext |
| 131 | + </code>{" "} |
| 132 | + on any page to give the AI awareness of the current view. |
| 133 | + </p> |
| 134 | + </div> |
| 135 | + <div className="rounded-xl border p-6 space-y-2"> |
| 136 | + <h2 className="font-semibold">Stateless by default</h2> |
| 137 | + <p className="text-sm text-muted-foreground"> |
| 138 | + In public mode the widget is stateless — no conversation |
| 139 | + history is persisted. Switch to{" "} |
| 140 | + <code className="font-mono text-xs"> |
| 141 | + mode="authenticated" |
| 142 | + </code>{" "} |
| 143 | + for persistent history. |
| 144 | + </p> |
| 145 | + </div> |
| 146 | + </div> |
| 147 | + |
| 148 | + <div className="rounded-xl border bg-muted/40 p-6 space-y-3"> |
| 149 | + <h2 className="font-semibold text-sm uppercase tracking-wider text-muted-foreground"> |
| 150 | + Usage |
| 151 | + </h2> |
| 152 | + <pre className="text-sm overflow-x-auto bg-background rounded-lg border p-4"> |
| 153 | + {`<ChatLayout |
| 154 | + apiBaseURL={baseURL} |
| 155 | + apiBasePath="/api/data" |
| 156 | + layout="widget" |
| 157 | + widgetHeight="520px" |
| 158 | + showSidebar={false} |
| 159 | +/>`} |
| 160 | + </pre> |
| 161 | + </div> |
| 162 | + </main> |
| 163 | + |
| 164 | + {/* Floating widget */} |
| 165 | + <div className="fixed bottom-6 right-6 z-50"> |
| 166 | + <ChatLayout |
| 167 | + apiBaseURL={baseURL} |
| 168 | + apiBasePath="/api/data" |
| 169 | + layout="widget" |
| 170 | + widgetHeight="520px" |
| 171 | + showSidebar={false} |
| 172 | + /> |
| 173 | + </div> |
| 174 | + </div> |
| 175 | + </StackProvider> |
| 176 | + </PageAIContextProvider> |
| 177 | + </QueryClientProvider> |
| 178 | + ); |
| 179 | +} |
0 commit comments