mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-16 15:45:13 +00:00
Add inline set-claim action support
This commit is contained in:
@@ -14,6 +14,10 @@ const adaptiveVisitorReaderCache = new Map<
|
||||
ReturnType<typeof createResourceReader<AdaptiveVisitorClaims | null>>
|
||||
>();
|
||||
|
||||
export function clearAdaptiveVisitorClaimsCache() {
|
||||
adaptiveVisitorReaderCache.clear();
|
||||
}
|
||||
|
||||
function createResourceReader<T>(promise: Promise<T>) {
|
||||
let result: T | null | undefined;
|
||||
|
||||
|
||||
@@ -1,13 +1,23 @@
|
||||
'use client';
|
||||
import { clearAdaptiveVisitorClaimsCache } from '@/components/Adaptive/AdaptiveVisitorContextProvider';
|
||||
import { tString, useLanguage } from '@/intl/client';
|
||||
import { useAI, useAIChatController, useAIChatState } from '../AI';
|
||||
import { useSetSearchState } from '../Search';
|
||||
import { Button, type ButtonProps, Input } from '../primitives';
|
||||
|
||||
type ClaimInput = {
|
||||
key: string;
|
||||
value: string | number | boolean | null;
|
||||
};
|
||||
|
||||
export function InlineActionButton(
|
||||
props: { action: 'ask' | 'search'; query?: string } & { buttonProps: ButtonProps } // TODO: Type this properly: Pick<api.DocumentInlineButton, 'action' | 'query'> & { buttonProps: ButtonProps }
|
||||
props: {
|
||||
action: 'ask' | 'search' | 'set-claim';
|
||||
query?: string;
|
||||
claims?: ClaimInput[];
|
||||
} & { buttonProps: ButtonProps } // TODO: Type this properly: Pick<api.DocumentInlineButton, 'action' | 'query'> & { buttonProps: ButtonProps }
|
||||
) {
|
||||
const { action, query, buttonProps } = props;
|
||||
const { action, query, claims, buttonProps } = props;
|
||||
|
||||
const { assistants } = useAI();
|
||||
const chatController = useAIChatController();
|
||||
@@ -32,11 +42,33 @@ export function InlineActionButton(
|
||||
}
|
||||
};
|
||||
|
||||
const handleSetClaimAction = () => {
|
||||
if (!claims?.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
const mergedClaims: Record<string, string | number | boolean | null> = {};
|
||||
for (const claim of claims) {
|
||||
mergedClaims[claim.key] = claim.value;
|
||||
}
|
||||
|
||||
document.cookie = `gitbook-visitor-public=${encodeURIComponent(
|
||||
JSON.stringify(mergedClaims)
|
||||
)}; path=/; SameSite=Lax`;
|
||||
|
||||
clearAdaptiveVisitorClaimsCache();
|
||||
window.location.reload();
|
||||
};
|
||||
|
||||
const icon =
|
||||
action === 'ask' && buttonProps.icon === 'gitbook-assistant' && assistants.length > 0
|
||||
? assistants[0]?.icon
|
||||
: buttonProps.icon;
|
||||
|
||||
if (action === 'set-claim') {
|
||||
return <Button {...buttonProps} onClick={handleSetClaimAction} />;
|
||||
}
|
||||
|
||||
if (!query) {
|
||||
return (
|
||||
<Input
|
||||
|
||||
@@ -17,11 +17,15 @@ export function InlineButton(props: InlineProps<api.DocumentInlineButton>) {
|
||||
};
|
||||
|
||||
const ButtonImplementation = () => {
|
||||
if ('action' in inline.data && 'query' in inline.data.action) {
|
||||
if ('action' in inline.data) {
|
||||
const actionData = inline.data.action;
|
||||
|
||||
return (
|
||||
<InlineActionButton
|
||||
action={inline.data.action.action}
|
||||
query={inline.data.action.query ?? ''}
|
||||
action={actionData.action}
|
||||
query={actionData.query ?? ''}
|
||||
// @ts-expect-error -- Temporary override until `set-claim` is available in @gitbook/api.
|
||||
claims={actionData.claims}
|
||||
buttonProps={buttonProps}
|
||||
/>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user