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


##########
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:
   ✅ **Customized review instruction saved!**
   
   **Instruction:**
   > Do not flag using the top-level response ID for ThemeRestApi responses 
when the pinned Flask-AppBuilder contract supplies both response.id and 
result.id with the same primary key.
   
   **Applied to:**
     - `superset-frontend/src/features/versionHistory/api.ts`
   
   ---
   💡 *To manage or update this instruction, visit: [CodeAnt AI 
Settings](https://app.codeant.ai/org/settings/learnings)*



-- 
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