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

refactor: use compatible systemInfo util API #2995

Merged
merged 5 commits into from
Feb 18, 2025

Conversation

Alex-huxiyang
Copy link
Collaborator

@Alex-huxiyang Alex-huxiyang commented Feb 17, 2025

🤔 这个变动的性质是?

  • 新特性提交
  • 日常 bug 修复
  • 站点、文档改进
  • 演示代码改进
  • 组件样式/交互改进
  • TypeScript 定义更新
  • 包体积优化
  • 性能优化
  • 功能增强
  • 国际化改进
  • 重构
  • 代码风格优化
  • 测试用例
  • 分支合并
  • 其他改动(是关于什么的改动?)

🔗 相关 Issue

💡 需求背景和解决方案

☑️ 请求合并前的自查清单

⚠️ 请自检并全部勾选全部选项⚠️

  • 文档已补充或无须补充
  • 代码演示已提供或无须提供
  • TypeScript 定义已补充或无须补充
  • fork仓库代码是否为最新避免文件冲突
  • Files changed 没有 package.json lock 等无关文件

Summary by CodeRabbit

  • 新增功能
    • 引入了统一的设备与窗口信息获取工具,为拖拽、菜单、下拉刷新、粘性定位等多项功能提供一致的数据支持。
  • 重构
    • 更新了各组件获取系统信息的逻辑,采用更稳定高效的方法,确保界面在不同设备上的显示更为准确。

Copy link

coderabbitai bot commented Feb 17, 2025

Important

Review skipped

Review was skipped due to path filters

⛔ Files ignored due to path filters (2)
  • src/packages/calendar/__tests__/__snapshots__/calendar.spec.tsx.snap is excluded by !**/*.snap
  • src/packages/range/__test__/__snapshots__/range.spec.tsx.snap is excluded by !**/*.snap

