Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

8.7 KB
Raw Blame
import { useCallback, useMemo, useState } from "react";
import { t } from "ttag";

import { useMetadataToasts } from "metabase/common/hooks";
import { useSelector } from "metabase/redux";
import { useGetSettingsQuery, useSetting } from "metabase/settings";
import { Button, Icon, Modal, Stack } from "metabase/ui";
import {
  useGetBranchesQuery,
  useUpdateRemoteSyncSettingsMutation,
} from "metabase-enterprise/api";
import { useGetLibraryCollection } from "metabase-enterprise/data-studio/library/hooks/use-get-library-collection";
import {
  COLLECTIONS_KEY,
  REMOTE_SYNC_KEY,
  TRANSFORMS_KEY,
} from "metabase-enterprise/remote_sync/constants";
import { getIsRemoteSyncReadOnly } from "metabase-enterprise/remote_sync/selectors";
import type {
  ForcePushCasualties,
  RemoteSyncConfigurationSettings,
  RemoteSyncConflictVariant,
} from "metabase-types/api";

import { ChangesLists } from "../ChangesLists";
import { CommitMessageSection } from "../PushChangesModal/CommitMessageSection";

import { BranchNameInput } from "./BranchNameInput";
import { ConflictingChangesList } from "./ConflictingChangesList";
import { ForcePushWarning } from "./ForcePushWarning";
import { OutOfSyncOptions } from "./OutOfSyncOptions";
import { SetupConflictInfo } from "./SetupConflictInfo";
import {
  useDiscardChangesAndImportAction,
  useMergeChangesAction,
  useMergeImportAction,
  usePushChangesAction,
  useStashToNewBranchAction,
} from "./hooks";
import {
  type OptionValue,
  getContinueButtonText,
  getModalTitle,
} from "./utils";

interface UnsyncedWarningModalProps {
  currentBranch: string;
  /** switch-branch variant only: the branch to switch to once the chosen action resolves local changes. */
  nextBranch?: string | null;
  onClose: VoidFunction;
  variant: RemoteSyncConflictVariant;
  /** Push variant only: whether a 3-way merge would apply cleanly (offers the Merge option). */
  canMerge?: boolean;
  /** Push variant only: labels of entities that conflict (shown when the merge isn't clean). */
  conflicts?: string[];
  /** Remote content a force push would discard; surfaced when the force-push option is selected. */
  forcePushCasualties?: ForcePushCasualties;
  /** Whether the remote history was rewritten (no merge base); adds context to the force-push warning. */
  historyRewritten?: boolean;
}

