Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit 59761e5

Browse files
author
Dan Greco
committed
Add vertical option
1 parent 65d00db commit 59761e5

File tree

7 files changed

+54
-22
lines changed

7 files changed

+54
-22
lines changed

dist/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,12 @@
6363
type: 'custom:threedy-card',
6464
base_entity: 'sensor.ender_3_v2',
6565
name: "Ender 3 v2",
66-
theme: "Default",
66+
theme: "Neumorphic",
6767
scale: 1,
6868
temperature_unit: 'C',
6969
printer_type: 'I3',
7070
round_temperature: false,
71+
vertical: true,
7172
use_24hr: true,
7273
light_entity: 'switch.light',
7374
camera_entity: "camera.test",

src/Components/Card/index.tsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Stats from '../Stats';
1111

1212
import styles from './styles';
1313
import Camera from "../Camera";
14+
import {percentComplete} from "../Stats/utils";
1415

1516

1617
const Card = ({ }) => {
@@ -39,6 +40,9 @@ const Card = ({ }) => {
3940

4041

4142
const theme = config.theme || 'Default';
43+
const vertical = config.vertical;
44+
const round = config.round;
45+
const percent = percentComplete(hass, config);
4246

4347

4448
const borderRadius = styles[theme] ? styles[theme].borderRadius : styles['Default'].borderRadius;
@@ -131,24 +135,31 @@ const Card = ({ }) => {
131135
</div>
132136

133137
<motion.div
134-
style={{ ...styles.Content }}
138+
style={{ ...styles.Content, flexDirection: vertical ? 'column' : 'row' }}
135139
animate={{ height: hidden ? 0.0 : 'auto', opacity: hidden ? 0.0 : 1.0, scale: hidden ? 0.0 : 1.0 }}
136140
transition={{ ease: "easeInOut", duration: 0.25 }}
137141
>
138-
<div style={{ ...styles.Section }}>
142+
<div style={{ ...styles.Section, width: vertical ? '100%' : '50%', height: vertical ? 'auto' : '100%', display: 'flex', flexDirection: 'row', justifyContent: 'space-between', paddingLeft: vertical ? 80 : 16, paddingRight: vertical ? 80 : 16 }}>
139143
<PrinterView
140144
toggleVideo={toggleVideo}
141145
hasCamera={config.camera_entity !== undefined}
142146
/>
147+
{
148+
vertical ? (
149+
<p style={{ width: '50%', fontSize: 36 }}>{round ? Math.round(percent) : percent}%</p>
150+
) : null
151+
}
143152
</div>
144153
<div
145154
style={{
146155
...styles.Section,
147-
paddingLeft: 16,
148-
paddingRight: 32
156+
paddingLeft: vertical ? 64 : 16,
157+
paddingRight: vertical ? 64 : 32,
158+
width: vertical ? '100%' : '50%',
159+
height: vertical ? 'auto' : '100%'
149160
}}
150161
>
151-
<Stats />
162+
<Stats showPercent={!vertical} />
152163
</div>
153164
</motion.div>
154165

src/Components/Card/styles.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ const styles = {
1313
},
1414
Section: {
1515
boxSizing: 'border-box',
16-
width: '50%',
17-
height: '100%',
1816
padding: '0 16px 32px 16px'
1917
},
2018
Default: {
@@ -60,7 +58,6 @@ const styles = {
6058
Content: {
6159
width: '100%',
6260
display: 'flex',
63-
flexDirection: 'row',
6461
justifyContent: 'center',
6562
alignItems: 'center',
6663
boxSizing: 'border-box'

src/Components/Stats/TimeStat.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,23 @@ type TimeStatProps = {
5656
const TimeStat: React.FC<TimeStatProps> = ({timeEntity, condition, config, direction}) => {
5757

5858
const [ time, setTime ] = useState<number>( timeEntity.state || 0);
59+
const [ lastIntervalId, setLastIntervalId ] = useState<number>(-1);
60+
61+
const incTime = () => setTime( time => time + direction );
5962

6063
useEffect(() => {
61-
setInterval(
62-
() => setTime(time => time + direction),
64+
65+
if (lastIntervalId !== -1) clearInterval(lastIntervalId);
66+
67+
setTime(timeEntity.state || 0);
68+
69+
const id = setInterval(
70+
incTime,
6371
1000
6472
);
65-
}, [])
6673

67-
useEffect(() => {
68-
setTime(timeEntity.state || 0);
74+
setLastIntervalId(id);
75+
6976
}, [timeEntity])
7077

7178
return (

src/Components/Stats/index.tsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,31 @@ import React, { useContext } from 'react';
22
import ThreedyContext from '../../Contexts/ThreedyContext';
33

44
import styles from './styles';
5-
import {renderStats} from "./utils";
5+
import {percentComplete, renderStats} from "./utils";
66

7+
type StatsProps = {
8+
showPercent?: boolean
9+
};
710

8-
const Stats = () => {
11+
const Stats: React.FC<StatsProps> = ({ showPercent = true }) => {
912

1013
const {
1114
hass,
1215
config,
1316
} = useContext(ThreedyContext);
1417

1518
const round = config.round === undefined ? true : config.round;
16-
const percentComplete = (hass.states[config.use_mqtt ? `${config.base_entity}_print_progress` : `${config.base_entity}_job_percentage`] || { state: -1.0 }).state;
19+
const percent = percentComplete(hass, config);
1720

1821
return (
1922
<div style={{ ...styles.Stats }}>
20-
<div style={{ ...styles.Percent }}>
21-
<p style={{ ...styles.PercentText }}>{round ? Math.round(percentComplete) : percentComplete}%</p>
22-
</div>
23+
{
24+
showPercent ? (
25+
<div style={{ ...styles.Percent }}>
26+
<p style={{ ...styles.PercentText }}>{round ? Math.round(percent) : percent}%</p>
27+
</div>
28+
) : (null)
29+
}
2330
<div style={{ ...styles.Monitored }}>
2431
{
2532
config.monitored ? renderStats(hass, config) : null

src/Components/Stats/utils.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ const renderStats = (
129129

130130
}
131131

132+
const percentComplete = (
133+
hass: HomeAssistant,
134+
config: ThreedyConfig
135+
) => {
136+
return (hass.states[config.use_mqtt ? `${config.base_entity}_print_progress` : `${config.base_entity}_job_percentage`] || { state: -1.0 }).state;
137+
}
138+
132139
export {
133-
renderStats
140+
renderStats,
141+
percentComplete
134142
}

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"removeComments": true,
99
"sourceMap": true,
1010
"allowJs": true,
11-
"jsx": "react"
11+
"jsx": "react",
12+
"allowSyntheticDefaultImports": true
1213
},
1314
"exclude": ["build"]
1415
}

0 commit comments

Comments
 (0)