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

Feat/treemap imported by #11

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/treemap/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface ChartData {
getModuleSize: (node: ModuleTree | ModuleTreeLeaf, sizeKey: SizeKey) => number;
getModuleIds: (node: ModuleTree | ModuleTreeLeaf) => ModuleIds;
getModuleColor: NodeColorGetter;
getModuleImports: (node: ModuleTree | ModuleTreeLeaf) => string[];
}

export type Context = StaticData & ChartData;
Expand Down Expand Up @@ -69,9 +70,13 @@ const drawChart = (

const nodeIdsCache = new Map<ModuleTree | ModuleTreeLeaf, ModuleIds>();

const nodeImportsCache = new Map<ModuleTree | ModuleTreeLeaf, Set<string>>();

const getModuleSize = (node: ModuleTree | ModuleTreeLeaf, sizeKey: SizeKey) =>
nodeSizesCache.get(node)?.[sizeKey] ?? 0;

const getModuleImports = (node: ModuleTree | ModuleTreeLeaf) => [...(nodeImportsCache.get(node) ?? [])].sort();

console.time("rawHierarchy eachAfter cache");
rawHierarchy.eachAfter((node) => {
const nodeData = node.data;
Expand All @@ -88,11 +93,34 @@ const drawChart = (
(acc, child) => getModuleSize(child, sizeKey) + acc,
0
);
nodeImportsCache.set(
nodeData,
new Set(
nodeData.children.reduce((acc, child) => {
const imported = getModuleImports(child).filter((id) => {
return !id.includes(nodeData.name);
});

return acc.concat(imported);
}, [] as string[])
)
);
}
} else {
const { nodeParts, nodeMetas } = data;
const mainUid = nodeParts[nodeData.uid].mainUid;

for (const sizeKey of availableSizeProperties) {
sizes[sizeKey] = data.nodeParts[nodeData.uid][sizeKey] ?? 0;
sizes[sizeKey] = nodeParts[nodeData.uid][sizeKey] ?? 0;
}
nodeImportsCache.set(
nodeData,
new Set(
nodeMetas[mainUid].importedBy
.filter(({ uid }) => Object.values(nodeMetas[uid].moduleParts).some((moduleId) => moduleId in nodeParts))
.map(({ uid }) => nodeMetas[uid].id)
)
);
}
nodeSizesCache.set(nodeData, sizes);
});
Expand All @@ -116,6 +144,7 @@ const drawChart = (
getModuleColor,
rawHierarchy,
layout,
getModuleImports,
}}
>
<Main />
Expand Down
17 changes: 6 additions & 11 deletions src/treemap/tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { format as formatBytes } from "bytes";
import { FunctionalComponent } from "preact";
import { HierarchyRectangularNode } from "d3-hierarchy";
import { LABELS } from "../sizes";
import { isModuleTree, ModuleTree, ModuleTreeLeaf, SizeKey } from "../../types/types";
import { ModuleTree, ModuleTreeLeaf, SizeKey } from "../../types/types";
import { StaticContext } from ".";

export interface TooltipProps {
Expand Down Expand Up @@ -47,7 +47,7 @@ export const Tooltip: FunctionalComponent<TooltipProps> = ({
root,
sizeProperty,
}) => {
const { availableSizeProperties, getModuleSize, data } = useContext(StaticContext);
const { availableSizeProperties, getModuleSize, data, getModuleImports } = useContext(StaticContext);

const ref = useRef<HTMLDivElement>(null);
const [style, setStyle] = useState({});
Expand All @@ -67,11 +67,7 @@ export const Tooltip: FunctionalComponent<TooltipProps> = ({
.map((d) => d.data.name)
.join("/");

let dataNode = null;
if (!isModuleTree(node.data)) {
const mainUid = data.nodeParts[node.data.uid].mainUid;
dataNode = data.nodeMetas[mainUid];
}
const imports = getModuleImports(node.data);

return (
<>
Expand All @@ -95,13 +91,12 @@ export const Tooltip: FunctionalComponent<TooltipProps> = ({
}
})}
<br />
{dataNode && dataNode.importedBy.length > 0 && (
{imports.length > 0 && (
<div>
<div>
<b>Imported By</b>:
</div>
{dataNode.importedBy.map(({ uid }) => {
const id = data.nodeMetas[uid].id;
{imports.map((id) => {
return <div key={id}>{id}</div>;
})}
</div>
Expand All @@ -116,7 +111,7 @@ export const Tooltip: FunctionalComponent<TooltipProps> = ({
)}
</>
);
}, [availableSizeProperties, data, getModuleSize, node, root.data, sizeProperty]);
}, [availableSizeProperties, data, getModuleSize, node, root.data, sizeProperty, getModuleImports]);

const updatePosition = (mouseCoords: { x: number; y: number }) => {
if (!ref.current) return;
Expand Down