checklist.tsx 5.9 KB

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