npx skills add ...
npx skills add langgenius/dify --skill component-refactoring
Refactor high-complexity React components in Dify frontend. Use when `pnpm analyze-component --json` shows complexity > 50 or lineCount > 300, when the user asks for code splitting, hook extraction, or complexity reduction, or when `pnpm analyze-component` warns to refactor before testing; avoid for simple/well-structured components, third-party wrappers, or when the user explicitly wants testing without refactoring.
npx skills add langgenius/dify --skill component-refactoring
Refactor high-complexity React components in the Dify frontend codebase with the patterns and workflow below.
Complexity Threshold: Components with complexity > 50 (measured by
pnpm analyze-component) should be refactored before testing.
web/)Use paths relative to web/ (e.g., app/components/...).
Use refactor-component for refactoring prompts and analyze-component for testing prompts and metrics.
| Score | Level | Action |
|---|---|---|
| 0-25 | 🟢 Simple | Ready for testing |
| 26-50 | 🟡 Medium | Consider minor refactoring |
| 51-75 | 🟠 Complex | Refactor before testing |
| 76-100 | 🔴 Very Complex | Must refactor |
When: Component has complex state management, multiple useState/useEffect, or business logic mixed with UI.
Dify Convention: Place hooks in a hooks/ subdirectory or alongside the component as use-<feature>.ts.
Dify Examples:
web/app/components/app/configuration/hooks/use-advanced-prompt-config.tsweb/app/components/app/configuration/debug/hooks.tsxweb/app/components/workflow/hooks/use-workflow.tsWhen: Single component has multiple UI sections, conditional rendering blocks, or repeated patterns.
Dify Convention: Place sub-components in subdirectories or as separate files in the same directory.
Dify Examples:
web/app/components/app/configuration/ directory structureweb/app/components/workflow/nodes/ per-node organizationWhen: Deep nesting (> 3 levels), complex ternaries, or multiple if/else chains.
When: Component directly handles API calls, data transformation, or complex async operations.
Dify Convention:
useInvalid / useReset.useQuery wrappers during refactoring; only extract a custom hook when it truly orchestrates multiple queries/mutations or shared derived state.Dify Examples:
web/service/use-workflow.tsweb/service/use-common.tsweb/service/knowledge/use-dataset.tsweb/service/knowledge/use-document.tsWhen: Component manages multiple modals with complex open/close states.
Dify Convention: Modals should be extracted with their state management.
When: Complex form validation, submission handling, or field transformation.
Dify Convention: Use @tanstack/react-form patterns from web/app/components/base/form/.
When: Component provides complex context values with multiple states.
Dify Reference: web/context/ directory structure
When: Refactoring workflow node components (web/app/components/workflow/nodes/).
Conventions:
use-interactions.ts_base components for common patternsWhen: Refactoring app configuration components.
Conventions:
web/app/components/app/configuration/When: Refactoring tool-related components (web/app/components/tools/).
Conventions:
web/service/use-tools.tsThis command will:
Identify:
Create a refactoring plan based on detected features:
| Detected Feature | Refactoring Action |
|---|---|
hasState: true + hasEffects: true | Extract custom hook |
hasAPI: true | Extract data/service hook |
hasEvents: true (many) | Extract event handlers |
lineCount > 300 | Split into sub-components |
maxComplexity > 50 | Simplify conditional logic |
After refactoring:
web/app/components/app/configuration/hooks/web/app/components/app/configuration/web/service/use-*.tsweb/app/components/workflow/hooks/web/app/components/base/form/frontend-testing - For testing refactored componentsweb/docs/test.md - Testing specification