Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug:🐛 fix double confirm dialog #914

Merged
merged 9 commits into from
Feb 17, 2025
25 changes: 9 additions & 16 deletions src/components/TreeView/TreeView.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect } from 'react';
import React, { useState, useEffect, useCallback } from 'react';
import {
TreeContainer,
NodeContainer,
Expand Down Expand Up @@ -45,8 +45,6 @@ const TreeView = ({
rootNodes,
dirtyNodeId,
resetDirtyNode,
hasUnsavedChanges = false,
unsavedChangesConfirmationMessage = 'You have unsaved changes. Are you sure you want to continue?',
}: TreeViewProps): JSX.Element => {
const [treeData, setTreeData] = useState<NodeData[]>(rootNodes);
const [loading, setLoading] = useState<number | string | null>();
Expand All @@ -56,7 +54,7 @@ const TreeView = ({
const [isNodeExpanded, setIsNodeExpanded] = useState(false);
const [executionCount, setExecutionCount] = useState(0);
const { pathname } = useLocation();

const nodeIdFromPath = pathname.split('/').filter(Boolean).pop() || '';
const getNodeChildCountAndCollapse = (
parentNodeId: string | number
): number => {
Expand Down Expand Up @@ -249,12 +247,6 @@ const TreeView = ({
node.onClick && node.onClick();
};

const handleOnClick = (node: NodeData): void => {
if (!hasUnsavedChanges || confirm(unsavedChangesConfirmationMessage)) {
selectNode(node);
}
};

const getParentPath = (node: NodeData, treeData: NodeData[]): any => {
if (!node.parentId) {
return [node.name];
Expand All @@ -280,14 +272,18 @@ const TreeView = ({
}
};

const node = treeData.find((node) => node.id === nodeIdFromPath);
if (node && node.onClick) {
node.onClick();
}

const getNodeLink = (node: NodeData): JSX.Element => {
const getBasePath = (pathname: string): string => {
const segments = pathname.split('/').filter(Boolean);
const basePath = `/${segments[0]}/${segments[1]}`;
return basePath;
};
const baseLibraryPath = getBasePath(pathname);

const parentPath = constructPath(node, treeData);
const finalPath = `${baseLibraryPath}/${parentPath}`;

Expand All @@ -296,17 +292,14 @@ const TreeView = ({
hasChildren={node.getChildren ? true : false}
isExpanded={node.isExpanded === true}
isVoided={node.isVoided === true}
isSelected={node.isSelected === true}
isSelected={node.id === nodeIdFromPath}
title={node.name}
>
{node.onClick ? (
<NodeLink
to={finalPath}
isExpanded={node.isExpanded === true}
isVoided={node.isVoided === true}
onClick={(): void => {
handleOnClick(node);
}}
isSelected={node.isSelected ? true : false}
>
{node.name}
</NodeLink>
Expand Down
15 changes: 5 additions & 10 deletions src/components/TreeView/style.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import styled, { css } from 'styled-components';
import { tokens } from '@equinor/eds-tokens';
import { NavLink } from 'react-router-dom';

export const TreeContainer = styled.div`
display: flex;
Expand Down Expand Up @@ -58,7 +59,7 @@ interface NodeNameProps {
hasChildren: boolean;
isExpanded: boolean;
isVoided: boolean;
isSelected: boolean;
isSelected?: boolean;
}

export const NodeName = styled.div<NodeNameProps>`
Expand Down Expand Up @@ -96,11 +97,12 @@ export const NodeName = styled.div<NodeNameProps>`
interface NodeLinkProps {
isExpanded: boolean;
isVoided: boolean;
isSelected: boolean;
}

export const NodeLink = styled.span<NodeLinkProps>`
export const NodeLink = styled(NavLink)<NodeLinkProps>`
cursor: pointer;
text-decoration: none;
color: inherit;

${(props): any =>
props.isExpanded &&
Expand All @@ -114,13 +116,6 @@ export const NodeLink = styled.span<NodeLinkProps>`
opacity: 0.5;
`}

${(props): any =>
props.isSelected &&
css`
color: ${tokens.colors.interactive.primary__resting.rgba};
background: ${tokens.colors.ui.background__light.rgba};
`}

:hover {
color: ${(props): string =>
!props.isVoided
Expand Down