Skip to content

fix(actions): correct options.limit application in ActionLogger#34054

Closed
mibragimov wants to merge 1 commit into
storybookjs:nextfrom
mibragimov:fix/issue-34052
Closed

fix(actions): correct options.limit application in ActionLogger#34054
mibragimov wants to merge 1 commit into
storybookjs:nextfrom
mibragimov:fix/issue-34052

Conversation

@mibragimov
Copy link
Copy Markdown

@mibragimov mibragimov commented Mar 6, 2026

Summary

Fixes a logic bug in the ActionLogger container where options.limit was not correctly applied when trimming the actions log.

The Bug

Actions are appended to the end of the array via newActions.push(action). The original code used:

return newActions.slice(0, action.options.limit);

slice(0, limit) keeps the first limit items (the oldest actions). Once the limit was reached, new actions were silently dropped rather than the oldest ones being evicted. This meant the actions panel would stop updating once it hit the limit.

The Fix

return action.options.limit ? newActions.slice(-action.options.limit) : newActions;

slice(-limit) keeps the last limit items (the most recent actions), so the panel always shows the latest actions and correctly evicts the oldest ones. The falsy guard prevents slice(-0) (which returns an empty array) when limit is 0 or undefined.

Closes #34052

Summary by CodeRabbit

  • Bug Fixes
    • Corrected action logging behavior: when reaching the action limit, the most recent actions are now retained instead of the earliest ones.

The actions log was not correctly applying the options.limit when
filtering log entries. The bug was in the slice direction:

- Actions are appended to the END of the array via newActions.push()
- slice(0, limit) keeps the FIRST 'limit' items (oldest actions)
- Once the limit was reached, NEW actions were silently dropped
  instead of the oldest being evicted

Fix: use slice(-limit) to keep the most recent 'limit' actions.
Also guard against limit being undefined/falsy to avoid slice(-0)
which would return an empty array.

Fixes storybookjs#34052
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Mar 6, 2026

📝 Walkthrough

Walkthrough

Modified the ActionLogger's action truncation logic to retain the most recent N actions instead of the oldest N actions when a limit is applied, changing from slice(0, limit) to slice(-limit).

Changes

Cohort / File(s) Summary
ActionLogger Update
code/core/src/actions/containers/ActionLogger/index.tsx
Changed action retention behavior from keeping the first N items to keeping the last N items when truncating the action list under a specified limit.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Possibly related PRs

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


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)

39-46: Immutability violation: mutating objects in state.

The linked issue #34052 also identifies that previous.count++ (line 42) and action.count = 1 (line 44) mutate objects directly. The shallow copy [...prevActions] doesn't protect nested object references, so previous still points to an object in the previous state. This violates React's immutability principles and could cause subtle bugs with re-renders or stale closures.

If addressing this is planned for a follow-up, consider leaving a TODO comment. Otherwise:

♻️ Proposed fix for immutability
   setActions((prevActions) => {
-    const newActions = [...prevActions];
-    const previous = newActions.length && newActions[newActions.length - 1];
-    if (previous && safeDeepEqual(previous.data, action.data)) {
-      previous.count++;
-    } else {
-      action.count = 1;
-      newActions.push(action);
-    }
+    const previous = prevActions.length && prevActions[prevActions.length - 1];
+    let newActions: ActionDisplay[];
+    if (previous && safeDeepEqual(previous.data, action.data)) {
+      newActions = [
+        ...prevActions.slice(0, -1),
+        { ...previous, count: previous.count + 1 },
+      ];
+    } else {
+      newActions = [...prevActions, { ...action, count: 1 }];
+    }
     return action.options.limit ? newActions.slice(-action.options.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 39 -
46, The code mutates existing objects in state via previous.count++ and
action.count = 1; instead, create new object copies before changing counts: when
merging with the previous item (identified by previous and safeDeepEqual) clone
previous (e.g., {...previous}) and increment the clone's count, replacing the
last element in newActions with that clone; when adding the incoming action
(symbol action) clone it with count: 1 (e.g., {...action, count: 1}) and push
the clone into newActions. Ensure no direct writes to previous or action occur;
if you intend to defer this change, add a TODO noting the immutability fix.
🤖 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 39-46: The code mutates existing objects in state via
previous.count++ and action.count = 1; instead, create new object copies before
changing counts: when merging with the previous item (identified by previous and
safeDeepEqual) clone previous (e.g., {...previous}) and increment the clone's
count, replacing the last element in newActions with that clone; when adding the
incoming action (symbol action) clone it with count: 1 (e.g., {...action, count:
1}) and push the clone into newActions. Ensure no direct writes to previous or
action occur; if you intend to defer this change, add a TODO noting the
immutability fix.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 83207b93-d27e-4c45-b5f2-9543615f3eee

📥 Commits

Reviewing files that changed from the base of the PR and between 90721f5 and 80b5463.

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

@mibragimov mibragimov closed this Mar 6, 2026
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

1 participant