From 60ec7c3f294d5bcb29ca6ab11104db44c5d6be1c Mon Sep 17 00:00:00 2001 From: Ari Sosnovsky Date: Tue, 16 Nov 2021 13:25:53 -0500 Subject: [PATCH] + added bubble editor + initial numeric editor + entity id input --- web/src/automations/utils.ts | 12 +++- .../ConditionEditor/editorRender.tsx | 38 ++++++++++- web/src/components/Inputs/InputText.tsx | 13 +++- .../Inputs/InputTextBubble.stories.tsx | 43 ++++++++++++ web/src/components/Inputs/InputTextBubble.tsx | 68 +++++++++++++++++++ web/src/components/Inputs/styles.ts | 67 +++++++++++++++++- web/src/styles/page.ts | 4 +- 7 files changed, 237 insertions(+), 8 deletions(-) create mode 100644 web/src/components/Inputs/InputTextBubble.stories.tsx create mode 100644 web/src/components/Inputs/InputTextBubble.tsx diff --git a/web/src/automations/utils.ts b/web/src/automations/utils.ts index ff752a4..b879d70 100644 --- a/web/src/automations/utils.ts +++ b/web/src/automations/utils.ts @@ -48,7 +48,7 @@ export const getDescriptionFromAutomationNode = ( switch(node.condition) { case "numeric_state": if(node.condition_data.entity_id) { - const center = typeof node.condition_data.entity_id === 'string' ? node.condition_data.entity_id : '...' + const center = prettyEntityId(node.condition_data.entity_id); if(node.condition_data.above) { if(node.condition_data.below) { return `${node.condition_data.above} < ${center} < ${node.condition_data.below}` @@ -77,6 +77,16 @@ export const getDescriptionFromAutomationNode = ( return 'n/a' } +export const prettyEntityId = (entityId: string| string[]) => { + if (typeof entityId === 'string') { + return entityId + } else if(entityId.length === 1) { + return entityId[0] + } else { + return "..." + } +} + // export const determineNodeTypes = (action: AutomationNode) : [AutomationNodeType, NodeSubType] => { // if ('condition' in action) { // switch (action.condition) { diff --git a/web/src/components/ConditionEditor/editorRender.tsx b/web/src/components/ConditionEditor/editorRender.tsx index 0f3f5f8..6914830 100644 --- a/web/src/components/ConditionEditor/editorRender.tsx +++ b/web/src/components/ConditionEditor/editorRender.tsx @@ -1,8 +1,9 @@ import { FC } from "react"; -import { AutomationCondition, LogicCondition, TemplateCondition } from "~/automations/types/conditions"; +import { AutomationCondition, LogicCondition, NumericCondition, TemplateCondition } from "~/automations/types/conditions"; import { getDescriptionFromAutomationNode } from "~/automations/utils"; import InputText from "../Inputs/InputText"; import InputTextArea from "../Inputs/InputTextArea"; +import { InputEntity } from "../Inputs/InputTextBubble"; import { ConditionNode } from "./ConditionNode"; @@ -22,6 +23,8 @@ export const getEditor = (condition: AutomationCondition): Editor => { return LogicViewer; case 'not': return LogicViewer; + case 'numeric_state': + return NumericStateEditor; default: return () =>
Not Ready
} @@ -50,6 +53,39 @@ export const TemplateEditor: Editor = ({ } +export const NumericStateEditor: Editor = ({ + onChange, + condition, +}) => { + return
+ onChange({ + ...condition, + condition_data: { + ...condition.condition_data, + alias + } + })}/> + onChange({ + ...condition, + condition_data: { + ...condition.condition_data, + entity_id + } + })} + /> + onChange({ + ...condition, + condition_data: { + ...condition.condition_data, + value_template + } + })} resizable/> +
+} + + export const LogicViewer: Editor = ({ condition, onChange, diff --git a/web/src/components/Inputs/InputText.tsx b/web/src/components/Inputs/InputText.tsx index 7781a98..c368bab 100644 --- a/web/src/components/Inputs/InputText.tsx +++ b/web/src/components/Inputs/InputText.tsx @@ -1,4 +1,4 @@ -import { useState } from "react"; +import { PropsWithChildren, useState } from "react"; import { useToolTip } from "~/tooltip/context"; import InputWrapper from "./InputWrapper"; import { useInputTextStyles } from "./styles"; @@ -8,6 +8,7 @@ export interface Props { label: string; value: string; onChange: (v: string) => void; + onEnter?: () => void; additionalTooltipFilters?: Record; } export default function InputText({ @@ -15,8 +16,10 @@ export default function InputText({ textBoxFor, value="", onChange, + onEnter=()=>{}, additionalTooltipFilters={}, -}: Props) { + children, +}: PropsWithChildren) { const {classes} = useInputTextStyles({}); const tooltip = useToolTip(); const [isFocused, setIsFocused] = useState(false) @@ -24,6 +27,11 @@ export default function InputText({ { + if (e.key === 'Enter') { + onEnter(); + } + }} onChange={e => { e.preventDefault(); onChange(e.target.value) @@ -46,5 +54,6 @@ export default function InputText({ } }} /> + {children} } \ No newline at end of file diff --git a/web/src/components/Inputs/InputTextBubble.stories.tsx b/web/src/components/Inputs/InputTextBubble.stories.tsx new file mode 100644 index 0000000..07d2f2c --- /dev/null +++ b/web/src/components/Inputs/InputTextBubble.stories.tsx @@ -0,0 +1,43 @@ +import { ComponentMeta, ComponentStory } from "@storybook/react"; +import { useState } from "react"; +import { usePageTheme } from "~/styles/page"; +import InputTextBubble, { InputEntity } from "./InputTextBubble"; + +export default { + title: 'Inputs/InputTextBubble', + component: InputTextBubble, + parameters: { actions: { argTypesRegex: '^on.*' } }, + argTypes: { + label: { + defaultValue: 'Sample Title' + } + } + } as ComponentMeta; + + + export const ArrayText: ComponentStory = args => { + const {classes} = usePageTheme({}); + const [value, setValue] = useState(['wow','lots of bubbles', 'itchen', 'bathroom']) + return
+
+ +
+
+} + +export const JustText: ComponentStory = args => { + const {classes} = usePageTheme({}); + const [value, setValue] = useState('hello') + return
+ +
+} + + +export const EntityId: ComponentStory = args => { + const {classes} = usePageTheme({}); + const [value, setValue] = useState(['sensor.bathroom']) + return
+ +
+} diff --git a/web/src/components/Inputs/InputTextBubble.tsx b/web/src/components/Inputs/InputTextBubble.tsx new file mode 100644 index 0000000..d46cfe1 --- /dev/null +++ b/web/src/components/Inputs/InputTextBubble.tsx @@ -0,0 +1,68 @@ +import { FC, useState } from "react"; +import { useToolTip } from "~/tooltip/context"; +import InputText from "./InputText"; +import InputWrapper from "./InputWrapper"; +import { useInputBubblesStyles, useInputTextStyles } from "./styles"; + +export interface Props { + label: string; + textBoxFor?: string; + value: string | string[]; + onChange: (v: string[]) => void; + additionalTooltipFilters?: Record; +} +export default function InputTextBubble({ + value="", + label, + textBoxFor, + onChange, + additionalTooltipFilters={}, +}: Props) { + const {classes} = useInputBubblesStyles({}); + let selected: string[]; + if (typeof value === 'string') { + selected = [value] + } else { + selected = value + } + const [text, setText] = useState(''); + const addNew = () => { + if(text) { + onChange([text].concat(selected)) + setText("") + } + }; + + return setText(t)} + onEnter={() => addNew()} + additionalTooltipFilters={additionalTooltipFilters} + > +
+ {selected.map((s, i) =>
onChange([ + ...selected.slice(0, i), + ...selected.slice(i+1), + ])}> + {s} + X +
)} +
+ +
+} + +export const InputEntity: FC<{ + value: string | string[]; + onChange: (v: string[]) => void; +}> = ({ + value, + onChange, +}) => \ No newline at end of file diff --git a/web/src/components/Inputs/styles.ts b/web/src/components/Inputs/styles.ts index c93f070..6c77044 100644 --- a/web/src/components/Inputs/styles.ts +++ b/web/src/components/Inputs/styles.ts @@ -1,5 +1,5 @@ -import { Styles } from "jss"; import { createAppUseStyles, AppTheme } from "~/styles/theme"; +import Color from 'chroma-js'; const commonInputStyle = (theme: AppTheme) => ({ input: { @@ -76,4 +76,67 @@ export const useInputViewEditStyles = createAppUseStyles<{ pencil: { } -})); \ No newline at end of file +})); + + +export const useInputBubblesStyles = createAppUseStyles<{}>(theme => ({ + bubbles: { + display: 'flex', + flexDirection: 'row', + maxHeight: 50, + maxWidth: '100%', + overflowX: 'auto', + overflowY: 'hidden', + scrollbarWidth: 'thin', + }, + bubble: { + position: 'relative', + border: `1px solid ${theme.primaryAccent}`, + borderRadius: 10, + backgroundColor: Color(theme.primary).set('rgb.g', 150).alpha(0.35).hex(), + color: Color(theme.primary).brighten(5).hex(), + fontSize: 16, + + marginLeft: 1, + padding: 5, + width: 'fit-content', + whiteSpace: 'nowrap', + + cursor: 'pointer', + "&:hover": { + backgroundColor: Color(theme.primary).set('rgb.r', 150).alpha(0.35).hex(), + color: Color(theme.primary).brighten(5).alpha(0.35).hex(), + }, + + "&:hover $deleteIcon": { + display: 'block', + } + }, + deleteIcon: { + display: 'none', + position: "absolute", + left: "48%", + top: 5, + color: "white", + }, + addBtn: { + position: "absolute", + top: 10, + right: 10, + fontSize: 20, + cursor: 'pointer', + borderStyle: 'solid', + borderWidth: 1, + borderColor: theme.secondary, + backgroundColor: theme.secondary, + color: theme.secondaryAccent, + padding: "3px 7px", + transition: "200ms ease-out", + "&:hover": { + borderRadius: 100, + backgroundColor: Color(theme.primary).set('rgb.g', 150).alpha(0.35).hex(), + borderColor: theme.secondaryAccent, + }, + } +})); + diff --git a/web/src/styles/page.ts b/web/src/styles/page.ts index dd31e35..2e21593 100644 --- a/web/src/styles/page.ts +++ b/web/src/styles/page.ts @@ -7,7 +7,7 @@ export const usePageTheme = createAppUseStyles(theme => ({ width: '100vw', margin: 0, padding: 0, - overflow: 'hidden', + overflow: 'auto', maxWidth: '100vw', maxHeight: '100vh', @@ -22,7 +22,7 @@ export const usePageTheme = createAppUseStyles(theme => ({ // children display: 'flex', flexDirection: 'column', - justifyContent: 'center', + justifyContent: 'flex-start', alignItems: 'center', '> div': {