Files
monkey-ai-web/src/pages/Tutorial/index.jsx
T
zhanjunhao ac9bbfd472 fix(i18n): 补齐九语翻译并修复国际化检查
补齐历史缺译并统一整句插值。九语覆盖 100%,静态检查及 713 项单测通过。
2026-11-17 14:10:25 +08:00

870 lines
28 KiB
React
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
Copyright (C) 2025 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import { Trans } from 'react-i18next';
import { I18N_TECHNICAL_TEXT } from '../../i18n/technicalText.js';
import { writeClipboardText } from '../../helpers/clipboard';
import React, { useState, useContext, useRef } from 'react';
import { useTranslation } from 'react-i18next';
import { Link, useNavigate, useSearchParams } from 'react-router-dom';
import {
Typography,
Button,
Card,
Tag,
Toast,
Tabs,
TabPane,
} from '@/components/ui';
import {
IconCopy,
IconTick,
IconArrowRight,
IconKey,
IconCreditCard,
IconCode,
IconSend,
IconSearch,
IconClose,
IconVideo,
IconArticle,
} from '@/components/ui/icons';
import { StatusContext } from '../../context/Status';
import { DEFAULT_LOGIN_REDIRECT } from '../../helpers/data';
import { isMiniProgramWebView } from '../../helpers/miniProgram';
import VideoTutorial from './components/VideoTutorial';
import './styles.css';
const { Title, Paragraph, Text } = Typography;
const Tutorial = () => {
const { t } = useTranslation();
const navigate = useNavigate();
const [searchParams] = useSearchParams();
const [copiedIndex, setCopiedIndex] = useState(null);
const [statusState] = useContext(StatusContext);
const [activeOS, setActiveOS] = useState('windows');
const [activeTutorialTab, setActiveTutorialTab] = useState('video'); // 默认视频教程
const scrollContainerRef = useRef(null);
// 处理关闭教程
const handleClose = () => {
// 检查是否有来源参数,如果有则返回来源页面,否则返回默认页面
const fromPage = searchParams.get('from');
if (fromPage) {
navigate(`/${fromPage}`);
} else {
navigate(DEFAULT_LOGIN_REDIRECT);
}
};
// 获取后台配置的文档地址
const docsLink =
statusState?.status?.docs_link || 'https://docs.weytoken.com/';
// API 服务器地址(生产环境)
const serverAddress = 'https://api.weytoken.com';
const steps = [
{
key: 'create-key',
title: t('创建密钥'),
icon: <IconKey />,
color: 'var(--brand-gradient-via)',
},
{
key: 'view-models',
title: t('查看可用模型'),
icon: <IconSearch />,
color: 'var(--brand-gradient-to)',
},
{
key: 'recharge',
title: t('充值'),
icon: <IconCreditCard />,
color: 'var(--brand-primary)',
},
{
key: 'install-client',
title: t('安装客户端'),
icon: <IconCode />,
color: 'var(--brand-gradient-from)',
},
{
key: 'start-journey',
title: t('开启之旅'),
icon: <IconSend />,
color: 'var(--brand-primary-hover)',
},
];
// 复制代码
const copyCode = async (code, index) => {
try {
await writeClipboardText(code);
setCopiedIndex(index);
Toast.success(t('已复制到剪贴板'));
setTimeout(() => setCopiedIndex(null), 2000);
} catch (err) {
Toast.error(t('复制失败'));
}
};
// 代码块渲染函数:返回 JSX 而非作为内联组件,避免 Tutorial 重渲染时被识别为新的组件类型导致子树重新挂载
const renderCodeBlock = (code, language, index) => (
<div className='tutorial-code-block'>
<div className='tutorial-code-header'>
<Tag size='small' color='cyan'>
{language}
</Tag>
<Button
size='small'
theme='borderless'
icon={copiedIndex === index ? <IconTick /> : <IconCopy />}
onClick={() => copyCode(code, index)}
className='tutorial-copy-btn'
>
{copiedIndex === index ? t('已复制') : t('复制')}
</Button>
</div>
<pre className='tutorial-code-content'>
<code>{code}</code>
</pre>
</div>
);
// 步骤1:创建密钥 - 使用 JSX 变量而非内联组件,避免 Tutorial 重渲染时被识别为新组件类型导致子树重新挂载(连带 scroll 容器重建、滚动位置丢失)
const createKeyStep = (
<div className='tutorial-step-content'>
<div className='tutorial-step-header'>
<div
className='tutorial-step-icon'
style={{
background:
'linear-gradient(135deg, var(--brand-gradient-via), var(--brand-gradient-to))',
}}
>
<IconKey size='large' />
</div>
<div>
<Title heading={3}>{t('第一步:创建 API 密钥')}</Title>
<Paragraph type='secondary'>
{t('API 密钥是调用 AI 服务的凭证,请妥善保管')}
</Paragraph>
</div>
</div>
<Card className='tutorial-card tutorial-card-animated'>
<div className='tutorial-steps-list'>
<div className='tutorial-step-item'>
<span className='tutorial-step-number'>1</span>
<span>
{t('点击顶部导航栏的')}
<Link to='/user-center/key-management' className='tutorial-link'>
「{t('密钥管理')}」
</Link>
</span>
</div>
<div className='tutorial-step-item'>
<span className='tutorial-step-number'>2</span>
<span>{t('点击「创建密钥」按钮')}</span>
</div>
<div className='tutorial-step-item'>
<span className='tutorial-step-number'>3</span>
<span>{t('填写密钥名称(如:my-first-key)')}</span>
</div>
<div className='tutorial-step-item'>
<span className='tutorial-step-number'>4</span>
<span>{t('选择密钥分组')}</span>
</div>
<div className='tutorial-step-item'>
<span className='tutorial-step-number'>5</span>
<span>{t('点击「提交」完成创建')}</span>
</div>
</div>
</Card>
<Card className='tutorial-card tutorial-warning-card'>
<div className='tutorial-warning-content'>
<div className='tutorial-warning-icon'>
{I18N_TECHNICAL_TEXT.WARNING_ICON}
</div>
<div>
<Text strong>{t('重要提示')}</Text>
<Paragraph
type='secondary'
style={{ marginBottom: 0, marginTop: 4 }}
>
{t('密钥只会显示一次,请立即复制保存!格式如:')}
</Paragraph>
<code className='tutorial-key-example'>
sk-xxxxxxxxxxxxxxxxxxxxxxxx
</code>
</div>
</div>
</Card>
<div className='tutorial-action-buttons'>
<Link to='/user-center/key-management'>
<Button
type='primary'
theme='solid'
size='large'
icon={<IconKey />}
className='tutorial-action-btn'
>
{t('前往创建密钥')}
</Button>
</Link>
</div>
</div>
);
// 步骤2:查看可用模型
const viewModelsStep = (
<div className='tutorial-step-content'>
<div className='tutorial-step-header'>
<div
className='tutorial-step-icon'
style={{
background:
'linear-gradient(135deg, var(--brand-gradient-to), var(--brand-primary-hover))',
}}
>
<IconSearch size='large' />
</div>
<div>
<Title heading={3}>{t('第二步:查看密钥可用模型')}</Title>
<Paragraph type='secondary'>
{t('创建密钥后,可以查看该密钥有权限使用的模型')}
</Paragraph>
</div>
</div>
<Card className='tutorial-card tutorial-card-animated'>
<div className='tutorial-steps-list'>
<div className='tutorial-step-item'>
<span className='tutorial-step-number'>1</span>
<span>
<Trans
t={t}
i18nKey='在<part0>「密钥管理」</part0>页面找到刚创建的密钥'
components={{
part0: (
<Link
to='/user-center/key-management'
className='tutorial-link'
/>
),
}}
/>
</span>
</div>
<div className='tutorial-step-item'>
<span className='tutorial-step-number'>2</span>
<span>{t('点击密钥卡片右侧的「查看可用模型」按钮')}</span>
</div>
<div className='tutorial-step-item'>
<span className='tutorial-step-number'>3</span>
<span>{t('在弹出窗口中查看该密钥可以调用的所有模型')}</span>
</div>
</div>
</Card>
<Card className='tutorial-card tutorial-tip-card'>
<div className='tutorial-tip-content'>
<div className='tutorial-tip-icon'>
{I18N_TECHNICAL_TEXT.TIP_ICON}
</div>
<div>
<Text strong>{t('小提示')}</Text>
<Paragraph
type='secondary'
style={{ marginBottom: 0, marginTop: 4 }}
>
{t('常用模型推荐:')}
</Paragraph>
<div className='tutorial-model-tags'>
<Tag color='violet' size='large'>
claude-3-5-sonnet
</Tag>
<Tag color='blue' size='large'>
gpt-4o
</Tag>
<Tag color='cyan' size='large'>
gemini-pro
</Tag>
</div>
</div>
</div>
</Card>
</div>
);
// 步骤3:充值
const rechargeStep = (
<div className='tutorial-step-content'>
<div className='tutorial-step-header'>
<div
className='tutorial-step-icon'
style={{
background:
'linear-gradient(135deg, var(--brand-primary), var(--brand-gradient-via))',
}}
>
<IconCreditCard size='large' />
</div>
<div>
<Title heading={3}>{t('第三步:账户充值')}</Title>
<Paragraph type='secondary'>
{t('充值后即可开始使用 AI 服务')}
</Paragraph>
</div>
</div>
<Card className='tutorial-card tutorial-card-animated'>
<div className='tutorial-steps-list'>
<div className='tutorial-step-item'>
<span className='tutorial-step-number'>1</span>
<span>
<Trans
t={t}
i18nKey='点击页面右上角的<part0>「充值」</part0>按钮'
components={{
part0: (
<Link
to='/recharge'
className='tutorial-link tutorial-link-orange'
/>
),
}}
/>
</span>
</div>
<div className='tutorial-step-item'>
<span className='tutorial-step-number'>2</span>
<span>{t('选择充值金额')}</span>
</div>
<div className='tutorial-step-item'>
<span className='tutorial-step-number'>3</span>
<span>{t('完成支付,余额实时到账')}</span>
</div>
</div>
</Card>
<Card className='tutorial-card tutorial-info-card'>
<div className='tutorial-info-content'>
<div className='tutorial-info-icon'>
{I18N_TECHNICAL_TEXT.MONEY_ICON}
</div>
<div>
<Text strong>{t('计费说明')}</Text>
<Paragraph
type='secondary'
style={{ marginBottom: 0, marginTop: 4 }}
>
{t(
'平台按 Token 消耗计费,不同模型价格不同。可在「模型服务」页面查看详细价格。',
)}
</Paragraph>
</div>
</div>
</Card>
<div className='tutorial-action-buttons'>
<Link to='/recharge'>
<Button
type='warning'
theme='solid'
size='large'
icon={<IconCreditCard />}
className='tutorial-action-btn tutorial-action-btn-orange'
>
{t('前往充值')}
</Button>
</Link>
</div>
</div>
);
// 步骤4:安装客户端
const installClientStep = (
<div className='tutorial-step-content'>
<div className='tutorial-step-header'>
<div
className='tutorial-step-icon'
style={{
background:
'linear-gradient(135deg, var(--brand-gradient-from), var(--brand-gradient-via))',
}}
>
<IconCode size='large' />
</div>
<div>
<Title heading={3}>{t('第四步:安装 Claude Code 客户端')}</Title>
<Paragraph type='secondary'>
{t('Claude Code 是 Anthropic 官方的命令行 AI 编程助手')}
</Paragraph>
</div>
</div>
<Card className='tutorial-card tutorial-card-animated'>
<Title heading={5} style={{ marginBottom: 16 }}>
{t('安装 Claude Code')}
</Title>
{renderCodeBlock(
'npm install -g @anthropic-ai/claude-code',
'bash',
'install-claude',
)}
<Paragraph type='tertiary' style={{ marginTop: 8 }}>
{t('或使用 npx 直接运行:')}
<code>npx @anthropic-ai/claude-code</code>
</Paragraph>
</Card>
<Card className='tutorial-card tutorial-card-animated'>
<Title heading={5} style={{ marginBottom: 16 }}>
{t('配置环境变量')}
</Title>
<Tabs
type='button'
activeKey={activeOS}
onChange={setActiveOS}
className='tutorial-os-tabs'
>
<TabPane
tab={
<span className='tutorial-tab-label'>
{I18N_TECHNICAL_TEXT.WINDOWS_PLATFORM}
</span>
}
itemKey='windows'
>
<div className='tutorial-tab-content'>
<Paragraph type='secondary'>
{t('在 PowerShell 中执行(永久生效):')}
</Paragraph>
{renderCodeBlock(
`# 永久设置用户环境变量
[System.Environment]::SetEnvironmentVariable('ANTHROPIC_BASE_URL', '${serverAddress}', 'User')
[System.Environment]::SetEnvironmentVariable('ANTHROPIC_AUTH_TOKEN', 'sk-你的密钥', 'User')
# 设置后请重新打开终端窗口`,
'powershell',
'env-windows',
)}
<Paragraph type='tertiary' style={{ marginTop: 12 }}>
{t('环境变量将永久保存,重新打开终端后生效')}
</Paragraph>
</div>
</TabPane>
<TabPane
tab={
<span className='tutorial-tab-label'>
{I18N_TECHNICAL_TEXT.MACOS_PLATFORM}
</span>
}
itemKey='macos'
>
<div className='tutorial-tab-content'>
<Paragraph type='secondary'>
{t('在终端中执行(永久生效):')}
</Paragraph>
{renderCodeBlock(
`# 添加到 ~/.zshrc(永久生效)
echo 'export ANTHROPIC_BASE_URL="${serverAddress}"' >> ~/.zshrc
echo 'export ANTHROPIC_AUTH_TOKEN="sk-你的密钥"' >> ~/.zshrc
source ~/.zshrc`,
'bash',
'env-macos',
)}
<Paragraph type='tertiary' style={{ marginTop: 12 }}>
{t('如使用 bash 请将 ~/.zshrc 改为 ~/.bash_profile')}
</Paragraph>
</div>
</TabPane>
<TabPane
tab={
<span className='tutorial-tab-label'>
{I18N_TECHNICAL_TEXT.LINUX_PLATFORM}
</span>
}
itemKey='linux'
>
<div className='tutorial-tab-content'>
<Paragraph type='secondary'>
{t('在终端中执行(永久生效):')}
</Paragraph>
{renderCodeBlock(
`# 添加到 ~/.bashrc(永久生效)
echo 'export ANTHROPIC_BASE_URL="${serverAddress}"' >> ~/.bashrc
echo 'export ANTHROPIC_AUTH_TOKEN="sk-你的密钥"' >> ~/.bashrc
source ~/.bashrc`,
'bash',
'env-linux',
)}
<Paragraph type='tertiary' style={{ marginTop: 12 }}>
{t('如使用 zsh 请将 ~/.bashrc 改为 ~/.zshrc')}
</Paragraph>
</div>
</TabPane>
</Tabs>
</Card>
<Card className='tutorial-card tutorial-card-animated'>
<Title heading={5} style={{ marginBottom: 16 }}>
{t('启动使用')}
</Title>
{renderCodeBlock('claude', 'bash', 'start-claude')}
<div className='tutorial-commands-grid'>
<div className='tutorial-command-item'>
<code>/help</code>
<span>{t('查看帮助')}</span>
</div>
<div className='tutorial-command-item'>
<code>/config</code>
<span>{t('配置设置')}</span>
</div>
<div className='tutorial-command-item'>
<code>/clear</code>
<span>{t('清空对话')}</span>
</div>
<div className='tutorial-command-item'>
<code>/exit</code>
<span>{t('退出程序')}</span>
</div>
</div>
</Card>
</div>
);
// 步骤5:开启之旅
const startJourneyStep = (
<div className='tutorial-step-content'>
<div className='tutorial-step-header'>
<div
className='tutorial-step-icon'
style={{
background:
'linear-gradient(135deg, var(--brand-primary-hover), var(--brand-primary))',
}}
>
<IconSend size='large' />
</div>
<div>
<Title heading={3}>{t('第五步:开启 AI 之旅')}</Title>
<Paragraph type='secondary'>
{t('恭喜!您已完成所有准备工作')}
</Paragraph>
</div>
</div>
<div className='tutorial-completion-card'>
<div className='tutorial-completion-icon'>
{I18N_TECHNICAL_TEXT.CELEBRATION_ICON}
</div>
<Title heading={4}>{t('准备就绪!')}</Title>
<Paragraph>
{t('您已经完成了所有配置,现在可以开始使用 AI 服务了')}
</Paragraph>
<div className='tutorial-completion-actions'>
<Button
type='primary'
theme='solid'
size='large'
icon={<IconTick />}
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
handleClose();
}}
className='tutorial-completion-btn'
>
{t('完成教程')}
</Button>
</div>
</div>
<div className='tutorial-next-steps'>
<Title heading={5} style={{ marginBottom: 20 }}>
{t('后续探索')}
</Title>
<div className='tutorial-explore-grid'>
<Link
to='/market'
className='tutorial-explore-card tutorial-explore-card-purple'
>
<div className='tutorial-explore-icon'>
{I18N_TECHNICAL_TEXT.DIAMOND_ICON}
</div>
<div className='tutorial-explore-content'>
<Text strong>{t('模型超市')}</Text>
<Paragraph type='secondary' style={{ marginBottom: 0 }}>
{t('查看所有可用模型及价格')}
</Paragraph>
</div>
<IconArrowRight className='tutorial-explore-arrow' />
</Link>
<a
href={docsLink}
target='_blank'
rel='noopener noreferrer'
className='tutorial-explore-card tutorial-explore-card-blue'
>
<div className='tutorial-explore-icon'>
{I18N_TECHNICAL_TEXT.BOOKS_ICON}
</div>
<div className='tutorial-explore-content'>
<Text strong>{t('使用文档')}</Text>
<Paragraph type='secondary' style={{ marginBottom: 0 }}>
{t('查看更多使用指南和 API 文档')}
</Paragraph>
</div>
<IconArrowRight className='tutorial-explore-arrow' />
</a>
<Link
to='/experience-center'
className='tutorial-explore-card tutorial-explore-card-green'
>
<div className='tutorial-explore-icon'>
{I18N_TECHNICAL_TEXT.GAME_ICON}
</div>
<div className='tutorial-explore-content'>
<Text strong>{t('体验中心')}</Text>
<Paragraph type='secondary' style={{ marginBottom: 0 }}>
{t('在线体验 AI 对话功能')}
</Paragraph>
</div>
<IconArrowRight className='tutorial-explore-arrow' />
</Link>
<Link
to='/user-center'
className='tutorial-explore-card tutorial-explore-card-orange'
>
<div className='tutorial-explore-icon'>
{I18N_TECHNICAL_TEXT.CHART_ICON}
</div>
<div className='tutorial-explore-content'>
<Text strong>{t('用户中心')}</Text>
<Paragraph type='secondary' style={{ marginBottom: 0 }}>
{t('查看使用统计和调用记录')}
</Paragraph>
</div>
<IconArrowRight className='tutorial-explore-arrow' />
</Link>
</div>
</div>
</div>
);
// 所有步骤数组(直接引用 JSX 变量,保持元素类型稳定,避免重新挂载)
const allStepComponents = [
{
component: createKeyStep,
title: t('创建密钥'),
color: 'var(--brand-gradient-via)',
},
{
component: viewModelsStep,
title: t('查看可用模型'),
color: 'var(--brand-gradient-to)',
},
{
component: rechargeStep,
title: t('充值'),
color: 'var(--brand-primary)',
},
{
component: installClientStep,
title: t('安装客户端'),
color: 'var(--brand-gradient-from)',
},
{
component: startJourneyStep,
title: t('开启之旅'),
color: 'var(--brand-primary-hover)',
},
];
// WeyAi 小程序里不出现充值步骤(helpers/miniProgram.js)。其余步骤保留原编号,
// 与标题里的「第四步」等一致。
const inMiniProgram = isMiniProgramWebView();
const visibleSteps = steps
.map((step, index) => ({ ...step, number: index + 1 }))
.filter((step) => !(inMiniProgram && step.key === 'recharge'));
const visibleStepComponents = allStepComponents
.map((step, index) => ({ ...step, number: index + 1 }))
.filter((step) => !(inMiniProgram && step.component === rechargeStep));
// 图文教程内容 - 滚动浏览模式(使用 JSX 变量而非内联组件,避免状态更新时重新挂载导致滚动位置重置)
const stepsTutorial = (
<div className='tutorial-scroll-wrapper'>
{/* 顶部快速导航 */}
<div className='tutorial-quick-nav'>
<div className='tutorial-quick-nav-inner'>
{visibleSteps.map((step, index) => (
<a
key={step.key}
href={`#tutorial-step-${index}`}
className='tutorial-quick-nav-item'
style={{ '--step-color': step.color }}
onClick={(e) => {
e.preventDefault();
const element = document.getElementById(
`tutorial-step-${index}`,
);
if (element && scrollContainerRef.current) {
element.scrollIntoView({
behavior: 'smooth',
block: 'start',
});
}
}}
>
<span className='tutorial-quick-nav-number'>{step.number}</span>
<span className='tutorial-quick-nav-title'>{step.title}</span>
</a>
))}
</div>
<div className='tutorial-scroll-hint'>
<span className='tutorial-scroll-hint-icon'>↓</span>
<span>{t('向下滚动查看所有步骤')}</span>
</div>
</div>
{/* 可滚动的内容区域 */}
<div className='tutorial-scroll-container' ref={scrollContainerRef}>
<div className='tutorial-all-steps'>
{visibleStepComponents.map((step, index) => (
<div
key={`step-wrapper-${index}`}
id={`tutorial-step-${index}`}
className='tutorial-step-section'
>
<div className='tutorial-step-divider'>
<div
className='tutorial-step-badge'
style={{
background: `linear-gradient(135deg, ${step.color}, color-mix(in srgb, ${step.color} 85%, transparent))`,
}}
>
{step.number} / {allStepComponents.length}
</div>
</div>
{step.component}
</div>
))}
</div>
{/* 底部完成提示 */}
<div className='tutorial-scroll-end'>
<div className='tutorial-scroll-end-icon'>
{I18N_TECHNICAL_TEXT.CELEBRATION_ICON}
</div>
<Paragraph type='secondary'>
{t('恭喜!您已浏览完所有教程内容')}
</Paragraph>
<Button
type='primary'
theme='solid'
icon={<IconTick />}
onClick={handleClose}
>
{t('完成教程')}
</Button>
</div>
</div>
</div>
);
return (
<div className='tutorial-container'>
{/* 头部 */}
<div className='tutorial-header'>
<div className='tutorial-header-content'>
<div className='tutorial-header-badge'>{t('快速上手')}</div>
<Title heading={1} className='tutorial-title'>
{t('新手教程')}
</Title>
<Paragraph type='secondary' className='tutorial-subtitle'>
{t('5 分钟快速上手,开启您的 AI 之旅')}
</Paragraph>
</div>
<Button
type='tertiary'
theme='borderless'
icon={<IconClose />}
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
handleClose();
}}
className='tutorial-close-btn'
size='large'
>
{t('关闭教程')}
</Button>
</div>
{/* 教程类型切换 Tab */}
<div className='tutorial-type-tabs'>
<Tabs
type='button'
activeKey={activeTutorialTab}
onChange={setActiveTutorialTab}
className='tutorial-main-tabs'
>
<TabPane
tab={
<span className='tutorial-tab-label-main'>
<IconVideo /> {t('视频教程')}
</span>
}
itemKey='video'
>
<div className='tutorial-tab-content-wrapper'>
<VideoTutorial />
</div>
</TabPane>
<TabPane
tab={
<span className='tutorial-tab-label-main'>
<IconArticle /> {t('图文教程')}
</span>
}
itemKey='steps'
>
<div className='tutorial-tab-content-wrapper'>{stepsTutorial}</div>
</TabPane>
</Tabs>
</div>
</div>
);
};
export default Tutorial;