dify/web/app/components/plugins/permission-setting-modal/modal.tsx

94 lines
3.2 KiB
TypeScript
Raw Normal View History

2024-09-16 18:58:39 +08:00
'use client'
import type { FC } from 'react'
2024-10-15 19:20:59 +08:00
import React, { useCallback, useState } from 'react'
2024-09-16 18:58:39 +08:00
import { useTranslation } from 'react-i18next'
import Modal from '@/app/components/base/modal'
import OptionCard from '@/app/components/workflow/nodes/_base/components/option-card'
import Button from '@/app/components/base/button'
2024-10-15 19:20:59 +08:00
import type { Permissions } from '@/app/components/plugins/types'
import { PermissionType } from '@/app/components/plugins/types'
2024-09-16 18:58:39 +08:00
2024-10-15 21:31:06 +08:00
const i18nPrefix = 'plugin.privilege'
2024-09-16 18:58:39 +08:00
type Props = {
2024-10-15 19:20:59 +08:00
payload: Permissions
2024-09-16 18:58:39 +08:00
onHide: () => void
2024-10-15 19:20:59 +08:00
onSave: (payload: Permissions) => void
2024-09-16 18:58:39 +08:00
}
const PluginSettingModal: FC<Props> = ({
2024-10-15 19:20:59 +08:00
payload,
2024-09-16 18:58:39 +08:00
onHide,
2024-10-15 19:20:59 +08:00
onSave,
2024-09-16 18:58:39 +08:00
}) => {
const { t } = useTranslation()
2024-10-15 19:20:59 +08:00
const [tempPrivilege, setTempPrivilege] = useState<Permissions>(payload)
const handlePrivilegeChange = useCallback((key: string) => {
return (value: PermissionType) => {
setTempPrivilege({
...tempPrivilege,
[key]: value,
})
}
}, [tempPrivilege])
const handleSave = useCallback(() => {
onSave(tempPrivilege)
onHide()
}, [tempPrivilege])
2024-09-16 18:58:39 +08:00
return (
<Modal
2024-10-15 19:20:59 +08:00
isShow
2024-09-16 18:58:39 +08:00
onClose={onHide}
closable
className='!p-0 w-[420px]'
>
<div className='flex flex-col items-start w-[420px] rounded-2xl border border-components-panel-border bg-components-panel-bg shadows-shadow-xl'>
<div className='flex pt-6 pb-3 pl-6 pr-14 items-start gap-2 self-stretch'>
2024-10-15 21:31:06 +08:00
<span className='self-stretch text-text-primary title-2xl-semi-bold'>{t(`${i18nPrefix}.title`)}</span>
2024-09-16 18:58:39 +08:00
</div>
<div className='flex px-6 py-3 flex-col justify-center items-start gap-4 self-stretch'>
{[
2024-10-15 21:35:56 +08:00
{ title: t(`${i18nPrefix}.whoCanInstall`), key: 'canManagement', value: tempPrivilege.canManagement },
2024-10-15 21:31:06 +08:00
{ title: t(`${i18nPrefix}.whoCanDebug`), key: 'canDebugger', value: tempPrivilege.canDebugger },
2024-10-15 19:20:59 +08:00
].map(({ title, key, value }) => (
2024-09-16 18:58:39 +08:00
<div key={key} className='flex flex-col items-start gap-1 self-stretch'>
2024-10-15 21:31:06 +08:00
<div className='flex h-6 items-center gap-0.5'>
2024-09-16 18:58:39 +08:00
<span className='text-text-secondary system-sm-semibold'>{title}</span>
</div>
<div className='flex items-start gap-2 justify-between w-full'>
2024-10-15 19:20:59 +08:00
{[PermissionType.everyone, PermissionType.admin, PermissionType.noOne].map(option => (
2024-09-16 18:58:39 +08:00
<OptionCard
key={option}
2024-10-15 21:31:06 +08:00
title={t(`${i18nPrefix}.${option}`)}
2024-10-15 19:20:59 +08:00
onSelect={() => handlePrivilegeChange(key)(option)}
2024-09-16 18:58:39 +08:00
selected={value === option}
className="flex-1"
/>
))}
</div>
</div>
))}
</div>
<div className='flex h-[76px] p-6 pt-5 justify-end items-center gap-2 self-stretch'>
<Button
className='min-w-[72px]'
onClick={onHide}
>
2024-10-15 21:31:06 +08:00
{t('common.operation.cancel')}
2024-09-16 18:58:39 +08:00
</Button>
<Button
className='min-w-[72px]'
variant={'primary'}
2024-10-15 19:20:59 +08:00
onClick={handleSave}
2024-09-16 18:58:39 +08:00
>
2024-10-15 21:31:06 +08:00
{t('common.operation.save')}
2024-09-16 18:58:39 +08:00
</Button>
</div>
</div>
</Modal>
)
}
export default React.memo(PluginSettingModal)