CodeRabbit blocks several paths by default. You can override this behavior by explicitly including those paths in the path filters. For example, including **/dist/** will override the default block on the dist directory, by removing the pattern from both the lists.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

此次拉取请求对多个组件中获取系统信息的方式进行了调整。原先直接调用 Taro.getSystemInfoSync() 的方式被替换为统一使用工具函数 getWindowInfo(),部分组件中也改为异步调用。此外,下拉刷新逻辑中对设备信息的获取切换为使用 getDeviceInfo()。同时,新建了一个工具模块,提供统一的系统信息获取接口,包括 getDeviceInfogetWindowInfogetAppBaseInfo 函数以及 IDeviceInfo 接口。

Changes

文件(s) 变更摘要
src/packages/avatarcropper/avatarcropper.taro.tsx, src/packages/drag/.../demo4.tsx, src/packages/drag/drag.taro.tsx, src/packages/menuitem/.../menuitem.taro.tsx, src/packages/sticky/.../sticky.taro.tsx, src/packages/virtuallist/.../virtuallist.taro.tsx getWindowInfo() 替换原先的 Taro.getSystemInfoSync(),更新窗口/系统信息的获取及相关计算逻辑,其中部分组件改为异步调用。
src/packages/pulltorefresh/.../pulltorefresh.taro.tsx 在下拉刷新组件中,用 getDeviceInfo() 替换 Taro.getSystemInfoSync() 以获取设备信息。
src/utils/get-system-info.ts 新增工具模块,提供 getDeviceInfogetWindowInfogetAppBaseInfo 三个函数及 IDeviceInfo 接口,用于统一系统信息获取。

Sequence Diagram(s)

sequenceDiagram
    participant Comp as 组件
    participant Util as getWindowInfo()
    participant Taro as Taro API
    Comp->>Util: 请求窗口/系统信息
    Util->>Taro: 检查 API 支持 (Taro.canIUse)
    alt API 可用
        Util->>Taro: 调用 Taro.getWindowInfo()
    else 无支持
        Util->>Taro: 调用 Taro.getSystemInfoSync()
    end
    Taro-->>Util: 返回系统信息
    Util-->>Comp: 传回信息
Loading
sequenceDiagram
    participant Comp as 组件
    participant Util as getDeviceInfo()
    participant Taro as Taro API
    Comp->>Util: 请求设备信息
    Util->>Taro: 检查设备 API 支持 (Taro.canIUse)
    alt API 可用
        Util->>Taro: 调用 Taro.getDeviceInfo()
    else 无支持
        Util->>Taro: 调用 Taro.getSystemInfoSync()
    end
    Taro-->>Util: 返回设备信息
    Util-->>Comp: 传回设备信息
Loading

Possibly related PRs

Suggested reviewers

  • oasis-cloud

Poem

嗨,我是小兔,蹦蹦跳跳,
代码中换掉了老套路,
Taro.getSystemInfoSync 已成往事,
用上 getWindowInfo 多么妙不可言,
再加上新函数,系统信息齐全,🐰
我为这变更欢欣鼓舞,跃跃欲试!
祝代码飞扬,好运连连!


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • 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 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.

@github-actions github-actions bot added 2.x Target branch 2.x action:review This PR needs more reviews (less than 2 approvals) labels Feb 17, 2025
Copy link

@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: 2

🔭 Outside diff range comments (1)
src/packages/drag/drag.taro.tsx (1)

63-86: 🛠️ Refactor suggestion

建议完善异步函数的错误处理

当前异步函数缺少错误处理机制,且与定时器的交互可能存在问题。

 const getInfo = async () => {
+  try {
     const el = myDrag.current
     if (el) {
       const { top, left, bottom, right } = boundary
       const { screenWidth, windowHeight } = getWindowInfo()
 
       const {
         width,
         height,
         top: dragTop,
         left: dragLeft,
       } = await getRectByTaro(dragRef.current)
 
       setBoundaryState({
         top: -dragTop + top,
         left: -dragLeft + left,
         bottom: windowHeight - dragTop - bottom - Math.ceil(height),
         right: screenWidth - dragLeft - right - Math.ceil(width),
       })
 
       middleLine.current =
         screenWidth - width - dragLeft - (screenWidth - width) / 2
     }
+  } catch (error) {
+    console.error('获取拖拽元素信息失败:', error)
+    // 提供合理的降级处理
+  }
 }
🧹 Nitpick comments (4)
src/utils/get-system-info.ts (1)

3-4: 类型定义建议补充说明

建议为 IDeviceInfo 接口添加更详细的类型说明文档,解释为什么要排除 deviceAbiCPUType 属性。

+/**
+ * 设备信息接口,排除了在某些平台上不支持的属性
+ * @excludes deviceAbi - 设备指令集,仅在特定平台支持
+ * @excludes CPUType - CPU 类型,仅在特定平台支持
+ */
 interface IDeviceInfo
   extends Omit<Taro.getDeviceInfo.Result, 'deviceAbi' | 'CPUType'> {}
src/packages/sticky/sticky.taro.tsx (1)

127-128: 建议优化窗口高度获取方式

当前在渲染路径中直接调用 getWindowInfo(),可能导致不必要的重复计算。建议使用 useMemo 或将窗口高度保存在状态中。

+  const windowHeight = useMemo(() => getWindowInfo().windowHeight, [])
-  const windowHeight = getWindowInfo().windowHeight
   setFixed(windowHeight - threshold < curRootRect.bottom)
src/packages/drag/drag.taro.tsx (1)

142-150: 建议优化定时器与异步函数的交互

当前使用定时器延迟调用异步函数,建议改用 useEffect 的清理函数来处理异步操作的取消。

 useEffect(() => {
+  let mounted = true
   timer.current = window.setTimeout(() => {
-    getInfo()
+    if (mounted) {
+      getInfo().catch(error => {
+        console.error('初始化拖拽元素失败:', error)
+      })
+    }
   }, 300)
 
   return () => {
+    mounted = false
     clearTimeout(timer.current)
   }
 }, [])
