forked from awesomestvi/navet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-zone-layout.ts
More file actions
50 lines (44 loc) · 1.75 KB
/
use-zone-layout.ts
File metadata and controls
50 lines (44 loc) · 1.75 KB
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
import { useMemo } from 'react';
import { HOME_WIDGET_ROOM, isAllRooms } from '@/app/constants/rooms';
import type { DeviceWithType } from '@/app/types/device.types';
import type { CustomCard } from '../stores/custom-cards-store';
import { resolveCardZone } from './resolve-card-zone';
import { ZONE_ORDERED, type ZoneName } from './zone-types';
export interface ZoneSection {
zone: ZoneName;
orderedIds: string[];
}
/**
* Partitions all home-screen cards into named zones.
*
* Device cards are bucketed by device type (or stored zone override).
* Custom cards assigned to the home screen are included, bucketed by card type
* (or stored zone override on the card or in cardZones).
*
* Only zones that contain at least one card are returned.
*/
export function useZoneLayout(
deviceMap: Map<string, DeviceWithType>,
customCards: CustomCard[],
cardZones: Record<string, ZoneName>
): ZoneSection[] {
return useMemo(() => {
const buckets = new Map<ZoneName, string[]>(ZONE_ORDERED.map((z) => [z, []]));
const push = (zone: ZoneName, id: string) => {
const bucket = buckets.get(zone);
if (bucket) bucket.push(id);
};
// Device cards — all devices in the current room's deviceMap
for (const [id, device] of deviceMap) {
push(resolveCardZone(device.type, cardZones[id]), id);
}
// Custom widget cards — only those assigned to the home screen
for (const card of customCards) {
if (!isAllRooms(card.room) && card.room !== HOME_WIDGET_ROOM) continue;
push(resolveCardZone(card.type, card.zone ?? cardZones[card.id]), card.id);
}
return ZONE_ORDERED.map((z) => ({ zone: z, orderedIds: buckets.get(z) ?? [] })).filter(
(s) => s.orderedIds.length > 0
);
}, [deviceMap, customCards, cardZones]);
}