mirror of
https://github.com/langgenius/dify.git
synced 2026-06-07 16:32:01 +08:00
Co-authored-by: Yang <yang@Yangs-MacBook-Pro.local> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
71 lines
1.9 KiB
TypeScript
71 lines
1.9 KiB
TypeScript
import type { ButtonProps } from '@langgenius/dify-ui/button'
|
|
import type { PluginPayload } from '../types'
|
|
import type { FormSchema } from '@/app/components/base/form/types'
|
|
import { Button } from '@langgenius/dify-ui/button'
|
|
import {
|
|
memo,
|
|
useState,
|
|
} from 'react'
|
|
import ApiKeyModal from './api-key-modal'
|
|
|
|
export type AddApiKeyButtonProps = {
|
|
pluginPayload: PluginPayload
|
|
buttonVariant?: ButtonProps['variant']
|
|
buttonText?: string
|
|
disabled?: boolean
|
|
onUpdate?: () => void
|
|
formSchemas?: FormSchema[]
|
|
/**
|
|
* If provided, clicking the button calls this callback instead of mounting
|
|
* the modal inline. Use this when the button lives inside a Popover that
|
|
* would unmount the modal on outside-click; the parent should render the
|
|
* ApiKeyModal at a level above the Popover.
|
|
*/
|
|
onClick?: () => void
|
|
}
|
|
const AddApiKeyButton = ({
|
|
pluginPayload,
|
|
buttonVariant = 'secondary-accent',
|
|
buttonText = 'Use Api Key',
|
|
disabled,
|
|
onUpdate,
|
|
formSchemas = [],
|
|
onClick,
|
|
}: AddApiKeyButtonProps) => {
|
|
const [isApiKeyModalOpen, setIsApiKeyModalOpen] = useState(false)
|
|
const [isApiKeyModalMounted, setIsApiKeyModalMounted] = useState(false)
|
|
const handleClick = onClick ?? (() => {
|
|
setIsApiKeyModalMounted(true)
|
|
setIsApiKeyModalOpen(true)
|
|
})
|
|
|
|
return (
|
|
<>
|
|
<Button
|
|
className="w-full"
|
|
variant={buttonVariant}
|
|
onClick={handleClick}
|
|
disabled={disabled}
|
|
>
|
|
{buttonText}
|
|
</Button>
|
|
{
|
|
// Only mount the modal here when in uncontrolled mode (no onClick prop).
|
|
!onClick && isApiKeyModalMounted && (
|
|
<ApiKeyModal
|
|
open={isApiKeyModalOpen}
|
|
onOpenChange={setIsApiKeyModalOpen}
|
|
pluginPayload={pluginPayload}
|
|
onClose={() => setIsApiKeyModalOpen(false)}
|
|
onUpdate={onUpdate}
|
|
formSchemas={formSchemas}
|
|
/>
|
|
)
|
|
}
|
|
</>
|
|
|
|
)
|
|
}
|
|
|
|
export default memo(AddApiKeyButton)
|