Skip to content

Refactor left side bar components to React functional API #8755

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

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

hotzenklotz
Copy link
Member

@hotzenklotz hotzenklotz commented Jul 4, 2025

This PR converts several class components in the left side-bar of the annotation UI to React functional components.

The following components were refactored:

  • HistrogramView
  • DatasetSettings in LayerSettingsTab
  • MappingSettingsView

TODO

  • Fix Sliders not working

Issues:

Contributes to #8747


(Please delete unneeded items, merge only when none are left open)

  • Added changelog entry (create a $PR_NUMBER.md file in unreleased_changes or use ./tools/create-changelog-entry.py)
  • Added migration guide entry if applicable (edit the same file as for the changelog)
  • Updated documentation if applicable
  • Adapted wk-libs python client if relevant API parts change
  • Removed dev-only changes like prints and application.conf edits
  • Considered common edge cases
  • Needs datastore update after deployment

@hotzenklotz hotzenklotz self-assigned this Jul 4, 2025
Copy link
Contributor

coderabbitai bot commented Jul 4, 2025

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

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

📝 Walkthrough

Walkthrough

This update refactors three React components—Histogram, DatasetSettings, and MappingSettingsView—from class-based components with Redux connect to modern functional components using React and Redux hooks. All internal state, side effects, and Redux interactions are now handled via hooks, eliminating class syntax and explicit prop drilling, while preserving existing UI and logic.

Changes

Files Change Summary
frontend/javascripts/viewer/view/left-border-tabs/histogram_view.tsx Refactored Histogram from class to functional component; replaced lifecycle and Redux connect with hooks.
frontend/javascripts/viewer/view/left-border-tabs/layer_settings_tab.tsx Refactored DatasetSettings from class to functional component; replaced all state and Redux connect with hooks.
frontend/javascripts/viewer/view/left-border-tabs/mapping_settings_view.tsx Refactored MappingSettingsView from class to functional component; used hooks for state and Redux interaction.

Poem

A hop, a skip, a modern twist,
Old classes gone, they won’t be missed!
Hooks now burrow where state once lay,
Redux connect has hopped away.
With every nibble, code’s more neat—
This bunny’s work is now complete!
🐇✨


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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate sequence diagram to generate a sequence diagram of the changes in 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.

@hotzenklotz
Copy link
Member Author

@coderabbitai review

Copy link
Contributor

coderabbitai bot commented Jul 7, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

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 (2)
frontend/javascripts/viewer/view/left-border-tabs/histogram_view.tsx (1)

86-385: Use Antd Slider’s onAfterChange instead of onChangeComplete

Antd’s Slider API doesn’t recognize onChangeComplete; it uses onAfterChange. Update the histogram view so your callback fires correctly.

• In frontend/javascripts/viewer/view/left-border-tabs/histogram_view.tsx (around line 298)
Replace

- onChangeComplete={onThresholdChange}
+ onAfterChange={onThresholdChange}

(and remove the obsolete onChangeComplete prop)

frontend/javascripts/viewer/view/left-border-tabs/layer_settings_tab.tsx (1)

952-964: Verify Slider Wrapper Implementations

I inspected the full implementation of the two wrapper components:

  • Both NumberSliderSetting and LogSliderSetting render an Ant Design Slider but only wire up the slider’s onAfterChange handler to their internal change methods (_onChange / onChangeInput), leaving onChange unused. This means dragging the slider won’t fire updates until you release the thumb.

Actionable fix:
• In both wrappers (frontend/javascripts/viewer/view/components/setting_input_views.tsx), bind the slider’s onChange prop to the internal handler instead of (or in addition to) onAfterChange.

Example diff for LogSliderSetting:

-  return (
-    <Slider
-      min={this.props.min}
-      max={this.props.max}
-      step={this.props.step}
-      defaultValue={this.props.defaultValue}
-      onAfterChange={this.onChangeInput}
-      disabled={this.props.disabled}
-      // ...
-    />
-  );
+  return (
+    <Slider
+      min={this.props.min}
+      max={this.props.max}
+      step={this.props.step}
+      defaultValue={this.props.defaultValue}
+      onChange={this.onChangeInput}       // fire continuously as thumb moves
+      onAfterChange={this.onChangeInput}  // keep release behavior if needed
+      disabled={this.props.disabled}
+      // ...
+    />
+  );

