Skip to content
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

Fix/ext dapp account bug #6412

Merged
merged 6 commits into from
Dec 26, 2024
Merged

Fix/ext dapp account bug #6412

merged 6 commits into from
Dec 26, 2024

Conversation

sidmorizon
Copy link
Contributor

@sidmorizon sidmorizon commented Dec 25, 2024

Summary by CodeRabbit

  • 新功能

    • 引入了用于连接以太坊钱包的演示界面 dappTest.html
    • 增加了 syncSceneData 函数,以增强账户选择逻辑。
    • AccountSelectorActions 中添加了 getCurrentSceneInfo 方法。
  • bug修复

    • 改进了事件发射过程的错误处理。
  • 文档

    • 更新了调试日志的格式以增强可读性。
  • 改进

    • 通过引入节流机制优化了账户更改处理性能。

(cherry picked from commit d8f050db1b3457ca0e273430abe880be0c1753c2)
Copy link

codesandbox bot commented Dec 25, 2024

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

Copy link
Contributor

coderabbitai bot commented Dec 25, 2024

总览

代码变更概述

这次代码变更涉及多个文件,主要集中在账户选择器、事件总线和调试工具等模块。变更包括优化账户同步逻辑、改进事件处理机制、调整网络选择策略,以及增强调试输出格式。

变更详情

代码变更

文件路径 变更摘要
packages/kit-bg/src/dbs/simple/entity/SimpleDbEntityAccountSelector.ts 使用 lodashcloneDeep 函数深拷贝选中的账户对象
packages/kit-bg/src/providers/ProviderApiEthereum.ts 新增 errorUtils 导入,为未来错误处理做准备
packages/kit/src/components/AccountSelector/AccountSelectorEffects.tsx 新增 syncSceneData 函数,处理特定场景下的账户选择逻辑
packages/kit/src/components/AccountSelector/hooks/useAutoSelectNetwork.tsx 根据场景名称调整网络选择策略
packages/kit/src/states/jotai/contexts/accountSelector/actions.tsx 新增 withNetworkSync 参数,优化账户同步机制
packages/kit/src/views/DAppConnection/hooks/useHandleAccountChanged.ts 从防抖改为节流,优化账户变更处理
packages/shared/src/eventBus/appEventBus.ts 增强事件总线的错误处理和事件属性管理
packages/shared/src/utils/debug/debugUtils.ts 改进调试日志输出格式

新增文件

  • packages/kit/src/views/DAppConnection/dappTest.html:用于测试以太坊钱包连接的 HTML 演示文件

序列图

对于账户选择器的变更,可以生成以下序列图:

sequenceDiagram
    participant User
    participant AccountSelector
    participant EventBus
    participant NetworkSelector

    User->>AccountSelector: 选择场景
    AccountSelector->>EventBus: 触发账户选择事件
    EventBus->>NetworkSelector: 同步网络信息
    NetworkSelector-->>AccountSelector: 返回网络配置
    AccountSelector->>User: 更新选中账户
Loading

这个序列图展示了新的账户选择流程,突出了场景、事件总线和网络选择器之间的交互。


🪧 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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. (Beta)
  • @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 or 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.

@sidmorizon sidmorizon marked this pull request as ready for review December 26, 2024 00:18
@sidmorizon sidmorizon enabled auto-merge (squash) December 26, 2024 00:19
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: 10

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 2bfc8fa and 2dbf6a6.

