Repository: appsmithorg/appsmith
File: app/client/src/utils/Analytics/betterbugs.ts (TypeScript)

Code before:
import type { User } from "constants/userConstants";
import { getAppsmithConfigs } from "ee/configs";
import log from "loglevel";
import store from "store";
import { getInstanceId } from "ee/selectors/organizationSelectors";
import {
  getCurrentApplicationId,
  getCurrentPageId,
} from "selectors/editorSelectors";
import { APPSMITH_BRAND_PRIMARY_COLOR } from "utils/BrandingUtils";
import { isAirgapped } from "ee/utils/airgapHelpers";

interface BetterbugsInstance {
  destroy: () => void;
  show: () => void;
  hide: () => void;
  openWidget: () => void;
  closeWidget: () => void;
  getMetadata?: () => Record<string | number, string | number | null>;
  setMetadata?: (
    metaData: Record<string | number, string | number | null>,
  ) => void;
}

class BetterbugsUtil {
  private static instance: BetterbugsInstance | null;

  /**
   * Get default styling for BetterBugs widget (light theme)
   */
  private static getDefaultStyles() {
    return {
      theme: "light" as const,
      primaryColor: APPSMITH_BRAND_PRIMARY_COLOR,
      primaryTextColor: "#ffffff",
    };
  }

  private static getRuntimeMetadata(): Record<
    string | number,
    string | number | null
  > {
    if (typeof window === "undefined") {
      return {
        url_path: null,
        application_id: null,
        page_id: null,
      };

Review comment (bug):
Instead of us defining the interface for the instance check if Betterbugs have exported an interface that we can use. Ask Cursor it might find it

Code after:
import type { User } from "constants/userConstants";
import type {
  Betterbugs as BetterbugsInstance,
  BetterbugsOptions,
} from "@betterbugs/web-sdk";
import { getAppsmithConfigs } from "ee/configs";
import log from "loglevel";
import store from "store";
import { getInstanceId } from "ee/selectors/organizationSelectors";
import {
  getCurrentApplicationId,
  getCurrentPageId,
} from "selectors/editorSelectors";
import { APPSMITH_BRAND_PRIMARY_COLOR } from "utils/BrandingUtils";
import { isAirgapped } from "ee/utils/airgapHelpers";

class BetterbugsUtil {
  private static instance: BetterbugsInstance | null;

  private static getDefaultParams(
    user: User | undefined,
    apiKey: string,
    appVersion: ReturnType<typeof getAppsmithConfigs>["appVersion"],
  ): BetterbugsOptions {
    const metadata: Record<string, string | null> = {
      instance_id: getInstanceId(store.getState()),
      tenant_id: store.getState().organization?.tenantId,
      version: appVersion.id,
      commit_sha: appVersion.sha,
      edition: appVersion.edition,
      release_date: appVersion.releaseDate,
      ...this.getRuntimeMetadata(),
    };

    const params: BetterbugsOptions = {
      apiKey,
      mode:
        process.env.NODE_ENV === "development" ? "development" : "production",
      showActionButton: false,
      styles: this.getDefaultStyles(),
      mainHeading: "Send support info",
      recordType: "recordVideo",
      position: { bottom: "30px", right: "20px" },
      successMessageHeaderText: "Information received",
      successMessageSubHeaderText:
        "Our support team will use it to review the issue",
      metaData: metadata,
    };