checklist.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import {
  2. memo,
  3. useState,
  4. } from 'react'
  5. import { useTranslation } from 'react-i18next'
  6. import {
  7. useEdges,
  8. useNodes,
  9. } from 'reactflow'
  10. import {
  11. RiCloseLine,
  12. RiListCheck3,
  13. } from '@remixicon/react'
  14. import BlockIcon from '../block-icon'
  15. import {
  16. useChecklist,
  17. useNodesInteractions,
  18. } from '../hooks'
  19. import type {
  20. CommonEdgeType,
  21. CommonNodeType,
  22. } from '../types'
  23. import cn from '@/utils/classnames'
  24. import {
  25. PortalToFollowElem,
  26. PortalToFollowElemContent,
  27. PortalToFollowElemTrigger,
  28. } from '@/app/components/base/portal-to-follow-elem'
  29. import {
  30. ChecklistSquare,
  31. } from '@/app/components/base/icons/src/vender/line/general'
  32. import { AlertTriangle } from '@/app/components/base/icons/src/vender/line/alertsAndFeedback'
  33. type WorkflowChecklistProps = {
  34. disabled: boolean
  35. }
  36. const WorkflowChecklist = ({
  37. disabled,
  38. }: WorkflowChecklistProps) => {
  39. const { t } = useTranslation()
  40. const [open, setOpen] = useState(false)
  41. const nodes = useNodes<CommonNodeType>()
  42. const edges = useEdges<CommonEdgeType>()
  43. const needWarningNodes = useChecklist(nodes, edges)
  44. const { handleNodeSelect } = useNodesInteractions()
  45. return (
  46. <PortalToFollowElem
  47. placement='bottom-end'
  48. offset={{
  49. mainAxis: 12,
  50. crossAxis: 4,
  51. }}
  52. open={open}
  53. onOpenChange={setOpen}
  54. >
  55. <PortalToFollowElemTrigger onClick={() => !disabled && setOpen(v => !v)}>
  56. <div
  57. className={cn(
  58. 'relative ml-0.5 flex items-center justify-center w-7 h-7 rounded-md',
  59. disabled && 'opacity-50 cursor-not-allowed',
  60. )}
  61. >
  62. <div
  63. className={cn('group flex items-center justify-center w-full h-full rounded-md cursor-pointer hover:bg-state-accent-hover', open && 'bg-state-accent-hover')}
  64. >
  65. <RiListCheck3
  66. className={cn('w-4 h-4 group-hover:text-components-button-secondary-accent-text', open ? 'text-components-button-secondary-accent-text' : 'text-components-button-ghost-text')}
  67. />
  68. </div>
  69. {
  70. !!needWarningNodes.length && (
  71. <div className='absolute -right-1.5 -top-1.5 flex items-center justify-center min-w-[18px] h-[18px] rounded-full border border-gray-100 text-white text-[11px] font-semibold bg-[#F79009]'>
  72. {needWarningNodes.length}
  73. </div>
  74. )
  75. }
  76. </div>
  77. </PortalToFollowElemTrigger>
  78. <PortalToFollowElemContent className='z-[12]'>
  79. <div
  80. className='w-[420px] rounded-2xl bg-white border-[0.5px] border-black/5 shadow-lg overflow-y-auto'
  81. style={{
  82. maxHeight: 'calc(2 / 3 * 100vh)',
  83. }}
  84. >
  85. <div className='sticky top-0 bg-white flex items-center pl-4 pr-3 pt-3 h-[44px] text-md font-semibold text-gray-900 z-[1]'>
  86. <div className='grow'>{t('workflow.panel.checklist')}{needWarningNodes.length ? `(${needWarningNodes.length})` : ''}</div>
  87. <div
  88. className='shrink-0 flex items-center justify-center w-6 h-6 cursor-pointer'
  89. onClick={() => setOpen(false)}
  90. >
  91. <RiCloseLine className='w-4 h-4 text-gray-500' />
  92. </div>
  93. </div>
  94. <div className='py-2'>
  95. {
  96. !!needWarningNodes.length && (
  97. <>
  98. <div className='px-4 text-xs text-gray-400'>{t('workflow.panel.checklistTip')}</div>
  99. <div className='px-4 py-2'>
  100. {
  101. needWarningNodes.map(node => (
  102. <div
  103. key={node.id}
  104. className='mb-2 last-of-type:mb-0 border-[0.5px] border-gray-200 bg-white shadow-xs rounded-lg cursor-pointer'
  105. onClick={() => {
  106. handleNodeSelect(node.id)
  107. setOpen(false)
  108. }}
  109. >
  110. <div className='flex items-center p-2 h-9 text-xs font-medium text-gray-700'>
  111. <BlockIcon
  112. type={node.type}
  113. className='mr-1.5'
  114. toolIcon={node.toolIcon}
  115. />
  116. <span className='grow truncate'>
  117. {node.title}
  118. </span>
  119. </div>
  120. <div className='border-t-[0.5px] border-t-black/2'>
  121. {
  122. node.unConnected && (
  123. <div className='px-3 py-2 bg-gray-25 rounded-b-lg'>
  124. <div className='flex text-xs leading-[18px] text-gray-500'>
  125. <AlertTriangle className='mt-[3px] mr-2 w-3 h-3 text-[#F79009]' />
  126. {t('workflow.common.needConnectTip')}
  127. </div>
  128. </div>
  129. )
  130. }
  131. {
  132. node.errorMessage && (
  133. <div className='px-3 py-2 bg-gray-25 rounded-b-lg'>
  134. <div className='flex text-xs leading-[18px] text-gray-500'>
  135. <AlertTriangle className='mt-[3px] mr-2 w-3 h-3 text-[#F79009]' />
  136. {node.errorMessage}
  137. </div>
  138. </div>
  139. )
  140. }
  141. </div>
  142. </div>
  143. ))
  144. }
  145. </div>
  146. </>
  147. )
  148. }
  149. {
  150. !needWarningNodes.length && (
  151. <div className='mx-4 mb-3 py-4 rounded-lg bg-gray-50 text-gray-400 text-xs text-center'>
  152. <ChecklistSquare className='mx-auto mb-[5px] w-8 h-8 text-gray-300' />
  153. {t('workflow.panel.checklistResolved')}
  154. </div>
  155. )
  156. }
  157. </div>
  158. </div>
  159. </PortalToFollowElemContent>
  160. </PortalToFollowElem>
  161. )
  162. }
  163. export default memo(WorkflowChecklist)