Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

16.8 KB
Raw Blame
import { LinkFormData } from "@/ui/links/link-builder/link-builder-provider";
import { useLinkBuilderKeyboardShortcut } from "@/ui/links/link-builder/use-link-builder-keyboard-shortcut";
import { ProBadgeTooltip } from "@/ui/shared/pro-badge-tooltip";
import { Button, Combobox, Modal, Tooltip, UTM_PARAMETERS } from "@dub/ui";
import { Crosshairs3, Trash } from "@dub/ui/icons";
import {
  cn,
  constructURLFromUTMParams,
  COUNTRIES,
  getParamsFromURL,
  isValidUrl,
  pluralize,
} from "@dub/utils";
import {
  Dispatch,
  Fragment,
  SetStateAction,
  useCallback,
  useId,
  useMemo,
  useState,
} from "react";
import { useForm, useFormContext } from "react-hook-form";

function TargetingModal({
  showTargetingModal,
  setShowTargetingModal,
}: {
  showTargetingModal: boolean;
  setShowTargetingModal: Dispatch<SetStateAction<boolean>>;
}) {
  const id = useId();

  const {
    watch: watchParent,
    getValues: getValuesParent,
    setValue: setValueParent,
  } = useFormContext<LinkFormData>();

  const {
    watch,
    register,
    setValue,
    reset,
    handleSubmit,
    formState: { isDirty },
  } = useForm<Pick<LinkFormData, "ios" | "android" | "geo">>({
    values: {
      ios: getValuesParent("ios"),
      android: getValuesParent("android"),
      geo: getValuesParent("geo"),
    },
  });

  const geo = watch("geo");

  const [iosParent, androidParent, geoParent] = watchParent([
    "ios",
    "android",
    "geo",
  ]);

  const parentEnabled = Boolean(
    iosParent || androidParent || Object.keys(geoParent || {}).length > 0,
  );

  // Get UTM parameters from the parent URL that need to be added on blur
  const getNewParams = useCallback(
    (targetURL: string) => {
      if (!targetURL?.trim() || !isValidUrl(targetURL)) return;

      const parentUrl = getValuesParent("url");
      const parentParams = getParamsFromURL(parentUrl);
      const targetParams = getParamsFromURL(targetURL);

      const newParams = UTM_PARAMETERS.filter(
        ({ key }) => parentParams?.[key] && !targetParams?.[key],
      ).map(({ key }) => [key, parentParams[key]]);

      return newParams.length ? Object.fromEntries(newParams) : null;
    },
    [getValuesParent],
  );

  return (
    <>
      <Modal
        showModal={showTargetingModal}
        setShowModal={setShowTargetingModal}
        className="sm:max-w-[500px]"
      >
        <form
          className="px-5 py-4"
          onSubmit={(e) => {
            e.stopPropagation();
            handleSubmit((data) => {
              setValueParent("ios", data.ios, { shouldDirty: true });
              setValueParent("android", data.android, { shouldDirty: true });

              // Filter out empty geo values
              const geo = Object.fromEntries(
                Object.entries(data.geo || {}).filter(
                  ([key, value]) => key?.trim() && value?.trim(),
                ),
              );
              setValueParent("geo", Object.keys(geo).length > 0 ? geo : null, {
                shouldDirty: true,
              });

              setShowTargetingModal(false);
            })(e);
          }}
        >
          <div className="flex items-center justify-between">
            <h3 className="text-lg font-medium">Targeting</h3>
            <div className="max-md:hidden">
              <Tooltip
                content={
                  <div className="px-2 py-1 text-xs text-neutral-700">
                    Press{" "}
                    <strong className="font-medium text-neutral-950">G</strong>{" "}
                    to open this quickly
                  </div>
                }
                side="right"
              >
                <kbd className="flex size-6 cursor-default items-center justify-center gap-1 rounded-md border border-neutral-200 font-sans text-xs text-neutral-950">
                  G
                </kbd>
              </Tooltip>
            </div>
          </div>

          <div className="scrollbar-hide -m-1 mt-6 flex max-h-[calc(100dvh-250px)] flex-col gap-6 overflow-y-auto p-1">
            {/* Geo */}
            <div>
              <div className="flex items-center gap-2">
                <span className="block text-sm font-medium text-neutral-700">
                  Geo Targeting
                </span>
                <ProBadgeTooltip content="Redirect your users to different links based on their location. [Learn more about geo targeting.](https://dub.co/help/article/geo-targeting)" />
              </div>
              <div className="mt-2">
                {geo && (
                  <div className="relative mb-2 grid grid-cols-[min-content_1fr_min-content] gap-y-2">
                    {Object.entries(geo).map(([key, value]) => (
                      <Fragment key={key}>
                        <div className="z-[1]">
                          <Combobox
                            selected={
                              key ? { value: key, label: COUNTRIES[key] } : null
                            }
                            setSelected={(option) => {
                              if (!option) return;
                              const newGeo = {};
                              delete Object.assign(newGeo, geo, {
                                [option.value]: value,
                              })[key];
                              setValue("geo", newGeo, { shouldDirty: true });
                            }}
                            options={Object.entries(COUNTRIES)
                              // show United States first
                              .sort((a, b) =>
                                a[0] === "US" ? -1 : b[0] === "US" ? 1 : 0,
                              )
                              .filter(
                                ([ck]) =>
                                  ck === key || !Object.keys(geo).includes(ck),
                              )
                              .map(([key, value]) => ({
                                icon: (
                                  <img
                                    alt={value}
                                    src={`https://flag.vercel.app/m/${key}.svg`}
                                    className="mr-1 h-2.5 w-4"
                                  />
                                ),
                                value: key,
                                label: value,
                              }))}
                            icon={
                              key ? (
                                <img
                                  alt={COUNTRIES[key]}
                                  src={`https://flag.vercel.app/m/${key}.svg`}
                                  className="h-2.5 w-4"
                                />
                              ) : undefined
                            }
                            caret={true}
                            placeholder="Country"
                            searchPlaceholder="Search countries..."
                            buttonProps={{
                              className: cn(
                                "w-32 sm:w-40 rounded-r-none border-r-transparent justify-start px-2.5",
                                "data-[state=open]:ring-1 data-[state=open]:ring-neutral-500 data-[state=open]:border-neutral-500",
                                "focus:ring-1 focus:ring-neutral-500 focus:border-neutral-500 transition-none",
                                !key && "text-neutral-600",
                              ),
                            }}
                            optionClassName="sm:max-w-[200px]"
                          />
                        </div>
                        <input
                          type="text"
                          id={`${id}-${key}`}
                          placeholder="https://example.com"
                          className="z-0 h-full grow rounded-r-md border border-neutral-300 text-sm placeholder-neutral-400 focus:z-[1] focus:border-neutral-500 focus:ring-neutral-500"
                          value={value}
                          onChange={(e) => {
                            setValue(
                              `geo`,
                              {
                                ...((geo as object) || {}),
                                [key]: e.target.value,
                              },
                              { shouldDirty: true },
                            );
                          }}
                          onBlur={(e) => {
                            const newParams = getNewParams(e.target.value);

                            if (newParams)
                              setValue(
                                `geo`,
                                {
                                  ...((geo as object) || {}),
                                  [key]: constructURLFromUTMParams(
                                    value,
                                    newParams,
                                  ),
                                },
                                { shouldDirty: true },
                              );
                          }}
                        />
                        <div className="pl-1.5">
                          <Button
                            variant="danger-outline"
                            icon={<Trash className="size-4" />}
                            className="bg-red-600/5 px-3 text-red-600 hover:bg-red-600/10 hover:text-red-700"
                            onClick={() => {
                              const newGeo = { ...((geo as object) || {}) };
                              delete newGeo[key];
                              setValue("geo", newGeo, { shouldDirty: true });
                            }}
                          />
                        </div>
                      </Fragment>
                    ))}
                  </div>
                )}
                <Button
                  type="button"
                  variant="secondary"
                  text="Add location"
                  className="h-9"
                  onClick={() => {
                    setValue(
                      "geo",
                      { ...((geo as object) || {}), "": "" },
                      { shouldDirty: true },
                    );
                  }}
                  disabled={Object.keys(geo || {}).includes("")}
                />
              </div>
            </div>

            {/* iOS */}
            <div>
              <div className="flex items-center gap-2">
                <label
                  htmlFor={`${id}-ios-url`}
                  className="block text-sm font-medium text-neutral-700"
                >
                  iOS Targeting
                </label>
                <ProBadgeTooltip content="Redirect your iOS users to a different link. [Learn more about device targeting.](https://dub.co/help/article/device-targeting)" />
              </div>
              <div className="mt-2 rounded-md shadow-sm">
                <input
                  id={`${id}-ios-url`}
                  placeholder="https://apps.apple.com/app/1611158928"
                  className="block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
                  {...register("ios", {
                    onBlur: (e) => {
                      const newParams = getNewParams(e.target.value);

                      if (newParams)
                        setValue(
                          "ios",
                          constructURLFromUTMParams(e.target.value, newParams),
                          { shouldDirty: true },
                        );
                    },
                  })}
                />
              </div>
            </div>

            {/* Android */}
            <div>
              <div className="flex items-center gap-2">
                <label
                  htmlFor={`${id}-android-url`}
                  className="block text-sm font-medium text-neutral-700"
                >
                  Android Targeting
                </label>
                <ProBadgeTooltip content="Redirect your Android users to a different link. [Learn more about device targeting.](https://dub.co/help/article/device-targeting)" />
              </div>
              <div className="mt-2 rounded-md shadow-sm">
                <input
                  id={`${id}-android-url`}
                  placeholder="https://play.google.com/store/apps/details?id=com.disney.disneyplus"
                  className="block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
                  {...register("android", {
                    onBlur: (e) => {
                      const newParams = getNewParams(e.target.value);

                      if (newParams)
                        setValue(
                          "android",
                          constructURLFromUTMParams(e.target.value, newParams),
                          { shouldDirty: true },
                        );
                    },
                  })}
                />
              </div>
            </div>
          </div>

          <div className="mt-6 flex items-center justify-between">
            <div>
              {parentEnabled && (
                <button
                  type="button"
                  className="text-xs font-medium text-neutral-700 transition-colors hover:text-neutral-950"
                  onClick={() => {
                    setValueParent("ios", null, { shouldDirty: true });
                    setValueParent("android", null, { shouldDirty: true });
                    setValueParent("geo", null, { shouldDirty: true });
                    setShowTargetingModal(false);
                  }}
                >
                  Remove targeting
                </button>
              )}
            </div>
            <div className="flex items-center gap-2">
              <Button
                type="button"
                variant="secondary"
                text="Cancel"
                className="h-9 w-fit"
                onClick={() => {
                  reset();
                  setShowTargetingModal(false);
                }}
              />
              <Button
                type="submit"
                variant="primary"
                text={parentEnabled ? "Save" : "Add targeting"}
                className="h-9 w-fit"
                disabled={!isDirty}
              />
            </div>
          </div>
        </form>
      </Modal>
    </>
  );
}

export function getTargetingLabel({
  ios,
  android,
  geo,
}: Pick<LinkFormData, "ios" | "android" | "geo">) {
  const geoEnabled = Object.keys(geo || {}).length > 0;

  const targets = [Boolean(ios), Boolean(android), geoEnabled];
  const count = targets.filter(Boolean).length;
  const countries = Object.keys(geo || {});

  if (count === 0) return "Targeting";
  if (count === 1) {
    const index = targets.findIndex(Boolean);
    if (index <= 1) return ["iOS", "Android"][index];
    if (!geoEnabled) return "Targeting";

    // Geo
    if (countries.length === 1 && countries[0]) return countries[0];
    return `${countries.length} ${pluralize("Target", countries.length)}`;
  }

  return `${count + (countries.length > 1 ? countries.length - 1 : 0)} Targets`;
}

function TargetingButton({
  setShowTargetingModal,
}: {
  setShowTargetingModal: Dispatch<SetStateAction<boolean>>;
}) {
  const { watch } = useFormContext<LinkFormData>();
  const [ios, android, geo] = watch(["ios", "android", "geo"]);

  useLinkBuilderKeyboardShortcut("g", () => setShowTargetingModal(true));

  const geoEnabled = Object.keys(geo || {}).length > 0;
  const enabled = Boolean(ios || android || geoEnabled);

  const label = useMemo(
    () => getTargetingLabel({ ios, android, geo }),
    [ios, android, geo],
  );

  return (
    <Button
      variant="secondary"
      text={label}
      icon={
        <Crosshairs3 className={cn("size-4", enabled && "text-blue-500")} />
      }
      className="h-8 w-fit gap-1.5 px-2.5 text-xs font-medium text-neutral-700"
      onClick={() => setShowTargetingModal(true)}
    />
  );
}

export function useTargetingModal() {
  const [showTargetingModal, setShowTargetingModal] = useState(false);

  const TargetingModalCallback = useCallback(() => {
    return (
      <TargetingModal
        showTargetingModal={showTargetingModal}
        setShowTargetingModal={setShowTargetingModal}
      />
    );
  }, [showTargetingModal, setShowTargetingModal]);

  const TargetingButtonCallback = useCallback(() => {
    return <TargetingButton setShowTargetingModal={setShowTargetingModal} />;
  }, [setShowTargetingModal]);

  return useMemo(
    () => ({
      setShowTargetingModal,
      TargetingModal: TargetingModalCallback,
      TargetingButton: TargetingButtonCallback,
    }),
    [setShowTargetingModal, TargetingModalCallback, TargetingButtonCallback],
  );
}