|
| 1 | +import { useCallback, useMemo } from "react"; |
| 2 | +import { useTranslation } from "react-i18next"; |
| 3 | +import { useShallow } from "zustand/react/shallow"; |
| 4 | +import { useAppStore } from "../../../store.js"; |
| 5 | +import type { FeatureWithAnySubFeatures, Group } from "../../../types.js"; |
| 6 | +import { sendMessage } from "../../../websocket/WebSocketManager.js"; |
| 7 | +import Feature from "../../features/Feature.js"; |
| 8 | +import FeatureWrapper from "../../features/FeatureWrapper.js"; |
| 9 | +import { getFeatureKey } from "../../features/index.js"; |
| 10 | + |
| 11 | +type ExposesProps = { |
| 12 | + sourceIdx: number; |
| 13 | + group: Group; |
| 14 | +}; |
| 15 | + |
| 16 | +/** |
| 17 | + * Use non-clashing Map keys. |
| 18 | + * NOTE: fallback to `type` because despite the ZHC typing, `name` isn't actually always defined |
| 19 | + */ |
| 20 | +const getFeatureId = (feature: FeatureWithAnySubFeatures) => |
| 21 | + feature.property ? `__property_${feature.property}` : feature.name ? `__name_${feature.name}` : `__type_${feature.type}`; |
| 22 | + |
| 23 | +/** |
| 24 | + * Recursively find common features to a reference set (including sub-features if any). |
| 25 | + * Returns a list of cloned features, with possible sub-features. |
| 26 | + */ |
| 27 | +const findCommonFeatures = ( |
| 28 | + refFeatures: FeatureWithAnySubFeatures[], |
| 29 | + otherFeatureMaps: Map<string, FeatureWithAnySubFeatures>[], |
| 30 | +): FeatureWithAnySubFeatures[] => { |
| 31 | + const common: FeatureWithAnySubFeatures[] = []; |
| 32 | + |
| 33 | + for (const refFeature of refFeatures) { |
| 34 | + const refId = getFeatureId(refFeature); |
| 35 | + const matchingFeatures: FeatureWithAnySubFeatures[] = []; |
| 36 | + |
| 37 | + // check if feature exists in all other devices |
| 38 | + for (const otherFeatureMap of otherFeatureMaps) { |
| 39 | + const match = otherFeatureMap.get(refId); |
| 40 | + |
| 41 | + if (!match) { |
| 42 | + continue; |
| 43 | + } |
| 44 | + |
| 45 | + matchingFeatures.push(match); |
| 46 | + } |
| 47 | + |
| 48 | + if (matchingFeatures.length === 0) { |
| 49 | + continue; |
| 50 | + } |
| 51 | + |
| 52 | + // clone the feature |
| 53 | + const commonFeature = { ...refFeature }; |
| 54 | + |
| 55 | + // if feature has sub-features, recursively find common ones |
| 56 | + if ("features" in refFeature && refFeature.features.length > 0) { |
| 57 | + const otherSubFeaturesMaps: Map<string, FeatureWithAnySubFeatures>[] = []; |
| 58 | + |
| 59 | + for (const matchingFeature of matchingFeatures) { |
| 60 | + if ("features" in matchingFeature) { |
| 61 | + otherSubFeaturesMaps.push(new Map((matchingFeature.features as FeatureWithAnySubFeatures[]).map((f) => [getFeatureId(f), f]))); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + const commonSubFeatures = findCommonFeatures(refFeature.features as FeatureWithAnySubFeatures[], otherSubFeaturesMaps); |
| 66 | + |
| 67 | + // only include feature if it has common sub-features |
| 68 | + if (commonSubFeatures.length > 0) { |
| 69 | + // check is superfluous, already done in wrapping if, just with the original (only required by typing) |
| 70 | + if ("features" in commonFeature) { |
| 71 | + commonFeature.features = commonSubFeatures; |
| 72 | + } |
| 73 | + |
| 74 | + common.push(commonFeature); |
| 75 | + } |
| 76 | + } else { |
| 77 | + common.push(commonFeature); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return common; |
| 82 | +}; |
| 83 | + |
| 84 | +export default function Exposes({ sourceIdx, group }: ExposesProps) { |
| 85 | + const { t } = useTranslation("common"); |
| 86 | + const devices = useAppStore(useShallow((state) => state.devices[sourceIdx])); |
| 87 | + const deviceScenesFeatures = useAppStore(useShallow((state) => state.deviceScenesFeatures[sourceIdx])); |
| 88 | + const firstMember = group.members.length > 0 ? group.members[0] : undefined; |
| 89 | + const refDevice = firstMember ? devices.find((d) => d.ieee_address === firstMember.ieee_address) : undefined; |
| 90 | + const refEndpointName = refDevice && firstMember ? refDevice.endpoints[firstMember.endpoint]?.name : undefined; |
| 91 | + |
| 92 | + const groupData = useMemo(() => { |
| 93 | + if (!refDevice) { |
| 94 | + return []; |
| 95 | + } |
| 96 | + |
| 97 | + const refFeatures = deviceScenesFeatures[refDevice.ieee_address] ?? []; |
| 98 | + |
| 99 | + if (refFeatures.length === 0) { |
| 100 | + return []; |
| 101 | + } |
| 102 | + |
| 103 | + const refEndpointFeatures = refFeatures.filter( |
| 104 | + // XXX: show if feature has no endpoint? |
| 105 | + (f) => !f.endpoint || Number(f.endpoint) === firstMember?.endpoint || f.endpoint === refEndpointName, |
| 106 | + ); |
| 107 | + |
| 108 | + if (group.members.length === 1) { |
| 109 | + return refEndpointFeatures; |
| 110 | + } |
| 111 | + |
| 112 | + const otherMembersFeatures: Map<string, FeatureWithAnySubFeatures>[] = []; |
| 113 | + |
| 114 | + for (let i = 1; i < group.members.length; i++) { |
| 115 | + const member = group.members[i]; |
| 116 | + const memberFeatures = deviceScenesFeatures[member.ieee_address]; |
| 117 | + |
| 118 | + if (!memberFeatures || memberFeatures.length === 0) { |
| 119 | + return []; |
| 120 | + } |
| 121 | + |
| 122 | + const memberEndpointFeaturesMap = new Map<string, FeatureWithAnySubFeatures>(); |
| 123 | + |
| 124 | + for (const memberFeature of memberFeatures) { |
| 125 | + // XXX: show if feature has no endpoint? |
| 126 | + if ( |
| 127 | + !memberFeature.endpoint || |
| 128 | + Number(memberFeature.endpoint) === member.endpoint || |
| 129 | + memberFeature.endpoint === refDevice.endpoints[member.endpoint]?.name |
| 130 | + ) { |
| 131 | + memberEndpointFeaturesMap.set(getFeatureId(memberFeature), memberFeature); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + otherMembersFeatures.push(memberEndpointFeaturesMap); |
| 136 | + } |
| 137 | + |
| 138 | + return findCommonFeatures(refEndpointFeatures, otherMembersFeatures); |
| 139 | + }, [firstMember?.endpoint, refDevice, refEndpointName, deviceScenesFeatures, group.members]); |
| 140 | + |
| 141 | + const onChange = useCallback( |
| 142 | + async (value: Record<string, unknown>) => { |
| 143 | + await sendMessage<"{friendlyNameOrId}/set">( |
| 144 | + sourceIdx, |
| 145 | + // @ts-expect-error templated API endpoint |
| 146 | + `${group.id}/set`, |
| 147 | + value, |
| 148 | + ); |
| 149 | + }, |
| 150 | + [sourceIdx, group.id], |
| 151 | + ); |
| 152 | + |
| 153 | + const onRead = useCallback( |
| 154 | + async (value: Record<string, unknown>) => { |
| 155 | + await sendMessage<"{friendlyNameOrId}/get">( |
| 156 | + sourceIdx, |
| 157 | + // @ts-expect-error templated API endpoint |
| 158 | + `${group.id}/get`, |
| 159 | + value, |
| 160 | + ); |
| 161 | + }, |
| 162 | + [sourceIdx, group.id], |
| 163 | + ); |
| 164 | + |
| 165 | + return refDevice && groupData.length > 0 ? ( |
| 166 | + <div className="list bg-base-100"> |
| 167 | + {groupData.map((expose) => ( |
| 168 | + <Feature |
| 169 | + key={getFeatureKey(expose)} |
| 170 | + feature={expose} |
| 171 | + device={refDevice} |
| 172 | + deviceState={{}} |
| 173 | + onChange={onChange} |
| 174 | + onRead={onRead} |
| 175 | + featureWrapperClass={FeatureWrapper} |
| 176 | + parentFeatures={[]} |
| 177 | + /> |
| 178 | + ))} |
| 179 | + </div> |
| 180 | + ) : ( |
| 181 | + t(($) => $.empty_exposes_definition) |
| 182 | + ); |
| 183 | +} |
0 commit comments