Spaces:
Running

File size: 2,864 Bytes
e0fff03
bcbd0a4
e0fff03
bcbd0a4
e0fff03
bcbd0a4
e0fff03
bcbd0a4
e0fff03
 
 
 
bcbd0a4
 
6e7bb10
 
bcbd0a4
e0fff03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bcbd0a4
6e7bb10
 
 
 
e0fff03
 
 
 
 
 
 
 
 
 
bcbd0a4
e0fff03
 
bcbd0a4
e0fff03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bcbd0a4
6e7bb10
 
 
bcbd0a4
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import React, { useState, useEffect } from 'react';
import { Handle, Position } from '@xyflow/react';
import { Loader2 } from 'lucide-react';
import { useTheme } from '../../../../context/ThemeContext';
import axios from 'axios';

const PathNode = ({ id, data, selected }) => {
  const { isDarkMode } = useTheme();
  const [path, setPath] = useState(data.path || "");
  const [loading, setLoading] = useState(false);

  // Extract properties from data
  const color = data.color || (isDarkMode ? '#ffffff' : '#31363F');
  const strokeWidth = data.strokeWidth || 3;
  const width = data.width || 100;
  const height = data.height || 100;

  // Load large path data from backend on mount if not loaded
  useEffect(() => {
    if (!path && data.hasLargeData) {
      setLoading(true);
      const API_URL = import.meta.env.VITE_API_URL;
      axios.get(`${API_URL}/TaskNodes/${id}/data`)
        .then(res => {
          if (res.data && res.data.data) {
            setPath(res.data.data);
            if (data.updateNodeData) {
              data.updateNodeData(id, { path: res.data.data, isLoadedFromBackend: true });
            }
          }
        })
        .catch(err => console.error('[PathNode] Error loading large path data:', err))
        .finally(() => setLoading(false));
    }
  }, [id, path, data.hasLargeData]);

  // Sync if data changes from remote
  useEffect(() => {
    if (data.path && data.path !== path) {
      setPath(data.path);
    }
  }, [data.path]);

  return (
    <div 
      className={`relative group ${selected ? 'ring-2 ring-primary/20 rounded-md' : ''}`}
      style={{ width, height }}
    >
      {loading ? (
        <div className="w-full h-full flex items-center justify-center bg-secondary/5 rounded border border-dashed border-primary/10">
          <Loader2 size={16} className="text-primary animate-spin" />
        </div>
      ) : (
        /* SVG Path Renderer */
        <svg 
          width={width}
          height={height}
          viewBox={`0 0 ${width} ${height}`}
          style={{ 
            display: 'block',
            overflow: 'visible'
          }}
        >
          {path && (
            <path
              d={path}
              fill="none"
              stroke={color}
              strokeWidth={strokeWidth}
              strokeLinecap="round"
              strokeLinejoin="round"
              style={{ 
                pointerEvents: 'visibleStroke', // Allows selecting the path by clicking the stroke
                cursor: 'pointer' 
              }}
            />
          )}
        </svg>
      )}

      {/* Invisible Handles */}
      <Handle type="target" position={Position.Top} className="!opacity-0 !pointer-events-none" />
      <Handle type="source" position={Position.Bottom} className="!opacity-0 !pointer-events-none" />
    </div>
  );
};

export default PathNode;