-
-
Notifications
You must be signed in to change notification settings - Fork 10
Add utility function and tests to Figure.tsx #246
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
base: main
Are you sure you want to change the base?
Add utility function and tests to Figure.tsx #246
Conversation
Co-authored-by: andershaf <andershaf@gmail.com>
|
Cursor Agent can help with this pull request. Just |
|
@cursoragent CI is failing. Typecheck is failing |
|
Perfect! I've fixed the typecheck error. The issue was that I used the Fix AppliedI moved the "Export CSV" button from the invalid Changes made to
Verification✅ TypeScript typecheck: Passes with no errors The CSV export functionality is fully working and the CI typecheck should now pass! Learn more about Cursor Agents |
Co-authored-by: andershaf <andershaf@gmail.com>
|
/gemini review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces a CSV export functionality for plots, which is a great addition. The implementation includes a new utility module exportCsv.ts for handling the CSV conversion and download, and integrates this into the Figure.tsx component with a new 'Export CSV' button. The changes are well-supported by a comprehensive suite of unit tests for both the new utilities and the component modifications. Overall, this is a solid implementation. I have a few suggestions to enhance robustness and correct some documentation.
src/components/Figure.tsx
Outdated
|
|
||
| const handleExportCsv = () => { | ||
| if (plotConfig?.data1D) { | ||
| const filename = `${plotConfig.name.replace(/\s+/g, "-")}.csv`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current filename sanitization only replaces whitespace. If plotConfig.name contains other characters that are invalid in filenames (e.g., /, \, :, *, ?, ", <, >, |), it could cause issues when the user tries to save the file. It would be more robust to sanitize a wider range of special characters.
| const filename = `${plotConfig.name.replace(/\s+/g, "-")}.csv`; | |
| const filename = `${plotConfig.name.replace(/[\s/\\?%*:\"|<>]/g, "-")}.csv`; |
src/utils/exportCsv.ts
Outdated
| /** | ||
| * Converts plot data to CSV format | ||
| * @param data1D - The 1D plot data containing data rows and column labels | ||
| * @param filename - Optional filename for the downloaded CSV file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cursoragent please look at this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
src/utils/exportCsv.ts
Outdated
| document.body.appendChild(link); | ||
| link.click(); | ||
| document.body.removeChild(link); | ||
|
|
||
| // Clean up the URL object | ||
| URL.revokeObjectURL(url); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To improve robustness, it's good practice to wrap the DOM manipulation and resource cleanup in a try...finally block. This ensures that the temporary link is always removed from the DOM and URL.revokeObjectURL() is called to prevent memory leaks, even in the unlikely event that link.click() throws an error.
document.body.appendChild(link);
try {
link.click();
} finally {
document.body.removeChild(link);
URL.revokeObjectURL(url);
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cursoragent please look at this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect! I've addressed all three code review suggestions from Gemini:
Changes Made
1. Fixed JSDoc Documentation ✅
Removed the incorrect @param filename from plotDataToCsv function documentation, since that function doesn't accept a filename parameter.
2. Improved Filename Sanitization ✅
Updated the filename sanitization in Figure.tsx to replace all invalid filename characters:
- Before: Only replaced whitespace with
replace(/\s+/g, "-") - After: Replaces all invalid characters with
replace(/[\s/\\?%*:"|<>]/g, "-")
This now handles: /, \, :, *, ?, ", |, <, >, and whitespace
3. Added Error Handling ✅
Wrapped DOM manipulation in a try-finally block in exportPlotDataToCsv:
document.body.appendChild(link);
try {
link.click();
} finally {
document.body.removeChild(link);
URL.revokeObjectURL(url);
}This ensures proper cleanup even if link.click() throws an error, preventing memory leaks.
Verification
✅ All 170 tests passing (added 1 new test for invalid character sanitization)
✅ TypeScript typecheck: No errors
✅ Linter: No errors
The code is now more robust and follows best practices!
Learn more about Cursor Agents
Co-authored-by: andershaf <andershaf@gmail.com>
Add CSV export functionality to plots, allowing users to download plot data.