action-buttons.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import React, { type FC, useMemo } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import { useKeyPress } from 'ahooks'
  4. import { useDocumentContext } from '../../index'
  5. import Button from '@/app/components/base/button'
  6. import { getKeyboardKeyCodeBySystem, getKeyboardKeyNameBySystem } from '@/app/components/workflow/utils'
  7. type IActionButtonsProps = {
  8. handleCancel: () => void
  9. handleSave: () => void
  10. loading: boolean
  11. actionType?: 'edit' | 'add'
  12. handleRegeneration?: () => void
  13. isChildChunk?: boolean
  14. }
  15. const ActionButtons: FC<IActionButtonsProps> = ({
  16. handleCancel,
  17. handleSave,
  18. loading,
  19. actionType = 'edit',
  20. handleRegeneration,
  21. isChildChunk = false,
  22. }) => {
  23. const { t } = useTranslation()
  24. const mode = useDocumentContext(s => s.mode)
  25. const parentMode = useDocumentContext(s => s.parentMode)
  26. useKeyPress(['esc'], (e) => {
  27. e.preventDefault()
  28. handleCancel()
  29. })
  30. useKeyPress(`${getKeyboardKeyCodeBySystem('ctrl')}.s`, (e) => {
  31. e.preventDefault()
  32. if (loading)
  33. return
  34. handleSave()
  35. }
  36. , { exactMatch: true, useCapture: true })
  37. const isParentChildParagraphMode = useMemo(() => {
  38. return mode === 'hierarchical' && parentMode === 'paragraph'
  39. }, [mode, parentMode])
  40. return (
  41. <div className='flex items-center gap-x-2'>
  42. <Button
  43. onClick={handleCancel}
  44. >
  45. <div className='flex items-center gap-x-1'>
  46. <span className='text-components-button-secondary-text system-sm-medium'>{t('common.operation.cancel')}</span>
  47. <span className='px-[1px] bg-components-kbd-bg-gray rounded-[4px] text-text-tertiary system-kbd'>ESC</span>
  48. </div>
  49. </Button>
  50. {(isParentChildParagraphMode && actionType === 'edit' && !isChildChunk)
  51. ? <Button
  52. onClick={handleRegeneration}
  53. disabled={loading}
  54. >
  55. <span className='text-components-button-secondary-text system-sm-medium'>
  56. {t('common.operation.saveAndRegenerate')}
  57. </span>
  58. </Button>
  59. : null
  60. }
  61. <Button
  62. variant='primary'
  63. onClick={handleSave}
  64. disabled={loading}
  65. >
  66. <div className='flex items-center gap-x-1'>
  67. <span className='text-components-button-primary-text'>{t('common.operation.save')}</span>
  68. <div className='flex items-center gap-x-0.5'>
  69. <span className='w-4 h-4 bg-components-kbd-bg-white rounded-[4px] text-text-primary-on-surface system-kbd capitalize'>{getKeyboardKeyNameBySystem('ctrl')}</span>
  70. <span className='w-4 h-4 bg-components-kbd-bg-white rounded-[4px] text-text-primary-on-surface system-kbd'>S</span>
  71. </div>
  72. </div>
  73. </Button>
  74. </div>
  75. )
  76. }
  77. ActionButtons.displayName = 'ActionButtons'
  78. export default React.memo(ActionButtons)