Skip to content

Conversation

adamwoodnz
Copy link
Contributor

@adamwoodnz adamwoodnz commented Aug 27, 2025

Fixes CHARTS-86: Charts: restructure shared/utils/hooks directories

Proposed changes:

No functional changes, only moves:

  • Moves everything out of components/shared which has become a bit of a dumping ground. All files and folders moved to either components, hooks or utils. Components by definition are shared/reusable, so they have all been moved into a flat structure.
  • Implements modern React conventions by moving hooks from utils to separate files prefixed with use-
  • Moves any tests to a nested test dir
  • Adds index files to hooks and utils for better devex, and updates imports
  • Renames use-chart-legend-data to use-chart-legend-items for accuracy
  • Renames things with global-chart- to global-charts- for consistency

Other information:

  • Have you written new tests for your changes, if applicable?
  • Have you checked the E2E test CI results, and verified that your changes do not break them?
  • Have you tested your changes on WordPress.com, if applicable (if so, you'll see a generated comment below with a script to run)?

Jetpack product discussion

Does this pull request change what data or activity we track or use?

Testing instructions:

  • Go to http://localhost:50240/?path=/docs/js-packages-charts and ensure the storybook runs
  • Smoke test the storybook for runtime errors due to misconfigured imports
  • Browse the source in your IDE or GitHub, and observe the flat file structure. Look for inconsistencies like tests instead of test, hooks not following naming conventions, etc.

@adamwoodnz adamwoodnz self-assigned this Aug 27, 2025
Copy link
Contributor

github-actions bot commented Aug 27, 2025

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WoA dev site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin (Jetpack), and enable the charts-86-charts-restructure-sharedutilshooks-directories branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack charts-86-charts-restructure-sharedutilshooks-directories

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

Copy link
Contributor

github-actions bot commented Aug 27, 2025

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Review, ...).
  • ✅ Add a "[Type]" label (Bug, Enhancement, Janitorial, Task).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!

