Skip to content

Commit

Permalink
Fix: Issues on PAT panels (#867)
Browse files Browse the repository at this point in the history
* Handle root node expansion from search

* Remove ee panel from pa panels

* Fix timeouts leak on panel change

* No early return, change condition to not add click event and allow styles to still render

* Make searchbar sticky and update click logic
  • Loading branch information
mayan-000 authored Nov 18, 2024
1 parent de8d5ba commit 521d6e1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
} from '@google-psat/design-system';
import { I18n } from '@google-psat/i18n';
import classNames from 'classnames';
import ExplorableExplanation from './explorableExplanation';

/**
* Internal dependencies.
Expand Down Expand Up @@ -64,12 +63,6 @@ const ProtectedAudience = () => {
className: 'p-4',
},
},
{
title: 'Explorable Explanation',
content: {
Element: ExplorableExplanation,
},
},
{
title: 'Interest Groups',
content: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const Topics = () => {
githubUrl:
'https://github.com/patcg-individual-drafts/topics/blob/main/taxonomy_v2.md',
},
className: 'overflow-hidden',
},
},
{
Expand All @@ -89,6 +90,7 @@ const Topics = () => {
githubUrl:
'https://github.com/patcg-individual-drafts/topics/blob/main/taxonomy_v1.md',
},
className: 'overflow-hidden',
},
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ interface TaxonomyTreeProps {

const TaxonomyTree = ({ taxonomyUrl, githubUrl }: TaxonomyTreeProps) => {
const divRef = useRef<HTMLDivElement>(null);
const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const timeoutRef = useRef<ReturnType<typeof setTimeout>[]>([]);
const [taxonomyArray, setTaxonomyArray] = useState<string[]>([]);

useEffect(() => {
Expand Down Expand Up @@ -72,31 +72,32 @@ const TaxonomyTree = ({ taxonomyUrl, githubUrl }: TaxonomyTreeProps) => {

const nodeClickHandler = useCallback((value: string) => {
const clicker = (id: string, nextId?: string) => {
if (nextId && document.getElementById(nextId)) {
return;
}
const shouldClick = Boolean(!(nextId && document.getElementById(nextId)));

const svgGroup = document.getElementById(id);

if (svgGroup) {
const clickEvent = new MouseEvent('click', {
view: window,
bubbles: true,
cancelable: true,
});
if (shouldClick) {
const clickEvent = new MouseEvent('click', {
view: window,
bubbles: true,
cancelable: true,
});

svgGroup.dispatchEvent(clickEvent);
svgGroup.dispatchEvent(clickEvent);
}

svgGroup.scrollIntoView({ behavior: 'smooth', block: 'center' });

svgGroup.style.fill = 'orangered';
svgGroup.style.transition = 'fill 1s';

timeoutRef.current = setTimeout(() => {
const timeout = setTimeout(() => {
svgGroup.style.fill = '';
svgGroup.style.fontWeight = '';
svgGroup.style.transition = '';
}, 3000);

timeoutRef.current.push(timeout);
}
};

Expand All @@ -106,20 +107,28 @@ const TaxonomyTree = ({ taxonomyUrl, githubUrl }: TaxonomyTreeProps) => {
const _id = id.trim().split(' ').join('');
const nextId = nodeIds[idx + 1]?.trim().split(' ').join('');

clicker(_id, nextId);
if (idx !== 0 && _id === '') {
return;
}

clicker(_id || 'tax-tree-root-node', nextId); // if no id is provided it's the root of the tree
});
}, []);

useEffect(() => {
const timeouts = timeoutRef.current;

return () => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
if (timeouts) {
timeouts.forEach((timeout) => {
clearTimeout(timeout);
});
}
};
}, []);

return (
<div className="relative">
<div className="relative h-full flex flex-col">
<SearchDropdown values={taxonomyArray} onSelect={nodeClickHandler} />
<a
href={githubUrl}
Expand All @@ -133,7 +142,7 @@ const TaxonomyTree = ({ taxonomyUrl, githubUrl }: TaxonomyTreeProps) => {
width="14"
/>
</a>
<div className="p-4" ref={divRef} />
<div className="p-4 overflow-auto" ref={divRef} />
</div>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,10 @@ const Tree = async (
const nodeEnter = node
.enter()
.append('g')
.attr('id', (d) => d.data.name.split(' ').join(''))
.attr(
'id',
(d) => d.data.name.split(' ').join('') || 'tax-tree-root-node'
) // if no name means it is root of the tree
.attr('transform', () => `translate(${source.y0},${source.x0})`)
.attr('fill-opacity', 0)
.attr('stroke-opacity', 0)
Expand Down

0 comments on commit 521d6e1

Please sign in to comment.