Skip to content

Commit 211cf3f

Browse files
committed
[Docs Site] Add tracking events and hide footer elements on DocsAI
1 parent da6b0f1 commit 211cf3f

File tree

7 files changed

+69
-7
lines changed

7 files changed

+69
-7
lines changed

src/components/DocsAI.tsx

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { useState } from "react";
1+
import { useEffect, useState } from "react";
22
import Markdown from "react-markdown";
33
import remarkBreaks from "remark-breaks";
44
import remarkGfm from "remark-gfm";
55
import { fetchEventSource } from "@microsoft/fetch-event-source";
66
import { Ring } from "ldrs/react";
77
import { MdOutlineThumbUp, MdOutlineThumbDown } from "react-icons/md";
8+
import { track } from "~/util/zaraz";
89
import "ldrs/react/Ring.css";
910

1011
type Messages = {
@@ -38,15 +39,46 @@ function Messages({
3839
messages: Messages;
3940
loading: boolean;
4041
}) {
42+
const [listenersAdded, setListenersAdded] = useState<Set<string>>(new Set());
4143
const [feedbackGiven, setFeedbackGiven] = useState<Set<string>>(new Set());
4244

45+
useEffect(() => {
46+
const messages = document.querySelectorAll<HTMLDivElement>(
47+
"[data-docs-ai-message]",
48+
);
49+
50+
for (const message of messages) {
51+
if (listenersAdded.has(message.dataset.queryId ?? "")) {
52+
continue;
53+
}
54+
55+
const links = message.querySelectorAll<HTMLAnchorElement>("a");
56+
57+
for (const link of links) {
58+
link.addEventListener("click", () => {
59+
track("click chat link", {
60+
value: link.innerText,
61+
href: link.href,
62+
});
63+
});
64+
}
65+
66+
setListenersAdded((prev) =>
67+
new Set(prev).add(message.dataset.queryId ?? ""),
68+
);
69+
}
70+
}, [messages]);
71+
4372
const classes = {
4473
base: "w-fit max-w-3/4 rounded p-4",
4574
user: "bg-cl1-brand-orange text-cl1-black self-end",
4675
assistant: "self-start bg-(--sl-color-bg-nav)",
4776
};
4877

4978
const handleFeedback = async (queryId: string, positive: boolean) => {
79+
track("submit chat feedback", {
80+
value: positive.toString(),
81+
});
5082
await sendCSATFeedback(queryId, positive);
5183
setFeedbackGiven((prev) => new Set(prev).add(queryId));
5284
};
@@ -59,6 +91,8 @@ function Messages({
5991
<div key={index} className="flex flex-col gap-2">
6092
<div
6193
className={`${classes.base} ${message.role === "user" ? classes.user : classes.assistant}`}
94+
data-docs-ai-message={true}
95+
data-query-id={message.queryId}
6296
>
6397
<Markdown remarkPlugins={[remarkGfm, remarkBreaks]}>
6498
{message.content}
@@ -71,7 +105,7 @@ function Messages({
71105
</p>
72106
<ul>
73107
{message.sources.map((source) => (
74-
<li>
108+
<li key={source.file_path}>
75109
<a href={source.file_path} target="_blank">
76110
{source.title}
77111
</a>
@@ -124,6 +158,10 @@ export default function SupportAI() {
124158
const [messages, setMessages] = useState<Messages>([]);
125159

126160
async function handleSubmit() {
161+
track("submit chat", {
162+
value: question,
163+
});
164+
127165
setLoading(true);
128166
setMessages((messages) => [
129167
...messages,

src/components/overrides/Footer.astro

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ const links = Object.entries({
7070
7171
const homepage = Astro.locals.starlightRoute.entry.id === "";
7272
const splash = Astro.locals.starlightRoute.entry.data.template === "splash";
73+
const showFeedback = Astro.locals.starlightRoute.entry.data.feedback;
7374
7475
let isProduction = false;
7576
@@ -82,7 +83,7 @@ if (
8283
---
8384

8485
{
85-
!homepage && (
86+
!homepage && showFeedback && (
8687
<div class="feedback-prompt">
8788
<FeedbackPrompt client:idle />
8889
</div>

src/components/overrides/Page.astro

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,13 @@ if (props.hasSidebar) {
3939
};
4040
}
4141
42-
props.pagination.prev = prev;
43-
props.pagination.next = next;
42+
if (data.prev !== false) {
43+
props.pagination.prev = prev;
44+
}
45+
46+
if (data.next !== false) {
47+
props.pagination.next = next;
48+
}
4449
}
4550
}
4651

src/components/overrides/TableOfContents.astro

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Icon } from "@astrojs/starlight/components";
55
import FeedbackPrompt from "../FeedbackPrompt.tsx";
66
77
const tags = Astro.locals.starlightRoute.entry.data.tags;
8+
const showFeedback = Astro.locals.starlightRoute.entry.data.feedback;
89
---
910

1011
<Default />
@@ -29,8 +30,14 @@ const tags = Astro.locals.starlightRoute.entry.data.tags;
2930
</>
3031
)
3132
}
32-
<br />
33-
<FeedbackPrompt client:idle />
33+
{
34+
showFeedback && (
35+
<>
36+
<br />
37+
<FeedbackPrompt client:idle />
38+
</>
39+
)
40+
}
3441
{
3542
!Astro.url.pathname.startsWith("/support/") && (
3643
<>

src/content/docs/support/ai.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ sidebar:
55
order: 8
66
label: Docs AI
77
badge: Beta
8+
next: false
9+
prev: false
10+
feedback: false
811
---
912

1013
import DocsAI from "~/components/DocsAI.tsx";

src/schemas/base.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,8 @@ export const baseSchema = ({ image }: SchemaContext) =>
131131
})
132132
.optional(),
133133
icon: SidebarIconSchema(),
134+
feedback: z
135+
.boolean()
136+
.default(true)
137+
.describe("Whether to hide the FeedbackPrompt component on a page"),
134138
});

src/styles/tailwind.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@
144144
}
145145

146146
@layer base {
147+
pre {
148+
white-space: pre-wrap;
149+
}
150+
147151
:root {
148152
--blue-accent-200: rgb(15, 0, 107);
149153
--blue-accent-600: rgb(46, 105, 255);

0 commit comments

Comments
 (0)