Skip to content

Actions: Retain newest items when limit is reached#34081

Closed
anujboddu wants to merge 2 commits into
storybookjs:nextfrom
anujboddu:fix/34052-actions-limit-immutability
Closed

Actions: Retain newest items when limit is reached#34081
anujboddu wants to merge 2 commits into
storybookjs:nextfrom
anujboddu:fix/34052-actions-limit-immutability

Conversation

@anujboddu
Copy link
Copy Markdown

@anujboddu anujboddu commented Mar 9, 2026

What I changed

Addresses #34052 by fixing action log retention behavior and removing object mutation in ActionLogger.

✅ Fixes in ActionLogger

  • Avoids mutating previous state entries (previous.count++)
  • Avoids mutating incoming action objects (action.count = 1)
  • Uses immutable updates for both merge and append paths
  • Keeps newest entries when the panel reaches actions.limit via slice(-limit)
  • Handles limit <= 0 safely by returning an empty list

File:

  • code/core/src/actions/containers/ActionLogger/index.tsx

✅ Story to showcase behavior when limit is reached

Added a story that sets actions.limit = 3 and emits 5 clicks to demonstrate that only the latest 3 actions are retained.

File:

  • code/core/template/stories/parameters-actions.stories.ts

✅ Documentation updates

Documented actions parameters:

  • limit (default 50) and that it keeps most recent actions when full
  • clearOnStoryChange (default true)

File:

  • docs/essentials/actions.mdx

Why

The previous implementation made a shallow array copy, then mutated nested action objects, which breaks immutability expectations and can cause subtle state issues. It also trimmed with slice(0, limit), which retains oldest entries and drops newest at capacity.

Notes

  • Installed workspace dependencies with yarn install.
  • I couldn't run a small file-scoped lint task directly in this workspace setup; relying on CI to run full checks.

Fixes #34052

Summary by CodeRabbit

  • New Features

    • Actions addon now enforces a configurable limit on retained actions (default: 50) and clears history when limit is non-positive.
    • Repeated identical actions are aggregated (counted) instead of listed repeatedly.
    • Added a Storybook story demonstrating the limit behavior.
  • Documentation

    • Documented new limit and clearOnStoryChange Actions addon parameters.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Mar 9, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: d4ce706a-0de1-4c2d-a3fb-cb7ba45f7fc7

📥 Commits

Reviewing files that changed from the base of the PR and between 82b8ef6 and 567c035.

📒 Files selected for processing (2)
  • code/core/src/actions/containers/ActionLogger/index.tsx
  • code/core/template/stories/parameters-actions.stories.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • code/core/template/stories/parameters-actions.stories.ts
  • code/core/src/actions/containers/ActionLogger/index.tsx

📝 Walkthrough

Walkthrough

Enforces and bounds action history in the ActionLogger by introducing a configurable limit and immutable updates; adds a Storybook story exercising the limit and documents new limit and clearOnStoryChange parameters.

Changes

Cohort / File(s) Summary
ActionLogger Implementation
code/core/src/actions/containers/ActionLogger/index.tsx
Introduce limit = action.options.limit; return [] if limit <= 0; update history immutably by incrementing last entry's count or appending a new entry; ensure returned history is sliced to the most recent limit entries.
Test Story
code/core/template/stories/parameters-actions.stories.ts
Add LimitRetainsNewest story exporting args and parameters.actions.limit = 3 with a play function that clicks a button five times to exercise retention behavior.
Documentation
docs/essentials/actions.mdx
Add API docs for two new parameters: limit (number, default 50) controlling retained action count, and clearOnStoryChange (boolean, default true) controlling panel clearing on story navigation.

Sequence Diagram(s)

(omitted)

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 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.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@code/core/src/actions/containers/ActionLogger/index.tsx`:
- Around line 40-56: action.options.limit is used directly causing
NaN/fractional values to bypass the limit guard and produce incorrect slice
behavior; normalize it first (e.g., coerce to a finite non-negative integer
using Number.isFinite + Math.trunc/Math.max) and then use that normalized value
in the existing guard (limit <= 0) and both slice calls
(updatedActions.slice(-limit) and [...prevActions, nextAction].slice(-limit));
update references to the local variable named limit in this block so previous,
safeDeepEqual, prevActions, updatedActions, and nextAction logic remains
unchanged but uses the sanitized limit.

In `@code/core/template/stories/parameters-actions.stories.ts`:
- Line 35: The play function currently destructures canvasElement from an
untyped parameter causing an implicit any; import PlayFunctionContext and
annotate the play parameter with that type (e.g., change the play signature to
accept a PlayFunctionContext and then destructure canvasElement from it) and add
the corresponding import for PlayFunctionContext so the parameter is strongly
typed like the other stories.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: f344ddba-000f-4cf2-9c31-b3c294f733b7

📥 Commits

Reviewing files that changed from the base of the PR and between f159b96 and 82b8ef6.

📒 Files selected for processing (3)
  • code/core/src/actions/containers/ActionLogger/index.tsx
  • code/core/template/stories/parameters-actions.stories.ts
  • docs/essentials/actions.mdx

Comment thread code/core/src/actions/containers/ActionLogger/index.tsx Outdated
Comment thread code/core/template/stories/parameters-actions.stories.ts Outdated
@anujboddu
Copy link
Copy Markdown
Author

anujboddu commented Mar 9, 2026

Addressed the CodeRabbit review items in the latest commit:

  • Normalized actions.limit before using it (finite + trunc + clamp) so NaN/fractional values can't bypass retention behavior.
  • Typed the story play context with PlayFunctionContext<any> in parameters-actions.stories.ts to match other template stories.

@valentinpalkovic valentinpalkovic changed the title fix(actions): retain newest items when limit is reached Actions: retain newest items when limit is reached Mar 10, 2026
@valentinpalkovic valentinpalkovic moved this to Empathy Queue (prioritized) in Core Team Projects Mar 10, 2026
@Sidnioulz Sidnioulz changed the title Actions: retain newest items when limit is reached Actions: Retain newest items when limit is reached Mar 11, 2026
@Sidnioulz Sidnioulz self-assigned this Mar 11, 2026
@Sidnioulz Sidnioulz added the ci:normal Run our default set of CI jobs (choose this for most PRs). label Mar 11, 2026
@valentinpalkovic
Copy link
Copy Markdown
Contributor

Closing in favour of #34053

@github-project-automation github-project-automation Bot moved this from Empathy Queue (prioritized) to Done in Core Team Projects Mar 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

addon: actions bug ci:normal Run our default set of CI jobs (choose this for most PRs).

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

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

3 participants