cleaner auth refresh, node selection

This commit is contained in:
Kaushik Narayan R 2025-03-12 19:06:14 -07:00
parent 79fbfe601e
commit 70ee8c6b0e
2 changed files with 89 additions and 46 deletions

View File

@ -36,59 +36,58 @@ function App(): React.ReactNode {
const [auth, setAuth] = useState(false); const [auth, setAuth] = useState(false);
const refreshAuth = async () => { const refreshAuth = async () => {
const resp = await apiAuthCheck(); // reauth
if (resp === undefined) { const refreshResp = await apiAuthRefresh();
if (refreshResp === undefined) {
showErrorToastNotification("Please try again after sometime"); showErrorToastNotification("Please try again after sometime");
setAuth(false); setAuth(false);
return false; return false;
} }
if (resp.status === 200) { if (refreshResp.status === 200) {
// Success // Success
showInfoToastNotification("Refreshed session.");
setAuth(true); setAuth(true);
return true; return true;
} }
if (resp.status >= 500) { if (refreshResp.status >= 500) {
setAuth(false); setAuth(false);
showErrorToastNotification(resp.data.message); showErrorToastNotification(refreshResp.data.message);
return false; return false;
} }
if (resp.status === 401) { if (refreshResp.status === 401) {
// reauth // not authed
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;
}
setAuth(false); setAuth(false);
showWarnToastNotification(refreshResp.data.message);
return false; return false;
} }
setAuth(false); setAuth(false);
showWarnToastNotification(resp.data.message); showWarnToastNotification(refreshResp.data.message);
return false; return false;
}; };
useEffect(() => { useEffect(() => {
(async () => { const checkAuth = async () => {
await refreshAuth(); 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 = () => { const updateWindowDimensions = () => {

View File

@ -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} />