[PR #32301] refactor(web): extract custom hooks from complex components and add comprehensive tests #33657

Closed
opened 2026-02-21 20:53:40 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/32301

State: closed
Merged: Yes


Important

  1. Make sure you have read our contribution guidelines
  2. Ensure there is an associated issue and you have been assigned to it
  3. Use the correct syntax to link this PR: Fixes #<issue number>.

Summary

Refactor three high-complexity components by extracting business logic into custom hooks,
reducing component complexity and improving testability:

  • configure-button.tsx (complexity 64→12): Extract useConfigureButton hook with
    React Query integration (useWorkflowToolDetailByAppID) replacing manual useState +
    useEffect data fetching; add pure helper isParametersOutdated.
  • doc.tsx (261→98 lines): Extract useDocToc hook for TOC state/scroll management
    and TocPanel component for the table-of-contents UI.
  • install-multi.tsx (223→~100 lines): Extract useInstallMultiState hook for
    plugin installation state management.

Test Coverage

  • Add dedicated hook tests: use-configure-button.spec.ts (40 tests),
    use-doc-toc.spec.ts, use-install-multi-state.spec.ts (comprehensive renderHook tests)
  • Add toc-panel.spec.tsx for the extracted TOC component
  • Simplify configure-button.spec.tsx by removing logic tests now covered by hook tests
  • Add afterEach(vi.restoreAllMocks) across test files for consistent cleanup

Other

  • Add useWorkflowToolDetailByAppID and invalidation hook to service/use-tools.ts
  • Prune stale ESLint suppressions

Test plan

  • pnpm test app/components/tools/workflow-tool/ — 164 tests pass
  • pnpm test app/components/develop/ — all tests pass
  • pnpm test app/components/plugins/install-plugin/ — all tests pass
  • pnpm lint:fix — no new errors
  • pnpm type-check:tsgo — no type errors

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran make lint and make type-check (backend) and cd web && npx lint-staged (frontend) to appease the lint gods
**Original Pull Request:** https://github.com/langgenius/dify/pull/32301 **State:** closed **Merged:** Yes --- > [!IMPORTANT] > > 1. Make sure you have read our [contribution guidelines](https://github.com/langgenius/dify/blob/main/CONTRIBUTING.md) > 1. Ensure there is an associated issue and you have been assigned to it > 1. Use the correct syntax to link this PR: `Fixes #<issue number>`. ## Summary Refactor three high-complexity components by extracting business logic into custom hooks, reducing component complexity and improving testability: - **`configure-button.tsx`** (complexity 64→12): Extract `useConfigureButton` hook with React Query integration (`useWorkflowToolDetailByAppID`) replacing manual `useState` + `useEffect` data fetching; add pure helper `isParametersOutdated`. - **`doc.tsx`** (261→98 lines): Extract `useDocToc` hook for TOC state/scroll management and `TocPanel` component for the table-of-contents UI. - **`install-multi.tsx`** (223→~100 lines): Extract `useInstallMultiState` hook for plugin installation state management. ### Test Coverage - Add dedicated hook tests: `use-configure-button.spec.ts` (40 tests), `use-doc-toc.spec.ts`, `use-install-multi-state.spec.ts` (comprehensive `renderHook` tests) - Add `toc-panel.spec.tsx` for the extracted TOC component - Simplify `configure-button.spec.tsx` by removing logic tests now covered by hook tests - Add `afterEach(vi.restoreAllMocks)` across test files for consistent cleanup ### Other - Add `useWorkflowToolDetailByAppID` and invalidation hook to `service/use-tools.ts` - Prune stale ESLint suppressions ## Test plan - [x] `pnpm test app/components/tools/workflow-tool/` — 164 tests pass - [x] `pnpm test app/components/develop/` — all tests pass - [x] `pnpm test app/components/plugins/install-plugin/` — all tests pass - [x] `pnpm lint:fix` — no new errors - [x] `pnpm type-check:tsgo` — no type errors ## Checklist - [ ] This change requires a documentation update, included: [Dify Document](https://github.com/langgenius/dify-docs) - [x] I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!) - [x] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change. - [x] I've updated the documentation accordingly. - [x] I ran `make lint` and `make type-check` (backend) and `cd web && npx lint-staged` (frontend) to appease the lint gods
yindo added the pull-request label 2026-02-21 20:53:40 -05:00
yindo closed this issue 2026-02-21 20:53:40 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#33657