codeant-ai-for-open-source[bot] commented on code in PR #41551: URL: https://github.com/apache/superset/pull/41551#discussion_r3690968949
########## superset-frontend/src/features/versionHistory/canRestoreDashboard.ts: ########## @@ -0,0 +1,41 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import type { RootState } from 'src/dashboard/types'; + +/** + * Whether the current user may restore a version of this dashboard. + * + * The dashboard counterpart of `canOverwriteSlice`, and it excludes + * externally managed dashboards for the same reason: their source of truth + * lives outside Superset, so a restore would be overwritten again by the next + * sync. + * + * Exported as one selector because the gate has more than one consumer — the + * history panel and the preview banner — and the header menu's own + * `is_managed_externally` check already showed how easily those drift apart. + * The panel is reachable directly via `?version_history=true`, so a gate + * applied only where the menu entry renders is not applied at all. The restore + * endpoint checks editorship but not external management, which leaves this as + * the only guard. + */ +export const selectCanRestoreDashboard = (state: RootState): boolean => + (state.dashboardInfo?.dash_edit_perm ?? false) && + !state.dashboardInfo?.is_managed_externally; Review Comment: **Suggestion:** The client-side gate is narrower than the restore endpoint's authorization contract. `dash_edit_perm` is derived from `canUserEditDashboard`, which only checks the dashboard's `editors`, while the server's `is_editor` also accepts subjects from `extra_editors`. Users authorized through `EXTRA_EDITORS_RESOLVER` will therefore have Restore hidden or disabled even though the restore request is permitted. Use a permission value that includes extra editors, or expose the server's effective editorship result to the frontend. [api mismatch] <details> <summary><b>Severity Level:</b> Major ⚠️</summary> ```mdx - ❌ Extra-editor users cannot access dashboard restore. - ⚠️ Restore endpoint authorization and UI gating disagree. - ⚠️ Direct version-history URLs are rejected client-side. ``` </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=15957f9ac65547718d66fb8ea1eda91c&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=15957f9ac65547718d66fb8ea1eda91c&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <details> <summary><b>Prompt for AI Agent 🤖 </b></summary> ```mdx This is a comment left during a code review. **Path:** superset-frontend/src/features/versionHistory/canRestoreDashboard.ts **Line:** 37:39 **Comment:** *Api Mismatch: The client-side gate is narrower than the restore endpoint's authorization contract. `dash_edit_perm` is derived from `canUserEditDashboard`, which only checks the dashboard's `editors`, while the server's `is_editor` also accepts subjects from `extra_editors`. Users authorized through `EXTRA_EDITORS_RESOLVER` will therefore have Restore hidden or disabled even though the restore request is permitted. Use a permission value that includes extra editors, or expose the server's effective editorship result to the frontend. Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41551&comment_hash=aac59f9c60eefada2bba3f6af851f2c3223c2bab87c0a667b95e32d366d3bb81&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41551&comment_hash=aac59f9c60eefada2bba3f6af851f2c3223c2bab87c0a667b95e32d366d3bb81&reaction=dislike'>👎</a> ########## superset-frontend/src/features/versionHistory/sessionLogMiddleware.ts: ########## @@ -0,0 +1,86 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import type { Middleware } from 'redux'; +import { FeatureFlag, isFeatureEnabled } from '@superset-ui/core'; +import { t } from '@apache-superset/core/translation'; +import { appendVersionSessionLog, clearVersionSessionLog } from './reducer'; + +// Action types are inlined (rather than imported from the explore +// module) so this middleware does not pull explore code into every +// page's bundle; the store applies it globally. Exported so the test +// suite can assert they match the real explore constants — a rename +// there would otherwise silently kill the session log. +export const SET_FIELD_VALUE = 'SET_FIELD_VALUE'; +export const HYDRATE_EXPLORE = 'HYDRATE_EXPLORE'; + +interface SessionLogState { + user?: { firstName?: string; lastName?: string }; + explore?: { + controls?: Record<string, { label?: unknown } | undefined>; + }; +} + +function controlLabel(state: SessionLogState, controlName: string): string { + const label = state.explore?.controls?.[controlName]?.label; + return typeof label === 'string' && label + ? label + : controlName.replace(/_/g, ' '); +} + +function userName(state: SessionLogState): string | null { + const name = [state.user?.firstName, state.user?.lastName] + .filter(Boolean) + .join(' '); + return name || null; +} + +/** + * Records unsaved explore control changes in the version history + * session log ("Current version" section) and resets the log whenever + * the explore page (re)hydrates — initial load, save, or restore. + */ +export const versionSessionLogMiddleware: Middleware = + store => next => action => { + const result = next(action); + if (!isFeatureEnabled(FeatureFlag.VersionHistory)) { + return result; + } + if (action.type === HYDRATE_EXPLORE) { + store.dispatch(clearVersionSessionLog()); + } else if ( + action.type === SET_FIELD_VALUE && + typeof action.controlName === 'string' && + // Effects rewrite controls with no user gesture (transferred-control + // cleanup after load, derived values set alongside another control); + // logging those would tell the user they have unsaved edits they never + // made. Only user-initiated changes belong in the session log. + !action.programmatic + ) { Review Comment: **Suggestion:** The middleware treats every `SET_FIELD_VALUE` action without `programmatic: true` as a user edit, but the shared action creator defaults that property to false and existing non-user control rewrites still dispatch the action without the marker. Those rewrites will create phantom session-log entries, causing the UI to report unsaved changes and block restore operations on otherwise untouched charts. Mark every non-user dispatch at its source, or make the middleware distinguish user-originated actions rather than relying on an opt-in flag. [logic error] <details> <summary><b>Severity Level:</b> Major ⚠️</summary> ```mdx - ❌ Automatic datasource normalization blocks chart restoration. - ⚠️ Current version shows phantom control changes. - ⚠️ Users receive incorrect unsaved-change warnings. ``` </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=5b934d977cfb4714abf5140d0c6c7cac&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=5b934d977cfb4714abf5140d0c6c7cac&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <details> <summary><b>Prompt for AI Agent 🤖 </b></summary> ```mdx This is a comment left during a code review. **Path:** superset-frontend/src/features/versionHistory/sessionLogMiddleware.ts **Line:** 66:74 **Comment:** *Logic Error: The middleware treats every `SET_FIELD_VALUE` action without `programmatic: true` as a user edit, but the shared action creator defaults that property to false and existing non-user control rewrites still dispatch the action without the marker. Those rewrites will create phantom session-log entries, causing the UI to report unsaved changes and block restore operations on otherwise untouched charts. Mark every non-user dispatch at its source, or make the middleware distinguish user-originated actions rather than relying on an opt-in flag. Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41551&comment_hash=99b6939cd784d389acfc0ce2e0bcd5548a900aee8ac1a180ad8ec08d1ebfca23&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41551&comment_hash=99b6939cd784d389acfc0ce2e0bcd5548a900aee8ac1a180ad8ec08d1ebfca23&reaction=dislike'>👎</a> -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
