mirror of
https://github.com/20kaushik02/spotify-manager-web.git
synced 2025-12-06 09:44:06 +00:00
cleaner auth refresh, node selection
This commit is contained in:
parent
79fbfe601e
commit
70ee8c6b0e
43
src/App.tsx
43
src/App.tsx
@ -36,23 +36,6 @@ function App(): React.ReactNode {
|
|||||||
const [auth, setAuth] = useState(false);
|
const [auth, setAuth] = useState(false);
|
||||||
|
|
||||||
const refreshAuth = async () => {
|
const refreshAuth = 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) {
|
|
||||||
// reauth
|
// reauth
|
||||||
const refreshResp = await apiAuthRefresh();
|
const refreshResp = await apiAuthRefresh();
|
||||||
if (refreshResp === undefined) {
|
if (refreshResp === undefined) {
|
||||||
@ -79,16 +62,32 @@ function App(): React.ReactNode {
|
|||||||
setAuth(false);
|
setAuth(false);
|
||||||
showWarnToastNotification(refreshResp.data.message);
|
showWarnToastNotification(refreshResp.data.message);
|
||||||
return false;
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
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);
|
setAuth(false);
|
||||||
showWarnToastNotification(resp.data.message);
|
showWarnToastNotification(resp.data.message);
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
checkAuth();
|
||||||
useEffect(() => {
|
|
||||||
(async () => {
|
|
||||||
await refreshAuth();
|
|
||||||
})();
|
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const updateWindowDimensions = () => {
|
const updateWindowDimensions = () => {
|
||||||
|
|||||||
@ -96,6 +96,19 @@ const initialInteractive: Interactive = {
|
|||||||
elsSel: true,
|
elsSel: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const nodeOptions: Partial<Node> = {
|
||||||
|
style: {
|
||||||
|
backgroundColor: "white",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const selectedNodeOptions: Partial<Node> = {
|
||||||
|
style: {
|
||||||
|
backgroundColor: "white",
|
||||||
|
boxShadow: "0px 0px 60px 20px red",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
const edgeOptions: DefaultEdgeOptions = {
|
const edgeOptions: DefaultEdgeOptions = {
|
||||||
animated: false,
|
animated: false,
|
||||||
style: {
|
style: {
|
||||||
@ -133,6 +146,7 @@ const Graph = (): React.ReactNode => {
|
|||||||
const flowInstance = useReactFlow();
|
const flowInstance = useReactFlow();
|
||||||
const [playlistNodes, setPlaylistNodes] = useState<Node[]>(initialNodes);
|
const [playlistNodes, setPlaylistNodes] = useState<Node[]>(initialNodes);
|
||||||
const [linkEdges, setLinkEdges] = useState<Edge[]>(initialEdges);
|
const [linkEdges, setLinkEdges] = useState<Edge[]>(initialEdges);
|
||||||
|
const [selectedNodeID, setSelectedNodeID] = useState<Node["id"]>("");
|
||||||
const [selectedEdgeID, setSelectedEdgeID] = useState<Edge["id"]>("");
|
const [selectedEdgeID, setSelectedEdgeID] = useState<Edge["id"]>("");
|
||||||
const [interactive, setInteractive] =
|
const [interactive, setInteractive] =
|
||||||
useState<Interactive>(initialInteractive);
|
useState<Interactive>(initialInteractive);
|
||||||
@ -140,6 +154,7 @@ const Graph = (): React.ReactNode => {
|
|||||||
|
|
||||||
const onFlowInit: OnInit = (_instance) => {
|
const onFlowInit: OnInit = (_instance) => {
|
||||||
console.debug("flow loaded");
|
console.debug("flow loaded");
|
||||||
|
console.log(selectedNodeID);
|
||||||
};
|
};
|
||||||
|
|
||||||
// base event handling
|
// base event handling
|
||||||
@ -153,18 +168,30 @@ const Graph = (): React.ReactNode => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const onFlowSelectionChange: OnSelectionChangeFunc = useCallback(
|
const onFlowSelectionChange: OnSelectionChangeFunc = useCallback(
|
||||||
({ edges }) => {
|
({ nodes, edges }) => {
|
||||||
const selectedID = edges[0]?.id ?? "";
|
console.log(nodes);
|
||||||
setSelectedEdgeID(selectedID);
|
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) =>
|
setLinkEdges((eds) =>
|
||||||
eds.map((ed) =>
|
eds.map((ed) =>
|
||||||
ed.id === selectedID
|
ed.id === edgeSelection
|
||||||
? { ...ed, ...selectedEdgeOptions }
|
? { ...ed, ...selectedEdgeOptions }
|
||||||
: { ...ed, ...edgeOptions }
|
: { ...ed, ...edgeOptions }
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
[setLinkEdges]
|
[]
|
||||||
);
|
);
|
||||||
useOnSelectionChange({
|
useOnSelectionChange({
|
||||||
onChange: onFlowSelectionChange,
|
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(
|
finalLayout.nodes.push(
|
||||||
...unconnectedNodes.map((node, idx) => {
|
...unconnectedNodes.map((node, idx) => {
|
||||||
const position = {
|
const position = {
|
||||||
x:
|
x:
|
||||||
nodeOffsets.unconnected.origin.x +
|
// nodeOffsets.unconnected.origin.x +
|
||||||
|
(largestX ?? nodeOffsets.unconnected.origin.x) / 2 +
|
||||||
Math.floor(idx / 5) * nodeOffsets.unconnected.scaling.x,
|
Math.floor(idx / 5) * nodeOffsets.unconnected.scaling.x,
|
||||||
y:
|
y:
|
||||||
nodeOffsets.unconnected.origin.y +
|
// nodeOffsets.unconnected.origin.y +
|
||||||
|
(largestY ?? nodeOffsets.unconnected.origin.y) +
|
||||||
|
100 +
|
||||||
Math.floor(idx % 5) * nodeOffsets.unconnected.scaling.y,
|
Math.floor(idx % 5) * nodeOffsets.unconnected.scaling.y,
|
||||||
};
|
};
|
||||||
const x = position.x - (node.measured?.width ?? 0) / 2;
|
const x = position.x - (node.measured?.width ?? 0) / 2;
|
||||||
@ -385,11 +419,11 @@ const Graph = (): React.ReactNode => {
|
|||||||
id: `${pl.playlistID}`,
|
id: `${pl.playlistID}`,
|
||||||
position: {
|
position: {
|
||||||
x:
|
x:
|
||||||
nodeOffsets.unconnected.origin.x +
|
nodeOffsets.connected.origin.x +
|
||||||
Math.floor(idx / 15) * nodeOffsets.unconnected.scaling.x,
|
Math.floor(idx / 15) * nodeOffsets.connected.scaling.x,
|
||||||
y:
|
y:
|
||||||
nodeOffsets.unconnected.origin.y +
|
nodeOffsets.connected.origin.y +
|
||||||
Math.floor(idx % 15) * nodeOffsets.unconnected.scaling.y,
|
Math.floor(idx % 15) * nodeOffsets.connected.scaling.y,
|
||||||
},
|
},
|
||||||
data: {
|
data: {
|
||||||
label: pl.playlistName,
|
label: pl.playlistName,
|
||||||
@ -478,7 +512,8 @@ const Graph = (): React.ReactNode => {
|
|||||||
onBeforeDelete={onFlowBeforeDelete}
|
onBeforeDelete={onFlowBeforeDelete}
|
||||||
nodesDraggable={interactive.ndDrag}
|
nodesDraggable={interactive.ndDrag}
|
||||||
nodesConnectable={interactive.ndConn}
|
nodesConnectable={interactive.ndConn}
|
||||||
nodesFocusable={false}
|
nodesFocusable
|
||||||
|
edgesFocusable
|
||||||
elementsSelectable={interactive.elsSel}
|
elementsSelectable={interactive.elsSel}
|
||||||
deleteKeyCode={["Delete"]}
|
deleteKeyCode={["Delete"]}
|
||||||
multiSelectionKeyCode={null}
|
multiSelectionKeyCode={null}
|
||||||
@ -499,10 +534,19 @@ const Graph = (): React.ReactNode => {
|
|||||||
<PiSupersetOf size={36} />
|
<PiSupersetOf size={36} />
|
||||||
Backfill Link
|
Backfill Link
|
||||||
</Button>
|
</Button>
|
||||||
|
<Button>
|
||||||
|
<PiSupersetOf size={36} />
|
||||||
|
Backfill Chain
|
||||||
|
</Button>
|
||||||
|
<hr className={styles.divider} />
|
||||||
<Button onClickMethod={pruneLink}>
|
<Button onClickMethod={pruneLink}>
|
||||||
<PiSubsetOf size={36} />
|
<PiSubsetOf size={36} />
|
||||||
Prune Link
|
Prune Link
|
||||||
</Button>
|
</Button>
|
||||||
|
<Button>
|
||||||
|
<PiSubsetOf size={36} />
|
||||||
|
Prune Link
|
||||||
|
</Button>
|
||||||
<hr className={styles.divider} />
|
<hr className={styles.divider} />
|
||||||
<Button onClickMethod={() => arrangeLayout("TB")}>
|
<Button onClickMethod={() => arrangeLayout("TB")}>
|
||||||
<IoIosGitNetwork size={36} />
|
<IoIosGitNetwork size={36} />
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user