Files
monkey-ai-web/src/components/creation/WorkFeed/ApiCodePanel.jsx
T
zhanjunhao cb9d836227 chore(repo): 移除重复版权文件头并纳入共享单测
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
2026-11-20 11:23:49 +08:00

157 lines
4.8 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.
import {
Code2 as ActionCode2,
ChevronDown as ActionChevronDown,
Copy as ActionCopy,
KeyRound as ActionKeyRound,
} from '@/components/common/icons/ActionIcons';
import React, { useContext, useMemo, useState } from 'react';
import { Button } from '@/components/ui';
import { useTranslation } from 'react-i18next';
import { useNavigate } from 'react-router-dom';
import { StatusContext } from '../../../context/Status';
import { copy, showSuccess } from '../../../helpers';
import { trackCreationEvent } from '../../../helpers/creationEvents';
import {
API_CODE_LANGS,
apiBaseUrlOf,
buildApiCall,
renderApiCode,
} from '../../../hooks/creation/creationApiCode';
/** 秘钥管理模块;from=creation_studio 用于记录 key_created_from_studio(WP C-1)。 */
export const KEY_MANAGEMENT_FROM_STUDIO =
'/key-management?from=creation_studio';
const LANG_LABELS = {
curl: 'cURL',
python: 'Python',
javascript: 'JavaScript',
};
const currentOrigin = () => {
try {
return window.location.origin;
} catch (e) {
return '';
}
};
/**
* 获取 API 代码(WP C-1,方案 §4.4):详情抽屉内的折叠面板,由参数快照生成等价的
* /v1/images|video/generations 调用(curl / Python / JavaScript),标注"API 同价",
* "创建密钥"直达秘钥管理。首次展开记 api_code_open。
*/
const ApiCodePanel = ({ task }) => {
const { t } = useTranslation();
const navigate = useNavigate();
const [statusState] = useContext(StatusContext);
const [open, setOpen] = useState(false);
const [lang, setLang] = useState('curl');
const [tracked, setTracked] = useState(false);
const baseUrl = apiBaseUrlOf(statusState?.status, currentOrigin());
const call = useMemo(() => buildApiCall(task, { baseUrl }), [task, baseUrl]);
const code = useMemo(() => renderApiCode(call, lang), [call, lang]);
const panelId = `wf-api-${task?.id || 'x'}`;
const toggle = () => {
const next = !open;
setOpen(next);
if (next && !tracked) {
setTracked(true);
trackCreationEvent('api_code_open', {
kind: task?.kind || '',
model: call.body.model || '',
});
}
};
return (
<section className='wf-api'>
<button
type='button'
className='app-action-button wf-api-toggle'
aria-expanded={open}
aria-controls={panelId}
onClick={toggle}
>
<ActionCode2 size={14} aria-hidden='true' />
<span className='wf-api-title'>{t('获取 API 代码')}</span>
<span className='wf-api-badge'>{t('API 同价')}</span>
<ActionChevronDown
size={14}
aria-hidden='true'
className={`wf-api-caret${open ? ' is-open' : ''}`}
/>
</button>
{open ? (
<div id={panelId} className='wf-api-body'>
<p className='wf-api-desc'>
{t('用同样的参数通过 API 调用,价格与体验中心一致')}
</p>
<div className='wf-api-bar'>
<div
className='wf-api-tabs'
role='tablist'
aria-label={t('代码语言')}
>
{API_CODE_LANGS.map((key) => (
<button
key={key}
type='button'
role='tab'
aria-selected={lang === key}
className={`wf-api-tab${lang === key ? ' is-active' : ''}`}
onClick={() => setLang(key)}
>
{LANG_LABELS[key]}
</button>
))}
</div>
<Button
size='small'
theme='borderless'
icon={<ActionCopy size={12} aria-hidden='true' />}
onClick={async () => {
if (await copy(code)) showSuccess(t('已复制'));
}}
>
{t('复制')}
</Button>
</div>
<pre
className='wf-api-code app-scrollbar'
role='tabpanel'
aria-label={LANG_LABELS[lang]}
tabIndex={0}
>
<code>{code}</code>
</pre>
{call.notes.length ? (
<ul className='wf-api-notes'>
{call.notes.map((note) => (
<li key={note}>{t(note, { path: call.path })}</li>
))}
</ul>
) : null}
<div className='wf-api-foot'>
<span className='wf-api-hint'>
{t('把 sk-xxx 替换为你的 API 密钥')}
</span>
<Button
size='small'
theme='solid'
icon={<ActionKeyRound size={12} aria-hidden='true' />}
onClick={() => navigate(KEY_MANAGEMENT_FROM_STUDIO)}
>
{t('创建密钥')}
</Button>
</div>
</div>
) : null}
</section>
);
};
export default ApiCodePanel;