|
1 | 1 | import React, { Component } from "react"; |
2 | 2 | import styled from "styled-components"; |
3 | | -import { ToastContainer, toast } from "react-toastify"; |
4 | | -import { Flex, Box } from "reflexbox/styled-components"; |
5 | | -import * as Sentry from "@sentry/browser"; |
6 | | -import { CaptureConsole as CaptureConsoleIntegration } from "@sentry/integrations"; |
7 | 3 |
|
8 | | -import "react-toastify/dist/ReactToastify.css"; |
9 | | -import "./Toast.css"; |
10 | | - |
11 | | -import { Config, TileConfig } from "./types"; |
12 | | -import HomeAssistant from "./hass"; |
13 | | -import Header from "./components/Header"; |
| 4 | +import { Config } from "./types"; |
| 5 | +import PanelKit from "./components/PanelKit"; |
14 | 6 |
|
15 | 7 | const ConfigContainer = styled.div` |
16 | 8 | position: absolute; |
@@ -53,194 +45,12 @@ const ConfigErrorContainer = styled.div` |
53 | 45 | } |
54 | 46 | `; |
55 | 47 |
|
56 | | -const Container = styled.div``; |
57 | | - |
58 | | -interface PanelKitProps { |
59 | | - config: Config; |
60 | | - gridWidth: number; |
61 | | - tileSize: number; |
62 | | -} |
63 | | - |
64 | | -interface PanelKitState { |
65 | | - isReady: boolean; |
66 | | -} |
67 | | - |
68 | | -class TileErrorBoundary extends Component { |
69 | | - readonly state: { |
70 | | - error: Error | ErrorEvent | null; |
71 | | - } = { |
72 | | - error: null, |
73 | | - }; |
74 | | - |
75 | | - componentDidCatch(error: ErrorEvent | Error) { |
76 | | - this.setState({ error }); |
77 | | - } |
78 | | - |
79 | | - render() { |
80 | | - if (this.state.error) |
81 | | - return ( |
82 | | - <div> |
83 | | - <h3>Tile Error</h3> |
84 | | - {this.state.error.message} |
85 | | - </div> |
86 | | - ); |
87 | | - return this.props.children; |
88 | | - } |
89 | | -} |
90 | | - |
91 | | -class PanelKit extends Component<PanelKitProps, PanelKitState> { |
92 | | - static defaultProps = { |
93 | | - gridWidth: 8, |
94 | | - tileSize: 167, |
95 | | - }; |
96 | | - |
97 | | - hass: HomeAssistant; |
98 | | - |
99 | | - readonly state = { |
100 | | - isReady: false, |
101 | | - }; |
102 | | - |
103 | | - constructor(props: PanelKitProps, context: any) { |
104 | | - super(props, context); |
105 | | - |
106 | | - const sentryDsn = |
107 | | - process.env.REACT_APP_SENTRY_DSN || this.props.config.sentryDsn; |
108 | | - if (sentryDsn) { |
109 | | - console.log(`[sentry] Initialized with DSN: ${sentryDsn}`); |
110 | | - Sentry.init({ |
111 | | - dsn: sentryDsn, |
112 | | - integrations: [ |
113 | | - new CaptureConsoleIntegration({ |
114 | | - levels: ["warn", "error"], |
115 | | - }), |
116 | | - ], |
117 | | - }); |
118 | | - } |
119 | | - |
120 | | - const url = |
121 | | - process.env.REACT_APP_HASS_URL || |
122 | | - this.props.config.url || |
123 | | - "http://localhost:8123"; |
124 | | - |
125 | | - Sentry.setTag("hass.url", url); |
126 | | - |
127 | | - this.hass = new HomeAssistant({ |
128 | | - // we prioritize the environment variables to ease development |
129 | | - url, |
130 | | - accessToken: |
131 | | - process.env.REACT_APP_HASS_ACCESS_TOKEN || |
132 | | - this.props.config.accessToken || |
133 | | - "", |
134 | | - onReady: this.onReady, |
135 | | - onError: (error: Error | ErrorEvent) => { |
136 | | - Sentry.captureException(error); |
137 | | - toast.error(error.message); |
138 | | - }, |
139 | | - onOpen: () => { |
140 | | - toast.success("Connected to Home Assistant."); |
141 | | - }, |
142 | | - }); |
143 | | - } |
144 | | - |
145 | | - componentDidMount() { |
146 | | - this.hass.connect(); |
147 | | - } |
148 | | - |
149 | | - componentWillUnmount() { |
150 | | - this.hass.disconnect(); |
151 | | - delete this.hass; |
152 | | - } |
153 | | - |
154 | | - onReady = () => { |
155 | | - this.setState({ isReady: true }); |
156 | | - }; |
157 | | - |
158 | | - // TODO: should cache this somewhere |
159 | | - getCameraList(): string[] { |
160 | | - const results: string[] = []; |
161 | | - function recurse(tiles: TileConfig[]) { |
162 | | - tiles.forEach((tile) => { |
163 | | - if (tile.tiles) recurse(tile.tiles); |
164 | | - else if (tile.entityId && tile.entityId.indexOf("camera.") === 0) |
165 | | - results.push(tile.entityId); |
166 | | - }); |
167 | | - } |
168 | | - recurse(this.props.config.tiles); |
169 | | - return results; |
170 | | - } |
171 | | - |
172 | | - renderTiles(tiles: TileConfig[], colWidth = 1, depth = 0) { |
173 | | - const hass = this.hass; |
174 | | - const cameraList = this.getCameraList(); |
175 | | - const { tileSize } = this.props; |
176 | | - return ( |
177 | | - <Flex flexWrap="wrap" alignContent="space-evenly" p={depth ? "10px" : 0}> |
178 | | - {tiles.map((tile, index) => { |
179 | | - let { width, height } = tile; |
180 | | - if (!width) width = 1; |
181 | | - if (!height) height = 1; |
182 | | - |
183 | | - width = Math.min(width, colWidth); |
184 | | - |
185 | | - if (tile.tiles) { |
186 | | - return ( |
187 | | - <Box key={index} width={[1, 1 / 2, width / colWidth]}> |
188 | | - {this.renderTiles(tile.tiles, width, depth + 1)} |
189 | | - </Box> |
190 | | - ); |
191 | | - } else { |
192 | | - return ( |
193 | | - <Box |
194 | | - key={index} |
195 | | - width={[1 / 2, width / colWidth]} |
196 | | - p="4px" |
197 | | - style={{ minHeight: tileSize * height }} |
198 | | - > |
199 | | - <TileErrorBoundary> |
200 | | - <tile.type hass={hass} cameraList={cameraList} {...tile} /> |
201 | | - </TileErrorBoundary> |
202 | | - </Box> |
203 | | - ); |
204 | | - } |
205 | | - })} |
206 | | - </Flex> |
207 | | - ); |
208 | | - } |
209 | | - |
210 | | - renderContent() { |
211 | | - return ( |
212 | | - <React.Fragment> |
213 | | - <Header /> |
214 | | - {this.renderTiles(this.props.config.tiles, this.props.gridWidth)} |
215 | | - </React.Fragment> |
216 | | - ); |
217 | | - } |
218 | | - |
219 | | - render() { |
220 | | - return ( |
221 | | - <Container> |
222 | | - {this.state.isReady ? ( |
223 | | - this.renderContent() |
224 | | - ) : ( |
225 | | - <p>Connecting to Home Assistant...</p> |
226 | | - )} |
227 | | - <ToastContainer |
228 | | - position="bottom-right" |
229 | | - hideProgressBar |
230 | | - newestOnTop |
231 | | - limit={1} |
232 | | - /> |
233 | | - </Container> |
234 | | - ); |
235 | | - } |
236 | | -} |
237 | | - |
238 | | -interface AppProps { |
| 48 | +interface Props { |
239 | 49 | config: Config; |
240 | 50 | configError?: ErrorEvent | null; |
241 | 51 | } |
242 | 52 |
|
243 | | -export default class App extends Component<AppProps> { |
| 53 | +export default class App extends Component<Props> { |
244 | 54 | render() { |
245 | 55 | const { configError } = this.props; |
246 | 56 | if (configError) { |
|
0 commit comments