async function configureFabric()

in src/hooks/useKnockoutExplorer.ts [117:236]


async function configureFabric(): Promise<Explorer> {
  // These are the versions of Fabric that Data Explorer supports.
  const SUPPORTED_FABRIC_VERSIONS = ["2", FABRIC_RPC_VERSION];

  let firstContainerOpened = false;
  let explorer: Explorer;
  return new Promise<Explorer>((resolve) => {
    window.addEventListener(
      "message",
      async (event) => {
        if (isInvalidParentFrameOrigin(event)) {
          return;
        }

        if (!shouldProcessMessage(event)) {
          return;
        }

        const data: FabricMessageV2 | FabricMessageV3 = event.data?.data;
        if (!data) {
          return;
        }

        switch (data.type) {
          case "initialize": {
            const fabricVersion = data.version;
            if (!SUPPORTED_FABRIC_VERSIONS.includes(fabricVersion)) {
              // TODO Surface error to user and log to telemetry
              useDialog
                .getState()
                .showOkModalDialog("Unsupported Fabric version", `Unsupported Fabric version: ${fabricVersion}`);
              Logger.logError(`Unsupported Fabric version: ${fabricVersion}`, "Explorer/configureFabric");
              console.error(`Unsupported Fabric version: ${fabricVersion}`);
              return;
            }

            if (fabricVersion === "2") {
              // ----------------- TODO: Remove this when FabricMessageV2 is deprecated -----------------
              const initializationMessage = data.message as {
                connectionId: string;
                isVisible: boolean;
              };

              explorer = createExplorerFabricLegacy(initializationMessage, data.version);
              await scheduleRefreshFabricToken(true);
              resolve(explorer);
              await explorer.refreshAllDatabases();
              if (userContext.fabricContext.isVisible) {
                firstContainerOpened = true;
                openFirstContainer(explorer, userContext.fabricContext.databaseName);
              }
              // -----------------------------------------------------------------------------------------
            } else if (fabricVersion === FABRIC_RPC_VERSION) {
              const initializationMessage = data.message as InitializeMessageV3<CosmosDbArtifactType>;
              explorer = createExplorerFabric(initializationMessage, data.version);

              if (initializationMessage.artifactType === CosmosDbArtifactType.MIRRORED_KEY) {
                // Do not show Home tab for Mirrored
                useTabs.getState().closeReactTab(ReactTabKind.Home);
              }

              // All tokens used in fabric expire, so schedule a refresh
              // For Mirrored key, we need the token right away to get the database and containers list.
              if (isFabricMirroredKey()) {
                await scheduleRefreshFabricToken(true);
              } else {
                scheduleRefreshFabricToken(false);
              }

              resolve(explorer);
              await explorer.refreshAllDatabases();

              const { databaseName } = userContext.fabricContext;
              if (userContext.fabricContext.isVisible && databaseName) {
                firstContainerOpened = true;
                openFirstContainer(explorer, databaseName);
              }
            }

            break;
          }
          case "newContainer":
            explorer.onNewCollectionClicked();
            break;
          case "authorizationToken":
          case "allResourceTokens_v2":
          case "accessToken": {
            handleCachedDataMessage(data);
            break;
          }
          case "explorerVisible": {
            userContext.fabricContext.isVisible = data.message.visible;
            if (userContext.fabricContext.isVisible && !firstContainerOpened) {
              const { databaseName } = userContext.fabricContext;
              if (databaseName !== undefined) {
                firstContainerOpened = true;
                openFirstContainer(explorer, databaseName);
              }
            }
            break;
          }
          case "refreshResourceTree": {
            explorer.onRefreshResourcesClick();
            break;
          }
          default:
            console.error(`Unknown Fabric message type: ${JSON.stringify(data)}`);
            break;
        }
      },
      false,
    );

    sendMessage({
      type: FabricMessageTypes.Ready,
      id: "ready",
      params: [DATA_EXPLORER_RPC_VERSION],
    });
  });
}