Repeat the same adjustment in NumberSliderSetting. This ensures the UI reacts immediately when dragging.

🧹 Nitpick comments (3)
frontend/javascripts/viewer/view/left-border-tabs/histogram_view.tsx (1)

205-207: Consider optimizing canvas updates

The useEffect without a dependency array will run updateCanvas on every render. While this works, it could be optimized by specifying the actual dependencies that should trigger a canvas update.

Consider adding dependencies to avoid unnecessary canvas redraws:

  useEffect(() => {
    updateCanvas();
- });
+ }, [updateCanvas, intensityRangeMin, intensityRangeMax]);
frontend/javascripts/viewer/view/left-border-tabs/layer_settings_tab.tsx (2)

337-443: Consider breaking down this large component

While the refactoring from class to functional component is well done, this component is quite large (1600+ lines). Consider extracting some of the logic into custom hooks or smaller components for better maintainability.

Consider extracting:

  1. Layer management logic into a custom hook (e.g., useLayerManagement)
  2. Volume tracing logic into useVolumeTracing
  3. The various button/menu item generators into a separate utility file

This would make the component more maintainable and easier to test.


962-963: Consider replacing _.partial with arrow functions for clarity

While _.partial works, using arrow functions would be more idiomatic in modern React and clearer to read.

Replace:

- onChange={_.partial(onChangeLayer, layerName, "gammaCorrectionValue")}
+ onChange={(value) => onChangeLayer(layerName, "gammaCorrectionValue", value)}

This pattern appears multiple times throughout the component and could be updated for consistency and readability.

Also applies to: 977-977, 1029-1029, 1110-1110, 1350-1351, 1359-1360, 1369-1369, 1376-1377, 1381-1381, 1387-1387

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between c88c3b1 and 4cdc70e.

📒 Files selected for processing (3)
  • frontend/javascripts/viewer/view/left-border-tabs/histogram_view.tsx (4 hunks)
  • frontend/javascripts/viewer/view/left-border-tabs/layer_settings_tab.tsx (42 hunks)
  • frontend/javascripts/viewer/view/left-border-tabs/mapping_settings_view.tsx (3 hunks)
