From 92c165654d4c617edd3e1edf60809ee452b336a2 Mon Sep 17 00:00:00 2001 From: Dan Greco Date: Wed, 17 Feb 2021 13:41:57 -0500 Subject: [PATCH] Finish advanced config --- .idea/.gitignore | 5 + .idea/modules.xml | 8 ++ .idea/threedy.iml | 12 ++ .idea/vcs.xml | 6 + src/Configurator/Components/Button/index.js | 38 ++++++ src/Configurator/Components/Button/styles.js | 18 +++ src/Configurator/utils.js | 115 +++++++++++++++++++ 7 files changed, 202 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/modules.xml create mode 100644 .idea/threedy.iml create mode 100644 .idea/vcs.xml create mode 100644 src/Configurator/Components/Button/index.js create mode 100644 src/Configurator/Components/Button/styles.js create mode 100644 src/Configurator/utils.js diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..b58b603 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,5 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..826fc3b --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/threedy.iml b/.idea/threedy.iml new file mode 100644 index 0000000..0c8867d --- /dev/null +++ b/.idea/threedy.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/Configurator/Components/Button/index.js b/src/Configurator/Components/Button/index.js new file mode 100644 index 0000000..c7a8a41 --- /dev/null +++ b/src/Configurator/Components/Button/index.js @@ -0,0 +1,38 @@ +import React, { useRef, useState, useEffect } from 'react'; +import { motion } from 'framer-motion'; + +import styles from './styles'; + +const Button = ({ onClick, children, style }) => { + + const [active, setActive] = useState(false); + + const ref = useRef(); + + const handleClick = e => { + + if (!ref.current.contains(e.target)) { + setActive(false); + } + + } + + return ( + setActive(true)} + onMouseUp={() => setActive(false)} + onMouseLeave={() => setActive(false)} + > + { children } + + ) + +}; + +export default Button; \ No newline at end of file diff --git a/src/Configurator/Components/Button/styles.js b/src/Configurator/Components/Button/styles.js new file mode 100644 index 0000000..2c8952c --- /dev/null +++ b/src/Configurator/Components/Button/styles.js @@ -0,0 +1,18 @@ +const styles = { + Button: { + border: 'none', + outline: 'none', + padding: '0 32px', + boxSizing: 'border-box', + fontSize: 16, + fontWeight: 'bold', + lineHeight: '48px', + borderRadius: 8, + textAlign: 'center', + cursor: 'pointer', + backgroundColor: 'rgba(0,0,0,0.05)', + color: 'var(--primary-text-color)' + } +}; + +export default styles; \ No newline at end of file diff --git a/src/Configurator/utils.js b/src/Configurator/utils.js new file mode 100644 index 0000000..d32c0dd --- /dev/null +++ b/src/Configurator/utils.js @@ -0,0 +1,115 @@ +const conditions = [ + 'Status', + 'ETA', + 'Elapsed', + 'Hotend', + 'Bed' +]; + +const printerTypes = { + "I3": "I3", + "Cantilever": "Cantilever" +} + +const themes = { + 'Default': 'Default', + 'Neumorphic': 'Neumorphic' +} + + +/* Printer Entity ID -> Name; E.G. sensor.printer_name_current_state -> Printer Name */ +const printerName = ( entityId ) => { + + if ( !entityId ) { + return undefined; + } + + return entityId + .replace('_current_state', '') + .replace('sensor.', '') + .split("_") + .map(s => s.charAt(0).toUpperCase() + s.substring(1)) + .join(" "); + +} + +/* Printer Entity ID -> Base Entity; E.G. sensor.printer_name_current_state -> sensor.printer_name */ +const printerBase = ( entityId ) => { + + if ( !entityId ) { + return undefined; + } + + return entityId + .replace('_current_state', ''); + +} + +const getPrinters = ( hass ) => { + + const printers = {}; + + Object.keys( hass.states ).filter( + entityId => (/sensor\..*_current_state/g).test(entityId) + ).map( + entityId => { + + const name = printerName(entityId); + const base = printerBase(entityId); + + printers[name] = base; + + } + ) + + + return printers; +} + +const getToggleables = ( hass ) => { + + const toggleables = {}; + + Object.keys(hass.states).filter( + entityId => (/^(switch|light)/g).test(entityId) + ).map( toggleable => toggleables[toggleable] = toggleable ); + + return toggleables; + +} + +/* Updates the Threedy Card config given updates */ +const updateConfig = ( threedy, modifiedConfig, updates ) => { + + const event = new Event("config-changed", { + bubbles: true, + composed: true + }); + event.detail = { + config: { + ...modifiedConfig, + ...updates + } + }; + threedy.dispatchEvent(event); + + return event.detail.config; +} + +const updateValue = ( _updateConfig, key, value ) => { + _updateConfig({ + [key]: value + }) +} + + +export { + conditions, + printerTypes, + themes, + printerName, + getPrinters, + getToggleables, + updateConfig, + updateValue +}