Skip to content

Commit

Permalink
Merge branch 'main' into backend
Browse files Browse the repository at this point in the history
+ Do the filtering and sorting in the API
  • Loading branch information
amberstarlight committed Aug 21, 2024
2 parents c3ac542 + b744df4 commit e99c914
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
20 changes: 15 additions & 5 deletions packages/api/routes/devices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,28 @@ import express, { Request, Response, Router } from "express";
import { Zigbee2MqttService } from "../zigbee2mqttService";
import { ApiError } from "./api";
import { range } from "../utils";
import { Scene } from "@starlight/types";
import { type Device, Scene } from "@starlight/types";

const router = express.Router();

const deviceSort = (a: Device, b: Device) => {
const x: number = parseInt(a.friendly_name[0]);
const y: number = parseInt(b.friendly_name[0]);

return (
+isFinite(x) - +isFinite(y) ||
a.friendly_name.localeCompare(b.friendly_name, undefined, { numeric: true })
);
};

export function deviceRouter(zigbee2mqttService: Zigbee2MqttService): Router {
// get data about all existing devices
router.get("/", async (req: Request, res: Response) => {
const devices = await zigbee2mqttService.getDevices();
const deviceQuery = req.query.parameter?.toString();
const filteredDevices = devices.filter(
(device) => device.device.type !== "Coordinator",
);
const cleanedDevices = devices
.filter((device) => device.device.type !== "Coordinator")
.sort((a, b) => deviceSort(a.device, b.device));

if (deviceQuery !== undefined) {
const queriedData = devices.map((device) => device.device[deviceQuery]);
Expand All @@ -26,7 +36,7 @@ export function deviceRouter(zigbee2mqttService: Zigbee2MqttService): Router {
}

return res.status(200).json({
data: filteredDevices,
data: cleanedDevices,
});
});

Expand Down
13 changes: 8 additions & 5 deletions packages/ui/src/components/DeviceCard/DeviceCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import { StyledText, StyledHeader } from "../../utils/theme";
import { type Device } from "@starlight/types";

const emojiLookup = {
light: "💡",
switch: "🔌",
fan: "🌡️",
numeric: "📶",
climate: "❄️️",
cover: "🪟",
fan: "🌡️",
light: "💡",
lock: "🔒",
climate: "❄️️",
switch: "🔌",
};

const Card = styled.div`
Expand All @@ -21,6 +22,8 @@ const Card = styled.div`
cursor: pointer;
border-radius: 2rem;
opacity: ${(props) => (props.dimmed ? 1 : 0.5)};
&:hover {
background-color: ${({ theme }) => theme.hover};
}
Expand All @@ -35,7 +38,7 @@ function DeviceCard(props: { device: Device; onClick: Function }) {
}

return (
<Card onClick={props.onClick}>
<Card onClick={props.onClick} dimmed={props.device.supported}>
<StyledHeader>
{deviceEmoji} {props.device.friendly_name}
</StyledHeader>
Expand Down
6 changes: 1 addition & 5 deletions packages/ui/src/components/DeviceList/DeviceList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@ import { Link } from "wouter";
import DeviceCard from "../DeviceCard/DeviceCard";
import { type Device } from "@starlight/types";

const deviceSort = (a: Device, b: Device) =>
+isFinite(a.friendly_name[0]) - +isFinite(b.friendly_name[0]) ||
a.friendly_name.localeCompare(b.friendly_name, undefined, { numeric: true });

function DeviceList(props: { devices: Device[]; onClick: Function }) {
return (
<div>
{props.devices.sort(deviceSort).map((device: Device) => (
{props.devices.map((device: Device) => (
<Link
href={`/devices/${device.ieee_address}`}
key={device.ieee_address}
Expand Down

0 comments on commit e99c914

Please sign in to comment.