version-history-button.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import React, { type FC, useCallback } from 'react'
  2. import { RiHistoryLine } from '@remixicon/react'
  3. import { useTranslation } from 'react-i18next'
  4. import { useKeyPress } from 'ahooks'
  5. import Button from '../../base/button'
  6. import Tooltip from '../../base/tooltip'
  7. import { getKeyboardKeyCodeBySystem } from '../utils'
  8. type VersionHistoryButtonProps = {
  9. onClick: () => Promise<unknown> | unknown
  10. }
  11. const VERSION_HISTORY_SHORTCUT = ['⌘', '⇧', 'H']
  12. const PopupContent = React.memo(() => {
  13. const { t } = useTranslation()
  14. return (
  15. <div className='flex items-center gap-x-1'>
  16. <div className='text-text-secondary system-xs-medium px-0.5'>
  17. {t('workflow.common.versionHistory')}
  18. </div>
  19. <div className='flex items-center gap-x-0.5'>
  20. {VERSION_HISTORY_SHORTCUT.map(key => (
  21. <span
  22. key={key}
  23. className='rounded-[4px] bg-components-kbd-bg-white text-text-tertiary system-kbd px-[1px]'
  24. >
  25. {key}
  26. </span>
  27. ))}
  28. </div>
  29. </div>
  30. )
  31. })
  32. PopupContent.displayName = 'PopupContent'
  33. const VersionHistoryButton: FC<VersionHistoryButtonProps> = ({
  34. onClick,
  35. }) => {
  36. const handleViewVersionHistory = useCallback(async () => {
  37. await onClick?.()
  38. }, [onClick])
  39. useKeyPress(`${getKeyboardKeyCodeBySystem('ctrl')}.shift.h`, (e) => {
  40. e.preventDefault()
  41. handleViewVersionHistory()
  42. }
  43. , { exactMatch: true, useCapture: true })
  44. return <Tooltip
  45. popupContent={<PopupContent />}
  46. noDecoration
  47. popupClassName='rounded-lg border-[0.5px] border-components-panel-border bg-components-tooltip-bg
  48. shadow-lg shadow-shadow-shadow-5 backdrop-blur-[5px] p-1.5'
  49. >
  50. <Button
  51. className={'p-2'}
  52. onClick={handleViewVersionHistory}
  53. >
  54. <RiHistoryLine className='w-4 h-4 text-components-button-secondary-text' />
  55. </Button>
  56. </Tooltip>
  57. }
  58. export default VersionHistoryButton