All files Room.tsx

53.57% Statements 15/28
71.42% Branches 10/14
50% Functions 5/10
62.5% Lines 15/24

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                          3x 5x 5x   5x 5x   5x   5x 6x             5x                       5x 5x             5x 5x             5x                                                                                                                                         6x                                                            
import { useEffect, useState } from "react";
import { HubDevice } from "~/types/HubDevice";
import { useMutation } from "@tanstack/react-query";
import { useStores } from "~/hooks/useStores";
import { Device } from "./Device";
import { RoomIllustration } from "./RoomIllustration";
import { sanitizeName } from "~/utils/sanitizeName";
import {
	IoIosCheckmarkCircleOutline,
	IoIosCloseCircleOutline,
} from "react-icons/io";
import { GrAppsRounded } from "react-icons/gr";
 
export const Room = ({ devices }: { devices: HubDevice[] }) => {
	const { hub } = useStores();
	const { archive } = useStores();
 
	const [showSuccess, setShowSuccess] = useState(false);
	const [showError, setShowError] = useState(false);
 
	const roomName = sanitizeName(devices[0]?.Room_location ?? "");
 
	const visibleDevices = devices.filter(
		(device) => !archive.contains(device.connectedDeviceId),
	);
 
	const {
		mutate: controlDevice,
		isError,
		isSuccess,
	} = useMutation({
		mutationFn: async (device: HubDevice) => {
			const updatedDevice = {
				...device,
				isActive: !device.isActive,
			};
 
			const res = await hub.service.controlDevice(updatedDevice);
			return { res };
		},
	});
 
	useEffect(() => {
		if (isISuccess) {
			setShowSuccess(true);
			const timeout = setTimeout(() => setShowSuccess(false), 3000);
			return () => clearTimeout(timeout);
		}
	}, [isSuccess]);
 
	useEffect(() => {
		if (isIError) {
			setShowError(true);
			const timeout = setTimeout(() => setShowError(false), 3000);
			return () => clearTimeout(timeout);
		}
	}, [isError]);
 
	return (
		<article className="bg-base-100 shadow-md py-3 px-6 rounded-4xl md:flex md:flex-col items-center text-base-content border-base-300 border-1">
			<RoomIllustration
				roomName={roomName}
				data-cy={`room-illustration`}
				aria-hidden="true"
			/>
 
			<div className="md:flex md:flex-col w-full mt-4">
				<h2
					id={`room-heading-${roomName}`}
					className="text-accent font-extrabold text-2xl text-center"
				>
					{roomName || "Room"}
				</h2>
 
				{/* Alerts */}
				<div role="status" aria-live="polite" aria-atomic="true">
					{showSuccess && (
						<div
							role="alert"
							className="alert alert-success text-success-content mt-3"
							aria-describedby="success-message"
						>
							<IoIosCheckmarkCircleOutline
								size={20}
								aria-hidden="true"
							/>
							<span id="success-message">
								Success! The device was updated.
							</span>
						</div>
					)}
 
					{showError && (
						<div
							role="alert"
							className="alert alert-error text-error-content mt-3"
							aria-describedby="error-message"
						>
							<IoIosCloseCircleOutline
								size={20}
								aria-hidden="true"
							/>
							<span id="error-message">
								Oops! There was an issue updating the device.
							</span>
						</div>
					)}
				</div>
 
				<div className="divider mt-5" role="separator">
					<div className="text-md flex items-center gap-2">
						<GrAppsRounded aria-hidden="true" />
						<span>Available Devices</span>
					</div>
				</div>
 
				<div role="region">
					<h3 id={`devices-heading-${roomName}`} className="sr-only">
						Devices in {roomName || "this room"}
					</h3>
 
					{visibleDevices.length ? (
						<ul
							className="flex flex-col gap-6 px-2 py-4"
							role="list"
						>
							{visibleDevices.map((device) => (
								<li key={device.connectedDeviceId}>
									<Device
										device={device}
										controlDevice={controlDevice}
									/>
									<div
										data-cy={`device-${device.name.replace(
											/\s+/g,
											"",
										)}`}
										className="sr-only"
										aria-hidden="true"
									/>
								</li>
							))}
						</ul>
					) : (
						<div
							className="text-center py-4"
							role="status"
							aria-live="polite"
						>
							<span>Oops! No visible devices at the moment.</span>
						</div>
					)}
				</div>
			</div>
		</article>
	);
};