You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

modal.tsx 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useCallback, useState } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import Modal from '@/app/components/base/modal'
  6. import OptionCard from '@/app/components/workflow/nodes/_base/components/option-card'
  7. import Button from '@/app/components/base/button'
  8. import type { Permissions, ReferenceSetting } from '@/app/components/plugins/types'
  9. import { PermissionType } from '@/app/components/plugins/types'
  10. import type { AutoUpdateConfig } from './auto-update-setting/types'
  11. import AutoUpdateSetting from './auto-update-setting'
  12. import { defaultValue as autoUpdateDefaultValue } from './auto-update-setting/config'
  13. import Label from './label'
  14. const i18nPrefix = 'plugin.privilege'
  15. type Props = {
  16. payload: ReferenceSetting
  17. onHide: () => void
  18. onSave: (payload: ReferenceSetting) => void
  19. }
  20. const PluginSettingModal: FC<Props> = ({
  21. payload,
  22. onHide,
  23. onSave,
  24. }) => {
  25. const { t } = useTranslation()
  26. const { auto_upgrade: autoUpdateConfig, permission: privilege } = payload || {}
  27. const [tempPrivilege, setTempPrivilege] = useState<Permissions>(privilege)
  28. const [tempAutoUpdateConfig, setTempAutoUpdateConfig] = useState<AutoUpdateConfig>(autoUpdateConfig || autoUpdateDefaultValue)
  29. const handlePrivilegeChange = useCallback((key: string) => {
  30. return (value: PermissionType) => {
  31. setTempPrivilege({
  32. ...tempPrivilege,
  33. [key]: value,
  34. })
  35. }
  36. }, [tempPrivilege])
  37. const handleSave = useCallback(async () => {
  38. await onSave({
  39. permission: tempPrivilege,
  40. auto_upgrade: tempAutoUpdateConfig,
  41. })
  42. onHide()
  43. }, [onHide, onSave, tempAutoUpdateConfig, tempPrivilege])
  44. return (
  45. <Modal
  46. isShow
  47. onClose={onHide}
  48. closable
  49. className='w-[620px] max-w-[620px] !p-0'
  50. >
  51. <div className='shadows-shadow-xl flex w-full flex-col items-start rounded-2xl border border-components-panel-border bg-components-panel-bg'>
  52. <div className='flex items-start gap-2 self-stretch pb-3 pl-6 pr-14 pt-6'>
  53. <span className='title-2xl-semi-bold self-stretch text-text-primary'>{t(`${i18nPrefix}.title`)}</span>
  54. </div>
  55. <div className='flex flex-col items-start justify-center gap-4 self-stretch px-6 py-3'>
  56. {[
  57. { title: t(`${i18nPrefix}.whoCanInstall`), key: 'install_permission', value: tempPrivilege?.install_permission || PermissionType.noOne },
  58. { title: t(`${i18nPrefix}.whoCanDebug`), key: 'debug_permission', value: tempPrivilege?.debug_permission || PermissionType.noOne },
  59. ].map(({ title, key, value }) => (
  60. <div key={key} className='flex flex-col items-start gap-1 self-stretch'>
  61. <Label label={title} />
  62. <div className='flex w-full items-start justify-between gap-2'>
  63. {[PermissionType.everyone, PermissionType.admin, PermissionType.noOne].map(option => (
  64. <OptionCard
  65. key={option}
  66. title={t(`${i18nPrefix}.${option}`)}
  67. onSelect={() => handlePrivilegeChange(key)(option)}
  68. selected={value === option}
  69. className="flex-1"
  70. />
  71. ))}
  72. </div>
  73. </div>
  74. ))}
  75. </div>
  76. <AutoUpdateSetting payload={tempAutoUpdateConfig} onChange={setTempAutoUpdateConfig} />
  77. <div className='flex h-[76px] items-center justify-end gap-2 self-stretch p-6 pt-5'>
  78. <Button
  79. className='min-w-[72px]'
  80. onClick={onHide}
  81. >
  82. {t('common.operation.cancel')}
  83. </Button>
  84. <Button
  85. className='min-w-[72px]'
  86. variant={'primary'}
  87. onClick={handleSave}
  88. >
  89. {t('common.operation.save')}
  90. </Button>
  91. </div>
  92. </div>
  93. </Modal>
  94. )
  95. }
  96. export default React.memo(PluginSettingModal)