Skip to content

fix(actions): prevent state mutation and keep newest actions on limit#34218

Closed
Sigmabrogz wants to merge 1 commit into
storybookjs:nextfrom
Sigmabrogz:fix/issue-34052-action-logger
Closed

fix(actions): prevent state mutation and keep newest actions on limit#34218
Sigmabrogz wants to merge 1 commit into
storybookjs:nextfrom
Sigmabrogz:fix/issue-34052-action-logger

Conversation

@Sigmabrogz
Copy link
Copy Markdown

@Sigmabrogz Sigmabrogz commented Mar 19, 2026

Fixes #34052. This PR prevents state mutations when updating ActionLogger's actions in React and fixes the slice logic so that the newest actions are kept when reaching the limit (using slice(-limit) instead of slice(0, limit)).

I have tested these changes to make sure they follow the immutability rules and ensure that action.count does not mutate the original objects.

(Auto-generated AI PR by Sigma)

Summary by CodeRabbit

  • Bug Fixes

    • Fixed action logging to retain the most recent actions when limits are applied.
    • Improved state consistency during action tracking operations.
  • Refactor

    • Streamlined action state update logic for better reliability.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Mar 19, 2026

📝 Walkthrough

Walkthrough

The addAction state update logic in the ActionLogger component was refactored to prevent mutation of prior action objects and improve limit handling. It now safely computes the previous action reference, creates new objects when incrementing counts, and uses optional chaining with slice operations for limit-based trimming.

Changes

Cohort / File(s) Summary
State Update Refactoring
code/core/src/actions/containers/ActionLogger/index.tsx
Rewrote addAction logic to avoid object mutations: computes previous as the last action or null, replaces/appends objects instead of mutating, and changes limit trimming from prefix slice to suffix slice with optional chaining (action.options?.limit).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

📝 Coding Plan
  • Generate coding plan for human review comments

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

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
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.

🧹 Nitpick comments (1)
code/core/src/actions/containers/ActionLogger/index.tsx (1)

49-50: Minor inconsistency: optional chaining on options is unnecessary.

Per the ActionDisplay interface in code/core/src/actions/models/ActionDisplay.ts, options is a required field (not optional), while limit within ActionOptions can be undefined due to Partial<Options>. The optional chaining on options is inconsistent with line 55 which accesses actions[0].options.clearOnStoryChange directly.

The slice fix using slice(-limit) correctly keeps the most recent N actions, aligning with the rolling window behavior intended by the default limit: 50.

♻️ Suggested consistency fix
-      const limit = action.options?.limit;
+      const limit = action.options.limit;
       return limit ? newActions.slice(-limit) : newActions;
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@code/core/src/actions/containers/ActionLogger/index.tsx` around lines 49 -
50, The optional chaining on action.options is unnecessary because
ActionDisplay.options is required; change "const limit = action.options?.limit"
to "const limit = action.options.limit" in the ActionLogger logic so it
consistently accesses the required options field (keep the existing conditional
return using limit and the slice behavior: newActions.slice(-limit) when limit
is truthy, otherwise return newActions).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@code/core/src/actions/containers/ActionLogger/index.tsx`:
- Around line 49-50: The optional chaining on action.options is unnecessary
because ActionDisplay.options is required; change "const limit =
action.options?.limit" to "const limit = action.options.limit" in the
ActionLogger logic so it consistently accesses the required options field (keep
the existing conditional return using limit and the slice behavior:
newActions.slice(-limit) when limit is truthy, otherwise return newActions).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 888fd826-4ab8-448c-b73f-45b1669503ef

📥 Commits

Reviewing files that changed from the base of the PR and between 1240072 and fe3a0a6.

📒 Files selected for processing (1)
  • code/core/src/actions/containers/ActionLogger/index.tsx

@valentinpalkovic
Copy link
Copy Markdown
Contributor

Closing in favour of #34053

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.

[Bug]: Potential logic bug with options.limit in Actions addon

2 participants