fix(sql-lab): prevent crash when host shell lacks useAppDispatch export#40591
Conversation
In Module Federation deployments where the host shell shares src/views/store as a singleton, a version skew between the shell and the SQL Lab remote chunk can leave useAppDispatch undefined at runtime. This causes `TypeError: o.useAppDispatch is not a function` when any SQL Lab component tries to render. Root cause: commit 785a08c added useAppDispatch to store.ts and commit 6842bb3 migrated all SQL Lab components to call it. When a host shell is built against a version of store.ts that predates 785a08c, the shared store module it provides has no useAppDispatch export. SQL Lab (built from the newer code) destructures an undefined value and then immediately tries to call it. Fix: introduce a thin shim at src/SqlLab/hooks/useAppDispatch.ts that re-exports useAppDispatch with a nullish-coalesce fallback to useDispatch. All SQL Lab components now import from the shim instead of directly from src/views/store. In a correctly up-to-date deployment the shim is transparent; in a version-skewed deployment SQL Lab continues to work (with untyped dispatch) rather than crashing. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR hardens SQL Lab against Module Federation version skew by decoupling SQL Lab’s dispatch hook usage from the host shell’s shared src/views/store singleton. It introduces a SQL Lab–local useAppDispatch shim that falls back to react-redux’s useDispatch, preventing a runtime crash when the host store module is older and lacks the useAppDispatch export.
Changes:
- Added
src/SqlLab/hooks/useAppDispatch.tsto provide a SQL Lab–localuseAppDispatchimplementation without runtime imports fromsrc/views/store. - Updated SQL Lab components to import
useAppDispatchfrom the new shim instead ofsrc/views/store. - Left existing selector usage (
useAppSelector) unchanged where needed.
Reviewed changes
Copilot reviewed 20 out of 20 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| superset-frontend/src/SqlLab/hooks/useAppDispatch.ts | Adds SQL Lab-local dispatch hook shim using react-redux useDispatch with AppDispatch as a type-only import. |
| superset-frontend/src/SqlLab/components/TablePreview/index.tsx | Switches useAppDispatch import to SQL Lab-local shim. |
| superset-frontend/src/SqlLab/components/TableExploreTree/useTreeData.ts | Switches useAppDispatch import to SQL Lab-local shim. |
| superset-frontend/src/SqlLab/components/TableExploreTree/index.tsx | Switches useAppDispatch import to SQL Lab-local shim. |
| superset-frontend/src/SqlLab/components/TableElement/index.tsx | Switches useAppDispatch import to SQL Lab-local shim. |
| superset-frontend/src/SqlLab/components/SqlEditorTopBar/useDatabaseSelector.ts | Switches useAppDispatch import to SQL Lab-local shim. |
| superset-frontend/src/SqlLab/components/SqlEditorTabHeader/index.tsx | Switches useAppDispatch import to SQL Lab-local shim. |
| superset-frontend/src/SqlLab/components/SqlEditorLeftBar/index.tsx | Switches useAppDispatch import to SQL Lab-local shim. |
| superset-frontend/src/SqlLab/components/SqlEditor/index.tsx | Switches useAppDispatch import to SQL Lab-local shim. |
| superset-frontend/src/SqlLab/components/SouthPane/index.tsx | Switches useAppDispatch import to SQL Lab-local shim. |
| superset-frontend/src/SqlLab/components/SaveDatasetModal/index.tsx | Switches only useAppDispatch to SQL Lab-local shim (keeps useAppSelector from store). |
| superset-frontend/src/SqlLab/components/ResultSet/index.tsx | Switches useAppDispatch import to SQL Lab-local shim. |
| superset-frontend/src/SqlLab/components/QueryTable/index.tsx | Switches useAppDispatch import to SQL Lab-local shim. |
| superset-frontend/src/SqlLab/components/QueryLimitSelect/index.tsx | Switches useAppDispatch import to SQL Lab-local shim. |
| superset-frontend/src/SqlLab/components/QueryAutoRefresh/index.tsx | Switches useAppDispatch import to SQL Lab-local shim. |
| superset-frontend/src/SqlLab/components/PopEditorTab/index.tsx | Switches useAppDispatch import to SQL Lab-local shim. |
| superset-frontend/src/SqlLab/components/ExploreCtasResultsButton/index.tsx | Switches useAppDispatch import to SQL Lab-local shim. |
| superset-frontend/src/SqlLab/components/EditorWrapper/useKeywords.ts | Switches useAppDispatch import to SQL Lab-local shim. |
| superset-frontend/src/SqlLab/components/EditorWrapper/index.tsx | Switches useAppDispatch import to SQL Lab-local shim. |
| superset-frontend/src/SqlLab/components/EditorAutoSync/index.tsx | Switches useAppDispatch import to SQL Lab-local shim. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #40591 +/- ##
==========================================
- Coverage 63.94% 63.94% -0.01%
==========================================
Files 2658 2660 +2
Lines 143011 143008 -3
Branches 32866 32866
==========================================
- Hits 91454 91451 -3
Misses 49994 49994
Partials 1563 1563
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Code Review Agent Run #d5e2a2Actionable Suggestions - 0Review Details
Bito Usage GuideCommands Type the following command in the pull request comment and save the comment.
Refer to the documentation for additional commands. Configuration This repository uses Documentation & Help |
| import { styled } from '@apache-superset/core/theme'; | ||
| import { extendedDayjs as dayjs } from '@superset-ui/core/utils/dates'; | ||
| import { useAppDispatch, useAppSelector } from 'src/views/store'; | ||
| import { useAppSelector } from 'src/views/store'; |
There was a problem hiding this comment.
useAppSelector still imports from src/views/store, could this also crash the app if shell lacks useAppSelector?
|
The |
rebenitez1802
left a comment
There was a problem hiding this comment.
Left a comment that is worth checking before merging otherwise, LGTM
✅ Deploy Preview for superset-docs-preview ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Code Review Agent Run #b9d9a9Actionable Suggestions - 0Review Details
Bito Usage GuideCommands Type the following command in the pull request comment and save the comment.
Refer to the documentation for additional commands. Configuration This repository uses Documentation & Help |
|
Addressed the SQL Lab concern by routing both I also live-validated this against a local Superset OSS environment: SQL Lab loads, the SQL editor mounts after adding a tab, and there are no |
SUMMARY
Fixes
TypeError: o.useAppDispatch is not a functionwhen loading SQL Lab in Module Federation deployments where the host shell shares an oldersrc/views/storesingleton.The SQL Lab remote can be built from a version that expects
useAppDispatch, while the host shell may provide a shared store module built before that export existed. This PR routes SQL Lab dispatch usage through a SQL Lab-local shim that falls back toreact-redux'suseDispatch, so SQL Lab does not crash under that version skew.The shim imports
AppDispatchas a type only and avoids runtime imports fromsrc/views/store. That keeps the fix isolated to SQL Lab without introducing a store bootstrap cycle throughstore.ts -> persistSqlLabStateEnhancer -> EditorAutoSync -> useAppDispatch.BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
Not applicable.
TESTING INSTRUCTIONS
Manual verification:
src/views/storeshared singleton from beforeuseAppDispatchwas exported.TypeError: useAppDispatch is not a function.Local checks run:
npm run lint -- src/SqlLab/hooks/useAppDispatch.tsgit diff --checkADDITIONAL INFORMATION