src/packages/avatarcropper/avatarcropper.taro.tsx (1)

11-11: 优化系统信息获取方式

使用 getWindowInfo 工具函数获取系统信息,使代码更加规范和统一。

建议在注释中说明 systemInfo 包含的具体信息,以提高代码可读性。

-  const systemInfo = getWindowInfo()
+  // 获取窗口信息,包含 pixelRatio 和窗口尺寸
+  const systemInfo = getWindowInfo()

Also applies to: 122-122

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between d4533e4 and 7bced5d.

📒 Files selected for processing (8)
  • src/packages/avatarcropper/avatarcropper.taro.tsx (2 hunks)
  • src/packages/drag/demos/taro/demo4.tsx (1 hunks)
  • src/packages/drag/drag.taro.tsx (2 hunks)
  • src/packages/menuitem/menuitem.taro.tsx (4 hunks)
  • src/packages/pulltorefresh/pulltorefresh.taro.tsx (2 hunks)
  • src/packages/sticky/sticky.taro.tsx (2 hunks)
  • src/packages/virtuallist/virtuallist.taro.tsx (2 hunks)
  • src/utils/get-system-info.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build
🔇 Additional comments (4)
src/utils/get-system-info.ts (1)

18-22:

❓ Verification inconclusive

建议统一返回类型处理

getSystemInfoSync 返回的数据结构可能与新 API 不完全匹配,建议添加类型转换确保返回数据的一致性。

Also applies to: 27-31


🏁 Script executed:

#!/bin/bash
# 检查 Taro API 返回类型的差异
ast-grep --pattern 'interface $Type extends Taro.$_.Result'

Length of output: 61


请开发者重点确认 Taro API 返回值类型一致性

经过尝试使用自动脚本搜索代码库,没有获得明确的类型接口信息来直接验证 Taro.getWindowInfo()Taro.getSystemInfoSync() 的返回数据结构是否一致。因此请手动确认这两个 API 的返回值结构是否存在差异。

  • 如果存在结构不一致,请添加必要的类型转换或数据适配代码,确保统一返回类型。
  • 该问题同样适用于文件中 27-31 行代码。
src/packages/virtuallist/virtuallist.taro.tsx (1)

14-14: 系统信息获取方式的改进

使用 getWindowInfo 工具函数替代直接调用 Taro.getSystemInfoSync() 是一个很好的改进,它提供了更好的抽象和一致性。

Also applies to: 58-60

src/packages/pulltorefresh/pulltorefresh.taro.tsx (1)

12-12: 平台检测逻辑优化

使用 getDeviceInfo 工具函数替代直接调用系统 API 进行平台检测,提高了代码的可维护性。

Also applies to: 156-157

src/packages/menuitem/menuitem.taro.tsx (1)

16-16: 统一使用 getWindowInfo 获取窗口信息

将所有获取窗口高度的地方统一使用 getWindowInfo 工具函数,提高了代码的一致性和可维护性。

Also applies to: 110-110, 167-167, 181-181

src/utils/get-system-info.ts Show resolved Hide resolved
src/packages/drag/demos/taro/demo4.tsx Show resolved Hide resolved
@pull-request-size pull-request-size bot added size/L and removed size/M labels Feb 17, 2025
Copy link

codecov bot commented Feb 17, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 85.56%. Comparing base (d4533e4) to head (c095e0b).
Report is 1 commits behind head on next.

Additional details and impacted files
@@            Coverage Diff            @@
##           next    #2995       +/-   ##
=========================================
+ Coverage      0   85.56%   +85.56%     
=========================================
  Files         0      267      +267     
  Lines         0    18000    +18000     
  Branches      0     2726     +2726     
=========================================
+ Hits          0    15401    +15401     
- Misses        0     2596     +2596     
- Partials      0        3        +3     

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

@oasis-cloud oasis-cloud merged commit 6db53bc into jdf2e:next Feb 18, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.x Target branch 2.x action:review This PR needs more reviews (less than 2 approvals) size/L
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants