Skip to content

refactor: decouple discussions data-fetching from trigger into widget prefetch lifecycle#1895

Open
awais-ansari wants to merge 1 commit intoopenedx:masterfrom
awais-ansari:aansari/clean-up-hide-upsell
Open

refactor: decouple discussions data-fetching from trigger into widget prefetch lifecycle#1895
awais-ansari wants to merge 1 commit intoopenedx:masterfrom
awais-ansari:aansari/clean-up-hide-upsell

Conversation

@awais-ansari
Copy link
Copy Markdown
Contributor

Summary

Decouples the discussions widget's data-fetching side effect from DiscussionsTrigger
into the widget's prefetch lifecycle hook, which SidebarContextProvider calls on mount.

Motivation

Previously, DiscussionsTrigger owned its own useEffect to dispatch
getCourseDiscussionTopics. This had two problems:

  1. Data-fetch tied to render — if the trigger didn't render (e.g., widget
    marked unavailable), topics were never loaded.
  2. Sidebar flickeringdiscussionsIsAvailable checked unit-level topic data
    (unit?.id && unit?.enabledInContext), causing the DISCUSSIONS widget to toggle
    availability on every unit navigation and triggering the priority cascade
    (COURSE_OUTLINE fallback → switch back).

Changes

File Change
widgets/discussions/widgetConfig.js Add prefetch function; change isAvailable to course-level check (baseUrl && hasDiscussionTab)
widgets/discussions/DiscussionsTrigger.jsx Remove useEffect, useDispatch, getConfig, ensureConfig, useMemo — trigger is now a pure render component
sidebar/SidebarContextProvider.jsx Add useEffect to call widget.prefetch() on mount; add useDispatch; remove unit from getAvailableWidgets context
sidebar/SidebarContextProvider.test.jsx Add mocks for react-redux, @edx/frontend-platform, and thunks
widgets/discussions/DiscussionsTrigger.test.jsx Pre-dispatch getCourseDiscussionTopics in beforeEach; fix async assertions
sidebar/hooks/useSidebarSync.js Fix JSDoc: FunctionObject for ref params
sidebar/hooks/useResponsiveBehavior.js Fix JSDoc: FunctionObject for ref param
sidebar/README.md Add prefetch to widget structure; update context object (remove unit); add prefetch docs
sidebar/ARCHITECTURE.md Add prefetch to SidebarContextProvider responsibilities
widgets/discussions/README.md Update isAvailable signature; add prefetch section; document two-tier availability

Behavioral Change

  • isAvailable scope: Unit-level → course-level. The DISCUSSIONS trigger stays
    visible across the entire course. The trigger/sidebar components still check
    topic?.id && topic?.enabledInContext at render time, showing nothing for units
    without a topic.
  • No breaking changes to external widget API — prefetch is optional.

Testing

  • All existing sidebar + discussions tests pass (12/12 + 2/2)
  • No ESLint errors
  • Manual verification: trigger bar stable during unit navigation

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 9, 2026

Codecov Report

❌ Patch coverage is 93.33333% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 91.27%. Comparing base (0664dc3) to head (d992898).

Files with missing lines Patch % Lines
...urseware/course/sidebar/SidebarContextProvider.jsx 80.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1895   +/-   ##
=======================================
  Coverage   91.27%   91.27%           
=======================================
  Files         343      343           
  Lines        5766     5768    +2     
  Branches     1387     1384    -3     
=======================================
+ Hits         5263     5265    +2     
  Misses        484      484           
  Partials       19       19           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

1 participant