Skip to content

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

1.6 KB
Raw Blame
import { useCallback } from "react";
import { t } from "ttag";

import { useToast } from "metabase/common/hooks";
import { Button, Tooltip } from "metabase/ui";
import { useImportChangesMutation } from "metabase-enterprise/api";

import { trackPullChanges } from "../../analytics";

interface PullChangesButtonProps {
  dirty: boolean; // disambiguation: local form state, not the git-synced collection state
  branch: string;
  forcePull: boolean;
}

export const PullChangesButton = (props: PullChangesButtonProps) => {
  const { branch, forcePull, dirty } = props;
  const [importChanges, { isLoading: isImporting }] =
    useImportChangesMutation();
  const [sendToast] = useToast();

  const handlePullChanges = useCallback(async () => {
    try {
      // In admin settings the configured branch is the operational target and the assertion.
      await importChanges({
        branch,
        force: forcePull,
        expected_branch: branch,
      }).unwrap();

      trackPullChanges({
        triggeredFrom: "admin-settings",
        force: forcePull,
      });

      sendToast({
        message: t`Latest changes have been pulled successfully`,
        icon: "check",
      });
    } catch (error) {
      sendToast({
        message: t`Failed to pull changes`,
        variant: "negative",
      });
    }
  }, [importChanges, branch, forcePull, sendToast]);

  return (
    <Tooltip label={t`Save settings before pulling changes`} disabled={!dirty}>
      <Button
        disabled={isImporting || dirty}
        loading={isImporting}
        onClick={handlePullChanges}
      >
        {t`Pull changes now`}
      </Button>
    </Tooltip>
  );
};