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 (
{open ? (

{t('用同样的参数通过 API 调用,价格与体验中心一致')}

{API_CODE_LANGS.map((key) => ( ))}
            {code}
          
{call.notes.length ? ( ) : null}
{t('把 sk-xxx 替换为你的 API 密钥')}
) : null}
); }; export default ApiCodePanel;