📒 Files selected for processing (10)
  • packages/kit-bg/src/dbs/simple/entity/SimpleDbEntityAccountSelector.ts (2 hunks)
  • packages/kit-bg/src/providers/ProviderApiEthereum.ts (1 hunks)
  • packages/kit/src/components/AccountSelector/AccountSelectorEffects.tsx (1 hunks)
  • packages/kit/src/components/AccountSelector/hooks/useAutoSelectNetwork.tsx (2 hunks)
  • packages/kit/src/states/jotai/contexts/accountSelector/actions.tsx (9 hunks)
  • packages/kit/src/views/DAppConnection/components/DAppAccountList/DAppAccountListItem.tsx (1 hunks)
  • packages/kit/src/views/DAppConnection/dappTest.html (1 hunks)
  • packages/kit/src/views/DAppConnection/hooks/useHandleAccountChanged.ts (3 hunks)
  • packages/shared/src/eventBus/appEventBus.ts (2 hunks)
  • packages/shared/src/utils/debug/debugUtils.ts (1 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
packages/shared/src/eventBus/appEventBus.ts

[error] 333-333: Avoid the delete operator which can impact performance.

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)

🔇 Additional comments (21)
packages/kit/src/views/DAppConnection/hooks/useHandleAccountChanged.ts (4)

3-4: 导入节流方法,替代原本的防抖。依赖看起来正常。


45-48: 用 ref 追踪最新状态。逻辑清晰,写法简洁。


Line range hint 50-71: 节流回调的设置合理。避免频繁调用,性能更好。


73-77: 在依赖更新时调用节流函数,流程简洁明了。

packages/shared/src/utils/debug/debugUtils.ts (1)

22-27: 使用 console.groupCollapsed 展开日志。可读性提升,排查更方便。

packages/kit/src/components/AccountSelector/hooks/useAutoSelectNetwork.tsx (6)

4-5: 新增 networkUtils 和 EAccountSelectorSceneName,方便网络逻辑处理。


9-9: 引入 sceneInfo,获取更多场景信息。


17-18: 分离 selectedAccount,提取 networkId,便于后续判断。


25-26: 通过 sceneInfo 获取 sceneName、sceneUrl,增强可读性。


60-67: 发现场景为 discover 且是 AllNetwork 时清空网络 ID,避免混淆。


83-91: 依赖中包含 sceneName,确保场景改动能及时触发逻辑更新。

packages/kit-bg/src/dbs/simple/entity/SimpleDbEntityAccountSelector.ts (2)

1-2: 通过 cloneDeep 避免共享引用,减少不必要的对象污染。


149-149: 返回深拷贝,外部修改不会影响原对象。安全性更高。

packages/kit/src/components/AccountSelector/AccountSelectorEffects.tsx (2)

233-261: 新增函数逻辑检查无误
此函数根据特定条件更新账号,并避免通过 eventEmitDisabled: true 造成循环触发事件。逻辑简洁可读,测试时需留意远程事件标记。


266-274: 事件监听与清理配置正确
注册与移除事件均有调用,确保组件卸载时正常清理。不易出现事件泄漏问题。

packages/shared/src/eventBus/appEventBus.ts (1)

385-393: 设置远程事件标记逻辑合理
此处对 payload.$$isRemoteEvent 的设定能区分本地事件与远程事件,但仍需留意空值判断,避免不必要的属性更新。

packages/kit/src/states/jotai/contexts/accountSelector/actions.tsx (5)

57-57: 网络工具导入可接受
networkUtils 在后续代码中用于网络判断逻辑,符合需求。


299-303: 新增方法获取场景信息
getCurrentSceneInfo 对获取场景数据有益,后续可考虑更多场景兼容性。


316-316: 使用场景信息以快速判定
此处通过 sceneInfo 优化判断和后续逻辑分支,让代码更具可读性。


365-371: 按场景自动修正推导类型
基于全局来源更新 deriveType,确保网络兼容一致。逻辑紧凑易读。


Line range hint 1396-1430: 场景同步函数扩展
syncFromScene 新增 withNetworkSync 可灵活控制网络同步。处理细节简洁,避免强制同步带来潜在的意外切换。

@sidmorizon sidmorizon merged commit 7ef94b3 into x Dec 26, 2024
8 checks passed
@sidmorizon sidmorizon deleted the fix/ext-dapp-account-bug branch December 26, 2024 06:40
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.

3 participants