Skip to content

[WIKI-181] refactor: invalid file handling#7139

Merged
sriramveeraghanta merged 1 commit intopreviewfrom
refactor/file-handling
May 30, 2025
Merged

[WIKI-181] refactor: invalid file handling#7139
sriramveeraghanta merged 1 commit intopreviewfrom
refactor/file-handling

Conversation

@aaryan610
Copy link
Member

@aaryan610 aaryan610 commented May 30, 2025

Description

This PR refactors the way editor handles invalid files by removing invalid file check from all places except the upload function.

Type of Change

  • Code refactoring

Summary by CodeRabbit

  • Bug Fixes

    • Improved file upload error handling by centralizing invalid file alerts and ensuring clearer feedback when unsupported files are uploaded.
    • Enhanced reliability of file uploads by handling cases where an upload URL may be missing.
  • Refactor

    • Simplified and streamlined the file upload process, removing redundant validation steps and unused parameters.
    • Standardized internal references for plugin settings to improve maintainability.

No changes to the user interface or component props.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented May 30, 2025

Walkthrough

The changes refactor file upload and validation logic by centralizing invalid file handling into a single callback, simplifying parameter usage in hooks and helper functions, and updating type signatures to support undefined upload URLs. Additionally, a string literal for transaction meta is replaced with a constant for consistency.

Changes

File(s) Change Summary
packages/editor/src/core/extensions/custom-image/components/image-uploader.tsx Centralized invalid file handling via a new handleInvalidFile callback; removed redundant parameters.
packages/editor/src/core/hooks/use-file-upload.ts Updated callback and function signatures; removed file validation from multi-file upload; improved error handling.
packages/editor/src/core/plugins/file/delete.ts Replaced string literal meta key with a constant reference for transaction meta handling.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CustomImageUploader
    participant useUploader
    participant useDropZone

    User->>CustomImageUploader: Select or drop file(s)
    CustomImageUploader->>useDropZone: Handle file input
    useDropZone->>CustomImageUploader: onFileChange callback
    CustomImageUploader->>useUploader: Upload file(s), pass handleInvalidFile
    useUploader-->>CustomImageUploader: Calls handleInvalidFile on error
    useUploader-->>CustomImageUploader: Returns uploaded file URL(s) or error
Loading

Possibly related PRs

  • makeplane/plane#7129: Modifies CustomImageUploader and useUploader to refactor invalid file handling and upload callbacks, directly related to the changes in image upload logic.

Suggested labels

ready to merge

Suggested reviewers

  • sriramveeraghanta
  • Palanikannan1437

Poem

A rabbit hopped through fields of code,
Tidying uploads on the road.
With errors caught in just one place,
And constants set with tidy grace,
The files now leap and never trip—
A hoppy, happy, bug-free trip! 🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🔭 Outside diff range comments (1)
packages/editor/src/core/hooks/use-file-upload.ts (1)

163-187: ⚠️ Potential issue

Critical: File validation completely removed from multi-file upload.

The removal of file validation from uploadFirstFileAndInsertRemaining could allow invalid files to be processed. While the PR aims to centralize validation, this function now uploads files without any validation checks.

This could lead to:

  • Invalid file types being uploaded
  • Files exceeding size limits being processed
  • No user feedback for invalid files in multi-file scenarios

Consider either:

  1. Ensuring the uploader callback performs validation (which it should based on the useUploader hook)
  2. Adding a comment to clarify that validation is handled by the uploader
  3. Pre-validating files before processing to provide immediate feedback
 export const uploadFirstFileAndInsertRemaining = async (args: TMultipleFileArgs) => {
   const { editor, filesList, pos, type, uploader } = args;
   const filesArray = Array.from(filesList);
   if (filesArray.length === 0) {
     console.error("No files found to upload.");
     return;
   }
 
+  // Note: File validation is performed within the uploader callback
   // Upload the first file
   const firstFile = filesArray[0];
   uploader(firstFile);
🧹 Nitpick comments (1)
packages/editor/src/core/extensions/custom-image/components/image-uploader.tsx (1)

89-91: Consider improving error notification UX.

Using alert() for error messages provides poor user experience. Consider using a toast notification system or inline error messages for better UX.

Example implementation with a toast system:

-  const handleInvalidFile = useCallback((_error: EFileError, _file: File, message: string) => {
-    alert(message);
-  }, []);
+  const handleInvalidFile = useCallback((error: EFileError, file: File, message: string) => {
+    // If a toast system is available in your app
+    // toast.error(message);
+    
+    // Or dispatch an error to a global error handler
+    console.error(`File upload error for ${file.name}: ${message}`, error);
+    
+    // For now, keeping alert as fallback
+    alert(message);
+  }, []);
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4a97d7c and a669f4c.

📒 Files selected for processing (3)
  • packages/editor/src/core/extensions/custom-image/components/image-uploader.tsx (2 hunks)
  • packages/editor/src/core/hooks/use-file-upload.ts (6 hunks)
  • packages/editor/src/core/plugins/file/delete.ts (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Analyze (javascript)
🔇 Additional comments (5)
packages/editor/src/core/plugins/file/delete.ts (1)

3-4: Good refactoring to use constants!

Replacing the hardcoded string with CORE_EDITOR_META.SKIP_FILE_DELETION improves maintainability and reduces the risk of typos.

Also applies to: 37-37

packages/editor/src/core/hooks/use-file-upload.ts (3)

10-18: Type signature updates align with centralized error handling.

The changes to allow undefined return from editorCommand and include the File object in onInvalidFile callback improve error handling capabilities.


33-76: Proper error handling for undefined upload URLs.

Good addition of explicit error handling when the upload command returns undefined. The error message is clear and the file object is correctly passed to the onInvalidFile callback.


98-141:

Details

✅ Verification successful

Verify file validation is still performed for dropped files.

The removal of acceptedMimeTypes, maxFileSize, and onInvalidFile parameters from useDropZone suggests that file validation has been centralized. However, ensure that dropped files are still validated during the upload process to prevent invalid files from being processed.

Run the following script to verify file validation is performed in the uploader:


🏁 Script executed:

#!/bin/bash
# Description: Check if file validation is still performed in the upload flow

# Search for isFileValid usage in the upload functions
rg -A 10 "isFileValid" packages/editor/src/core/hooks/use-file-upload.ts

# Check if the uploader callback passed to useDropZone performs validation
ast-grep --pattern 'uploadFile = $$$' packages/editor/src/core/hooks/use-file-upload.ts

Length of output: 861


Validation confirmed in uploader; no further changes needed

The isFileValid check in uploadFirstFileAndInsertRemaining (packages/editor/src/core/hooks/use-file-upload.ts) ensures dropped files are still validated against acceptedMimeTypes and maxFileSize. Invalid files are halted before upload.

packages/editor/src/core/extensions/custom-image/components/image-uploader.tsx (1)

105-110: Hook usage correctly updated to match new interfaces.

The removal of validation parameters from useDropZone and uploadFirstFileAndInsertRemaining calls properly aligns with the centralized validation approach in the upload flow.

Also applies to: 140-147

@aaryan610 aaryan610 changed the title refactor: invalid file handling [WIKI-181] refactor: invalid file handling May 30, 2025
@makeplane
Copy link

makeplane bot commented May 30, 2025

Pull Request Linked with Plane Work Items

Comment Automatically Generated by Plane

@sriramveeraghanta sriramveeraghanta merged commit 01b685e into preview May 30, 2025
5 of 6 checks passed
@sriramveeraghanta sriramveeraghanta deleted the refactor/file-handling branch May 30, 2025 12:48
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.

3 participants