'use client' import type { AccessChannels, ApiKey, Environment, } from '@dify/contracts/enterprise/types.gen' import type { ReactNode } from 'react' import { Button } from '@langgenius/dify-ui/button' import { Switch, SwitchSkeleton } from '@langgenius/dify-ui/switch' import { useMutation } from '@tanstack/react-query' import { useAtomValue } from 'jotai' import { useState } from 'react' import { useTranslation } from 'react-i18next' import { consoleQuery } from '@/service/client' import { DeploymentEmptyState, DeploymentStateMessage } from '../../../components/empty-state' import { deploymentRouteAppInstanceIdAtom } from '../../../route-state' import { CopyPill } from '../components/endpoint' import { developerApiSettingsQueryAtom } from '../state' import { ApiKeyGenerateMenu } from './api-key-generate-menu' import { ApiKeyList } from './api-key-list' import { CreatedApiTokenDialog } from './created-token-dialog' import { DeveloperApiDocsDrawer } from './docs-drawer' import { DeveloperApiSkeleton } from './skeleton' type CreatedApiToken = { appInstanceId: string token: string } function DeveloperApiSwitch({ checked, accessChannels, disabled }: { checked: boolean accessChannels?: AccessChannels disabled?: boolean }) { const { t } = useTranslation('deployments') const appInstanceId = useAtomValue(deploymentRouteAppInstanceIdAtom) const toggleDeveloperAPI = useMutation(consoleQuery.enterprise.accessService.updateAccessChannels.mutationOptions()) return ( { if (!appInstanceId) return toggleDeveloperAPI.mutate({ params: { appInstanceId }, body: { appInstanceId, webAppEnabled: accessChannels?.webAppEnabled ?? false, developerApiEnabled: enabled, }, }) }} /> ) } export function DeveloperApiHeaderSwitch() { const { t } = useTranslation('deployments') const developerApiSettingsQuery = useAtomValue(developerApiSettingsQueryAtom) const accessChannels = developerApiSettingsQuery.data?.accessChannels const apiEnabled = accessChannels?.developerApiEnabled ?? false if (developerApiSettingsQuery.isLoading) return return (
{apiEnabled ? t('overview.enabled') : t('overview.disabled')}
) } 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 (
) } export function DeveloperApiSection() { const { t } = useTranslation('deployments') const appInstanceId = useAtomValue(deploymentRouteAppInstanceIdAtom) const [createdApiToken, setCreatedApiToken] = useState() const developerApiSettingsQuery = useAtomValue(developerApiSettingsQueryAtom) const accessChannels = developerApiSettingsQuery.data?.accessChannels const apiEnabled = accessChannels?.developerApiEnabled ?? false const apiUrl = developerApiSettingsQuery.data?.developerApiUrl.apiUrl const apiKeys: ApiKey[] = developerApiSettingsQuery.data?.apiKeys ?? [] const environments = developerApiSettingsQuery.data?.environments ?? [] const visibleCreatedApiToken = createdApiToken && createdApiToken.appInstanceId === appInstanceId ? createdApiToken.token : undefined const hasSelectableEnvironment = environments.some(environment => Boolean(environment.id)) if (developerApiSettingsQuery.isLoading) return if (developerApiSettingsQuery.isError || !appInstanceId) return {t('common.loadFailed')} if (!apiEnabled) { return ( ) } return (
{apiUrl && ( )} {hasSelectableEnvironment ? ( setCreatedApiToken({ appInstanceId, token })} > {({ trigger }) => apiKeys.length === 0 ? ( ) : ( )} ) : apiKeys.length === 0 ? ( ) : ( )} {visibleCreatedApiToken && ( setCreatedApiToken(undefined)} /> )}
) }