Skip to content

types: add types to useLayout #1269

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

Merged
merged 8 commits into from
Apr 21, 2025
Merged

Conversation

SonyLeo
Copy link
Contributor

@SonyLeo SonyLeo commented Mar 31, 2025

English | 简体中文

PR

PR Checklist

Please check if your PR fulfills the following requirements:

  • The commit message follows our Commit Message Guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)
  • Built its own designer, fully self-validated

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

Background and solution

What is the current behavior?

Issue Number: N/A

What is the new behavior?

useLayout 增加类型声明

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Summary by CodeRabbit

  • Refactor
    • Improved internal structure and type consistency to enhance overall system stability and reliability, contributing to a smoother user experience.
    • Added new TypeScript interfaces for better type safety in the plugin layout system.
    • Updated function signatures to include explicit parameter and return types, improving clarity and reducing potential errors.

Copy link
Contributor

coderabbitai bot commented Mar 31, 2025

Walkthrough

The changes introduce a significant expansion of TypeScript interfaces and type annotations within the useLayout.ts file. New interfaces define various entities related to the plugin layout system, while existing variables and functions have been updated to include explicit type definitions. Additionally, storage usage has been refined with the inclusion of generic types in useStorage calls. Error handling has been improved with explicit error messages. These modifications enhance type safety and clarity across the codebase without altering core logic or control flow.

Changes

File Change Summary
packages/layout/src/composable/useLayout.ts (Interfaces) Added interfaces: IMetaApi, IPluginPosition, IPluginState, IDimension, IPluginBase, IPlugins, ISettings, ILayoutState, IPluginStorageItem, IPluginStorage, IPlugin.
packages/layout/src/composable/useLayout.ts (Function Signatures) Updated function signatures with parameter and return type annotations for functions including getMoveDragBarState, changeMoveDragBarState, changeMenuShown, changeLeftFixedPanels, changeRightFixedPanels, getScale, getPluginState, getSettingState, getDimension, setDimension, activeSetting, closeSetting, activePlugin, closePlugin, getPluginWidth, changePluginWidth, getPluginByLayout, getPluginsByLayout, getPluginById, getPluginsByPosition, changePluginLayout, dragPluginLayout, isSameSide, getPluginShown, changePluginShown, isPanelWidthResizable, initPluginStorageReactive.
packages/layout/src/composable/useLayout.ts (Variables) Added explicit type annotations to constants PLUGIN_POSITION, pluginState, and layoutState.
packages/layout/src/composable/useLayout.ts (Storage Calls) Specified generic types in useStorage calls for stored values (e.g., useStorage<boolean>, useStorage<string[]>).
packages/layout/src/composable/useLayout.ts (Error Handling) Refined localStorage parsing logic to throw errors with explicit messages.

Suggested labels

refactor-main

Suggested reviewers

  • hexqi
  • gene9831

Poem

I'm a rabbit in a codey glen,
Hopping through types again and again,
Interfaces and generics leap up so high,
Annotations sparkle like stars in the sky,
With each type-safe hop, our code feels just right!
🐇💻

Tip

⚡💬 Agentic Chat (Pro Plan, General Availability)
  • We're introducing multi-step agentic chat in review comments and issue comments, within and outside of PR's. This feature enhances review and issue discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments and add commits to existing pull requests.

📜 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 21dd813 and 3f249b1.