export const SyncConflictModal = (props: UnsyncedWarningModalProps) => {
  const {
    onClose,
    currentBranch,
    nextBranch,
    variant,
    canMerge,
    conflicts,
    forcePushCasualties,
    historyRewritten,
  } = props;
  const [optionValue, setOptionValue] = useState<OptionValue>();
  const [newBranchName, setNewBranchName] = useState<string>("");
  // The push variant collects a commit message here, since merge/force/new-branch all push.
  const [commitMessage, setCommitMessage] = useState<string>("");
  const { sendErrorToast } = useMetadataToasts();
  const isRemoteSyncEnabled = !!useSetting(REMOTE_SYNC_KEY);
  const isRemoteSyncReadOnly = useSelector(getIsRemoteSyncReadOnly);
  const { data: settingValues } = useGetSettingsQuery();
  const { data: libraryCollection } = useGetLibraryCollection({
    skip: !isRemoteSyncEnabled,
  });
  const { data: branchesData } = useGetBranchesQuery();
  const existingBranches = useMemo(
    () => branchesData?.items || [],
    [branchesData],
  );
  const [updateRemoteSyncSettings, { isLoading: isUpdatingSettings }] =
    useUpdateRemoteSyncSettingsMutation();
  const { pushChanges, isPushingChanges } = usePushChangesAction();
  const { mergeChanges, isMerging } = useMergeChangesAction();
  const { mergeImport, isMergingImport } = useMergeImportAction();
  const { stashToNewBranch, isStashing } =
    useStashToNewBranchAction(existingBranches);
  const { discardChangesAndImport, isImporting } =
    useDiscardChangesAndImportAction();

  const markLibraryAndTransformsAsSynced = useCallback(async () => {
    try {
      const remoteSyncSettings: RemoteSyncConfigurationSettings = {
        // Unjustified type cast. FIXME
        [COLLECTIONS_KEY]: (settingValues as RemoteSyncConfigurationSettings)[
          COLLECTIONS_KEY
        ],
        [TRANSFORMS_KEY]: true,
      };

      if (libraryCollection?.id) {
        remoteSyncSettings[COLLECTIONS_KEY] = {
          ...remoteSyncSettings[COLLECTIONS_KEY],
          [libraryCollection.id]: !!libraryCollection?.id,
        };
      }

      await updateRemoteSyncSettings(remoteSyncSettings).unwrap();
    } catch (error) {
      sendErrorToast(t`Failed to mark library and transforms as synced`);
      throw error;
    }
  }, [
    libraryCollection?.id,
    sendErrorToast,
    settingValues,
    updateRemoteSyncSettings,
  ]);

  const message = commitMessage.trim() || undefined;

  const handleContinueButtonClick = async () => {
    if (!optionValue) {
      return;
    }

    if (optionValue === "push" || optionValue === "force-push") {
      await pushChanges(
        currentBranch,
        optionValue === "force-push",
        onClose,
        message,
      );
    }

    if (optionValue === "merge") {
      // Pull merges into local only; push merges and pushes the result.
      if (variant === "pull") {
        await mergeImport(currentBranch, onClose);
      } else {
        await mergeChanges(currentBranch, onClose, message);
      }
    }

    if (optionValue === "new-branch") {
      if (variant === "setup") {
        await markLibraryAndTransformsAsSynced();
      }

      await stashToNewBranch(newBranchName, onClose, message);
    }

    if (optionValue === "discard") {
      // nextBranch is set on a switch-branch discard (the branch we're switching to); otherwise we discard
      // and reload the current branch. currentBranch is the expected-branch assertion (caught if a stale tab
      // switched under us).
      await discardChangesAndImport(
        nextBranch || currentBranch,
        currentBranch,
        onClose,
      );
    }
  };

  const isProcessing =
    isImporting ||
    isPushingChanges ||
    isMerging ||
    isMergingImport ||
    isStashing ||
    isUpdatingSettings;
  const isButtonDisabled = useMemo(() => {
    let disabled = !optionValue || isProcessing;

    if (optionValue === "new-branch") {
      disabled ||= !newBranchName || existingBranches.includes(newBranchName);
    }

    return disabled;
  }, [existingBranches, isProcessing, newBranchName, optionValue]);

  return (
    <Modal
      density="relaxed"
      onClose={onClose}
      opened
      title={getModalTitle(variant, canMerge)}
      withCloseButton={false}
    >
      <Stack gap="xl">
        <ConflictSummary variant={variant} conflicts={conflicts} />

        <OutOfSyncOptions
          currentBranch={currentBranch}
          handleOptionChange={setOptionValue}
          isRemoteSyncReadOnly={isRemoteSyncReadOnly}
          optionValue={optionValue}
          variant={variant}
          canMerge={canMerge}
        />

        {optionValue === "force-push" && forcePushCasualties && (
          <ForcePushWarning
            casualties={forcePushCasualties}
            branch={currentBranch}
            historyRewritten={historyRewritten}
          />
        )}

        {optionValue === "new-branch" && (
          <BranchNameInput
            existingBranches={existingBranches}
            setValue={setNewBranchName}
            value={newBranchName}
          />
        )}

        {/* Pushing (merge / force / new branch) needs a commit message; pull/switch/setup don't. */}
        {variant === "push" && optionValue && optionValue !== "discard" && (
          <CommitMessageSection
            value={commitMessage}
            onChange={setCommitMessage}
          />
        )}
      </Stack>

      <Modal.Footer>
        <Button onClick={onClose} variant="subtle" color="neutral">
          {t`Cancel`}
        </Button>
        <Button
          color={optionValue === "discard" ? "negative" : "brand"}
          disabled={isButtonDisabled}
          leftSection={
            optionValue === "force-push" ? <Icon name="warning" /> : undefined
          }
          loading={isProcessing}
          onClick={handleContinueButtonClick}
          variant="filled"
          title={
            optionValue === "force-push"
              ? t`Force push will replace the remote version with your changes`
              : undefined
          }
        >
          {getContinueButtonText(optionValue)}
        </Button>
      </Modal.Footer>
    </Modal>
  );
};

interface ConflictSummaryProps {
  variant: RemoteSyncConflictVariant;
  conflicts?: string[];
}

function ConflictSummary({ variant, conflicts }: ConflictSummaryProps) {
  if (variant === "setup") {
    return <SetupConflictInfo />;
  }

  if (conflicts && conflicts.length > 0) {
    return <ConflictingChangesList conflicts={conflicts} />;
  }

  return <ChangesLists />;
}