-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Multiple fetches fixed
- Loading branch information
Showing
2 changed files
with
57 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,75 @@ | ||
import React, { useEffect, useState, useCallback } from 'react'; | ||
import React, { useEffect, useState, useMemo } from 'react'; | ||
|
||
const dataCache = {}; | ||
|
||
const Tooltip = ({ details, position, containerRef, apiUrl }) => { | ||
const [tooltipPosition, setTooltipPosition] = useState({ x: 0, y: 0 }); | ||
const [fetchedData, setFetchedData] = useState(null); | ||
const [loading, setLoading] = useState(false); | ||
const [error, setError] = useState(null); | ||
const Tooltip = ({ details, position, containerRef, tooltipData }) => { | ||
const [tooltip, setTooltip] = useState({ position: { x: 0, y: 0 }, data: null }); | ||
// console.log(tooltipData) | ||
|
||
useEffect(() => { | ||
if (containerRef.current && position) { | ||
const containerRect = containerRef.current.getBoundingClientRect(); | ||
const x = position.x - containerRect.left + 40; | ||
const y = position.y - containerRect.top; | ||
setTooltipPosition({ x, y }); | ||
} | ||
}, [position]); | ||
|
||
const fetchData = useCallback(async (uuid) => { | ||
if (dataCache[uuid]) { | ||
setFetchedData(dataCache[uuid]); | ||
return; | ||
} | ||
let matchingNode = null; | ||
|
||
setLoading(true); | ||
try { | ||
const response = await fetch(`${apiUrl}/nodes/${uuid}`); | ||
const data = await response.json(); | ||
const nodeData = data.data.nodes[0]; | ||
|
||
dataCache[uuid] = nodeData; | ||
|
||
setFetchedData(nodeData); | ||
} catch (err) { | ||
setError(err); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}, [apiUrl]); | ||
if (tooltipData && details.uuid) { | ||
matchingNode = tooltipData.inputLogical.links.find(node => node.uuid === details.uuid) | ||
|| tooltipData.inputData.links.find(node => node.uuid === details.uuid) | ||
|| tooltipData.outputLogical.links.find(node => node.uuid === details.uuid) | ||
|| tooltipData.outputData.links.find(node => node.uuid === details.uuid); | ||
|
||
useEffect(() => { | ||
if (details && details.uuid) { | ||
fetchData(details.uuid); | ||
} | ||
console.log(matchingNode); | ||
|
||
setTooltip({ position: { x, y }, data: matchingNode }); | ||
} | ||
}, [details, fetchData]); | ||
}, [position, containerRef, tooltipData, details]); | ||
|
||
const tooltipContent = useMemo(() => { | ||
if (!tooltip.data) return null; | ||
|
||
const { id, uuid, full_type, ctime, link_label } = tooltip.data; | ||
|
||
console.log("Tooltip Data:", tooltip.data); | ||
|
||
return ( | ||
<> | ||
<div className="mb-2 flex items-center"> | ||
<div className="h-3 w-3 rounded-full bg-blue-500 mr-2"></div> | ||
<span className="font-semibold">ID : </span> {id} | ||
</div> | ||
<div className="mb-2 flex items-center"> | ||
<div className="h-3 w-3 rounded-full bg-green-500 mr-2"></div> | ||
<span className="font-semibold">UUID : </span> {uuid} | ||
</div> | ||
<div className="mb-2 flex items-center"> | ||
<div className="h-3 w-3 rounded-full bg-orange-500 mr-2"></div> | ||
<span className="font-semibold">Type : </span> {full_type} | ||
</div> | ||
<div className="mb-2 flex items-center"> | ||
<div className="h-3 w-3 rounded-full bg-red-500 mr-2"></div> | ||
<span className="font-semibold">Created : </span> {ctime} | ||
</div> | ||
{link_label && ( | ||
<div className="mb-2 flex items-center"> | ||
<div className="h-3 w-3 rounded-full bg-indigo-500 mr-2"></div> | ||
<span className="font-semibold">Link Label : </span> {link_label} | ||
</div> | ||
)} | ||
</> | ||
); | ||
}, [tooltip.data]); | ||
|
||
if (!details) return null; | ||
if (!tooltip.data) return null; | ||
|
||
return ( | ||
<div | ||
className="absolute bg-white z-50 text-gray-800 border border-gray-200 rounded-lg shadow-md p-4 text-sm" | ||
style={{ top: tooltipPosition.y, left: tooltipPosition.x }} | ||
style={{ top: tooltip.position.y, left: tooltip.position.x }} | ||
> | ||
{loading && <div>Loading...</div>} | ||
{error && <div>Error: {error.message}</div>} | ||
{fetchedData && ( | ||
<> | ||
<div className="mb-2 flex items-center"> | ||
<div className="h-3 w-3 rounded-full bg-blue-500 mr-2"></div> | ||
<span className="font-semibold">ID : </span> {fetchedData.id} | ||
</div> | ||
<div className="mb-2 flex items-center"> | ||
<div className="h-3 w-3 rounded-full bg-green-500 mr-2"></div> | ||
<span className="font-semibold">UUID : </span> {fetchedData.uuid} | ||
</div> | ||
<div className="mb-2 flex items-center"> | ||
<div className="h-3 w-3 rounded-full bg-orange-500 mr-2"></div> | ||
<span className="font-semibold">Type : </span> {fetchedData.node_type} | ||
</div> | ||
<div className="mb-2 flex items-center"> | ||
<div className="h-3 w-3 rounded-full bg-red-500 mr-2"></div> | ||
<span className="font-semibold">Created : </span> {fetchedData.ctime} | ||
</div> | ||
<div className="mb-2 flex items-center"> | ||
<div className="h-3 w-3 rounded-full bg-gray-500 mr-2"></div> | ||
<span className="font-semibold">Modified : </span> {fetchedData.mtime} | ||
</div> | ||
{fetchedData.process_type && ( | ||
<div className="flex items-center"> | ||
<div className="h-3 w-3 rounded-full bg-indigo-500 mr-2"></div> | ||
<span className="font-semibold">Process Type : </span> {fetchedData.process_type} | ||
</div> | ||
)} | ||
</> | ||
)} | ||
{tooltipContent} | ||
</div> | ||
); | ||
}; | ||
|
||
export default Tooltip; | ||
export default Tooltip; |