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

[TS dashboard] Draw dependencies between files #17338

Merged
merged 8 commits into from
Mar 17, 2023

Conversation

mcmire
Copy link
Contributor

@mcmire mcmire commented Jan 21, 2023

Explanation

If you're thinking about converting a file to TypeScript, you might want
to know what files import that file and what files it imports. This
commit makes it so that if you click on a box in the visualization, it
will draw lines between that box and the other boxes which represent its
dependents and dependencies.

Screenshots/Screencaps

Screen.Recording.2023-02-22.at.10.28.36.AM.mov

Manual Testing Steps

  • Run yarn ts-migration:dashboard:watch in a terminal tab, then run open development/ts-migration-dashboard/build/final/index.html.
  • Click on a box (let's call it A) that you see on the dashboard. You may see red lines and/or blue lines which connect this box to other boxes.
  • Pick a red line and follow it up the tree to its box (let's call it B). This file is a dependency; if you open file A in your editor, you should see that it is importing B.
  • Pick a blue line and follow it down the tree to its box (let's call it C). This file is a dependent; if you open C in your editor, you should see that it is importing A.
  • Compare what you see to https://metamask.github.io/metamask-extension-ts-migration-dashboard/. While the boxes per level are now sorted, the boxes should live in the same levels — nothing should be rearranged.

Pre-merge author checklist

  • I've clearly explained:
    • What problem this PR is solving
    • How this problem was solved
    • How reviewers can test my changes
  • Sufficient automated test coverage has been added

Pre-merge reviewer checklist

  • Manual testing (e.g. pull and build branch, run in browser, test code being changed)
  • PR is linked to the appropriate GitHub issue
  • IF this PR fixes a bug in the release milestone, add this PR to the release milestone

If further QA is required (e.g. new feature, complex testing steps, large refactor), add the Extension QA Board label.

In this case, a QA Engineer approval will be be required.


Closes #16810.

@mcmire mcmire requested review from a team and kumavis as code owners January 21, 2023 00:34
@mcmire mcmire requested review from segun and removed request for a team January 21, 2023 00:34
@github-actions
Copy link
Contributor

CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes.

@metamaskbot
Copy link
Collaborator

Builds ready [203969f]
Page Load Metrics (1349 ± 144 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint101160125157
domContentLoaded102420681315296142
load102720681349300144
domInteractive102420681315296142
Bundle size diffs
  • background: 0 bytes
  • ui: 0 bytes
  • common: 0 bytes

@brad-decker
Copy link
Contributor

WHAT SHUT UP

brad-decker
brad-decker previously approved these changes Jan 23, 2023
@mcmire mcmire self-assigned this Jan 24, 2023
@mcmire mcmire marked this pull request as draft February 8, 2023 19:17
@mcmire
Copy link
Contributor Author

mcmire commented Feb 8, 2023

Marking this as draft until #17337 is merged.

@mcmire mcmire force-pushed the be-consistent-with-terminology branch 2 times, most recently from 3628891 to ed8ea8b Compare February 14, 2023 18:41
Base automatically changed from be-consistent-with-terminology to develop February 16, 2023 21:38
@mcmire mcmire force-pushed the show-connections-in-ts-migration-dashboard branch 2 times, most recently from 722e4c5 to 5ec1645 Compare February 22, 2023 01:05
@metamaskbot
Copy link
Collaborator

Builds ready [5ec1645]
Page Load Metrics (1600 ± 46 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint9412610984
domContentLoaded1468177115808742
load1481180016009646
domInteractive1468177115808742
Bundle size diffs
  • background: 0 bytes
  • ui: 0 bytes
  • common: 0 bytes

@mcmire mcmire force-pushed the show-connections-in-ts-migration-dashboard branch from 5ec1645 to c838b98 Compare February 22, 2023 16:55
@metamaskbot
Copy link
Collaborator

Builds ready [c838b98]
Page Load Metrics (1687 ± 84 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint1051891292211
domContentLoaded14192050166616981
load14192051168717684
domInteractive14192050166616981
Bundle size diffs
  • background: 0 bytes
  • ui: 0 bytes
  • common: 0 bytes

@mcmire mcmire force-pushed the show-connections-in-ts-migration-dashboard branch from c838b98 to d865af9 Compare February 22, 2023 17:27
export default function App() {
const partitions = readPartitionsFile();
const [boxRectsByModuleId, setBoxRectsById] = useState<Record<
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the build script runs, it will compile a dependency tree, so by this point we already know which files (modules) are in the codebase and how they relate to each other. The big change in this PR is to update the UI so that when the App component loads, we access the DOM and record the coordinates and dimensions of all of the box elements. This is necessary in order to draw the connection lines between boxes, as we need absolute coordinates to do so. That information is stored in a type called BoxRect, and the BoxModel wraps this to add BoxRects for dependencies and dependents tied to a box. To make things slightly faster you can look up the BoxRect and BoxModel for any box by the id of the module that it represents (which is the name for the file). Here we initialize one lookup object, which will be updated as the page is rendered, and then use that object to build a second lookup object.

? null
: boxRectsByModuleId[activeBoxRectId];

const registerBox = (id: string, boxRect: BoxRect) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is passed to each Box component and will store the dimensions and coordinates for the box on the App level so that we can access it later when the box is selected.

const isStorybookModule = /\.stories\.(?:js|tsx?)/u.test(module.id);
const ref = useRef<HTMLDivElement>(null);

useEffect(() => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of the code in this file was moved over from App, but this is new. Note the useEffect to capture the dimensions and coordinates of the DOM element when it's rendered.

const npmPackageMatch = givenChildModuleId.match(
/^node_modules\/(?:(@[^/]+)\/)?([^/]+)\/.+$/u,
);
const currentModule = modulesToFill[0];
Copy link
Contributor Author

@mcmire mcmire Feb 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the most difficult part of this PR to review, and in fairness, it's a bit gnarly. Basically, this function is run during the build phase, and its goal is to spit out all of the data that the UI can use to render the visualization. At this point we do have a simple version of the dependency graph — we have a list of modules and we know the dependencies for each module — but we don't know what "level" each module is at, and we also aren't able to easily access the dependents for a module. To determine both of these, we have to walk through the graph. This is a bit tricky, as there are so many nodes we can't use recursion, and in addition, we may encounter cycles (A imports B, which imports C, which imports A, which imports B...). The code to handle both of these is already present, but it's important to point out, as it's complicated. (I know — I really should add tests for this code. 😬 )

In any case, most of the changes in this file involve renaming "child" to "dependency" (as that makes more sense in the context), using for instead of forEach, and removing the typecasting. It's probably best to disable whitespace changes when viewing this PR to reduce some visual noise.

dependencyIds: Object.keys(module.dependencies).sort(),
});
});
Object.values(modulePartitions).forEach((partition) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is not strictly necessary to this PR, but it was a bit annoying that the boxes in each level were not sorted by filename, so this change does that.

existingDependencyModule.dependents.push(currentModule);
}

if (
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only part that's truly new in this file is this line, where, as we are iterating through the dependency of a given module, we make sure to associate that dependency with the module itself. This allows us to look up the dependencies later when we are rendering the visualization.

return { ...existingBoxRectsById, [id]: boxRect };
});
};
const toggleConnectionsFor = (id: string) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function does what it says — it either shows or hides the connections for a box that you click on.

);
}