🧰 Additional context used
🧠 Learnings (4)
📓 Common learnings
Learnt from: philippotto
PR: scalableminds/webknossos#8542
File: frontend/javascripts/viewer/model/sagas/volumetracing_saga.tsx:0-0
Timestamp: 2025-05-30T12:36:09.930Z
Learning: The WebKnossos frontend follows Redux immutability principles where reducers act immutably and create new state objects when changes occur. This makes reference equality checks (like `prevVolumeTracing.segments !== volumeTracing.segments`) reliable and efficient for detecting actual state changes, allowing memoization to work correctly.
frontend/javascripts/viewer/view/left-border-tabs/histogram_view.tsx (1)
Learnt from: philippotto
PR: scalableminds/webknossos#8542
File: frontend/javascripts/viewer/model/sagas/volumetracing_saga.tsx:0-0
Timestamp: 2025-05-30T12:36:09.930Z
Learning: The WebKnossos frontend follows Redux immutability principles where reducers act immutably and create new state objects when changes occur. This makes reference equality checks (like `prevVolumeTracing.segments !== volumeTracing.segments`) reliable and efficient for detecting actual state changes, allowing memoization to work correctly.
frontend/javascripts/viewer/view/left-border-tabs/mapping_settings_view.tsx (1)
Learnt from: philippotto
PR: scalableminds/webknossos#8542
File: frontend/javascripts/viewer/model/sagas/volumetracing_saga.tsx:0-0
Timestamp: 2025-05-30T12:36:09.930Z
Learning: The WebKnossos frontend follows Redux immutability principles where reducers act immutably and create new state objects when changes occur. This makes reference equality checks (like `prevVolumeTracing.segments !== volumeTracing.segments`) reliable and efficient for detecting actual state changes, allowing memoization to work correctly.
frontend/javascripts/viewer/view/left-border-tabs/layer_settings_tab.tsx (8)
Learnt from: dieknolle3333
PR: scalableminds/webknossos#8168
File: frontend/javascripts/oxalis/model/sagas/proofread_saga.ts:1039-1039
Timestamp: 2024-11-22T17:18:04.217Z
Learning: In `frontend/javascripts/oxalis/model/sagas/proofread_saga.ts`, when calling `getMagInfo`, the use of `volumeTracingLayer.resolutions` is intentional and should not be changed to `volumeTracingLayer.mags`.
Learnt from: knollengewaechs
PR: scalableminds/webknossos#8266
File: frontend/javascripts/components/mag_selection.tsx:51-60
Timestamp: 2024-12-16T15:48:37.065Z
Learning: In the `MagSelection` component (`frontend/javascripts/components/mag_selection.tsx`), the selected magnification `value` will always be present in `allMags` because only existing values can be selected by the user.
Learnt from: dieknolle3333
PR: scalableminds/webknossos#8168
File: frontend/javascripts/oxalis/model/reducers/skeletontracing_reducer_helpers.ts:125-125
Timestamp: 2024-11-22T17:17:39.914Z
Learning: In the `createNode` function within `skeletontracing_reducer_helpers.ts`, the property `resolution` is intentionally kept even when the parameter is renamed to `mag`.
Learnt from: dieknolle3333
PR: scalableminds/webknossos#8168
File: frontend/javascripts/oxalis/model/sagas/volumetracing_saga.tsx:433-434
Timestamp: 2024-11-22T17:19:07.947Z
Learning: In the codebase, certain usages of `segmentationLayer.resolutions` are intentionally retained and should not be changed to `segmentationLayer.mags` during refactoring.
Learnt from: frcroth
PR: scalableminds/webknossos#8609
File: app/models/dataset/Dataset.scala:753-775
Timestamp: 2025-05-12T13:07:29.637Z
Learning: In the `updateMags` method of DatasetMagsDAO (Scala), the code handles different dataset types distinctly:
1. Non-WKW datasets have `magsOpt` populated and use the first branch which includes axisOrder, channelIndex, and credentialId.
2. WKW datasets will have `wkwResolutionsOpt` populated and use the second branch which includes cubeLength.
3. The final branch is a fallback for legacy data.
This ensures appropriate fields are populated for each dataset type.
Learnt from: frcroth
PR: scalableminds/webknossos#8609
File: conf/evolutions/133-datasource-properties-in-db.sql:8-16
Timestamp: 2025-05-12T14:15:05.259Z
Learning: The database schema in WEBKNOSSOS has separate tables for dataset layers (`dataset_layers`) and magnifications (`dataset_mags`). The `dataFormat` field is stored in the layers table while magnification-specific fields like `cubeLength` (specific to WKW format) are stored in the mags table.
Learnt from: MichaelBuessemeyer
PR: scalableminds/webknossos#8485
File: frontend/javascripts/oxalis/model/accessors/dataset_layer_transformation_accessor.ts:384-392
Timestamp: 2025-04-01T09:45:17.527Z
Learning: The function `isRotationAndMirrorMaybeOnly` in the dataset_layer_transformation_accessor.ts is intentionally designed to allow mirroring transformations (negative scale values). It uses the length comparison (`scale.length() === NON_SCALED_VECTOR.length()`) rather than component equality to permit mirrored axes while ensuring the overall scale magnitude remains the same.
Learnt from: dieknolle3333
PR: scalableminds/webknossos#8229
File: frontend/javascripts/oxalis/model/accessors/dataset_accessor.ts:348-354
Timestamp: 2024-11-25T14:38:49.345Z
Learning: For the `getDatasetExtentAsProduct` function in `dataset_accessor.ts`, input validation for negative or zero dimensions is not necessary.
🔇 Additional comments (3)
frontend/javascripts/viewer/view/left-border-tabs/mapping_settings_view.tsx (2)

47-87: Clean refactoring to functional component

The conversion from class component to functional component is well executed. The hooks are used appropriately:

  • useWkSelector correctly replaces mapStateToProps
  • useCallback has the correct dependencies
  • useEffect properly replaces the lifecycle methods

88-224: Component logic well preserved

The event handlers and rendering logic have been successfully adapted to the functional component pattern while preserving the original behavior. The use of local variables instead of props/state references is clean and readable.

frontend/javascripts/viewer/view/left-border-tabs/layer_settings_tab.tsx (1)

