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

Commit

Permalink
Add vertical option
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Greco committed Mar 10, 2021
1 parent 65d00db commit 59761e5
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 22 deletions.
3 changes: 2 additions & 1 deletion dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@
type: 'custom:threedy-card',
base_entity: 'sensor.ender_3_v2',
name: "Ender 3 v2",
theme: "Default",
theme: "Neumorphic",
scale: 1,
temperature_unit: 'C',
printer_type: 'I3',
round_temperature: false,
vertical: true,
use_24hr: true,
light_entity: 'switch.light',
camera_entity: "camera.test",
Expand Down
21 changes: 16 additions & 5 deletions src/Components/Card/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Stats from '../Stats';

import styles from './styles';
import Camera from "../Camera";
import {percentComplete} from "../Stats/utils";


const Card = ({ }) => {
Expand Down Expand Up @@ -39,6 +40,9 @@ const Card = ({ }) => {


const theme = config.theme || 'Default';
const vertical = config.vertical;
const round = config.round;
const percent = percentComplete(hass, config);


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

<motion.div
style={{ ...styles.Content }}
style={{ ...styles.Content, flexDirection: vertical ? 'column' : 'row' }}
animate={{ height: hidden ? 0.0 : 'auto', opacity: hidden ? 0.0 : 1.0, scale: hidden ? 0.0 : 1.0 }}
transition={{ ease: "easeInOut", duration: 0.25 }}
>
<div style={{ ...styles.Section }}>
<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 }}>
<PrinterView
toggleVideo={toggleVideo}
hasCamera={config.camera_entity !== undefined}
/>
{
vertical ? (
<p style={{ width: '50%', fontSize: 36 }}>{round ? Math.round(percent) : percent}%</p>
) : null
}
</div>
<div
style={{
...styles.Section,
paddingLeft: 16,
paddingRight: 32
paddingLeft: vertical ? 64 : 16,
paddingRight: vertical ? 64 : 32,
width: vertical ? '100%' : '50%',
height: vertical ? 'auto' : '100%'
}}
>
<Stats />
<Stats showPercent={!vertical} />
</div>
</motion.div>

Expand Down
3 changes: 0 additions & 3 deletions src/Components/Card/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ const styles = {
},
Section: {
boxSizing: 'border-box',
width: '50%',
height: '100%',
padding: '0 16px 32px 16px'
},
Default: {
Expand Down Expand Up @@ -60,7 +58,6 @@ const styles = {
Content: {
width: '100%',
display: 'flex',
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
boxSizing: 'border-box'
Expand Down
17 changes: 12 additions & 5 deletions src/Components/Stats/TimeStat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,23 @@ type TimeStatProps = {
const TimeStat: React.FC<TimeStatProps> = ({timeEntity, condition, config, direction}) => {

const [ time, setTime ] = useState<number>( timeEntity.state || 0);
const [ lastIntervalId, setLastIntervalId ] = useState<number>(-1);

const incTime = () => setTime( time => time + direction );

useEffect(() => {
setInterval(
() => setTime(time => time + direction),

if (lastIntervalId !== -1) clearInterval(lastIntervalId);

setTime(timeEntity.state || 0);

const id = setInterval(
incTime,
1000
);
}, [])

useEffect(() => {
setTime(timeEntity.state || 0);
setLastIntervalId(id);

}, [timeEntity])

return (
Expand Down
19 changes: 13 additions & 6 deletions src/Components/Stats/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,31 @@ import React, { useContext } from 'react';
import ThreedyContext from '../../Contexts/ThreedyContext';

import styles from './styles';
import {renderStats} from "./utils";
import {percentComplete, renderStats} from "./utils";

type StatsProps = {
showPercent?: boolean
};

const Stats = () => {
const Stats: React.FC<StatsProps> = ({ showPercent = true }) => {

const {
hass,
config,
} = useContext(ThreedyContext);

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

return (
<div style={{ ...styles.Stats }}>
<div style={{ ...styles.Percent }}>
<p style={{ ...styles.PercentText }}>{round ? Math.round(percentComplete) : percentComplete}%</p>
</div>
{
showPercent ? (
<div style={{ ...styles.Percent }}>
<p style={{ ...styles.PercentText }}>{round ? Math.round(percent) : percent}%</p>
</div>
) : (null)
}
<div style={{ ...styles.Monitored }}>
{
config.monitored ? renderStats(hass, config) : null
Expand Down
10 changes: 9 additions & 1 deletion src/Components/Stats/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ const renderStats = (

}

const percentComplete = (
hass: HomeAssistant,
config: ThreedyConfig
) => {
return (hass.states[config.use_mqtt ? `${config.base_entity}_print_progress` : `${config.base_entity}_job_percentage`] || { state: -1.0 }).state;
}

export {
renderStats
renderStats,
percentComplete
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"removeComments": true,
"sourceMap": true,
"allowJs": true,
"jsx": "react"
"jsx": "react",
"allowSyntheticDefaultImports": true
},
"exclude": ["build"]
}

0 comments on commit 59761e5

Please sign in to comment.