export default function Connections({ activeBox }: { activeBox: BoxModel }) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While the boxes are rendered on screen using one div per box, the connection lines for a particular box are drawn all in one element using SVG.

If you're thinking about converting a file to TypeScript, you might want
to know what files import that file and what files it imports. This
commit makes it so that if you click on a box in the visualization, it
will draw lines between that box and the other boxes which represent its
dependents and dependencies.
@mcmire mcmire force-pushed the show-connections-in-ts-migration-dashboard branch from d865af9 to 5497640 Compare February 22, 2023 17:35
@mcmire mcmire marked this pull request as ready for review February 22, 2023 17:35
@metamaskbot
Copy link
Collaborator

Builds ready [5497640]
Page Load Metrics (1662 ± 44 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint99164121147
domContentLoaded1511187216388139
load1535187216629344
domInteractive1511187216388139
Bundle size diffs
  • background: 0 bytes
  • ui: 0 bytes
  • common: 0 bytes

@metamaskbot
Copy link
Collaborator

Builds ready [2c2daf9]
Page Load Metrics (1548 ± 56 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint972931294019
domContentLoaded12341760153212259
load12561761154811756
domInteractive12341760153212259
Bundle size diffs
  • background: 0 bytes
  • ui: 0 bytes
  • common: 0 bytes

@metamaskbot
Copy link
Collaborator

Builds ready [5fa26d1]
Page Load Metrics (1782 ± 55 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint104169132157
domContentLoaded15782008174510349
load15952008178211455
domInteractive15782008174510349
Bundle size diffs
  • background: 0 bytes
  • ui: 0 bytes
  • common: 0 bytes

@codecov
Copy link

codecov bot commented Mar 7, 2023

Codecov Report

Merging #17338 (b4d79f4) into develop (8ee6ece) will not change coverage.
The diff coverage is n/a.

❗ Current head b4d79f4 differs from pull request most recent head 97130c4. Consider uploading reports for the commit 97130c4 to get more accurate results

@@           Coverage Diff            @@
##           develop   #17338   +/-   ##
========================================
  Coverage    64.12%   64.12%           
========================================
  Files          912      912           
  Lines        35535    35535           
  Branches      9013     9013           
========================================
  Hits         22784    22784           
  Misses       12751    12751           

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@metamaskbot
Copy link
Collaborator

Builds ready [c0c22c0]
Page Load Metrics (1628 ± 68 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint97159126157
domContentLoaded13981915159913063
load14131916162814268
domInteractive13981915159913063
Bundle size diffs
  • background: 0 bytes
  • ui: 0 bytes
  • common: 0 bytes

@metamaskbot
Copy link
Collaborator

Builds ready [b4d79f4]
Page Load Metrics (1488 ± 21 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint9011610273
domContentLoaded1391158414694923
load1393158414884521
domInteractive1391158414694923
Bundle size diffs
  • background: 0 bytes
  • ui: 0 bytes
  • common: 0 bytes

Copy link
Contributor

@legobeat legobeat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when ico?

@metamaskbot
Copy link
Collaborator

Builds ready [50e7d63]
Page Load Metrics (1512 ± 34 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint90156108157
domContentLoaded1435164814985928
load1450172615127034
domInteractive1435164814985928
Bundle size diffs
  • background: 0 bytes
  • ui: 0 bytes
  • common: 0 bytes

@mcmire mcmire merged commit 3c622cd into develop Mar 17, 2023
@mcmire mcmire deleted the show-connections-in-ts-migration-dashboard branch March 17, 2023 02:03
@github-actions github-actions bot locked and limited conversation to collaborators Mar 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update TypeScript dashboard to show connections/dependencies between files
4 participants