diff --git a/src/App.tsx b/src/App.tsx index a00ab18..0fa9c43 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -36,59 +36,58 @@ function App(): React.ReactNode { const [auth, setAuth] = useState(false); const refreshAuth = async () => { - const resp = await apiAuthCheck(); - if (resp === undefined) { + // reauth + const refreshResp = await apiAuthRefresh(); + if (refreshResp === undefined) { showErrorToastNotification("Please try again after sometime"); setAuth(false); return false; } - if (resp.status === 200) { + if (refreshResp.status === 200) { // Success + showInfoToastNotification("Refreshed session."); setAuth(true); return true; } - if (resp.status >= 500) { + if (refreshResp.status >= 500) { setAuth(false); - showErrorToastNotification(resp.data.message); + showErrorToastNotification(refreshResp.data.message); return false; } - if (resp.status === 401) { - // reauth - const refreshResp = await apiAuthRefresh(); - if (refreshResp === undefined) { - showErrorToastNotification("Please try again after sometime"); - setAuth(false); - return false; - } - if (refreshResp.status === 200) { - // Success - showInfoToastNotification("Refreshed session."); - setAuth(true); - return true; - } - if (refreshResp.status >= 500) { - setAuth(false); - showErrorToastNotification(refreshResp.data.message); - return false; - } - if (refreshResp.status === 401) { - // not authed - setAuth(false); - return false; - } + if (refreshResp.status === 401) { + // not authed setAuth(false); - showWarnToastNotification(refreshResp.data.message); return false; } setAuth(false); - showWarnToastNotification(resp.data.message); + showWarnToastNotification(refreshResp.data.message); return false; }; useEffect(() => { - (async () => { - await refreshAuth(); - })(); + const checkAuth = async () => { + const resp = await apiAuthCheck(); + if (resp === undefined) { + showErrorToastNotification("Please try again after sometime"); + setAuth(false); + return false; + } + if (resp.status === 200) { + // Success + setAuth(true); + return true; + } + if (resp.status >= 500) { + setAuth(false); + showErrorToastNotification(resp.data.message); + return false; + } + if (resp.status === 401) await refreshAuth(); + setAuth(false); + showWarnToastNotification(resp.data.message); + return false; + }; + checkAuth(); }, []); const updateWindowDimensions = () => { diff --git a/src/pages/Graph/index.tsx b/src/pages/Graph/index.tsx index dfe795b..43bd526 100644 --- a/src/pages/Graph/index.tsx +++ b/src/pages/Graph/index.tsx @@ -96,6 +96,19 @@ const initialInteractive: Interactive = { elsSel: true, }; +const nodeOptions: Partial = { + style: { + backgroundColor: "white", + }, +}; + +const selectedNodeOptions: Partial = { + style: { + backgroundColor: "white", + boxShadow: "0px 0px 60px 20px red", + }, +}; + const edgeOptions: DefaultEdgeOptions = { animated: false, style: { @@ -133,6 +146,7 @@ const Graph = (): React.ReactNode => { const flowInstance = useReactFlow(); const [playlistNodes, setPlaylistNodes] = useState(initialNodes); const [linkEdges, setLinkEdges] = useState(initialEdges); + const [selectedNodeID, setSelectedNodeID] = useState(""); const [selectedEdgeID, setSelectedEdgeID] = useState(""); const [interactive, setInteractive] = useState(initialInteractive); @@ -140,6 +154,7 @@ const Graph = (): React.ReactNode => { const onFlowInit: OnInit = (_instance) => { console.debug("flow loaded"); + console.log(selectedNodeID); }; // base event handling @@ -153,18 +168,30 @@ const Graph = (): React.ReactNode => { ); const onFlowSelectionChange: OnSelectionChangeFunc = useCallback( - ({ edges }) => { - const selectedID = edges[0]?.id ?? ""; - setSelectedEdgeID(selectedID); + ({ nodes, edges }) => { + console.log(nodes); + console.log(edges); + + const nodeSelection = nodes[0]?.id ?? ""; + setSelectedNodeID(nodeSelection); + setPlaylistNodes((nds) => + nds.map((nd) => + nd.id === nodeSelection + ? { ...nd, ...selectedNodeOptions } + : { ...nd, ...nodeOptions } + ) + ); + const edgeSelection = edges[0]?.id ?? ""; + setSelectedEdgeID(edgeSelection); setLinkEdges((eds) => eds.map((ed) => - ed.id === selectedID + ed.id === edgeSelection ? { ...ed, ...selectedEdgeOptions } : { ...ed, ...edgeOptions } ) ); }, - [setLinkEdges] + [] ); useOnSelectionChange({ onChange: onFlowSelectionChange, @@ -330,14 +357,21 @@ const Graph = (): React.ReactNode => { }) ); + const connectedPositions = finalLayout.nodes.map((nd) => nd.position); + const largestX = connectedPositions.sort((a, b) => b.x - a.x)[0]?.x; + const largestY = connectedPositions.sort((a, b) => b.y - a.y)[0]?.y; + finalLayout.nodes.push( ...unconnectedNodes.map((node, idx) => { const position = { x: - nodeOffsets.unconnected.origin.x + + // nodeOffsets.unconnected.origin.x + + (largestX ?? nodeOffsets.unconnected.origin.x) / 2 + Math.floor(idx / 5) * nodeOffsets.unconnected.scaling.x, y: - nodeOffsets.unconnected.origin.y + + // nodeOffsets.unconnected.origin.y + + (largestY ?? nodeOffsets.unconnected.origin.y) + + 100 + Math.floor(idx % 5) * nodeOffsets.unconnected.scaling.y, }; const x = position.x - (node.measured?.width ?? 0) / 2; @@ -385,11 +419,11 @@ const Graph = (): React.ReactNode => { id: `${pl.playlistID}`, position: { x: - nodeOffsets.unconnected.origin.x + - Math.floor(idx / 15) * nodeOffsets.unconnected.scaling.x, + nodeOffsets.connected.origin.x + + Math.floor(idx / 15) * nodeOffsets.connected.scaling.x, y: - nodeOffsets.unconnected.origin.y + - Math.floor(idx % 15) * nodeOffsets.unconnected.scaling.y, + nodeOffsets.connected.origin.y + + Math.floor(idx % 15) * nodeOffsets.connected.scaling.y, }, data: { label: pl.playlistName, @@ -478,7 +512,8 @@ const Graph = (): React.ReactNode => { onBeforeDelete={onFlowBeforeDelete} nodesDraggable={interactive.ndDrag} nodesConnectable={interactive.ndConn} - nodesFocusable={false} + nodesFocusable + edgesFocusable elementsSelectable={interactive.elsSel} deleteKeyCode={["Delete"]} multiSelectionKeyCode={null} @@ -499,10 +534,19 @@ const Graph = (): React.ReactNode => { Backfill Link + +
+