📒 Files selected for processing (1)
  • packages/layout/src/composable/useLayout.ts (10 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/layout/src/composable/useLayout.ts
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: push-check

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.
    • 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.
  • @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

🧹 Nitpick comments (5)
packages/layout/src/composable/useLayout.ts (5)

75-77: Consider adding JSDoc comments for the PluginStorage interface

Since this is a key interface that represents the structure of plugin data in storage, adding JSDoc comments would help other developers understand its purpose and usage better.

+/**
+ * Interface representing plugin storage configuration.
+ * Each key represents a plugin name with its associated settings.
+ */
 interface PluginStorage {
   [key: string]: PluginStorageItem
 }

197-202: Consider adding a return type to the closeSetting function

While most functions have return type annotations, the closeSetting function is missing one. Consider adding the void return type for consistency.

-const closeSetting = (forceClose?: boolean) => {
+const closeSetting = (forceClose?: boolean): void => {
   const { settings } = layoutState
   if (!settings.fixedPanels.includes(settings.render) || forceClose) {
     settings.render = null
   }
 }

205-215: Add a return type annotation to the activePlugin function

The activePlugin function is missing a return type annotation. Since it returns a Promise, you should add the appropriate return type.

-const activePlugin = (name: string, noActiveRender: boolean) => {
+const activePlugin = (name: string, noActiveRender: boolean): Promise<any> => {
   const { plugins } = layoutState

   if (!noActiveRender) {
     plugins.render = name
   }

   return new Promise((resolve) => {
     nextTick(() => resolve(getMetaApi(name)))
   })
 }

218-223: Add a return type annotation to the closePlugin function

The closePlugin function is missing a return type annotation. For consistency, add the void return type.

-const closePlugin = (forceClose?: boolean) => {
+const closePlugin = (forceClose?: boolean): void => {
   const { plugins } = layoutState
   if (!plugins.fixedPanels.includes(plugins.render) || forceClose) {
     plugins.render = null
   }
 }

227-240: Add type annotation to getPluginWidth function's return value

While most functions have return type annotations, the type for the function's return value is missing in the implementation at line 242.

-const getPluginWidth = (name: string) => pluginStorageReactive.value[name]?.width || PLUGIN_DEFAULT_WIDTH
+const getPluginWidth = (name: string): number => pluginStorageReactive.value[name]?.width || PLUGIN_DEFAULT_WIDTH
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between f3ed925 and 8d7b905.

📒 Files selected for processing (1)
  • packages/layout/src/composable/useLayout.ts (14 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: push-check
🔇 Additional comments (17)
packages/layout/src/composable/useLayout.ts (17)

20-78: Well-structured interface definitions

These newly added TypeScript interfaces provide clear type definitions for the plugin layout system, improving code readability and type safety. The interfaces follow TypeScript best practices with appropriate property types.


79-86: LGTM - Type annotation for PLUGIN_POSITION

The type annotation for PLUGIN_POSITION correctly implements the PluginPosition interface.


88-90: LGTM - Type annotation for pluginState

The reactive pluginState object now has proper type annotation.


92-122: LGTM - Type annotation for layoutState

The layoutState object now has proper type annotation that matches its structure.


124-130: LGTM - Return type annotation for functions

The getMoveDragBarState and changeMoveDragBarState functions now have proper return type annotations.


132-149: Well-typed storage usage with generics

The useStorage calls now include generic types, which improves type safety when accessing these stored values.


151-161: LGTM - Type annotations for panel management functions

The functions for managing fixed panels now have proper parameter and return type annotations.


163-172: LGTM - Type annotations for dimension functions

The getScale, getPluginState, getSettingState, getDimension, and setDimension functions now have proper parameter and return type annotations.


175-186: LGTM - Type annotation for activeSetting function

The activeSetting function now has a proper parameter type annotation.


245-256: LGTM - Type annotations for changePluginWidth

The changePluginWidth function now has proper parameter type annotations.


272-283: Well-defined Plugin interface and related functions

The Plugin interface and getPluginById/getPluginsByPosition functions are now properly typed, improving type safety when dealing with plugin data.


286-290: LGTM - Type annotations for changePluginLayout

The changePluginLayout function now has proper parameter and return type annotations.


300-340: LGTM - Type annotations for dragPluginLayout

The dragPluginLayout function now has proper parameter and return type annotations.


343-351: LGTM - Type annotations for isSameSide

The isSameSide function now has proper parameter and return type annotations.


354-362: LGTM - Type annotations for plugin visibility functions

The getPluginShown and changePluginShown functions now have proper parameter and return type annotations.


369-374: LGTM - Type annotations for remaining functions

The isPanelWidthResizable and initPluginStorageReactive functions now have proper parameter and return type annotations.


20-415:

❓ Verification inconclusive

Verify type consistency across the codebase

The types added to this file are robust and appropriate. However, ensure that consuming code that calls these functions is updated to align with the new type requirements, especially for functions that now have more specific parameter types like changeMenuShown which requires 'left' | 'right'.


🏁 Script executed:

#!/bin/bash
# Search for calls to changeMenuShown with arguments that might not match the new type requirements
rg "changeMenuShown\(.*[^'\"](left|right)[^'\"].*\)" --type ts

Length of output: 68


Ensure all invocations of changeMenuShown strictly pass 'left' or 'right' as arguments.
Our automated search in the codebase for calls to changeMenuShown that might violate its parameter type did not reveal any instances of misuse. However, because the script output was inconclusive, please manually verify that all consuming code correctly uses the literal 'left' or 'right' values in accordance with the updated type requirements.

  • Verify that everywhere changeMenuShown is called, the argument is explicitly a 'left' or 'right' literal.
  • Confirm that any variables passed to it are properly typed to only allow these two values.

@gene9831
Copy link
Collaborator

gene9831 commented Apr 1, 2025

image vscode里面提示有20多个错误,应该有办法降低错误数量

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


image There are more than 20 errors in vscode, so there should be a way to reduce the number of errors

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

🧹 Nitpick comments (4)
packages/layout/src/composable/useLayout.ts (4)

21-23: The PluginNameType definition is a good start but could be more specific.

Using typeof PLUGIN_NAME correctly captures the type of the PLUGIN_NAME constant. However, consider using a more specific type if the structure of PLUGIN_NAME is known in advance, as this would provide better autocompletion support.


24-27: IMetaApi interface is too generic.

The IMetaApi interface is defined with an index signature that allows any property with any type. This provides very little type safety. Consider defining a more specific structure if the shape of the API is known.

interface IMetaApi {
-  [key: string]: any
+  // Add specific properties that are known to exist
+  // For example:
+  // getPlugin?: (name: string) => IPlugin
+  // Or at least constrain the value type if possible
+  [key: string]: Function | object | string | number | boolean
}

76-76: Use specific type instead of 'any' for pageStatus.

Using any for pageStatus reduces type safety. Consider using a more specific type, such as a string union type based on the available page statuses in constants.

-  pageStatus: any
+  pageStatus: typeof PAGE_STATUS[keyof typeof PAGE_STATUS] | string

97-97: Avoid using index signature with 'any' type in IPlugin.

The index signature [key: string]: any in the IPlugin interface reduces type safety. Consider either removing it if all properties are known, or constraining the value type.

interface IPlugin {
  id: string
  componentName?: string
  name?: string
  label?: string
-  [key: string]: any
+  // Add more specific properties or constrain the value type
+  [key: string]: string | number | boolean | undefined | object
}
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 8d7b905 and e691b2d.

📒 Files selected for processing (1)
  • packages/layout/src/composable/useLayout.ts (10 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: push-check
🔇 Additional comments (22)
packages/layout/src/composable/useLayout.ts (22)

14-14: Good addition of the RemovableRef import type.

Adding the type import for RemovableRef from @vueuse/core is a good practice for TypeScript, as it enhances type safety for the storage references used in the file.


196-198: Good usage of generic type parameters with useStorage.

Using generic type parameters with useStorage is a good practice that enhances type safety and provides better IDE support. This ensures that the stored values are correctly typed.


212-214: Good usage of generic type parameters with useStorage for array types.

Properly typing the arrays stored in localStorage using useStorage<string[]> ensures type safety and provides better IDE support.


300-304: Improved error handling with TypeScript instanceof check.

The improved error handling with a TypeScript instanceof check is a good addition. It provides more specific error handling and better error messages.


306-306: Good use of generic type for pluginStorageReactive.

Using the generic type IPluginStorage with useStorage ensures type safety for the plugin storage object.


315-317: Good conditional assignment for optional offset parameter.

The proper handling of the optional offset parameter with an undefined check is a good improvement.


103-141: Well-structured interface for the return type.

The IUseLayoutReturn interface is well-structured and provides a clear contract for the function's return value. This improves code maintainability and documentation.


143-150: Good annotation of PLUGIN_POSITION with interface type.

Adding the IPluginPosition type to the PLUGIN_POSITION constant improves type safety and documentation.


152-154: Good use of reactive with type annotation.

Using the IPluginState interface with reactive() ensures type safety for the reactive object.


156-186: Good use of ILayoutState interface with reactive.

The layoutState object is properly typed with the ILayoutState interface, which improves type safety and documentation.


188-194: Good addition of return types to getter and setter functions.

Adding proper return type annotations to the getter and setter functions improves code readability and type safety.


199-210: Good typing of menuName parameter with union type.

Using the union type 'left' | 'right' for the menuName parameter restricts the possible values to just these two options, which improves type safety.


215-225: Good addition of parameter and return types to panel manipulation functions.

Adding proper parameter and return type annotations to the changeLeftFixedPanels and changeRightFixedPanels functions improves code readability and type safety.


227-232: Good type annotations for getter functions.

Adding proper return type annotations to the getter functions improves code readability and type safety.


234-236: Good usage of Partial type for setDimension.

Using Partial for the setDimension function parameter allows for updating only a subset of the dimension properties, which is a good TypeScript practice.


291-304: Properly typed default export function with return type.

Adding the IUseLayoutReturn return type to the default export function improves type safety and documentation.


337-339: Well-typed getPluginById function.

The getPluginById function is now properly typed with parameter and return types, which improves type safety and documentation.


341-343: Good typing for getPluginsByPosition function.

The getPluginsByPosition function now has proper parameter and return types, including an array that may contain undefined values.


360-400: Good typing for dragPluginLayout function.

The dragPluginLayout function, which is complex, now has proper parameter and return types, which improves type safety and documentation.


403-411: Well-typed isSameSide function.

The isSameSide function now has proper parameter and return types, which improves type safety and documentation.


414-422: Typed getPluginShown and changePluginShown functions.

These functions now have proper parameter and return types, which improves type safety and documentation.


429-434: Properly typed utility functions.

The isPanelWidthResizable and initPluginStorageReactive functions now have proper parameter and return types, which improves type safety and documentation.

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

🧹 Nitpick comments (5)
packages/layout/src/composable/useLayout.ts (5)

261-266: Consider adding return type to closeSetting function.

While most functions have been properly typed, closeSetting is missing a return type annotation.

-const closeSetting = (forceClose?: boolean) => {
+const closeSetting = (forceClose?: boolean): void => {
  const { settings } = layoutState
  if (!settings.fixedPanels.includes(settings.render) || forceClose) {
    settings.render = ''
  }
}

282-287: Consider adding return type to closePlugin function.

While most functions have been properly typed, closePlugin is missing a return type annotation.

-const closePlugin = (forceClose?: boolean) => {
+const closePlugin = (forceClose?: boolean): void => {
  const { plugins } = layoutState
  if (!plugins.fixedPanels.includes(plugins.render) || forceClose) {
    plugins.render = ''
  }
}

289-289: Add return type to isEmptyPage function.

The isEmptyPage function should explicitly return a boolean type.

-const isEmptyPage = () => layoutState.pageStatus?.state === PAGE_STATUS.Empty
+const isEmptyPage = (): boolean => layoutState.pageStatus?.state === PAGE_STATUS.Empty

255-259: Consider adding type annotation to getFixedPanelsStatus function.

While most functions have been properly typed, getFixedPanelsStatus is missing return type annotation.

-const getFixedPanelsStatus = () => {
+const getFixedPanelsStatus = (): IFixedPanelsStatus => {
  const leftPanelFixed = leftFixedPanelsStorage.value.includes(layoutState.plugins.render)
  const rightPanelFixed = rightFixedPanelsStorage.value.includes(layoutState.settings.render)
  return { leftPanelFixed, rightPanelFixed }
}

346-350: Consider adding proper return type to changePluginLayout.

While most functions have been properly typed, changePluginLayout should explicitly return void.

-const changePluginLayout = (name: string, layout: string): void => {
+const changePluginLayout = (name: string, layout: string): void => {
  if (pluginStorageReactive.value[name]) {
    pluginStorageReactive.value[name].align = layout
  }
}
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between e691b2d and 6689683.

📒 Files selected for processing (1)
  • packages/layout/src/composable/useLayout.ts (10 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: push-check
🔇 Additional comments (18)
packages/layout/src/composable/useLayout.ts (18)

14-14: Good addition of RemovableRef type import.

Adding the RemovableRef type import from @vueuse/core ensures proper typing for the storage refs used throughout the file.


21-141: Well-structured type definitions improve code quality.

The comprehensive type definitions added here provide excellent structure and clarity to the codebase. The interfaces appropriately model the various aspects of the layout system, making the code more maintainable and enabling better IDE support with autocomplete and type checking.


143-143: Good type annotation for PLUGIN_POSITION constant.

Adding the IPluginPosition type to the PLUGIN_POSITION constant ensures type safety when accessing its properties.


152-154: Good type annotation for pluginState reactive object.

The IPluginState type annotation on the pluginState reactive object enhances type safety.


156-186: Proper typing for layoutState reactive object.

The ILayoutState type annotation on the complex layoutState reactive object improves type safety and provides better documentation of the object structure.


188-194: Well-typed getter and setter functions for MoveDragBarState.

Adding boolean return type to getMoveDragBarState and boolean parameter type to changeMoveDragBarState improves type safety.


196-197: Good use of generic types for storage hooks.

Using generic types with useStorage hooks (useStorage, useStorage<string[]>) ensures type safety for the stored values.


199-210: Properly typed changeMenuShown function.

The MenuType union type ('left' | 'right') for the menuName parameter ensures that only valid values can be passed.


212-213: Good use of generic types for fixed panels storage.

Using generic types with useStorage hooks for the fixedPanels arrays ensures type safety.


215-219: Well-typed panel modification functions.

Adding proper parameter and return types to changeLeftFixedPanels and changeRightFixedPanels makes the API more predictable.

Also applies to: 221-225


227-236: Clear return types for getter and setter functions.

Adding proper return types to getScale, getPluginState, getSettingState, getDimension, and parameter types to setDimension improves code clarity.


269-279: Proper Promise typing for activePlugin.

The Promise return type is now properly typed with IMetaApi, ensuring type safety for consumers of this function.


291-304: Improved error handling with type checking.

Good enhancement to the error handling with proper type checking using the instanceof Error check and more specific error messages.


306-306: Good use of generic type for plugin storage.

Using the generic type with useStorage hook for pluginStorageReactive ensures type safety.


312-321: Fixed object assignment in changePluginWidth.

The implementation now correctly initializes a new object with required properties when the plugin name doesn't exist in storage.


337-343: Well-typed utility functions for plugin retrieval.

The getPluginById and getPluginsByPosition functions now have proper parameter and return type annotations, making the API more predictable.


360-400: Thorough type annotations for dragPluginLayout function.

The complex dragPluginLayout function now has proper parameter types, improving code clarity and helping prevent potential bugs.


417-422: Fixed initialization in changePluginShown function.

Now properly initializes a new plugin object with required properties when it doesn't exist in storage.

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

🔭 Outside diff range comments (1)
packages/layout/src/composable/useLayout.ts (1)

119-119: ⚠️ Potential issue

Property pluginEvent not defined in IPlugins interface

The layoutState.plugins object contains a pluginEvent property, but this property is not defined in the IPlugins interface. This inconsistency could lead to type errors.

interface IPlugins {
  isShow: boolean
  fixedPanels: string[]
  render: string
  activating: boolean
  showDesignSettings: boolean
+  pluginEvent: 'all' | string
}
🧹 Nitpick comments (4)
packages/layout/src/composable/useLayout.ts (4)

20-23: IMetaApi interface is overly permissive

This interface uses [key: string]: any which provides minimal type safety. Consider defining more specific properties if known, or adding documentation explaining why this generic structure is necessary.

// MetaApi 类型定义
interface IMetaApi {
  [key: string]: any
+  // TODO: Add specific known properties to improve type safety
}

34-36: Consider using more specific union type for pluginEvent

Using 'all' | string is functionally equivalent to just string since it allows any string value. If there are specific valid values for pluginEvent, consider listing them explicitly in the union.

interface IPluginState {
-  pluginEvent: 'all' | string
+  pluginEvent: 'all' | 'specific1' | 'specific2' | string
}

70-77: Remove redundant undefined in optional properties

The ? operator already indicates that a property can be undefined, so adding | undefined is redundant.

interface IPluginStorageItem {
-  width?: number | undefined
+  width?: number
  offset?: number
  align?: string
  index: number
  isShow?: boolean
  widthResizable?: boolean
}

175-175: Inconsistent return type annotation style

The getScale function uses arrow function shorthand for return type, while other functions use explicit type annotations. For consistency, consider using the same style as other functions.

-const getScale = (): number => layoutState.dimension.scale
+const getScale = (): number => {
+  return layoutState.dimension.scale
+}
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 6689683 and 52e9559.

📒 Files selected for processing (1)
  • packages/layout/src/composable/useLayout.ts (10 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: push-check
🔇 Additional comments (5)
packages/layout/src/composable/useLayout.ts (5)

248-251: Improved error handling with type checking

Good use of type checking to handle errors properly. This ensures errors are propagated with appropriate messages.


267-267: Object creation with default values is now correct

This implementation properly sets default values when creating a new plugin storage entry, addressing the issue mentioned in a previous review.


367-367: Object creation with default values is now correct

This implementation properly sets default values when creating a new plugin storage entry, addressing the issue mentioned in a previous review.


289-291: Good use of union type for potential undefined returns

The function correctly types the return value as an array that may contain undefined elements (IPlugin | undefined)[], which accurately represents the function's behavior.


384-422:

❌ Incorrect review comment

Need explicit return type for object properties

The return object contains many methods and properties without type declarations. Consider adding type annotations to each method in the return object for maximum type safety.


🌐 Web query:

Is it necessary to declare types for properties in a return object in TypeScript, or will TypeScript infer these types correctly?

💡 Result:

In TypeScript, explicit type annotations for return object properties are generally not necessary because the compiler can accurately infer their types from the returned values. However, there are scenarios where explicit typing becomes beneficial or necessary.

Key Inference Capabilities

  1. Automatic type inference
    TypeScript infers object property types based on initial values:

    function getUser() {
      return {
        id: 1,          // inferred as number
        name: "Alice"   // inferred as string
      };                // Return type: { id: number; name: string }
    }
  2. Const assertions
    For literal values, use as const to preserve exact types:

    function getStatus() {
      return { code: 200, message: "OK" } as const;
    }
    // Inferred return type: { readonly code: 200; readonly message: "OK" }

When Explicit Typing Helps

Scenario Example Benefit
Multiple return branches function parse(input: string): { success: boolean; data?: string } { ... } Ensures consistent type across code paths [3][11]
Library development export function createConfig(): Readonly<Config> { ... } Improves declaration file generation [3]
Complex transformations type Transformed<T> = { [K in keyof T]: T[K] extends string ? Date : T[K] } Maintains intentional type relationships [4][13]
Documentation clarity interface User { id: number; name: string }
function getUser(): User { ... }
Acts as built-in documentation [9][12]

Inference Limitations

  • Mutable variables may widen types unintentionally:

    function getCounter() {
      const count = 0;  // inferred as number
      return { count, increment: () => count++ };
    }
    // count remains number, but explicit annotation could enforce stricter numeric subtypes
  • Empty objects require explicit typing:

    function createWidget(): Widget {
      return {};  // Error without Widget interface definition
    }

For most application code, relying on type inference is preferred to reduce boilerplate while maintaining type safety. Explicit return type annotations become valuable in libraries, complex functions, or when enforcing specific contracts across teams[3][6].

Citations:


Explicit Return Types Are Optional Here

Based on TypeScript’s strong type inference, the code in packages/layout/src/composable/useLayout.ts (lines 384–422) correctly infers all object property types from their implementations. Explicit return type annotations aren’t necessary unless you need a clear contract for external consumers or additional documentation. If this module is internal or its API is clear through inference, the current approach is acceptable.

Likely an incorrect or invalid review comment.

@gene9831
Copy link
Collaborator

gene9831 commented Apr 1, 2025

image packages/layout/src/composable/index.ts 中有几个类型错误,原因是类型未导出。需要给useLayout中的类型加上export导出

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


image There are several type errors in packages/layout/src/composable/index.ts because the type is not exported. You need to add export to the types in useLayout

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: 1

♻️ Duplicate comments (1)
packages/layout/src/composable/useLayout.ts (1)

239-240: 🛠️ Refactor suggestion

Missing return type definition for useLayout function

The main exported function useLayout lacks a return type annotation, which would complete the type safety of this module. Consider adding a return type that includes all the properties being returned.

-export default () => {
+export default (): {
+  isPanelWidthResizable: (name: string) => boolean
+  getFixedPanelsStatus: () => { leftPanelFixed: boolean, rightPanelFixed: boolean }
+  initPluginStorageReactive: (pluginList: IPluginStorage) => void
+  PLUGIN_NAME: any
+  PLUGIN_POSITION: IPluginPosition
+  // Add remaining return properties...
+} => {
🧹 Nitpick comments (5)
packages/layout/src/composable/useLayout.ts (5)

203-207: Missing return type for getFixedPanelsStatus function

The getFixedPanelsStatus function lacks a return type annotation, which would complete the type safety improvements in this file.

-const getFixedPanelsStatus = () => {
+const getFixedPanelsStatus = (): { leftPanelFixed: boolean, rightPanelFixed: boolean } => {

209-214: Missing return type for closeSetting function

The closeSetting function lacks a return type annotation.

-const closeSetting = (forceClose?: boolean) => {
+const closeSetting = (forceClose?: boolean): void => {

217-227: Improve return type annotation for activePlugin function

While the Promise return type is specified in the implementation, it would be clearer to include it in the function signature.

-const activePlugin = (name: string, noActiveRender: boolean) => {
+const activePlugin = (name: string, noActiveRender: boolean): Promise<IMetaApi> => {

230-235: Missing return type for closePlugin function

The closePlugin function lacks a return type annotation.

-const closePlugin = (forceClose?: boolean) => {
+const closePlugin = (forceClose?: boolean): void => {

237-237: Missing return type for isEmptyPage function

The isEmptyPage function lacks a return type annotation.

-const isEmptyPage = () => layoutState.pageStatus?.state === PAGE_STATUS.Empty
+const isEmptyPage = (): boolean => layoutState.pageStatus?.state === PAGE_STATUS.Empty
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 52e9559 and 6aad869.

📒 Files selected for processing (1)
  • packages/layout/src/composable/useLayout.ts (10 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: push-check
🔇 Additional comments (14)
packages/layout/src/composable/useLayout.ts (14)

20-89: Good addition of TypeScript interfaces

These interfaces provide clear structure and improve type safety throughout the codebase. The definitions are comprehensive and will help catch potential errors during development.


67-67: Avoid using any type for pageStatus

Using any loses type safety. Since pageStatus is accessed with a .state property on line 237, consider creating a more specific interface for this property.

-  pageStatus: any
+  pageStatus: {
+    state: string
+    [key: string]: any
+  }

91-134: Good type annotations for constants and state objects

The addition of type annotations to PLUGIN_POSITION, pluginState, and layoutState improves type safety and code readability.


136-142: Good type annotations for getMoveDragBarState and changeMoveDragBarState

The addition of return types and parameter types to these functions improves code clarity and prevents potential errors.


144-146: Good use of generics with useStorage

Using generic types with useStorage ensures proper type checking of stored values.


147-173: Good type annotations for menu and panel functions

The addition of parameter types and return types to changeMenuShown, changeLeftFixedPanels, and changeRightFixedPanels improves code clarity and prevents potential errors.


175-184: Good type annotations for getter and setter functions

The addition of return types to getScale, getPluginState, getSettingState, getDimension, and setDimension improves code clarity and prevents potential errors.


247-252: Good improvement in error handling

The error handling has been improved by checking if the error is an instance of Error before accessing its message property, and providing a default error message otherwise.


254-254: Good use of generic with useStorage for plugin storage

Using generic type with useStorage ensures proper type checking of stored plugin values.


257-273: Good type annotations for plugin width and layout functions

The addition of return types and parameter types to getPluginWidth, changePluginWidth, and getPluginByLayout improves code clarity and prevents potential errors.


260-269: Good fix for the object assignment issue

The previous implementation had a potential issue with object assignment. This has been fixed by properly initializing the object with all necessary properties.


285-291: Good type annotations for plugin retrieval functions

The addition of parameter types and return types to getPluginById and getPluginsByPosition improves code clarity and prevents potential errors.


367-368: Good fix for null reference issue

The previous implementation had a potential issue when trying to assign properties to an object that doesn't exist yet. This has been fixed by properly initializing the object first.


308-348: Good type annotations for dragPluginLayout function

The addition of parameter types and return type to dragPluginLayout improves code clarity and prevents potential errors.

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

🧹 Nitpick comments (4)
packages/layout/src/composable/useLayout.ts (4)

20-23: Interface definition for IMetaApi is too permissive

The IMetaApi interface only has an index signature with any type, which doesn't provide much type safety. Consider defining the expected properties and methods that this API should have, even if incomplete initially.

export interface IMetaApi {
-  [key: string]: any
+  // Add known properties with specific types
+  // For example:
+  getPlugins?: () => IPlugin[]
+  // Maintain the index signature for unknown properties
+  [key: string]: any
}

224-226: Promise return type is good, but lacks error handling

While adding the proper return type Promise<IMetaApi> is good, there's no error handling if getMetaApi fails.

return new Promise<IMetaApi>((resolve) => {
-  nextTick(() => resolve(getMetaApi(name)))
+  nextTick(() => {
+    try {
+      resolve(getMetaApi(name))
+    } catch (error) {
+      console.error(`Failed to get meta API for plugin ${name}:`, error)
+      resolve({} as IMetaApi)
+    }
+  })
})

248-252: Improved error handling but could be more specific

The error handling has been improved with proper type checking, but the error message for non-Error instances is generic.

if (error instanceof Error) {
  throw new Error(error.message)
} else {
-  throw new Error('Failed to parse plugin storage')
+  throw new Error(`Failed to parse plugin storage: ${String(error)}`)
}

239-423: Consider adding JSDoc comments to exported function

The main exported function and its returned methods lack JSDoc comments explaining their purpose, parameters, and return values.

Adding JSDoc comments would improve the developer experience and documentation:

+/**
+ * Provides utilities for managing the layout of plugins and settings in the application.
+ * @returns An object containing methods and properties for layout management.
+ */
export default () => {
  // ...
}
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 6aad869 and 4c236a0.

📒 Files selected for processing (1)
  • packages/layout/src/composable/useLayout.ts (10 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: push-check
🔇 Additional comments (11)
packages/layout/src/composable/useLayout.ts (11)

67-67: Avoid using any type for pageStatus

Using any loses type safety. Since pageStatus is accessed with a .state property on line 237, consider creating a more specific interface for this property.

-  pageStatus: any
+  pageStatus: {
+    state: string
+    [key: string]: any
+  }

70-77: Good type definition for IPluginStorageItem

The type definition for IPluginStorageItem properly captures all the properties used throughout the code with appropriate optionality markers. This will help prevent runtime errors when properties are accessed.


136-138: Good type safety for MoveDragBarState functions

Adding the boolean return type and parameter type to these functions improves type safety and makes the API more predictable.

Also applies to: 140-142


144-146: Good use of generic types with useStorage

Using generic types with useStorage ensures type safety when the stored values are accessed and modified throughout the code.


260-269: Properly handled type safety for changePluginWidth function

The function now has proper type annotations and the conditional check for undefined is explicit, improving code safety and readability.


285-287: Well-defined plugin lookup function with proper return type

The getPluginById function now properly indicates it can return undefined when no matching plugin is found, making it safer to use throughout the codebase.


289-291: Improved type safety in getPluginsByPosition

The function's return type accurately reflects that each item could be undefined if a plugin is not found, which helps prevent runtime errors.


308-348: Properly typed complex dragPluginLayout function

The complex dragPluginLayout function now has proper type annotations for parameters, making it easier to understand and use correctly.


366-368: Fixed initialization of plugin storage items

The code now properly initializes the plugin storage item as an object with the required properties, addressing the issue from previous reviews.


379-382: Type safe function to initialize plugin storage

The initPluginStorageReactive function now has a proper type annotation for its parameter, ensuring that the expected data structure is provided.


239-240:

❓ Verification inconclusive

Missing return type definition for useLayout function

The main exported function useLayout lacks a return type annotation, which would complete the type safety of this module.

-export default () => {
+export interface IUseLayoutReturn {
+  isPanelWidthResizable: (name: string) => boolean
+  getFixedPanelsStatus: () => { leftPanelFixed: boolean, rightPanelFixed: boolean }
+  initPluginStorageReactive: (pluginList: IPluginStorage) => void
+  PLUGIN_NAME: any
+  PLUGIN_POSITION: IPluginPosition
+  activeSetting: (name: string) => void
+  closeSetting: (forceClose?: boolean) => void
+  activePlugin: (name: string, noActiveRender: boolean) => Promise<IMetaApi>
+  closePlugin: (forceClose?: boolean) => void
+  layoutState: ILayoutState
+  getScale: () => number
+  setDimension: (data: Partial<IDimension>) => void
+  getDimension: () => IDimension
+  getPluginById: (pluginList: IPlugin[], pluginId: string) => IPlugin | undefined
+  pluginState: IPluginState
+  getPluginState: () => IPlugins
+  getSettingState: () => ISettings
+  isEmptyPage: () => boolean
+  // Add remaining return properties...
+}
+
+export default (): IUseLayoutReturn => {

🌐 Web query:

Are explicit return type interfaces recommended in Vue 3 composition API functions or is type inference sufficient?

💡 Result:

In Vue 3's Composition API, Type Inference is generally sufficient for most use cases due to its robust integration with TypeScript, but explicit return type interfaces are recommended in specific scenarios for enhanced type safety and clarity.

Type Inference Strengths

  1. Automatic type detection:
    Composition API's reactivity primitives (ref, reactive, computed) infer types automatically[1][6][13]. For example:

    const count = ref(0)  // Inferred as Ref<number>
                          // No explicit type needed
  2. Template ref auto-unwrapping:
    Ref values in templates are automatically unwrapped with correct type inference[16].

  3. Composable arguments:
    Function parameters in composables get inferred types when using TypeScript[15].

When to Use Explicit Return Types

Scenario Example Benefit
Complex return objects interface AuthState { user: Ref<User>, token: Ref<string> }
function useAuth(): AuthState { ... }
Prevents fragmented type declarations[11][15]
Union/intersection types function useToggle(): [Ref<boolean>, () => void] Avoids array-to-tuple conversion issues[4][15]
Public composables export function usePagination<T>(): PaginationResult<T> Ensures contract stability for consumers[15]
Conditional returns function useFetch<T>() => { data: Ref<T | null>, error: Ref<Error> } Clarifies branching logic outcomes[4]

Performance & Best Practices

  • Runtime vs Type-based declarations:
    Type-based props via generics (defineProps<{...}>()) have better inference than runtime declarations using PropType[1][5][11].

  • Computed properties:
    Explicit generics prevent inference errors:

    const double = computed<number>(() => ...)  // Enforce numeric return
  • IDE benefits:
    Explicit types improve autocomplete and type checking in IDEs like VSCode[15].

While Vue 3's Composition API reduces boilerplate through effective type inference[2][6][13], explicit return types become valuable in complex scenarios and public APIs. The official Vue documentation recommends leveraging TypeScript's inference where possible, while using explicit typing for shared libraries and critical business logic[1][3][5].

Citations:


Explicit Return Type for UseLayout Composable

Although Vue 3’s Composition API offers robust type inference, explicit return type annotations are recommended for public composables—especially when returning complex objects—to ensure clarity and maintainability. In this case, defining an explicit interface for the return value of useLayout enhances the API contract and aligns with best practices.

  • Define an interface (e.g., IUseLayoutReturn) specifying all properties and methods being returned.
  • Annotate the exported function with this interface to signal its public API clearly.

Revised diff snippet:

-export default () => {
+export interface IUseLayoutReturn {
+  isPanelWidthResizable: (name: string) => boolean;
+  getFixedPanelsStatus: () => { leftPanelFixed: boolean; rightPanelFixed: boolean };
+  initPluginStorageReactive: (pluginList: IPluginStorage) => void;
+  PLUGIN_NAME: any;
+  PLUGIN_POSITION: IPluginPosition;
+  activeSetting: (name: string) => void;
+  closeSetting: (forceClose?: boolean) => void;
+  activePlugin: (name: string, noActiveRender: boolean) => Promise<IMetaApi>;
+  closePlugin: (forceClose?: boolean) => void;
+  layoutState: ILayoutState;
+  getScale: () => number;
+  setDimension: (data: Partial<IDimension>) => void;
+  getDimension: () => IDimension;
+  getPluginById: (pluginList: IPlugin[], pluginId: string) => IPlugin | undefined;
+  pluginState: IPluginState;
+  getPluginState: () => IPlugins;
+  getSettingState: () => ISettings;
+  isEmptyPage: () => boolean;
+  // Add any additional return properties as needed...
+}
+
+export default (): IUseLayoutReturn => {

This explicit typing not only clarifies the module's interface for consumers but also aids in effective IDE support and type checking.

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

🧹 Nitpick comments (1)
packages/layout/src/composable/useLayout.ts (1)

20-23: Consider providing a more specific IMetaApi interface.

The current IMetaApi interface is very loose with [key: string]: any. Consider defining more specific properties if they are known to provide better type safety.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 4c236a0 and efab8aa.

📒 Files selected for processing (1)
  • packages/layout/src/composable/useLayout.ts (10 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: push-check
🔇 Additional comments (11)
packages/layout/src/composable/useLayout.ts (11)

20-92: Good addition of type interfaces.

The introduction of clear TypeScript interfaces for the layout system components provides better type safety and code documentation. These interfaces will help catch errors at compile time rather than runtime.


69-69: Replace any type with a more specific interface.

Using any loses type safety. Since pageStatus is accessed with a .state property elsewhere, consider creating a more specific interface.

-  pageStatus: any
+  pageStatus: {
+    state: string
+    [key: string]: unknown
+  }

102-102: Good use of generic type parameter with reactive.

The addition of the generic type parameter <IPluginState> to the reactive call ensures proper type checking of the reactive object properties.


146-148: Excellent use of generic types with useStorage.

Adding generic type parameters to useStorage calls provides better type safety for stored values.


226-226: Good Promise typing.

Adding the return type Promise<IMetaApi> to the promise ensures that consumers of this function know what type to expect when the promise resolves.


241-242: Missing return type definition for useLayout function.

The main exported function lacks a return type annotation, which would complete the type safety of this module.

-export default () => {
+interface IUseLayoutReturn {
+  isPanelWidthResizable: (name: string) => boolean
+  getFixedPanelsStatus: () => { leftPanelFixed: boolean, rightPanelFixed: boolean }
+  initPluginStorageReactive: (pluginList: IPluginStorage) => void
+  PLUGIN_NAME: any
+  PLUGIN_POSITION: IPluginPosition
+  activeSetting: (name: string) => void
+  closeSetting: (forceClose?: boolean) => void
+  activePlugin: (name: string, noActiveRender?: boolean) => Promise<IMetaApi>
+  closePlugin: (forceClose?: boolean) => void
+  layoutState: ILayoutState
+  getScale: () => number
+  setDimension: (data: Partial<IDimension>) => void
+  getDimension: () => IDimension
+  getPluginById: (pluginList: IPlugin[], pluginId: string) => IPlugin | undefined
+  pluginState: IPluginState
+  getPluginState: () => IPlugins
+  getSettingState: () => ISettings
+  isEmptyPage: () => boolean
+  getPluginWidth: (name: string) => number
+  changePluginWidth: (name: string, width: number, offset?: number) => void
+  leftFixedPanelsStorage: Ref<string[]>
+  rightFixedPanelsStorage: Ref<string[]>
+  leftMenuShownStorage: Ref<boolean>
+  rightMenuShownStorage: Ref<boolean>
+  changeLeftFixedPanels: (pluginName: string) => void
+  changeRightFixedPanels: (pluginName: string) => void
+  getPluginsByLayout: (layout?: string) => string[]
+  changePluginLayout: (name: string, layout: string) => void
+  getPluginByLayout: (name: string) => string
+  dragPluginLayout: (from: string, to: string, oldIndex: number, newIndex: number) => void
+  isSameSide: (from: string, to: string) => boolean
+  getPluginShown: (name: string) => boolean
+  changePluginShown: (name: string) => void
+  changeMenuShown: (menuName: 'left' | 'right') => void
+  getMoveDragBarState: () => boolean
+  changeMoveDragBarState: (state: boolean) => void
+  getPluginsByPosition: (position: string, pluginList: IPlugin[]) => (IPlugin | undefined)[]
+}
+
+export default (): IUseLayoutReturn => {

250-254: Improved error handling with TypeScript.

The error handling has been enhanced with proper type checking for Error instances, which is a good practice.


265-267: Good use of optional parameter type check.

The code now properly checks if the optional offset parameter is undefined before using it, which is safer than the previous version.


269-269: Fixed object initialization.

The initialization of the plugin storage object now includes all required properties, addressing the previous review comment.


369-369: Fixed object initialization.

The initialization of the plugin storage object now includes all required properties, addressing the previous review comment.


72-92:

✅ Verification successful

Consider exporting more interfaces for external use.

Based on the PR comments from gene9831, you might need to export additional interfaces to resolve type errors in other files that import from this module.


🏁 Script executed:

#!/bin/bash
# Check for interfaces and their imports/exports
rg "\binterface I\w+" --type ts packages/layout/src

Length of output: 936


Export Additional Interfaces for External Use – Resolved

The file now exports several interfaces beyond those in the initially shown snippet. In addition to IPluginStorageItem, IPluginStorage, and IPlugin, the file also exports interfaces such as IMetaApi, IPluginPosition, IPluginState, IDimension, IPluginBase, IPlugins, ISettings, and ILayoutState. These additional exports address the concerns raised by gene9831 regarding type errors in other modules.

No further changes are needed in this regard.

gene9831
gene9831 previously approved these changes Apr 2, 2025
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

🧹 Nitpick comments (4)
packages/layout/src/composable/useLayout.ts (4)

69-69: Consider replacing any type with a more specific interface.

The pageStatus property is using any type which loses type safety. Since it's accessed with a .state property later (line 242), consider using a more specific interface.

-  pageStatus: any
+  pageStatus: {
+    state: string
+    data: Record<string, unknown>
+  }

253-256: Consider rethrowing the original error to preserve stack trace information.

The current error handling creates a new error which loses the original stack trace. Consider rethrowing or including the original error as a cause.

try {
  const storedPlugin = localStorage.getItem('plugin')
  if (storedPlugin) {
    plugin = JSON.parse(storedPlugin) as IPluginStorage
  }
} catch (error) {
  if (error instanceof Error) {
-    throw new Error(error.message)
+    throw error
  }
  throw new Error('Failed to parse plugin storage')
}

244-244: Consider adding return type annotation to the main exported function.

For better type safety and documentation, consider adding a return type annotation to the main exported function.

You can either:

  1. Let TypeScript infer the return type (current approach)
  2. Create a specific return type interface

According to past comments, gene9831 suggested letting TypeScript infer the type, which is a valid approach.


168-178: Consider refactoring similar functions to reduce duplication.

The changeLeftFixedPanels and changeRightFixedPanels functions have nearly identical implementations and could be refactored into a single helper function.

+const toggleFixedPanel = (storage: Ref<string[]>, pluginName: string): void => {
+  storage.value = storage.value?.includes(pluginName)
+    ? storage.value?.filter((item) => item !== pluginName)
+    : [...storage.value, pluginName]
+}
+
 const changeLeftFixedPanels = (pluginName: string): void => {
-  leftFixedPanelsStorage.value = leftFixedPanelsStorage.value?.includes(pluginName)
-    ? leftFixedPanelsStorage.value?.filter((item) => item !== pluginName)
-    : [...leftFixedPanelsStorage.value, pluginName]
+  toggleFixedPanel(leftFixedPanelsStorage, pluginName)
 }

 const changeRightFixedPanels = (pluginName: string): void => {
-  rightFixedPanelsStorage.value = rightFixedPanelsStorage.value?.includes(pluginName)
-    ? rightFixedPanelsStorage.value?.filter((item) => item !== pluginName)
-    : [...rightFixedPanelsStorage.value, pluginName]
+  toggleFixedPanel(rightFixedPanelsStorage, pluginName)
 }

Note: You'll need to import Ref from 'vue' for this change.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between efab8aa and 21dd813.

📒 Files selected for processing (1)
  • packages/layout/src/composable/useLayout.ts (10 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: push-check
🔇 Additional comments (9)
packages/layout/src/composable/useLayout.ts (9)

222-222: LGTM! Properly typed parameter with optional flag.

The noActiveRender parameter is now correctly typed as an optional boolean parameter.


102-102: LGTM! Properly typed reactive state with generics.

Using generics with reactive enables TypeScript to strictly validate the data structure.


271-273: LGTM! Fixed object assignment issue.

The code properly initializes a new object with required properties when the plugin name is not found in storage.


371-373: LGTM! Fixed null reference issue.

The code now properly initializes a new object with required properties when the plugin name doesn't exist.


290-296: LGTM! Well-typed helper functions for plugin retrieval.

These functions properly use TypeScript types to ensure type safety when working with plugin data.


259-259: LGTM! Proper generic typing for storage.

Using generics with useStorage improves type safety for stored values.


149-150: LGTM! Well-typed storage variables.

The storage variables are properly typed with generics to ensure type safety.


93-100: LGTM! Well-typed constant with interface.

The PLUGIN_POSITION constant is now properly typed with the IPluginPosition interface.


20-92:

✅ Verification successful

LGTM! Well-structured interface definitions.

The TypeScript interface definitions are well-structured and provide clear type information for the data structures used throughout the code.

Based on the PR comments about VS Code errors, let's verify that necessary types are properly exported:


🏁 Script executed:

#!/bin/bash
# Check if there are any references to these interfaces in other files
rg -A 1 -B 1 "IPluginStorageItem|IPluginStorage|IPlugin" --type ts

Length of output: 4564


Verified: Interface Exports Are Correct

The interface definitions in packages/layout/src/composable/useLayout.ts are well-structured and properly exported. The grep search confirms that interfaces like IPluginStorageItem, IPluginStorage, and IPlugin are referenced in other parts of the codebase (e.g., in packages/vue-generator/src/index.d.ts), which indicates that the VS Code errors related to type exports have been resolved.

LGTM!

@hexqi hexqi added this to the v2.5.0 milestone Apr 16, 2025
@SonyLeo SonyLeo force-pushed the refactor/useLayout branch from 21dd813 to 3f249b1 Compare April 21, 2025 03:35
@hexqi hexqi merged commit 3a1a100 into opentiny:develop Apr 21, 2025
1 check passed
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.

4 participants