All files / Rindu/components/DeviceConnectControl DeviceConnectControl.tsx

12% Statements 6/50
0% Branches 0/28
0% Functions 0/10
13.33% Lines 6/45

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 24319x   19x 19x 19x 19x 19x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
import { ReactElement, useEffect, useRef, useState } from "react";
 
import { Heading } from "components";
import { DeviceConnect, Playing } from "components/icons";
import { useAuth, useSpotify, useToast, useTranslations } from "hooks";
import { templateReplace } from "utils";
import { getAvailableDevices, transferPlayback } from "utils/spotifyCalls";
 
export default function DeviceConnectControl(): ReactElement {
  const { isPremium } = useAuth();
  const { deviceId } = useSpotify();
  const { addToast } = useToast();
  const { translations } = useTranslations();
  const [devices, setDevices] = useState<SpotifyApi.UserDevice[]>([]);
  const currentActiveDevice = devices.find((device) => device.is_active);
  const thisDevice = devices.find((device) => device.id === deviceId);
  const currentDevice = currentActiveDevice ?? thisDevice;
  const devicesContainerRef = useRef<HTMLDivElement>(null);
  const devicesButtonRef = useRef<HTMLButtonElement>(null);
 
  useEffect(() => {
    const handleClickOutside = (event: MouseEvent) => {
      const clickedInSideDeviceContainer =
        devicesContainerRef.current?.contains(event.target as Node);
      const clickedDevicesButton = devicesButtonRef.current?.contains(
        event.target as Node
      );
 
      Iif (!clickedInSideDeviceContainer && !clickedDevicesButton) {
        setDevices([]);
      }
    };
 
    document.addEventListener("mousedown", handleClickOutside);
 
    return () => {
      document.removeEventListener("mousedown", handleClickOutside);
    };
  }, []);
 
  return (
    <div className="devices">
      {devices.length > 0 && (
        <div className="devices-container" ref={devicesContainerRef}>
          <header>
            <div className="playing">
              {currentDevice?.is_active ? <Playing /> : null}
            </div>
            <div className="heading">
              <Heading number={3}>Current device</Heading>
            </div>
            <p
              className={`device-name device ${
                currentDevice?.is_active ? "active" : ""
              }`}
            >
              {currentDevice?.name}
            </p>
          </header>
          {devices.length > 1 && (
            <div className="another-device-header">
              <Heading number={4}>Select another device</Heading>
            </div>
          )}
          <ul>
            {devices.map((device) => {
              const isActive = device.is_active;
              const isThisDevice = device.id === deviceId;
              const isCurrentDevice = currentDevice?.id === device.id;
 
              Iif (isActive && isThisDevice) return null;
              Iif (isCurrentDevice) return null;
              return (
                <li key={device.id}>
                  <button
                    className={`device device-connect ${
                      isActive ? "active" : "inactive"
                    }`}
                    onClick={async () => {
                      Iif (isActive || !device.id) return;
                      const transferPlaybackResponse = await transferPlayback([
                        device.id,
                      ]);
                      Iif (transferPlaybackResponse) {
                        addToast({
                          message: templateReplace(
                            translations.toastMessages.deviceConnectedTo,
                            [device.name]
                          ),
                          variant: "success",
                        });
                      }
                    }}
                  >
                    <div className="device-info">
                      <div className="device-name device">
                        {isThisDevice ? `This ${device.type}` : device.name}
                      </div>
                    </div>
                  </button>
                </li>
              );
            })}
          </ul>
        </div>
      )}
      <button
        type="button"
        aria-label="open device selector"
        ref={devicesButtonRef}
        onClick={() => {
          Iif (!isPremium) {
            addToast({
              variant: "error",
              message: translations.toastMessages.premiumRequired,
            });
            return;
          }
 
          Iif (devices.length === 0) {
            getAvailableDevices().then((res) => {
              Iif (res?.devices) {
                setDevices(res.devices);
              }
            });
            return;
          }
          setDevices([]);
        }}
        className="button playerButton"
      >
        <DeviceConnect fill={devices.length > 0 ? "#1db954" : "#ffffffb3"} />
      </button>
      <style jsx>{`
        .devices {
          display: flex;
          align-items: center;
          justify-content: center;
          z-index: 999999999999999999999999999;
          max-height: calc((var(--vh, 1vh) * 100) - 114px);
          overflow-y: auto;
          padding: 5px;
        }
        .another-device-header {
          margin-top: 10px;
 
          margin-bottom: 10px;
 
          padding: 0 10px;
        }
        .devices-container::before {
          border: 10px solid transparent;
          border-top-color: #282828;
          bottom: -20px;
          content: "";
          position: absolute;
          right: 141px;
        }
        .devices-container {
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;
          position: absolute;
          bottom: 70px;
          display: block;
          border-radius: 5px;
          background-color: #282828;
          box-shadow: 0px 2px 9px 0px rgb(0 0 0 / 5%);
          padding: 17px 3px 3px 3px;
          width: 300px;
        }
        .devices-container header {
          display: grid;
          justify-content: center;
          grid-template-columns: 0.3fr 1fr;
          grid-template-areas: "playing heading" "playing device-name";
        }
        .devices-container header .playing {
          grid-area: playing;
          display: flex;
          justify-content: center;
          align-items: center;
        }
        .devices-container header .heading {
          grid-area: heading;
        }
        .devices-container header .device-name {
          grid-area: device-name;
          display: flex;
          align-items: center;
          font-size: 1.2rem;
          font-weight: 500;
        }
        .device.active {
          color: #1db954;
        }
        button.device {
          background-color: transparent;
          width: max-content;
          min-width: 100%;
          border: none;
          display: flex;
          align-content: center;
          font-weight: 700;
          font-size: 14px;
          line-height: 16px;
          color: #fff;
          text-align: start;
          text-decoration: none;
          border-radius: 3px;
          align-items: center;
          justify-content: space-between;
          padding: 8px 10px;
          height: 40px;
        }
        button.device:hover {
          outline: none;
          background-color: #ffffff1a;
        }
        button {
          display: flex;
          justify-content: center;
          align-items: center;
          border: none;
          background-color: transparent;
          position: relative;
        }
        .button {
          width: 32px;
          height: 32px;
        }
        button:hover :global(svg path) {
          fill: #fff;
        }
        li {
          list-style: none;
        }
      `}</style>
    </div>
  );
}