Type ask_view trigger from @gitbook/api and drop cast helper

This commit is contained in:
Peter White
2026-10-06 15:01:34 +02:00
parent 0b1c1f4fc3
commit 1387d09ed4
3 changed files with 13 additions and 20 deletions
@@ -11,9 +11,10 @@ import {
type AIStreamResponseToolCallPending,
type AIToolCallResult,
SiteInsightsDisplayContext,
type SiteInsightsEventAskView,
} from '@gitbook/api';
import { getInsightsSession, trackAskView, useTrackEvent } from '../Insights';
import { getInsightsSession, useTrackEvent } from '../Insights';
import { type UpdateSearchState, useSetSearchState } from '../Search';
import { addRecentSearchQuery } from '../Search/recent-queries';
import type { AnyAIControl } from './controls';
@@ -60,7 +61,7 @@ export type AIChatStatus =
| 'error'
| 'confirm';
export type AskAITrigger = 'hover' | 'selection' | 'site' | 'page' | 'embed';
export type AskAITrigger = NonNullable<SiteInsightsEventAskView['trigger']>;
export type AIChatState = {
/**
@@ -308,7 +309,7 @@ export function AIChatProvider(props: {
(trigger?: AskAITrigger) => {
setIntercomLauncherHidden(true);
if (!renderMessageOptions?.asEmbeddable) {
trackAskView(trackEvent, trigger);
trackEvent({ type: 'ask_view', trigger });
}
const { initialQuery } = globalState.getState();
@@ -4,7 +4,7 @@ import React from 'react';
import * as api from '@gitbook/api';
import { trackAskView, useTrackEvent } from '../Insights';
import { useTrackEvent } from '../Insights';
import { LinkContext } from '../primitives';
import { useIsVisible } from '../VisibilityContext';
import {
@@ -60,10 +60,13 @@ export function EmbeddableAIChat(props: EmbeddableAIChatProps) {
return;
}
trackAskView(trackEvent, 'embed', {
pageId: null,
displayContext: api.SiteInsightsDisplayContext.Embed,
});
trackEvent(
{ type: 'ask_view', trigger: 'embed' },
{
pageId: null,
displayContext: api.SiteInsightsDisplayContext.Embed,
}
);
}, [trackEvent, isVisible]);
const tabsRef = React.useRef<HTMLDivElement>(null);
@@ -7,7 +7,6 @@ import type * as api from '@gitbook/api';
import { SiteInsightsDisplayContext } from '@gitbook/api';
import { OpenAPIOperationContextProvider } from '@gitbook/react-openapi';
import type { AskAITrigger } from '../AI';
import { type CurrentContentContext, useCurrentContent } from '../hooks';
import { getSession } from './sessions';
import { type VisitorResponse, useVisitor } from './visitorId';
@@ -44,7 +43,7 @@ export type TrackEventInput<EventName extends InsightsEventName = InsightsEventN
/**
* Callback to track an event.
*/
export type TrackEventCallback = <EventName extends InsightsEventName>(
type TrackEventCallback = <EventName extends InsightsEventName>(
event: TrackEventInput<EventName>,
ctx?: InsightsEventPageContext,
options?: InsightsEventOptions
@@ -224,16 +223,6 @@ export function useTrackEvent(): TrackEventCallback {
return React.useContext(InsightsContext);
}
/** Track an ask view while the frontend uses an API type that predates its trigger field. */
export function trackAskView(
trackEvent: TrackEventCallback,
trigger: AskAITrigger | undefined,
ctx?: InsightsEventPageContext
) {
// TODO: remove this cast once @gitbook/api includes SiteInsightsEventAskView.trigger.
trackEvent({ type: 'ask_view', trigger } as unknown as TrackEventInput<'ask_view'>, ctx);
}
/**
* The referrer when it's another page of this origin, so a full page load (e.g. an absolute link
* to a missing page) still records the page it came from.