1569-1641: Successful refactoring of complex component

The conversion of this large component from class-based to functional is well executed. All lifecycle methods have been properly replaced with hooks, and the Redux integration has been successfully updated to use hooks.

Comment on lines +326 to 334
onChange={(value) => {
// @ts-expect-error ts-migrate(2345) FIXME: Argument of type 'number' is not assignable to par... Remove this comment to see the full error message
value = Number.parseFloat(value);

if (value <= maxRange) {
setCurrentMin(value);
updateMinimumDebounced(value, layerName);
}
}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Remove type error comments and handle types properly

Instead of suppressing TypeScript errors, handle the InputNumber value types correctly.

Fix the type handling:

  onChange={(value) => {
-   // @ts-expect-error ts-migrate(2345) FIXME: Argument of type 'number' is not assignable to par... Remove this comment to see the full error message
-   value = Number.parseFloat(value);
-
-   if (value <= maxRange) {
-     setCurrentMin(value);
-     updateMinimumDebounced(value, layerName);
+   if (value !== null && value !== undefined) {
+     const numValue = typeof value === 'string' ? Number.parseFloat(value) : value;
+     if (!isNaN(numValue) && numValue <= maxRange) {
+       setCurrentMin(numValue);
+       updateMinimumDebounced(numValue, layerName);
+     }
    }
  }}

Apply the same fix to the max value handler at lines 359-367.

Also applies to: 359-367

🤖 Prompt for AI Agents
In frontend/javascripts/viewer/view/left-border-tabs/histogram_view.tsx around
lines 326 to 334 and 359 to 367, remove the @ts-expect-error comment and
properly handle the InputNumber value types by explicitly typing the event
parameter or value as a number or string, then safely converting it to a number
if needed. Ensure the onChange handlers correctly process the input without type
errors by refining the type definitions or adding appropriate type guards before
calling setCurrentMin or setCurrentMax and their respective update functions.

Comment on lines +244 to 252
const updateMinimumDebounced = useCallback(
debounce((value, layerName) => onChangeLayer(layerName, "min", value), 500),
[],
);

updateMaximumDebounced = _.debounce(
(value, layerName) => this.props.onChangeLayer(layerName, "max", value),
500,
const updateMaximumDebounced = useCallback(
debounce((value, layerName) => onChangeLayer(layerName, "max", value), 500),
[],
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix stale closure issue in debounced callbacks

The debounced callbacks have empty dependency arrays but reference onChangeLayer, which could lead to stale closures if onChangeLayer changes.

Update the dependency arrays:

  const updateMinimumDebounced = useCallback(
-   debounce((value, layerName) => onChangeLayer(layerName, "min", value), 500),
-   [],
+   debounce((value, layerName) => onChangeLayer(layerName, "min", value), 500),
+   [onChangeLayer],
  );

  const updateMaximumDebounced = useCallback(
-   debounce((value, layerName) => onChangeLayer(layerName, "max", value), 500),
-   [],
+   debounce((value, layerName) => onChangeLayer(layerName, "max", value), 500),
+   [onChangeLayer],
  );
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const updateMinimumDebounced = useCallback(
debounce((value, layerName) => onChangeLayer(layerName, "min", value), 500),
[],
);
updateMaximumDebounced = _.debounce(
(value, layerName) => this.props.onChangeLayer(layerName, "max", value),
500,
const updateMaximumDebounced = useCallback(
debounce((value, layerName) => onChangeLayer(layerName, "max", value), 500),
[],
);
const updateMinimumDebounced = useCallback(
debounce((value, layerName) => onChangeLayer(layerName, "min", value), 500),
[onChangeLayer],
);
const updateMaximumDebounced = useCallback(
debounce((value, layerName) => onChangeLayer(layerName, "max", value), 500),
[onChangeLayer],
);
🤖 Prompt for AI Agents
In frontend/javascripts/viewer/view/left-border-tabs/histogram_view.tsx around
lines 244 to 252, the debounced callbacks useCallback hooks have empty
dependency arrays but reference the onChangeLayer function, which can cause
stale closures if onChangeLayer changes. Fix this by adding onChangeLayer to the
dependency arrays of both useCallback hooks to ensure the latest version is used
in the debounced functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant