'use client' import type { ApiKey, Environment } from '@dify/contracts/enterprise/types.gen' import type { ReactNode } from 'react' import { Button } from '@langgenius/dify-ui/button' import { useAtomValue } from 'jotai' import { useState } from 'react' import { useTranslation } from 'react-i18next' import { deploymentRouteAppInstanceIdAtom } from '../../../route-state' import { DeploymentEmptyState, DeploymentStateMessage, } from '../../../shared/components/empty-state' import { CopyPill } from '../../../shared/components/endpoint' import { ApiKeyGenerateMenu } from '../api-keys/api-key-generate-menu' import { ApiKeyList } from '../api-keys/api-key-list' import { CreatedApiTokenDialog } from '../api-keys/created-token-dialog' import { DeveloperApiDocsDrawer } from '../docs/docs-drawer' import { developerApiSettingsAtom, developerApiSettingsIsErrorAtom, developerApiSettingsIsLoadingAtom, } from '../state' import { DeveloperApiSkeleton } from './skeleton' type CreatedApiToken = { appInstanceId: string token: string } function ApiKeyListSection({ apiKeys, environments, action, }: { apiKeys: ApiKey[] environments: Environment[] action?: ReactNode }) { const { t } = useTranslation('deployments') const hasAction = Boolean(action) return (
{t(($) => $['access.api.keyList'])}
{hasAction && (
{action}
)}
) } function DeveloperApiEndpoint({ apiUrl }: { apiUrl: string }) { const { t } = useTranslation('deployments') const [apiDocsOpen, setApiDocsOpen] = useState(false) return (
$['access.api.endpoint'])} value={apiUrl} className="min-w-0 flex-1" />
) } export function DeveloperApiSection() { const { t } = useTranslation('deployments') const appInstanceId = useAtomValue(deploymentRouteAppInstanceIdAtom) const [createdApiToken, setCreatedApiToken] = useState() const developerApiSettings = useAtomValue(developerApiSettingsAtom) const isLoading = useAtomValue(developerApiSettingsIsLoadingAtom) const isError = useAtomValue(developerApiSettingsIsErrorAtom) const accessChannels = developerApiSettings?.accessChannels const apiEnabled = accessChannels?.developerApiEnabled ?? false const apiUrl = developerApiSettings?.developerApiUrl.apiUrl const apiKeys: ApiKey[] = developerApiSettings?.apiKeys ?? [] const environments = developerApiSettings?.environments ?? [] const visibleCreatedApiToken = createdApiToken && createdApiToken.appInstanceId === appInstanceId ? createdApiToken.token : undefined const hasSelectableEnvironment = environments.some((environment) => Boolean(environment.id)) if (isLoading) return if (isError || !appInstanceId) return ( {t(($) => $['common.loadFailed'])} ) if (!apiEnabled) { return ( $['access.api.disabled'])} description={t(($) => $['access.api.disabledHint'])} /> ) } return (
{apiUrl && } {hasSelectableEnvironment ? ( setCreatedApiToken({ appInstanceId, token })} > {({ trigger }) => apiKeys.length === 0 ? ( $['access.api.noKeysTitle'])} description={t(($) => $['access.api.noKeys'])} action={trigger} /> ) : ( ) } ) : apiKeys.length === 0 ? ( $['access.api.emptyTitle'])} description={t(($) => $['access.api.empty'])} /> ) : ( )} {visibleCreatedApiToken && ( setCreatedApiToken(undefined)} /> )}
) }