Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

3.1 KB
Raw Blame
/* eslint-disable @repo/no-abstracted-overlay-trigger, @typescript-eslint/no-deprecated -- Legacy UI usage retained during migration to design-system components. */
import { useState } from "react";
import { Button } from "@/src/components/ui/button";
import {
  Dialog,
  DialogBody,
  DialogClose,
  DialogContent,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "@/src/components/ui/dialog";
import { api } from "@/src/utils/api";
import { showSuccessToast } from "@/src/features/notifications/showSuccessToast";
import { showToast } from "@/src/features/notifications/showToast";
import { classifyTrpcToastError } from "@/src/utils/trpcErrorClassification";
import { nanoid } from "nanoid";

export const StripeKeepPlanButton = ({
  orgId,
  stripeProductId,
  onProcessing,
  processing,
}: {
  orgId: string;
  stripeProductId: string;
  onProcessing: (id: string | null) => void;
  processing: boolean;
}) => {
  const [_opId, setOpId] = useState<string | null>(null);

  const clearSchedule = api.cloudBilling.clearPlanSwitchSchedule.useMutation({
    onSuccess: () => {
      showSuccessToast({
        operation: "billing_plan.keep",
        title: "Kept current plan",
        description: "",
      });
      onProcessing(null);
      setOpId(null);
      setTimeout(() => window.location.reload(), 500);
    },
    onError: (error) => {
      onProcessing(null);
      setOpId(null);
      showToast({
        type: "ERROR",
        title: "Failed to keep current plan",
        analytics: classifyTrpcToastError(error, "billing_plan.keep"),
      });
    },
  });

  return (
    <Dialog>
      <DialogTrigger asChild>
        <Button className="w-full" variant="default">
          Keep Plan
        </Button>
      </DialogTrigger>
      <DialogContent>
        <DialogHeader>
          <DialogTitle className="text-lg">
            Confirm Keeping Current Plan
          </DialogTitle>
        </DialogHeader>
        <DialogBody className="text-sm">
          <p>
            You have a scheduled plan change on your current subscription.
            Keeping your current plan will remove that schedule and you will
            remain on your existing plan.
          </p>
          <p>
            Your features and pricing will stay as-is; usage continues to be
            billed under your current plan. Do you want to keep your current
            plan and cancel the scheduled change?
          </p>
        </DialogBody>
        <DialogFooter>
          <DialogClose asChild>
            <Button variant="secondary">Go Back</Button>
          </DialogClose>
          <Button
            variant="default"
            onClick={() => {
              onProcessing(stripeProductId);
              // idempotency key for mutation operations with the stripe api
              let opId = _opId;
              if (!opId) {
                opId = nanoid();
                setOpId(opId);
              }
              clearSchedule.mutate({ orgId, opId });
            }}
            disabled={processing}
          >
            {processing ? "Keeping…" : "Confirm Keep Plan"}
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  );
};