-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPanelKit.tsx
More file actions
176 lines (154 loc) · 4.35 KB
/
Copy pathPanelKit.tsx
File metadata and controls
176 lines (154 loc) · 4.35 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
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
import React, { Component } from "react";
import styled from "styled-components";
import { ToastContainer, toast } from "react-toastify";
import { Flex, Box } from "reflexbox/styled-components";
import * as Sentry from "@sentry/browser";
import "react-toastify/dist/ReactToastify.css";
import "../Toast.css";
import { Config, TileConfig } from "../types";
import HomeAssistant from "../hass";
import EventManager from "./EventManager";
import Header from "./Header";
import TileErrorBoundary from "./TileErrorBoundary";
import { setupSentry } from "../sentry";
const Container = styled.div``;
interface Props {
config: Config;
gridWidth: number;
tileSize: number;
}
interface State {
isReady: boolean;
}
export default class PanelKit extends Component<Props, State> {
static defaultProps = {
gridWidth: 8,
tileSize: 167,
};
hass: HomeAssistant;
readonly state = {
isReady: false,
};
constructor(props: Props, context: any) {
super(props, context);
const sentryDsn =
process.env.REACT_APP_SENTRY_DSN || this.props.config.sentryDsn;
if (sentryDsn) {
setupSentry({
dsn: sentryDsn,
release: process.env.REACT_APP_GIT_SHA,
environment: process.env.NODE_ENV || "development",
});
Sentry.configureScope((scope) => {
scope.setTransaction("panelkit.boot");
});
}
const url =
process.env.REACT_APP_HASS_URL ||
this.props.config.url ||
"http://localhost:8123";
Sentry.setTag("hass.url", url);
this.hass = new HomeAssistant({
// we prioritize the environment variables to ease development
url,
accessToken:
process.env.REACT_APP_HASS_ACCESS_TOKEN ||
this.props.config.accessToken ||
"",
onReady: this.onReady,
onError: (error: Error | ErrorEvent) => {
Sentry.captureException(error);
toast.error(error.message);
},
onOpen: () => {
toast.success("Connected to Home Assistant.");
},
});
}
componentDidMount() {
this.hass.connect();
}
componentWillUnmount() {
this.hass.disconnect();
delete this.hass;
}
onReady = () => {
this.setState({ isReady: true });
};
// TODO: should cache this somewhere
getCameraList(): string[] {
const results: string[] = [];
function recurse(tiles: TileConfig[]) {
tiles.forEach((tile) => {
if (tile.tiles) recurse(tile.tiles);
else if (tile.entityId && tile.entityId.indexOf("camera.") === 0)
results.push(tile.entityId);
});
}
recurse(this.props.config.tiles);
return results;
}
renderTiles(tiles: TileConfig[], colWidth = 1, depth = 0) {
const hass = this.hass;
const cameraList = this.getCameraList();
const { tileSize } = this.props;
return (
<Flex flexWrap="wrap" alignContent="space-evenly" p={depth ? "10px" : 0}>
{tiles.map((tile, index) => {
let { width, height } = tile;
if (!width) width = 1;
if (!height) height = 1;
width = Math.min(width, colWidth);
if (tile.tiles) {
return (
<Box key={index} width={[1, 1 / 2, width / colWidth]}>
{this.renderTiles(tile.tiles, width, depth + 1)}
</Box>
);
} else {
return (
<Box
key={index}
width={[1 / 2, width / colWidth]}
p="4px"
style={{ minHeight: tileSize * height }}
>
<TileErrorBoundary>
<tile.type hass={hass} cameraList={cameraList} {...tile} />
</TileErrorBoundary>
</Box>
);
}
})}
</Flex>
);
}
renderContent() {
return (
<React.Fragment>
{this.props.config.events && (
<EventManager hass={this.hass} events={this.props.config.events} />
)}
<Header />
{this.renderTiles(this.props.config.tiles, this.props.gridWidth)}
</React.Fragment>
);
}
render() {
return (
<Container>
{this.state.isReady ? (
this.renderContent()
) : (
<p>Connecting to Home Assistant...</p>
)}
<ToastContainer
position="bottom-right"
hideProgressBar
newestOnTop
limit={1}
/>
</Container>
);
}
}