mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-10-12 07:58:49 +00:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4228625852 | |||
| e9c8ec79d2 |
@@ -14,6 +14,10 @@ const adaptiveVisitorReaderCache = new Map<
|
|||||||
ReturnType<typeof createResourceReader<AdaptiveVisitorClaims | null>>
|
ReturnType<typeof createResourceReader<AdaptiveVisitorClaims | null>>
|
||||||
>();
|
>();
|
||||||
|
|
||||||
|
export function clearAdaptiveVisitorClaimsCache() {
|
||||||
|
adaptiveVisitorReaderCache.clear();
|
||||||
|
}
|
||||||
|
|
||||||
function createResourceReader<T>(promise: Promise<T>) {
|
function createResourceReader<T>(promise: Promise<T>) {
|
||||||
let result: T | null | undefined;
|
let result: T | null | undefined;
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,23 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
import { clearAdaptiveVisitorClaimsCache } from '@/components/Adaptive/AdaptiveVisitorContextProvider';
|
||||||
import { tString, useLanguage } from '@/intl/client';
|
import { tString, useLanguage } from '@/intl/client';
|
||||||
import { useAI, useAIChatController, useAIChatState } from '../AI';
|
import { useAI, useAIChatController, useAIChatState } from '../AI';
|
||||||
import { useSetSearchState } from '../Search';
|
import { useSetSearchState } from '../Search';
|
||||||
import { Button, type ButtonProps, Input } from '../primitives';
|
import { Button, type ButtonProps, Input } from '../primitives';
|
||||||
|
|
||||||
|
type ClaimInput = {
|
||||||
|
key: string;
|
||||||
|
value: string | number | boolean | null;
|
||||||
|
};
|
||||||
|
|
||||||
export function InlineActionButton(
|
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 { assistants } = useAI();
|
||||||
const chatController = useAIChatController();
|
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 =
|
const icon =
|
||||||
action === 'ask' && buttonProps.icon === 'gitbook-assistant' && assistants.length > 0
|
action === 'ask' && buttonProps.icon === 'gitbook-assistant' && assistants.length > 0
|
||||||
? assistants[0]?.icon
|
? assistants[0]?.icon
|
||||||
: buttonProps.icon;
|
: buttonProps.icon;
|
||||||
|
|
||||||
|
if (action === 'set-claim') {
|
||||||
|
return <Button {...buttonProps} onClick={handleSetClaimAction} />;
|
||||||
|
}
|
||||||
|
|
||||||
if (!query) {
|
if (!query) {
|
||||||
return (
|
return (
|
||||||
<Input
|
<Input
|
||||||
|
|||||||
@@ -17,11 +17,15 @@ export function InlineButton(props: InlineProps<api.DocumentInlineButton>) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const ButtonImplementation = () => {
|
const ButtonImplementation = () => {
|
||||||
if ('action' in inline.data && 'query' in inline.data.action) {
|
if ('action' in inline.data) {
|
||||||
|
const actionData = inline.data.action;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<InlineActionButton
|
<InlineActionButton
|
||||||
action={inline.data.action.action}
|
action={actionData.action}
|
||||||
query={inline.data.action.query ?? ''}
|
query={actionData.query ?? ''}
|
||||||
|
// @ts-expect-error -- Temporary override until `set-claim` is available in @gitbook/api.
|
||||||
|
claims={actionData.claims}
|
||||||
buttonProps={buttonProps}
|
buttonProps={buttonProps}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import { hasFullWidthBlock, hasMoreThan, hasTopLevelBlock, isNodeEmpty } from '@
|
|||||||
import type { AncestorRevisionPage } from '@/lib/pages';
|
import type { AncestorRevisionPage } from '@/lib/pages';
|
||||||
import { tcls } from '@/lib/tailwind';
|
import { tcls } from '@/lib/tailwind';
|
||||||
import { DocumentView, DocumentViewSkeleton } from '../DocumentView';
|
import { DocumentView, DocumentViewSkeleton } from '../DocumentView';
|
||||||
|
import { InlineActionButton } from '../DocumentView/InlineActionButton';
|
||||||
import { TrackPageViewEvent } from '../Insights';
|
import { TrackPageViewEvent } from '../Insights';
|
||||||
import { PageFeedbackForm } from '../PageFeedback';
|
import { PageFeedbackForm } from '../PageFeedback';
|
||||||
import { CurrentPageProvider } from '../hooks/useCurrentPage';
|
import { CurrentPageProvider } from '../hooks/useCurrentPage';
|
||||||
@@ -81,6 +82,21 @@ export function PageBody(props: {
|
|||||||
pageHasToc ? 'page-has-toc' : 'page-no-toc'
|
pageHasToc ? 'page-has-toc' : 'page-no-toc'
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
|
<div>
|
||||||
|
Special button:
|
||||||
|
<InlineActionButton
|
||||||
|
action="set-claim"
|
||||||
|
claims={[
|
||||||
|
{
|
||||||
|
key: 'format',
|
||||||
|
value: ['docker', 'helm', 'generic', 'maven'][
|
||||||
|
Math.floor(Math.random() * 4)
|
||||||
|
] as string,
|
||||||
|
},
|
||||||
|
]}
|
||||||
|
buttonProps={{ label: 'Ask' }}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
<PreservePageLayout siteWidthWide={siteWidthWide} pageHasToc={pageHasToc} />
|
<PreservePageLayout siteWidthWide={siteWidthWide} pageHasToc={pageHasToc} />
|
||||||
{page.cover && page.layout.cover && page.layout.coverSize === 'hero' ? (
|
{page.cover && page.layout.cover && page.layout.coverSize === 'hero' ? (
|
||||||
<PageCover as="hero" page={page} cover={page.cover} context={context} />
|
<PageCover as="hero" page={page} cover={page.cover} context={context} />
|
||||||
|
|||||||
Reference in New Issue
Block a user