Skip to content

Commit 0961c65

Browse files
committed
Trying to auto-open the Kapa widget when the page loads
1 parent cdd8c4d commit 0961c65

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

apps/webapp/app/hooks/useKapaWidget.tsx

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import { useShortcuts } from "../components/primitives/ShortcutsProvider";
2-
import { useFeatures } from "~/hooks/useFeatures";
1+
import { useMatches, useSearchParams } from "@remix-run/react";
32
import { useCallback, useEffect, useState } from "react";
4-
import { useMatches } from "@remix-run/react";
5-
import { useTypedMatchesData } from "./useTypedMatchData";
3+
import { useFeatures } from "~/hooks/useFeatures";
64
import { type loader } from "~/root";
5+
import { useShortcuts } from "../components/primitives/ShortcutsProvider";
6+
import { useTypedMatchesData } from "./useTypedMatchData";
77

88
type OpenOptions = { mode: string; query: string; submit: boolean };
99

1010
declare global {
1111
interface Window {
1212
Kapa: (
1313
command: string,
14-
options?: (() => void) | OpenOptions,
14+
options?: (() => void) | { onRender?: () => void } | OpenOptions,
1515
remove?: string | { onRender?: () => void }
1616
) => void;
1717
}
@@ -77,6 +77,7 @@ export function useKapaWidget() {
7777
const features = useFeatures();
7878
const { disableShortcuts, enableShortcuts, areShortcutsEnabled } = useShortcuts();
7979
const [isKapaOpen, setIsKapaOpen] = useState(false);
80+
const [searchParams, setSearchParams] = useSearchParams();
8081

8182
const handleModalClose = useCallback(() => {
8283
setIsKapaOpen(false);
@@ -88,19 +89,35 @@ export function useKapaWidget() {
8889
disableShortcuts();
8990
}, [disableShortcuts]);
9091

92+
// Handle opening/closing
9193
useEffect(() => {
9294
if (!features.isManagedCloud || !kapa?.websiteId) return;
9395

94-
window.Kapa("render");
96+
window.Kapa("render", {
97+
onRender: () => {
98+
const aiHelp = searchParams.get("aiHelp");
99+
if (aiHelp) {
100+
setSearchParams((prev) => {
101+
prev.delete("aiHelp");
102+
return prev;
103+
});
104+
105+
//we need to decode the aiHelp string because it's urlencoded
106+
const decodedAiHelp = decodeURIComponent(aiHelp);
107+
openKapa(decodedAiHelp);
108+
}
109+
},
110+
});
95111
window.Kapa("onModalOpen", handleModalOpen);
96112
window.Kapa("onModalClose", handleModalClose);
97113

98114
return () => {
99115
window.Kapa("onModalOpen", handleModalOpen, "remove");
100116
window.Kapa("onModalClose", handleModalClose, "remove");
101117
};
102-
}, [features.isManagedCloud, kapa?.websiteId]);
118+
}, [features.isManagedCloud, kapa?.websiteId, searchParams, setSearchParams]);
103119

120+
// Handle opening the Kapa widget
104121
const openKapa = useCallback(
105122
(query?: string) => {
106123
if (!features.isManagedCloud || !kapa?.websiteId) return;

0 commit comments

Comments
 (0)