@github-actions github-actions bot added the [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. label Aug 27, 2025
Copy link

jp-launch-control bot commented Aug 27, 2025

Code Coverage Summary

Coverage changed in 1 file.

File Coverage Δ% Δ Uncovered
projects/js-packages/charts/src/providers/chart-context/global-charts-provider.tsx 32/32 (100.00%) 0.00% 0 💚

43 files are newly checked for coverage. Only the first 5 are listed here.

File Coverage
projects/js-packages/charts/src/utils/format-metric-value.ts 8/13 (61.54%) 💚
projects/js-packages/charts/src/components/line-chart/private/line-chart-annotation-label-popover.tsx 16/24 (66.67%) 💚
projects/js-packages/charts/src/components/private/chart-composition/chart-html.tsx 2/3 (66.67%) 💚
projects/js-packages/charts/src/components/private/chart-composition/chart-svg.tsx 2/3 (66.67%) 💚
projects/js-packages/charts/src/components/legend/hooks/use-chart-legend-items.ts 23/29 (79.31%) 💚

Full summary · PHP report · JS report

* @param options.customArgTypes - Override default legend positioning argTypes if needed
* @return Object containing story meta and story objects for legend positioning
*/
export function createLegendStories< T extends Record< string, unknown > >(
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 isn't used

@@ -23,10 +23,3 @@ export const getLongestTickWidth = < T extends AnyD3Scale >(

return getStringWidth( longestTick, labelStyle );
};

export const isSafari = () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved to it's own file

@adamwoodnz adamwoodnz changed the title Reorganise shared, hooks amd utils Reorganise shared, hooks and utils Aug 27, 2025
@adamwoodnz adamwoodnz force-pushed the charts-86-charts-restructure-sharedutilshooks-directories branch from d4c2573 to e8c4078 Compare August 27, 2025 22:02
@adamwoodnz adamwoodnz added [Type] Task and removed [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. labels Aug 27, 2025
@adamwoodnz adamwoodnz force-pushed the charts-86-charts-restructure-sharedutilshooks-directories branch 2 times, most recently from e8c4078 to 74fc7f2 Compare August 28, 2025 00:06
Base automatically changed from charts-84-charts-stable-colors-for-groups to trunk August 28, 2025 21:17
@adamwoodnz adamwoodnz force-pushed the charts-86-charts-restructure-sharedutilshooks-directories branch from 271320e to 7fd6892 Compare August 28, 2025 21:32
@adamwoodnz adamwoodnz force-pushed the charts-86-charts-restructure-sharedutilshooks-directories branch 2 times, most recently from 36a2b06 to bf9b891 Compare August 29, 2025 04:51
Comment on lines -23 to -40
// Utilities
export { mergeThemes } from './utils/merge-themes';

// Hooks
export { useDeepMemo, useGlobalChartTheme, useChartMouseHandler, useXYChartTheme } from './hooks';

// LeaderboardChart utilities
export { formatMetricValue } from './components/leaderboard-chart';

// Types
export type * from './types';
export type * from './visx/types';

export type { LineStyles, GridStyles, EventHandlerParams } from '@visx/xychart';

export type { RenderLineStartGlyphProps } from './components/line-chart/line-chart';
export type { LegendProps, BaseLegendProps, ChartLegendOptions } from './components/legend';
export type { ConversionFunnelChartProps, FunnelStep } from './components/conversion-funnel-chart';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

All exported from their respective component indexes

@adamwoodnz adamwoodnz force-pushed the charts-86-charts-restructure-sharedutilshooks-directories branch from bf9b891 to 2f422a8 Compare August 29, 2025 05:00
@adamwoodnz
Copy link
Contributor Author

adamwoodnz commented Aug 29, 2025

Todo:

  • Try installing build in woo-analytics and calypso to check imports
  • Move remaining line-chart types to the index
  • Document build warnings for discussion

@adamwoodnz adamwoodnz force-pushed the charts-86-charts-restructure-sharedutilshooks-directories branch from 1db8c86 to 4b27d18 Compare September 1, 2025 00:19
@adamwoodnz adamwoodnz force-pushed the charts-86-charts-restructure-sharedutilshooks-directories branch from 4b27d18 to 8a2d7c0 Compare September 1, 2025 00:31
Copy link
Contributor Author

@adamwoodnz adamwoodnz Sep 1, 2025

Choose a reason for hiding this comment

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

When running a build Rollup warns us about using these indexes:

(!) Export "useChartRegistration" of module "src/providers/chart-context/hooks/use-chart-registration.ts" was reexported through module "src/providers/chart-context/index.ts" while both modules are dependencies of each other and will end up in different chunks by current Rollup settings. This scenario is not well supported at the moment as it will produce a circular dependency between chunks and will likely lead to broken execution order.
Either change the import in "src/components/pie-chart/pie-chart.tsx" to point directly to the exporting module or do not use "output.preserveModules" to ensure these modules end up in the same chunk.
src/components/pie-chart/pie-chart.tsx

It sees them as circular dependencies, however Claude tells me this is a false positive:

Analysis
Based on my examination of the code, this is indeed a false positive warning. Here's why:

Dependency Flow Analysis
pie-chart.tsx imports useChartRegistration from src/providers/chart-context/index.ts
index.ts re-exports useChartRegistration from ./hooks/use-chart-registration
use-chart-registration.ts imports useGlobalChartsContext from ./use-global-charts-context
use-global-charts-context.ts imports GlobalChartsContext from ../global-charts-provider

Key Points:

  • No actual circular dependency exists - There's a clear one-way dependency flow:
    pie-chart.tsx → chart-context/index.ts → use-chart-registration.ts → use-global-charts-context.ts → global-charts-provider.tsx
  • No imports back to index.ts - None of the files in the chart-context directory import from the index.ts file, which would be required for a true circular dependency.
  • Rollup's preserveModules configuration - The warning is triggered by Rollup's preserveModules: true setting in your config (line 38 in rollup.config.mjs). This setting creates separate chunks for each module, and Rollup gets confused when:
    Module A exports something from Module B
    Module C imports from Module A (getting Module B's export)
    Rollup thinks this creates a circular dependency between chunks, even though the actual module dependency is linear

Why this happens:
The warning occurs because Rollup with preserveModules: true sees:
pie-chart.tsx imports from chart-context/index.ts
chart-context/index.ts re-exports from use-chart-registration.ts
Rollup puts these in different chunks and warns about potential execution order issues
However, the actual module resolution is straightforward and linear - there's no circular reference in the JavaScript module graph itself.

Conclusion:
This is a false positive. The code structure is sound and there's no actual circular dependency. This is a known limitation of Rollup when using preserveModules with re-export patterns. The warning can be safely ignored, or you could suppress it by adding a specific warning filter to your Rollup config if it becomes bothersome.

Given it is only a warning and we are exporting all the files and not a bundle I'm inclined to ignore, what do you think @kangzj? We could suppress the warning as it suggests...

I much prefer the imports using indexes, rather than separate imports from each file:

import {
	useXYChartTheme,
	useChartDataTransform,
	useChartMargin,
	useElementHeight,
} from '../../hooks';

Copy link
Contributor

Choose a reason for hiding this comment

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

Given it is only a warning and we are exporting all the files and not a bundle I'm inclined to ignore, what do you think @kangzj? We could suppress the warning as it suggests...

I much prefer the imports using indexes, rather than separate imports from each file:

Sounds good to me! Happy to address it later as well.

@adamwoodnz adamwoodnz force-pushed the charts-86-charts-restructure-sharedutilshooks-directories branch from 8a2d7c0 to 8a67369 Compare September 1, 2025 00:48
Copy link
Contributor

@kangzj kangzj left a comment

Choose a reason for hiding this comment

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

Thanks for working on this! Much cleaner and clearer 👍

@kangzj kangzj merged commit 710c54f into trunk Sep 1, 2025
85 checks passed
@kangzj kangzj deleted the charts-86-charts-restructure-sharedutilshooks-directories branch September 1, 2025 01:14
@github-actions github-actions bot removed the [Status] Needs Review This PR is ready for review. label Sep 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants