Skip to content

The free and privacy-friendly screen recorder with no limits 🎥

7.4 KB
Raw Blame
import React, { useContext, useEffect, useState, useRef } from "react";
import * as S from "@radix-ui/react-switch";

// Components
import { DropdownIcon } from "../../images/popup/images";

// Context
import { contentStateContext } from "../../context/ContentState";

export const BaseSwitch = ({ value, checked, onChange }) => (
  <S.Root
    className="SwitchRoot"
    id={value}
    checked={checked}
    onCheckedChange={onChange}
  >
    <S.Thumb className="SwitchThumb" />
  </S.Root>
);

const Switch = (props) => {
  const [contentState, setContentState] = useContext(contentStateContext);
  const switchRef = useRef(null);
  const switchId = props.anchorId || props.value || props.name;
  const switchRowId =
    props.rowAnchorId ||
    (props.anchorId ? `${props.anchorId}-row` : undefined);
  const [hideToolbarLabel, setHideToolbarLabel] = useState(
    chrome.i18n.getMessage("hideToolbarLabel")
  );
  const [hideToolbarState, setHideToolbarState] = useState(1);

  useEffect(() => {
    // Check click outside
    const handleClickOutside = (event) => {
      if (props.name != "hideUI") return;
      if (
        dropdownRef.current &&
        !dropdownRef.current.contains(event.target) &&
        !dropdownInRef.current.contains(event.target)
      ) {
        if (dropdownRef.current.querySelector(":hover")) return;
        if (dropdownInRef.current.querySelector(":hover")) return;
        // Check if any children of dropdownref are clicked also
        let children = dropdownRef.current.querySelectorAll("*");
        for (let i = 0; i < children.length; i++) {
          if (children[i].contains(event.target)) return;
        }

        dropdownRef.current.classList.remove("labelDropdownActive");
      }
    };

    // Bind the event listener
    document.addEventListener("click", handleClickOutside);

    return () => {
      // Unbind the event listener on clean up
      document.removeEventListener("click", handleClickOutside);
    };
  }, []);

  useEffect(() => {
    if (props.name === "hideUI") {
      if (contentState.hideToolbar) {
        setHideToolbarLabel(chrome.i18n.getMessage("hideToolbarLabel"));
        setHideToolbarState(1);
      } else if (contentState.toolbarHover) {
        setHideToolbarLabel(chrome.i18n.getMessage("toolbarHoverOnly"));
        setHideToolbarState(3);
      }
    }
  }, [contentState.hideToolbar]);

  const dropdownRef = useRef(null);
  const dropdownInRef = useRef(null);
  return (
    <form>
      <div className="SwitchRow" id={switchRowId}>
        <label
          className="Label"
          htmlFor={switchId}
          style={{ paddingRight: 15 }}
          onClick={(e) => {
            if (props.name === "hideUI") {
              e.preventDefault();
              e.stopPropagation();
              if (e.target.classList.contains("labelDropdownContentItem"))
                return;
              dropdownRef.current.classList.toggle("labelDropdownActive");
            }
          }}
        >
          {props.name !== "hideUI" && props.label}
          {props.name === "hideUI" && (
            <div className="labelDropdownWrap" ref={dropdownRef}>
              <div className="labelDropdown" ref={dropdownInRef}>
                {hideToolbarLabel}
                <img src={DropdownIcon} />
              </div>
              <div className="labelDropdownContent">
                <div
                  className="labelDropdownContentItem"
                  onClick={() => {
                    setContentState((prevContentState) => ({
                      ...prevContentState,
                      hideToolbar: true,
                      toolbarHover: false,
                    }));
                    chrome.storage.local.set({
                      hideToolbar: true,
                      toolbarHover: false,
                    });
                    setHideToolbarLabel(
                      chrome.i18n.getMessage("hideToolbarLabel")
                    );
                    dropdownRef.current.classList.remove("labelDropdownActive");
                    setHideToolbarState(1);
                  }}
                >
                  {chrome.i18n.getMessage("hideToolbarLabel")}
                </div>
                <div
                  className="labelDropdownContentItem"
                  onClick={() => {
                    setContentState((prevContentState) => ({
                      ...prevContentState,
                      hideToolbar: false,
                      toolbarHover: true,
                    }));
                    chrome.storage.local.set({
                      hideToolbar: false,
                      toolbarHover: true,
                    });
                    setHideToolbarLabel(
                      chrome.i18n.getMessage("toolbarHoverOnly")
                    );
                    dropdownRef.current.classList.remove("labelDropdownActive");
                    setHideToolbarState(3);
                  }}
                >
                  {chrome.i18n.getMessage("toolbarHoverOnly")}
                </div>
              </div>
            </div>
          )}
          {props.experimental && (
            <span className="ExperimentalLabel">Experimental</span>
          )}
        </label>
        {props.value ? (
          <S.Root
            className="SwitchRoot"
            id={switchId}
            ref={switchRef}
            checked={contentState[props.value]}
            disabled={props.disabled}
            onCheckedChange={(checked) => {
              if (props.disabled) return;

              setContentState((prevContentState) => ({
                ...prevContentState,
                [props.value]: checked,
              }));
              chrome.storage.local.set({ [props.value]: checked });

              if (props.value === "customRegion") {
                if (checked) {
                  chrome.storage.local.set({
                    region: true,
                  });
                }
              }

              if (props.name === "hideUI") {
                if (hideToolbarState === 1) {
                  setContentState((prevContentState) => ({
                    ...prevContentState,
                    hideToolbar: true,
                    toolbarHover: false,
                  }));
                  chrome.storage.local.set({
                    hideToolbar: true,
                    toolbarHover: false,
                  });
                } else if (hideToolbarState === 3) {
                  setContentState((prevContentState) => ({
                    ...prevContentState,
                    hideToolbar: false,
                    toolbarHover: true,
                  }));
                  chrome.storage.local.set({
                    hideToolbar: false,
                    toolbarHover: true,
                  });
                }
              } else if (props.name === "pushToTalk") {
                if (!checked) {
                  setContentState((prevContentState) => ({
                    ...prevContentState,
                    micActive: true,
                  }));
                }
              }

              if (typeof props.onChange === "function") {
                props.onChange(checked);
              }
            }}
          >
            <S.Thumb className="SwitchThumb" />
          </S.Root>
        ) : (
          <S.Root className="SwitchRoot" id={switchId}>
            <S.Thumb className="SwitchThumb" />
          </S.Root>
        )}
      </div>
    </form>
  );
};

export default Switch;