|
| 1 | +import { memo, useCallback, useEffect, useMemo, useRef } from 'react' |
| 2 | +import ReactMarkdown from 'react-markdown' |
| 3 | +import remarkGfm from 'remark-gfm' |
| 4 | +import { useUpdateNodeInternals, type NodeProps } from 'reactflow' |
| 5 | +import { cn } from '@/lib/utils' |
| 6 | +import { useUserPermissionsContext } from '@/app/workspace/[workspaceId]/providers/workspace-permissions-provider' |
| 7 | +import { useCurrentWorkflow } from '../../hooks' |
| 8 | +import { ActionBar } from '../workflow-block/components' |
| 9 | +import { useBlockState } from '../workflow-block/hooks' |
| 10 | +import type { WorkflowBlockProps } from '../workflow-block/types' |
| 11 | +import { usePanelEditorStore } from '@/stores/panel-new/editor/store' |
| 12 | +import { useWorkflowRegistry } from '@/stores/workflows/registry/store' |
| 13 | +import { useWorkflowStore } from '@/stores/workflows/workflow/store' |
| 14 | +import { useSubBlockStore } from '@/stores/workflows/subblock/store' |
| 15 | + |
| 16 | +interface NoteBlockNodeData extends WorkflowBlockProps {} |
| 17 | + |
| 18 | +const NOTE_MIN_WIDTH = 220 |
| 19 | +const NOTE_MIN_HEIGHT = 140 |
| 20 | + |
| 21 | +const MarkdownContent = memo(function MarkdownContent({ content }: { content: string }) { |
| 22 | + return ( |
| 23 | + <ReactMarkdown |
| 24 | + remarkPlugins={[remarkGfm]} |
| 25 | + components={{ |
| 26 | + p: ({ children }) => ( |
| 27 | + <p className='mb-2 text-sm leading-relaxed text-[#E5E5E5] last:mb-0'>{children}</p> |
| 28 | + ), |
| 29 | + strong: ({ children }) => <strong className='font-semibold text-white'>{children}</strong>, |
| 30 | + em: ({ children }) => <em className='text-[#B8B8B8]'>{children}</em>, |
| 31 | + h1: ({ children }) => ( |
| 32 | + <h1 className='mt-0 mb-[6px] text-lg font-semibold leading-snug text-[#E5E5E5]'> |
| 33 | + {children} |
| 34 | + </h1> |
| 35 | + ), |
| 36 | + h2: ({ children }) => ( |
| 37 | + <h2 className='mt-0 mb-[4px] text-base font-semibold leading-snug text-[#E5E5E5]'> |
| 38 | + {children} |
| 39 | + </h2> |
| 40 | + ), |
| 41 | + h3: ({ children }) => ( |
| 42 | + <h3 className='mt-0 mb-[4px] text-sm font-semibold leading-snug text-[#E5E5E5]'> |
| 43 | + {children} |
| 44 | + </h3> |
| 45 | + ), |
| 46 | + a: ({ href, children }) => ( |
| 47 | + <a |
| 48 | + href={href} |
| 49 | + target='_blank' |
| 50 | + rel='noopener noreferrer' |
| 51 | + className='font-medium text-[#33B4FF] underline-offset-2 hover:underline' |
| 52 | + > |
| 53 | + {children} |
| 54 | + </a> |
| 55 | + ), |
| 56 | + ul: ({ children }) => ( |
| 57 | + <ul className='mb-2 ml-5 list-disc text-sm leading-relaxed text-[#E5E5E5]'>{children}</ul> |
| 58 | + ), |
| 59 | + ol: ({ children }) => ( |
| 60 | + <ol className='mb-2 ml-5 list-decimal text-sm leading-relaxed text-[#E5E5E5]'>{children}</ol> |
| 61 | + ), |
| 62 | + li: ({ children }) => <li className='mb-1 last:mb-0'>{children}</li>, |
| 63 | + code: ({ inline, children }: any) => |
| 64 | + inline ? ( |
| 65 | + <code className='rounded bg-[#393939] px-1 py-0.5 text-xs text-[#F59E0B]'>{children}</code> |
| 66 | + ) : ( |
| 67 | + <code className='block rounded bg-[#1A1A1A] p-2 text-xs text-[#E5E5E5]'>{children}</code> |
| 68 | + ), |
| 69 | + blockquote: ({ children }) => ( |
| 70 | + <blockquote className='mb-3 border-l-2 border-[#F59E0B] pl-3 italic text-[#B8B8B8]'> |
| 71 | + {children} |
| 72 | + </blockquote> |
| 73 | + ), |
| 74 | + }} |
| 75 | + > |
| 76 | + {content} |
| 77 | + </ReactMarkdown> |
| 78 | + ) |
| 79 | +}) |
| 80 | + |
| 81 | +export const NoteBlock = memo(function NoteBlock({ id, data }: NodeProps<NoteBlockNodeData>) { |
| 82 | + const { type, config, name } = data |
| 83 | + const containerRef = useRef<HTMLDivElement>(null) |
| 84 | + const sizeRef = useRef<{ width: number; height: number } | null>(null) |
| 85 | + const updateNodeInternals = useUpdateNodeInternals() |
| 86 | + const updateBlockLayoutMetrics = useWorkflowStore((state) => state.updateBlockLayoutMetrics) |
| 87 | + |
| 88 | + const setCurrentBlockId = usePanelEditorStore((state) => state.setCurrentBlockId) |
| 89 | + const currentBlockId = usePanelEditorStore((state) => state.currentBlockId) |
| 90 | + const isFocused = currentBlockId === id |
| 91 | + |
| 92 | + const currentWorkflow = useCurrentWorkflow() |
| 93 | + const { isEnabled, isActive, diffStatus, isDeletedBlock } = useBlockState(id, currentWorkflow, data) |
| 94 | + |
| 95 | + const activeWorkflowId = useWorkflowRegistry((state) => state.activeWorkflowId) |
| 96 | + const storedValues = useSubBlockStore( |
| 97 | + useCallback( |
| 98 | + (state) => { |
| 99 | + if (!activeWorkflowId) return undefined |
| 100 | + return state.workflowValues[activeWorkflowId]?.[id] |
| 101 | + }, |
| 102 | + [activeWorkflowId, id] |
| 103 | + ) |
| 104 | + ) |
| 105 | + |
| 106 | + const noteValues = useMemo(() => { |
| 107 | + if (data.isPreview && data.subBlockValues) { |
| 108 | + const previewFormatState = data.subBlockValues.format |
| 109 | + const previewContentState = data.subBlockValues.content |
| 110 | + |
| 111 | + const extractedPreviewFormat = |
| 112 | + typeof previewFormatState === 'object' && previewFormatState !== null |
| 113 | + ? (previewFormatState as { value?: unknown }).value |
| 114 | + : previewFormatState |
| 115 | + const extractedPreviewContent = |
| 116 | + typeof previewContentState === 'object' && previewContentState !== null |
| 117 | + ? (previewContentState as { value?: unknown }).value |
| 118 | + : previewContentState |
| 119 | + |
| 120 | + return { |
| 121 | + format: typeof extractedPreviewFormat === 'string' ? extractedPreviewFormat : 'plain', |
| 122 | + content: typeof extractedPreviewContent === 'string' ? extractedPreviewContent : '', |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + const format = |
| 127 | + storedValues && typeof storedValues.format === 'string' |
| 128 | + ? storedValues.format |
| 129 | + : typeof storedValues?.format === 'object' && storedValues?.format !== null |
| 130 | + ? (storedValues.format as { value?: unknown }).value |
| 131 | + : undefined |
| 132 | + const content = |
| 133 | + storedValues && typeof storedValues.content === 'string' |
| 134 | + ? storedValues.content |
| 135 | + : typeof storedValues?.content === 'object' && storedValues?.content !== null |
| 136 | + ? (storedValues.content as { value?: unknown }).value |
| 137 | + : undefined |
| 138 | + |
| 139 | + return { |
| 140 | + format: typeof format === 'string' ? format : 'plain', |
| 141 | + content: typeof content === 'string' ? content : '', |
| 142 | + } |
| 143 | + }, [data.isPreview, data.subBlockValues, storedValues]) |
| 144 | + |
| 145 | + const trimmedContent = noteValues.content?.trim() ?? '' |
| 146 | + const isEmpty = trimmedContent.length === 0 |
| 147 | + const showMarkdown = noteValues.format === 'markdown' && !isEmpty |
| 148 | + |
| 149 | + const userPermissions = useUserPermissionsContext() |
| 150 | + |
| 151 | + useEffect(() => { |
| 152 | + const element = containerRef.current |
| 153 | + if (!element) return |
| 154 | + |
| 155 | + const observer = new ResizeObserver((entries) => { |
| 156 | + const entry = entries[0] |
| 157 | + if (!entry) return |
| 158 | + |
| 159 | + const width = Math.max(Math.round(entry.contentRect.width), NOTE_MIN_WIDTH) |
| 160 | + const height = Math.max(Math.round(entry.contentRect.height), NOTE_MIN_HEIGHT) |
| 161 | + |
| 162 | + const previous = sizeRef.current |
| 163 | + if (!previous || previous.width !== width || previous.height !== height) { |
| 164 | + sizeRef.current = { width, height } |
| 165 | + updateBlockLayoutMetrics(id, { width, height }) |
| 166 | + updateNodeInternals(id) |
| 167 | + } |
| 168 | + }) |
| 169 | + |
| 170 | + observer.observe(element) |
| 171 | + return () => observer.disconnect() |
| 172 | + }, [id, updateBlockLayoutMetrics, updateNodeInternals]) |
| 173 | + |
| 174 | + const hasRing = |
| 175 | + isActive || isFocused || diffStatus === 'new' || diffStatus === 'edited' || isDeletedBlock |
| 176 | + const ringStyles = cn( |
| 177 | + hasRing && 'ring-[1.75px]', |
| 178 | + isActive && 'ring-[#8C10FF] animate-pulse-ring', |
| 179 | + isFocused && 'ring-[#33B4FF]', |
| 180 | + diffStatus === 'new' && 'ring-[#22C55F]', |
| 181 | + diffStatus === 'edited' && 'ring-[#FF6600]', |
| 182 | + isDeletedBlock && 'ring-[#EF4444]' |
| 183 | + ) |
| 184 | + |
| 185 | + return ( |
| 186 | + <div className='group relative'> |
| 187 | + <div |
| 188 | + ref={containerRef} |
| 189 | + className={cn( |
| 190 | + 'relative z-[20] w-[250px] cursor-default select-none rounded-[8px] bg-[#232323]' |
| 191 | + )} |
| 192 | + onClick={() => setCurrentBlockId(id)} |
| 193 | + > |
| 194 | + <ActionBar blockId={id} blockType={type} disabled={!userPermissions.canEdit} /> |
| 195 | + |
| 196 | + <div |
| 197 | + className='note-drag-handle flex cursor-grab items-center justify-between border-[#393939] border-b p-[8px] [&:active]:cursor-grabbing' |
| 198 | + onMouseDown={(event) => { |
| 199 | + event.stopPropagation() |
| 200 | + }} |
| 201 | + > |
| 202 | + <div className='flex min-w-0 flex-1 items-center gap-[10px]'> |
| 203 | + <div |
| 204 | + className='flex h-[24px] w-[24px] flex-shrink-0 items-center justify-center rounded-[6px]' |
| 205 | + style={{ backgroundColor: isEnabled ? config.bgColor : 'gray' }} |
| 206 | + > |
| 207 | + <config.icon className='h-[16px] w-[16px] text-white' /> |
| 208 | + </div> |
| 209 | + <span |
| 210 | + className={cn('font-medium text-[16px]', !isEnabled && 'truncate text-[#808080]')} |
| 211 | + title={name} |
| 212 | + > |
| 213 | + {name} |
| 214 | + </span> |
| 215 | + </div> |
| 216 | + </div> |
| 217 | + |
| 218 | + <div className='relative px-[12px] py-[10px]'> |
| 219 | + <div className='relative whitespace-pre-wrap break-words'> |
| 220 | + {isEmpty ? ( |
| 221 | + <p className='text-sm italic text-[#868686]'>Add your note...</p> |
| 222 | + ) : showMarkdown ? ( |
| 223 | + <MarkdownContent content={trimmedContent} /> |
| 224 | + ) : ( |
| 225 | + <p className='text-sm leading-relaxed text-[#E5E5E5]'>{trimmedContent}</p> |
| 226 | + )} |
| 227 | + </div> |
| 228 | + </div> |
| 229 | + {hasRing && ( |
| 230 | + <div |
| 231 | + className={cn('pointer-events-none absolute inset-0 z-40 rounded-[8px]', ringStyles)} |
| 232 | + /> |
| 233 | + )} |
| 234 | + </div> |
| 235 | + </div> |
| 236 | + ) |
| 237 | +}) |
| 238 | + |
0 commit comments