codeant-ai-for-open-source[bot] commented on code in PR #41551:
URL: https://github.com/apache/superset/pull/41551#discussion_r3707432018


##########
superset-frontend/src/features/versionHistory/api.ts:
##########
@@ -0,0 +1,396 @@
+/**
+ * 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 { JsonObject, SupersetClient } from '@superset-ui/core';
+import rison from 'rison';
+import { t } from '@apache-superset/core/translation';
+import { DASHBOARD_GET_COLUMNS } from 'src/hooks/apiResources/dashboards';
+import { CHART_TYPE, MARKDOWN_TYPE } from 'src/dashboard/util/componentTypes';
+import type { ExploreResponsePayload } from 'src/explore/types';
+import type {
+  HydrateChartData,
+  HydrateDashboardData,
+} from 'src/dashboard/actions/hydrate';
+import type { Dashboard } from 'src/types/Dashboard';
+import type {
+  ActivityEntityKind,
+  ActivityInclude,
+  ActivityResponse,
+  ChartVersionSnapshot,
+  DashboardVersionSnapshot,
+  VersionedEntityType,
+  VersionSnapshot,
+} from './types';
+
+const API_RESOURCE: Record<VersionedEntityType, string> = {
+  chart: 'chart',
+  dashboard: 'dashboard',
+};
+
+export interface FetchActivityOptions {
+  include?: ActivityInclude;
+  page?: number;
+  pageSize?: number;
+  /**
+   * Case-insensitive free-text search over the full history (not just the
+   * loaded pages) — the server filters before paginating, so `count`
+   * reflects matches. Debounced upstream.
+   */
+  q?: string;
+}
+
+export async function fetchActivity(
+  entityType: VersionedEntityType,
+  uuid: string,
+  { include = 'all', page = 0, pageSize = 25, q }: FetchActivityOptions = {},
+): Promise<ActivityResponse> {
+  const params = new URLSearchParams({
+    include,
+    page: String(page),
+    page_size: String(pageSize),
+  });
+  const trimmedQ = q?.trim();
+  if (trimmedQ) {
+    params.set('q', trimmedQ);
+  }
+  const { json } = await SupersetClient.get({
+    endpoint: 
`/api/v1/${API_RESOURCE[entityType]}/${encodeURIComponent(uuid)}/activity/?${params.toString()}`,
+  });
+  return json as ActivityResponse;
+}
+
+export async function fetchVersionSnapshot(
+  entityType: 'chart',
+  uuid: string,
+  versionUuid: string,
+): Promise<ChartVersionSnapshot>;
+export async function fetchVersionSnapshot(
+  entityType: 'dashboard',
+  uuid: string,
+  versionUuid: string,
+): Promise<DashboardVersionSnapshot>;
+export async function fetchVersionSnapshot(
+  entityType: VersionedEntityType,
+  uuid: string,
+  versionUuid: string,
+): Promise<VersionSnapshot>;
+export async function fetchVersionSnapshot(
+  entityType: VersionedEntityType,
+  uuid: string,
+  versionUuid: string,
+): Promise<VersionSnapshot> {
+  const { json } = await SupersetClient.get({
+    endpoint: 
`/api/v1/${API_RESOURCE[entityType]}/${encodeURIComponent(uuid)}/versions/${encodeURIComponent(versionUuid)}/`,
+  });
+  return (json as { result: VersionSnapshot }).result;
+}
+
+export async function restoreVersion(
+  entityType: VersionedEntityType,
+  uuid: string,
+  versionUuid: string,
+): Promise<{ message: string }> {
+  const { json } = await SupersetClient.post({
+    endpoint: 
`/api/v1/${API_RESOURCE[entityType]}/${encodeURIComponent(uuid)}/versions/${encodeURIComponent(versionUuid)}/restore`,
+  });
+  return json as { message: string };
+}
+
+/** Creates a new chart from a version snapshot; returns the new chart id. */
+export async function createChartFromSnapshot(
+  snapshot: ChartVersionSnapshot,
+  name: string,
+): Promise<number> {
+  // The chart POST requires all three; the version table allows null for each
+  // (a delete version carries nulls throughout). Fail here with something the
+  // caller can turn into a toast rather than sending a payload the API will
+  // reject with a validation error the user cannot act on.
+  if (
+    snapshot.viz_type == null ||
+    snapshot.datasource_id == null ||
+    snapshot.datasource_type == null
+  ) {
+    throw new Error(
+      'This version does not record a visualization type and dataset, so a new 
chart cannot be built from it',
+    );
+  }
+  const { json } = await SupersetClient.post({
+    endpoint: '/api/v1/chart/',
+    jsonPayload: {
+      slice_name: name,
+      viz_type: snapshot.viz_type,
+      datasource_id: snapshot.datasource_id,
+      datasource_type: snapshot.datasource_type,
+      ...(snapshot.params != null && { params: snapshot.params }),
+      ...(snapshot.query_context != null && {
+        query_context: snapshot.query_context,
+      }),
+      ...(snapshot.description != null && {
+        description: snapshot.description,
+      }),
+      ...(snapshot.cache_timeout != null && {
+        cache_timeout: snapshot.cache_timeout,
+      }),
+    },
+  });
+  return (json as { id: number }).id;
+}
+
+/** The theme shape `dashboardInfo` holds, keyed by id in the snapshot. */
+export type DashboardTheme = NonNullable<Dashboard['theme']>;
+
+/**
+ * Resolves a snapshot's `theme_id` to the theme object hydration expects.
+ * The version table stores the foreign key, not the theme, so a snapshot
+ * taken under a different theme than the live dashboard needs one lookup.
+ */
+export async function fetchDashboardTheme(
+  themeId: number,
+): Promise<DashboardTheme> {
+  const { json } = await SupersetClient.get({
+    endpoint: `/api/v1/theme/${themeId}`,
+  });
+  const { id, result } = json as {
+    id: number;
+    result: Omit<DashboardTheme, 'id'>;
+  };
+  return { ...result, id };

Review Comment:
   **Suggestion:** The theme detail API returns the theme object under 
`result`, including its `id`; this destructuring instead reads a nonexistent 
top-level `json.id` and then overwrites any returned ID with `undefined`. A 
dashboard preview using a different theme therefore receives a theme without an 
ID, breaking theme identity and subsequent theme-dependent hydration logic. 
Read the ID from `result` (or preserve `result.id`) when constructing the 
returned theme. [api mismatch]
   
   <details>
   <summary><b>Severity Level:</b> Major ⚠️</summary>
   
   ```mdx
   - ⚠️ Dashboard version previews with changed themes lose theme identity.
   - ⚠️ Subsequent theme comparisons can treat the preview theme as unmatched.
   - ⚠️ Historical dashboard hydration receives an incomplete theme object.
   ```
   </details>
   
   [![Fix in 
Cursor](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-cursor-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=de1499bd125e4298ac72df90ba78339c&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 [![Fix in VSCode 
Claude](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-vscode-claude-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=de1499bd125e4298ac72df90ba78339c&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/api.ts
   **Line:** 168:172
   **Comment:**
        *Api Mismatch: The theme detail API returns the theme object under 
`result`, including its `id`; this destructuring instead reads a nonexistent 
top-level `json.id` and then overwrites any returned ID with `undefined`. A 
dashboard preview using a different theme therefore receives a theme without an 
ID, breaking theme identity and subsequent theme-dependent hydration logic. 
Read the ID from `result` (or preserve `result.id`) when constructing the 
returned theme.
   
   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=60c12a526c1dc119bb9382bb96e75c4790fd69e43a7fc065b694e8afca9d0ff5&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41551&comment_hash=60c12a526c1dc119bb9382bb96e75c4790fd69e43a7fc065b694e8afca9d0ff5&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]

Reply via email to