feat: enhance ChartExample type with Controls#7239
Conversation
WalkthroughThe PR enhances the example components system by making Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 1 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Bundle ReportBundle size has no change ✅ |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@www/src/views/ExamplesView.tsx`:
- Around line 9-15: The lookup in parseExampleComponent uses the record key
(entries()[0]) instead of the documented example identity (the
ChartExample.name), which can mis-resolve URLs; update parseExampleComponent to
compare params.name to the ChartExample.name (the second element of each tuple)
rather than the record key: use allExamplesFlattened and parseExampleComponent
to destructure each tuple to (key, example) and match example.name (or
example?.name) against exampleName, returning the example object when it
matches.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 3cfd348a-abbf-4236-93b8-78af63de1b71
📒 Files selected for processing (4)
www/src/docs/exampleComponents/types.tswww/src/views/APIViewNew.tsxwww/src/views/ExamplesView.tsxwww/test/docs/exampleComponents/exampleComponents.spec.tsx
| const allExamplesFlattened: ReadonlyArray<[string, ChartExample]> = Object.values(allExamples) | ||
| .map(e => e.examples) | ||
| .flatMap(Object.entries); | ||
|
|
||
| if (res && res.length) { | ||
| const example = allExamples[res[0]].examples[compName]; | ||
| return { | ||
| cateName: res[0], | ||
| exampleName: example.name, | ||
| ExampleComponent: example.Component, | ||
| sourceCode: example.sourceCode, | ||
| description: example.description, | ||
| }; | ||
| } | ||
| return null; | ||
| const parseExampleComponent = (exampleName: string): ChartExample | undefined => { | ||
| return allExamplesFlattened.find(examples => examples[0] === exampleName)?.[1]; | ||
| }; |
There was a problem hiding this comment.
Lookup key changed from example name to record key (can break URL resolution).
The parser now matches params.name against Object.entries(...)[0] (record key). But URL identity is documented as example name, so valid example URLs can resolve to NotFoundView when key and example.name diverge.
🔧 Proposed fix
-const allExamplesFlattened: ReadonlyArray<[string, ChartExample]> = Object.values(allExamples)
- .map(e => e.examples)
- .flatMap(Object.entries);
+const allExamplesFlattened: ReadonlyArray<ChartExample> = Object.values(allExamples)
+ .flatMap(e => Object.values(e.examples));
const parseExampleComponent = (exampleName: string): ChartExample | undefined => {
- return allExamplesFlattened.find(examples => examples[0] === exampleName)?.[1];
+ return allExamplesFlattened.find(example => example.name === exampleName);
};📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| const allExamplesFlattened: ReadonlyArray<[string, ChartExample]> = Object.values(allExamples) | |
| .map(e => e.examples) | |
| .flatMap(Object.entries); | |
| if (res && res.length) { | |
| const example = allExamples[res[0]].examples[compName]; | |
| return { | |
| cateName: res[0], | |
| exampleName: example.name, | |
| ExampleComponent: example.Component, | |
| sourceCode: example.sourceCode, | |
| description: example.description, | |
| }; | |
| } | |
| return null; | |
| const parseExampleComponent = (exampleName: string): ChartExample | undefined => { | |
| return allExamplesFlattened.find(examples => examples[0] === exampleName)?.[1]; | |
| }; | |
| const allExamplesFlattened: ReadonlyArray<ChartExample> = Object.values(allExamples) | |
| .flatMap(e => Object.values(e.examples)); | |
| const parseExampleComponent = (exampleName: string): ChartExample | undefined => { | |
| return allExamplesFlattened.find(example => example.name === exampleName); | |
| }; |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@www/src/views/ExamplesView.tsx` around lines 9 - 15, The lookup in
parseExampleComponent uses the record key (entries()[0]) instead of the
documented example identity (the ChartExample.name), which can mis-resolve URLs;
update parseExampleComponent to compare params.name to the ChartExample.name
(the second element of each tuple) rather than the record key: use
allExamplesFlattened and parseExampleComponent to destructure each tuple to
(key, example) and match example.name (or example?.name) against exampleName,
returning the example object when it matches.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #7239 +/- ##
==========================================
- Coverage 89.07% 89.07% -0.01%
==========================================
Files 539 539
Lines 41014 41009 -5
Branches 5555 5555
==========================================
- Hits 36532 36527 -5
Misses 4474 4474
Partials 8 8 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Staging Deployment Details
These deployments will remain available for 30 days. To update snapshots: Comment |
I added support for controls to all website examples, not just the ones in the guide. Also refactored the ExamplesView a little bit while here.
AI description below
This pull request refactors and enhances the handling of example components in the documentation site. The changes improve type safety, enable more flexible controls for chart examples, and simplify the logic for looking up and rendering examples. Notably, it updates the
ChartExampletype to support associated controls, streamlines the example lookup process, and improves error handling for missing examples.Type and API improvements:
ChartExampletype to accept a genericControlsType, support an optionalControlscomponent for interactive knobs/controls, and use a more specificToolTypefor thedefaultToolproperty. [1] [2]Example lookup and rendering simplification:
ExamplesView.tsxto flatten all examples into a single array, simplifying and improving the performance of the lookup process.ExamplesView.tsxto use the new flattened example lookup, display aNotFoundViewif an example is missing, and pass the newControlsanddefaultToolprops toCodeEditorWithPreview. [1] [2]Component integration:
ApiExamplesandExamplesViewImplto pass the newControlsprop from theChartExampletype to theCodeEditorWithPreviewcomponent, enabling interactive controls in examples. [1] [2]Testing and documentation:
Summary by CodeRabbit
Release Notes
New Features
Refactor