Skip to content

fix(risks): treatment plan disappears when switching strategies#2860

Merged
Marfuen merged 1 commit into
mainfrom
mariano/cs-402-bug-risk-treatment-plan-ui-bug
May 15, 2026
Merged

fix(risks): treatment plan disappears when switching strategies#2860
Marfuen merged 1 commit into
mainfrom
mariano/cs-402-bug-risk-treatment-plan-ui-bug

Conversation

@Marfuen
Copy link
Copy Markdown
Contributor

@Marfuen Marfuen commented May 15, 2026

Summary

  • Root cause: DescriptionEditor stored draft and mode as independent state synced to the value prop via 4 chained useEffects. When switching from a strategy with content to one without, mode flipped to 'edit' and the resync effect's edit-mode guard blocked reloading the original plan when switching back.
  • Fix: Rewrote DescriptionEditor to use a single draft: string | null state — null = preview mode (render value directly from props), string = edit mode. Eliminates all sync effects. Strategy changes remount the editor via key={strategy} so internal state initializes fresh.
  • Also: Hides the "Generate treatment plan" button for non-Mitigate strategies since regeneration always produces a mitigation plan and forces strategy to Mitigate.

What changed

Before After
4 useEffects + 2 refs for draft/mode sync 0 useEffects in DescriptionEditor (1 useLayoutEffect for textarea sizing)
draft + mode as separate state Single draft: string | null discriminated state
Regen transition detected via ref-tracking effect Draft cleared at point of user action (handleRegenerate)
eslint-disable on RegenProgress deps Proper dependency array

Test plan

  • Existing tests pass (10/10, 1 pre-existing failure on main unrelated)
  • New test: regenerate button hidden for Accept/Transfer strategies
  • New test: switching strategies shows correct description via strategyDescriptions
  • Manual: switch Mitigate → Accept → Mitigate, verify plan reappears
  • Manual: switch strategies rapidly, verify no data loss
  • Manual: regenerate treatment plan, verify new content appears in preview

🤖 Generated with Claude Code


Summary by cubic

Fixes disappearing treatment plans when switching strategies by simplifying the editor state and remounting per strategy. Also hides regeneration for non‑Mitigate strategies to prevent generating the wrong plan. Addresses CS‑402.

  • Bug Fixes

    • The plan now persists when toggling strategies; the editor uses a single draft: string | null state and remounts with key={strategy} to always reflect the correct description.
    • The "Generate/Regenerate" button is shown only for Mitigate, aligning with expected behavior and avoiding unintended strategy changes.
  • Refactors

    • Rewrote DescriptionEditor to remove four sync useEffects; only a useLayoutEffect remains for textarea sizing.
    • Draft is cleared on regenerate at user action, onRegenerate/regenerating are optional, and RegenProgress now has correct effect dependencies.

Written for commit 64978cb. Summary will update on new commits. Review in cubic

Root cause: DescriptionEditor stored `draft` and `mode` as independent
state synced to the `value` prop via a chain of 4 useEffects. When
switching from a strategy with content to one without, mode flipped to
'edit' and the resync effect's edit-mode guard blocked reloading the
original plan when switching back.

Rewrote the component to use a single `draft: string | null` state —
null means preview mode (render value directly from props), string
means edit mode. This eliminates the 4 sync effects entirely and makes
the state model trivial: enter edit → copy value into draft, save →
push draft up and clear, cancel → clear.

Strategy changes remount the editor via key={strategy} so internal
state initializes fresh from the new value.

Also hides the "Generate treatment plan" button for non-Mitigate
strategies since regeneration always produces a mitigation plan.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@linear
Copy link
Copy Markdown

linear Bot commented May 15, 2026

CS-402

@vercel
Copy link
Copy Markdown

vercel Bot commented May 15, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
app Ready Ready Preview, Comment May 15, 2026 4:14pm
comp-framework-editor Ready Ready Preview, Comment May 15, 2026 4:14pm
1 Skipped Deployment
Project Deployment Actions Updated (UTC)
portal Skipped Skipped May 15, 2026 4:14pm

Request Review

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

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

cubic analysis

No issues found across 3 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Linked issue analysis

Linked issue: CS-402: [BUG] - Risk treatment plan UI bug

Status Acceptance criteria Notes
⚠️ Treatment plan reappears when switching Mitigate → Accept → Mitigate (no disappearance or unexpected mode flip). The DescriptionEditor was rewritten to use a single discriminated draft state and TreatmentPlanTab now remounts the editor via key={strategy}, which addresses the root cause described in the PR. However the PR's manual verification for this scenario is still unchecked and there is no automated test explicitly asserting the Mitigate→Accept→Mitigate reappearance behavior.
Hide the 'Generate treatment plan' / 'Regenerate with AI' button for non‑Mitigate strategies. PR changes TreatmentPlanTab to pass onRegenerate only for Mitigate strategies and adds a unit test that asserts the regenerate button is hidden for non‑Mitigate strategies.
Switching strategies shows the correct description from strategyDescriptions. A unit test was added that renders the component with strategyDescriptions and asserts the shown text updates when the strategy radio is clicked.
⚠️ Regenerate treatment plan displays the new AI content in the preview after completion. DescriptionEditor now clears its draft on regenerate (setDraft(null)) so the parent-provided value/regenRun flow can surface the new content; RegenProgress hooks into run status. But there is no automated test validating that a completed regen updates the preview, and the PR's manual check 'regenerate treatment plan, verify new content appears in preview' is unchecked.
⚠️ Rapidly switching strategies does not cause data loss. Refactor (single draft state + remount by key) is designed to prevent the previous flaky sync behavior, but there is no automated test for rapid switching and the corresponding manual check remains unchecked.

Re-trigger cubic

@Marfuen Marfuen merged commit 9b052f5 into main May 15, 2026
10 checks passed
@Marfuen Marfuen deleted the mariano/cs-402-bug-risk-treatment-plan-ui-bug branch May 15, 2026 16:17
claudfuen pushed a commit that referenced this pull request May 15, 2026
## [3.55.2](v3.55.1...v3.55.2) (2026-05-15)

### Bug Fixes

* **people:** send single combined email for admin+employee invites with portal ([#2857](#2857)) ([a4adb20](a4adb20))
* **risks:** treatment plan disappears when switching strategies ([#2860](#2860)) ([9b052f5](9b052f5))
@claudfuen
Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 3.55.2 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

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.

2 participants