'use client'
import type { ApiKey, Environment } from '@dify/contracts/enterprise/types.gen'
import {
AlertDialog,
AlertDialogActions,
AlertDialogCancelButton,
AlertDialogConfirmButton,
AlertDialogContent,
AlertDialogDescription,
AlertDialogTitle,
} from '@langgenius/dify-ui/alert-dialog'
import { cn } from '@langgenius/dify-ui/cn'
import { toast } from '@langgenius/dify-ui/toast'
import { useMutation } from '@tanstack/react-query'
import { useState } from 'react'
import { useTranslation } from 'react-i18next'
import { consoleQuery } from '@/service/client'
import {
DetailTable,
DetailTableBody,
DetailTableCard,
DetailTableCardList,
DetailTableCell,
DetailTableHead,
DetailTableHeader,
DetailTableRow,
} from '../../../shared/components/detail-table'
import { API_KEY_DETAIL_TABLE_COLUMN_CLASS_NAMES } from '../table-styles'
function ApiKeyName({ apiKey }: { apiKey: ApiKey }) {
return {apiKey.displayName}
}
function EnvironmentBadge({ environment }: { environment?: Environment }) {
return (
{environment?.displayName ?? '—'}
)
}
function ApiKeyValue({ value }: { value: string }) {
return (
)
}
function RevokeApiKeyButton({ apiKey }: { apiKey: ApiKey }) {
const { t } = useTranslation('deployments')
const [showRevokeConfirm, setShowRevokeConfirm] = useState(false)
const revokeApiKey = useMutation(
consoleQuery.enterprise.accessService.deleteApiKey.mutationOptions(),
)
const isRevoking = revokeApiKey.isPending
const apiKeyName = apiKey.displayName
function handleRevoke() {
if (isRevoking) return
revokeApiKey.mutate(
{
params: {
appInstanceId: apiKey.appInstanceId,
environmentId: apiKey.environmentId,
apiKeyId: apiKey.id,
},
},
{
onSuccess: () => {
setShowRevokeConfirm(false)
toast.success(t(($) => $['access.api.revokeSuccess']))
},
onError: () => {
toast.error(t(($) => $['access.api.revokeFailed']))
},
},
)
}
function handleRevokeConfirmOpenChange(open: boolean) {
if (isRevoking) return
setShowRevokeConfirm(open)
}
return (
<>
{t(($) => $['access.api.revokeConfirmTitle'])}
{t(($) => $['access.api.revokeConfirmDescription'], { name: apiKeyName })}
{t(($) => $['operation.cancel'], { ns: 'common' })}
{t(($) => $['access.revoke'])}
>
)
}
function ApiKeyMobileRow({ apiKey, environment }: { apiKey: ApiKey; environment?: Environment }) {
const { t } = useTranslation('deployments')
const displayValue = apiKey.maskedToken
return (
{t(($) => $['access.api.table.key'])}
)
}
function ApiKeyDesktopRow({ apiKey, environment }: { apiKey: ApiKey; environment?: Environment }) {
const displayValue = apiKey.maskedToken
return (
)
}
function ApiKeyTableHeader() {
const { t } = useTranslation('deployments')
return (
{t(($) => $['access.api.table.name'])}
{t(($) => $['access.api.table.environment'])}
{t(($) => $['access.api.table.key'])}
{t(($) => $['access.api.table.action'])}
)
}
export function ApiKeyList({
apiKeys,
environments,
}: {
apiKeys: ApiKey[]
environments: Environment[]
}) {
const environmentById = new Map(environments.map((environment) => [environment.id, environment]))
return (
<>
{apiKeys.map((apiKey) => (
))}
{apiKeys.map((apiKey) => (
))}
>
)
}