mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-11-01 09:49:53 +00:00
fc06bc694b
Co-authored-by: Claude Haiku 5.5 <noreply@anthropic.com>
59 lines
1.7 KiB
TypeScript
59 lines
1.7 KiB
TypeScript
'use client';
|
|
|
|
import { getCodeTextFromId } from './utils';
|
|
import { useAIChatController, useAIConfig } from '@/components/AI';
|
|
import { AIChatIcon } from '@/components/AIChat';
|
|
import { useTrackEvent } from '@/components/Insights';
|
|
import { Button } from '@/components/primitives';
|
|
import { isAIChatEnabled } from '@/components/utils/isAIChatEnabled';
|
|
import { t, useLanguage } from '@/intl/client';
|
|
import { type ClassValue, tcls } from '@/lib/tailwind';
|
|
|
|
export function AskAICodeButton(props: {
|
|
codeId: string;
|
|
title?: string;
|
|
syntax?: string;
|
|
style: ClassValue;
|
|
}) {
|
|
const { codeId, title, syntax, style } = props;
|
|
|
|
const language = useLanguage();
|
|
const config = useAIConfig();
|
|
const chatController = useAIChatController();
|
|
const trackEvent = useTrackEvent();
|
|
|
|
if (!isAIChatEnabled(config.aiMode)) {
|
|
return null;
|
|
}
|
|
|
|
const onClick = () => {
|
|
trackEvent({ type: 'ask_trigger_click', trigger: 'code' });
|
|
const codeText = getCodeTextFromId(codeId);
|
|
if (!codeText?.trim()) {
|
|
return;
|
|
}
|
|
|
|
chatController.addReference({
|
|
type: 'code-block',
|
|
id: codeId,
|
|
label: title ?? 'Code',
|
|
content: codeText,
|
|
syntax,
|
|
});
|
|
chatController.open('code');
|
|
chatController.focus();
|
|
};
|
|
|
|
return (
|
|
<Button
|
|
size="xsmall"
|
|
variant="secondary"
|
|
icon={<AIChatIcon state="default" trademark={config.trademark} className="" />}
|
|
onClick={onClick}
|
|
className={tcls(style, 'translate-y-0!', 'print:hidden', '[[data-ai-chat]_&]:hidden')}
|
|
>
|
|
{t(language, 'ask')}
|
|
</Button>
|
|
);
|
|
}
|