Skip to content

Commit abde2bc

Browse files
feat(docs): persistent session id and clerk in docs (CopilotKit#518)
1 parent 615b964 commit abde2bc

13 files changed

Lines changed: 336 additions & 23 deletions

File tree

docs/components/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from "./frame";
22
export * from "./sidenav/sidenav-title-with-icon";
33
export * from "./reference/property-reference";
44
export * from "./copilotkit-css";
5+
export * from "./link-to-copilot-cloud";
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { useEffect, useState } from "react";
2+
import Link from "next/link";
3+
import { useAuth } from "@clerk/nextjs";
4+
import posthog from "posthog-js";
5+
6+
export function LinkToCopilotCloud({
7+
className,
8+
subPath,
9+
asLink = true,
10+
children
11+
}: {
12+
className?: string;
13+
subPath?: string;
14+
asLink?: boolean;
15+
children?: React.ReactNode;
16+
}) {
17+
const [isClient, setIsClient] = useState(false);
18+
const { userId } = useAuth();
19+
20+
useEffect(() => {
21+
setIsClient(true);
22+
}, []);
23+
24+
if (!isClient) {
25+
return null;
26+
}
27+
const url = new URL(`https://cloud.copilotkit.ai`);
28+
url.searchParams.set("ref", "docs");
29+
30+
const sessionId = posthog.get_session_id();
31+
32+
if (sessionId) {
33+
url.searchParams.set("session_id", sessionId);
34+
}
35+
36+
if (subPath) {
37+
url.pathname += subPath;
38+
}
39+
40+
let cn = `${className}`;
41+
42+
if (asLink) {
43+
cn = "_text-primary-600 decoration-from-font underline [text-underline-position:from-font]";
44+
}
45+
46+
return (
47+
<Link
48+
href={url.toString()}
49+
target="_blank"
50+
className={`${cn}`}
51+
>
52+
{
53+
children ? children : userId ? "Go to Copilot Cloud" : "Sign up to Copilot Cloud"
54+
}
55+
</Link>
56+
);
57+
}

docs/globals.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ button:has(.nested-title) > svg {
4848
@apply hidden;
4949
}
5050

51+
button:has(.disable-menu) > svg {
52+
@apply hidden;
53+
}
54+
55+
[id^="headlessui-menu-items-"] {
56+
@apply hidden !important;
57+
}
58+
5159
.nested-title {
5260
@apply text-gray-900 font-semibold [&:not(:first-child)]:mt-5 mb-2 pb-1.5;
5361
}
Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
1-
// Check that PostHog is client-side (used to handle Next.js SSR)
2-
import { useRouter } from "next/router";
1+
import { PostHogProvider as PostHogProviderBase } from "posthog-js/react";
32
import posthog from "posthog-js";
3+
import { useRouter } from "next/router";
44
import { useEffect } from "react";
5+
import { useAuth } from "@clerk/nextjs";
6+
import { useSearchParams } from "next/navigation";
57

68
const POSTHOG_KEY = process.env.POSTHOG_KEY;
79
const POSTHOG_HOST = process.env.POSTHOG_HOST;
810

911
if (typeof window !== "undefined") {
12+
// Get search param
13+
const searchParams = new URLSearchParams(window.location.search);
14+
const sessionId = searchParams.get("session_id");
15+
1016
if (POSTHOG_KEY && POSTHOG_HOST) {
17+
1118
posthog.init(POSTHOG_KEY, {
1219
api_host: POSTHOG_HOST,
1320
person_profiles: "identified_only",
21+
bootstrap: {
22+
sessionID: sessionId ?? undefined,
23+
},
1424
// Enable debug mode in development
1525
loaded: (posthog) => {
1626
if (process.env.NODE_ENV === "development") posthog.debug();
@@ -19,8 +29,18 @@ if (typeof window !== "undefined") {
1929
}
2030
}
2131

22-
export function usePostHog() {
32+
export function PostHogProvider({ children }: { children: React.ReactNode }) {
2333
const router = useRouter();
34+
const { userId } = useAuth();
35+
const searchParams = useSearchParams();
36+
37+
const sessionId = searchParams.get("session_id");
38+
39+
posthog?.set_config({
40+
bootstrap: {
41+
sessionID: sessionId ?? undefined,
42+
},
43+
});
2444

2545
useEffect(() => {
2646
if (!POSTHOG_KEY || !POSTHOG_HOST) return;
@@ -36,5 +56,11 @@ export function usePostHog() {
3656
};
3757
}, []);
3858

39-
return { posthog };
40-
}
59+
useEffect(() => {
60+
if (userId) {
61+
posthog?.identify(userId);
62+
}
63+
}, [userId]);
64+
65+
return <PostHogProviderBase client={posthog}>{children}</PostHogProviderBase>;
66+
}

docs/next.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default withNextra({
1212
POSTHOG_KEY: process.env.POSTHOG_KEY,
1313
POSTHOG_HOST: process.env.POSTHOG_HOST,
1414
SCARF_PIXEL_ID: process.env.SCARF_PIXEL_ID,
15+
CLERK_PUBLISHABLE_KEY: process.env.CLERK_PUBLISHABLE_KEY || "pk_live_Y2xlcmsuY29waWxvdGtpdC5haSQ",
1516
},
1617
async redirects() {
1718
return [

docs/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"author": "",
1414
"license": "ISC",
1515
"dependencies": {
16+
"@clerk/nextjs": "^5.2.8",
1617
"@stackblitz/sdk": "^1.11.0",
1718
"@vercel/og": "^0.6.2",
1819
"next": "^14.2.5",

docs/pages/_app.tsx

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import "../globals.css";
22
import { AppProps } from "next/app";
33
import { IBM_Plex_Sans } from "next/font/google";
44
import { useRB2B } from "../lib/hooks/useRB2B";
5-
import { PostHogProvider } from "posthog-js/react";
6-
import { usePostHog } from "../lib/hooks/usePostHog";
75
import { ScarfPixel } from "../lib/ScarfPixel";
6+
import { ClerkProvider } from "@clerk/nextjs";
7+
import { PostHogProvider } from "../lib/providers/PostHogProvider";
88

99
const plex = IBM_Plex_Sans({
1010
subsets: ["latin"],
@@ -13,16 +13,17 @@ const plex = IBM_Plex_Sans({
1313

1414
export default function App({ Component, pageProps }: AppProps) {
1515
useRB2B();
16-
const { posthog } = usePostHog();
1716

1817
return (
1918
<>
20-
<PostHogProvider client={posthog}>
21-
<main className={plex.className}>
22-
<Component {...pageProps} />
23-
</main>
24-
</PostHogProvider>
25-
<ScarfPixel />
19+
<ClerkProvider publishableKey={process.env.CLERK_PUBLISHABLE_KEY}>
20+
<PostHogProvider>
21+
<main className={plex.className}>
22+
<Component {...pageProps} />
23+
</main>
24+
</PostHogProvider>
25+
<ScarfPixel />
26+
</ClerkProvider>
2627
</>
2728
);
2829
}

docs/pages/_meta.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1+
import { LinkToCopilotCloud } from "../components/link-to-copilot-cloud";
2+
13
export default {
4+
"copilot-cloud": {
5+
title: (
6+
<LinkToCopilotCloud asLink={false} className="disable-menu bg-indigo-500 py-1 px-4 text-white font-medium rounded-full" />
7+
),
8+
type: "menu",
9+
},
210
"___getting-started": {
311
type: "separator",
412
title: "Getting Started",
@@ -15,15 +23,15 @@ export default {
1523
"tutorial-textarea": {
1624
title: "Tutorial: Textarea Autocomplete",
1725
},
18-
"concepts": {
26+
concepts: {
1927
title: <span className="nested-title">Concepts</span>,
2028
type: "doc",
2129
},
22-
"reference": {
30+
reference: {
2331
title: <span className="nested-title">Reference</span>,
2432
type: "doc",
2533
},
26-
"___contributing": {
34+
___contributing: {
2735
type: "separator",
2836
title: "Contributing",
2937
},
@@ -33,7 +41,7 @@ export default {
3341
"documentation-contributions": {
3442
title: "Documentation Contributions",
3543
},
36-
"___extras": {
44+
___extras: {
3745
type: "separator",
3846
title: "Extras",
3947
},

docs/pages/quickstart-chatbot.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Callout } from "nextra/components";
22
import { Tabs } from "nextra/components";
3+
import { LinkToCopilotCloud } from "../components";
34

45
# Quickstart: Chatbot
56

@@ -17,7 +18,7 @@ First, you must wrap all components that interact with your copilot with the [`<
1718
Additionally, let's use the [`<CopilotSidebar />`](/reference/components/CopilotSidebar) UI component to display the copilot chat sidebar. Some other options you can choose from are [`<CopilotPopup />`](/reference/components/CopilotPopup) and [`<CopilotChat />`](/reference/components/CopilotChat).
1819

1920
<Callout type="info">
20-
Copilot Cloud is the easiest way to get started with CopilotKit. [Sign up for free and get your public API key](https://cloud.copilotkit.ai), then replace `<your-public-api-key>` with your actual API key.
21+
Copilot Cloud is the easiest way to get started with CopilotKit. <LinkToCopilotCloud>Sign up for free and get your public API key</LinkToCopilotCloud>, then replace `<your-public-api-key>` with your actual API key.
2122

2223
If you prefer to self-host, you will need to [set up your Copilot Runtime endpoint](/concepts/copilot-runtime).
2324
</Callout>

docs/pages/reference/components/CopilotKit.mdx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Tabs } from "nextra/components";
22
import { PropertyReference } from "../../../components/reference/property-reference";
3+
import { LinkToCopilotCloud } from "../../../components";
34

45
# `<CopilotKit />`
56

@@ -10,7 +11,7 @@ This component will typically wrap your entire application (or a sub-tree of you
1011
<Tabs items={["Copilot Cloud (Recommended)", "Self-hosted Runtime"]}>
1112
<Tabs.Tab>
1213

13-
You can get your Copilot Cloud API key for free by [signing up here](https://cloud.copilotkit.ai).
14+
You can get your Copilot Cloud API key for free by <LinkToCopilotCloud>signing up here</LinkToCopilotCloud>.
1415

1516
```tsx
1617
import { CopilotKit } from "@copilotkit/react-core";
@@ -41,7 +42,7 @@ This component will typically wrap your entire application (or a sub-tree of you
4142
</PropertyReference>
4243

4344
<PropertyReference name="publicApiKey" type="string">
44-
Your Copilot Cloud API key. Don't have it yet? [Click here to sign up for free](https://cloud.copilotkit.ai).
45+
Your Copilot Cloud API key. Don't have it yet? <LinkToCopilotCloud>Click here to sign up for free</LinkToCopilotCloud>.
4546
</PropertyReference>
4647

4748
<PropertyReference name="cloudRestrictToTopic" cloudOnly type="{ validTopics?: string[]; invalidTopics?: string[]; }" >

0 commit comments

Comments
 (0)