Skip to content

Conversation

@arksia
Copy link

@arksia arksia commented Dec 1, 2025

修复多个上传组件同时粘贴上传会覆写 dataTransfer 中文件的 uid,导致文件状态更新异常的问题。
image
图中两个文件上传都失败了,但是第一个文件始终处于 uploading 状态

Summary by CodeRabbit

  • Bug Fixes

    • 改进文件上传处理:在上传流程中明确保留文件的名称、类型和修改时间,提升粘贴/拖拽场景下的元数据一致性。
    • 提升并发上传/粘贴场景的稳定性,防止不同上传实例间的冲突。
  • Tests

    • 新增并发粘贴场景测试,验证上传项的唯一标识在多个组件同时操作时稳定且互不干扰。

✏️ Tip: You can customize this high-level summary in your review settings.

@vercel
Copy link

vercel bot commented Dec 1, 2025

@arksia is attempting to deploy a commit to the React Component Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link

coderabbitai bot commented Dec 1, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Walkthrough

在粘贴/拖放文件处理路径中,AjaxUploader 将来自 dataTransfer.files 的每个文件显式重建为新的 File 实例;同时新增测试验证两个 Upload 组件同时粘贴时各自生成的 uid 不互相覆盖并使用 xhr 模拟响应完成流程。

Changes

Cohort / File(s) 更改摘要
文件重建与上传逻辑
src/AjaxUploader.tsx
onDataTransferFiles 中将 dataTransfer.files 的每个文件通过 File 构造函数重建为新的实例(保留 name、type、lastModified),并移除一处空行(无功能影响)。
并发上传测试覆盖
tests/uploader.spec.tsx
新增并发粘贴场景测试:渲染两个 Upload 组件、模拟粘贴产生文件、捕获并比较两侧的 uid(确保分别存在且互不相同),并通过 xhr 响应模拟服务器完成上传(用 requests[n].respond 代替直接调用回调)。

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 分钟

  • 重点检查 src/AjaxUploader.tsx 中 File 重建是否保留所有必要属性并不会破坏文件内容/引用语义(尤其在浏览器行为差异上)。
  • 校验 tests/uploader.spec.tsx 中使用的 xhr 响应模拟与测试隔离/时序是否可靠,确保并发断言不会产生假阳性/假阴性。

Possibly related PRs

Suggested reviewers

  • zombieJ

Poem

🐰 我是小兔子在代码林,
轻轻重建每个文件名,
粘贴瞬间不互侵,uid 各自真,
两个上传并肩行,测试敲响小鼓声,
跳着小步把改动送进主分支 🥕

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed PR标题准确总结了核心改动:修复多个上传组件同时粘贴时的uid冲突问题,与代码变更和PR目标完全对应。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 756b651 and 1aeb019.

📒 Files selected for processing (1)
  • tests/uploader.spec.tsx (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/uploader.spec.tsx

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist
Copy link

Summary of Changes

Hello @arksia, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求修复了一个关键错误,该错误影响了当多个组件同时处理粘贴文件时的文件上传功能。核心问题源于共享的文件引用导致 uid 冲突和错误的状态更新。解决方案涉及为每个上传操作创建独立的 File 对象,从而隔离它们的状态并确保正确的跟踪。此更改还附带了一个新的、专门的测试用例,以验证在同时粘贴场景下修复的有效性。

Highlights

  • 修复 UID 冲突: 解决了当多个上传组件同时粘贴上传文件时,dataTransfer 中的文件 uid 可能被覆盖,导致文件状态更新异常的问题。
  • 创建新的文件对象: 通过对 dataTransfer.files 进行映射,创建新的 File 对象实例,确保每个组件处理独立的文件实例,从而避免 uid 冲突。
  • 增强测试覆盖: 新增了一个测试用例,专门验证在多个上传组件同时处理粘贴事件时,文件 uid 不会被覆盖。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

你好,感谢你的贡献!这个修复通过为每个上传组件克隆文件对象,巧妙地解决了多个上传组件同时处理粘贴事件时可能出现的 uid 冲突问题。这是一个很棒的解决方案。我还添加了一些建议来改进代码和新添加的测试用例,使其更加健壮和简洁。请查看我的评论。

Comment on lines 98 to 103
let files: File[] = [...(dataTransfer.files || [])].map((file: File) => {
return new File([file], file.name, {
type: file.type,
lastModified: file.lastModified,
});
});

Choose a reason for hiding this comment

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

medium

为了代码更简洁,这里的 map 函数可以简化为单行表达式,使用隐式返回。

Suggested change
let files: File[] = [...(dataTransfer.files || [])].map((file: File) => {
return new File([file], file.name, {
type: file.type,
lastModified: file.lastModified,
});
});
let files: File[] = [...(dataTransfer.files || [])].map(file => (
new File([file], file.name, {
type: file.type,
lastModified: file.lastModified,
})
));

Comment on lines 640 to 641
await sleep(100);
requests[0].respond(200, {}, `["","${files[0].name}"]`);

Choose a reason for hiding this comment

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

medium

这个测试用例很好地覆盖了并发粘贴的场景。不过,它可以更健壮一些。目前它只完成了一个上传请求,并且没有验证两个组件生成的 uid 是唯一的。

我建议更新测试以:

  1. 断言创建了两个独立的请求。
  2. 断言 uid 是不同的。
  3. 完成两个上传请求,以确保两个组件的生命周期回调都得到测试。
      await sleep(100);

      expect(requests).toHaveLength(2);
      expect(uid1).toBeDefined();
      expect(uid2).toBeDefined();
      expect(uid1).not.toEqual(uid2);

      // 完成两个上传
      requests[0].respond(200, {}, `["","${files[0].name}"]`);
      requests[1].respond(200, {}, `["","${files[0].name}"]`);

@codecov
Copy link

codecov bot commented Dec 2, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.62%. Comparing base (c0188c8) to head (1aeb019).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #686      +/-   ##
==========================================
+ Coverage   89.58%   89.62%   +0.03%     
==========================================
  Files           6        6              
  Lines         317      318       +1     
  Branches       90       89       -1     
==========================================
+ Hits          284      285       +1     
  Misses         33       33              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@afc163
Copy link
Member

afc163 commented Dec 2, 2025

看下 gemini 的 review 建议

@arksia
Copy link
Author

arksia commented Dec 2, 2025

看下 gemini 的 review 建议

已根据 review 调整补充

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants