From 3e28974abf7818c17ba57d6b89702d0d21fc2dfb Mon Sep 17 00:00:00 2001 From: Ari Date: Tue, 26 Apr 2022 21:49:39 -0400 Subject: [PATCH] fixred tag scrolling --- .../AutomationEditor/AutoInfoBox.css | 32 ++++++- .../AutomationEditor/AutoInfoBox.stories.tsx | 4 +- .../AutomationEditor/AutoInfoBox.tsx | 2 +- .../src/components/AutomationEditor/index.css | 7 +- .../AutomationEditor/index.stories.tsx | 7 +- .../src/components/AutomationEditor/index.tsx | 56 +++++------- .../src/components/AutomationEditor/state.ts | 91 +++++++++++++++++++ webapp/src/components/Inputs/styles.ts | 4 +- 8 files changed, 155 insertions(+), 48 deletions(-) create mode 100644 webapp/src/components/AutomationEditor/state.ts diff --git a/webapp/src/components/AutomationEditor/AutoInfoBox.css b/webapp/src/components/AutomationEditor/AutoInfoBox.css index 38eff4d..b2d39c0 100644 --- a/webapp/src/components/AutomationEditor/AutoInfoBox.css +++ b/webapp/src/components/AutomationEditor/AutoInfoBox.css @@ -1,21 +1,32 @@ .automation-editor--info-box { - min-width: 25px; height: 100%; padding-left: 10px; padding-right: 10px; display: flex; flex-direction: column; justify-content: flex-start; - overflow: auto; + overflow: hidden; position: relative; } +.automation-editor--info-box-inner { + height: 100%; + overflow: auto; +} + +.automation-editor--info-box.show { + min-width: 25em; +} + +.automation-editor--info-box.hide { + min-width: 1em; +} + .automation-editor--info-box.hide>.automation-editor--info-box-inner { display: none; } .automation-editor--info-box--tags { - margin: 1em; padding: 0.25em 0.75em 1em 0.75em; border-color: var(--primary-accent); border-width: 1px; @@ -32,6 +43,17 @@ justify-content: center; align-items: center; flex-wrap: wrap; + gap: 0.5em; + margin-bottom: 0.5em; +} + +.automation-editor--info-box--tag>div { + margin: 0; +} + +.automation-editor--info-box--tag>div>input { + max-width: 5em; + padding: 1em 0.25em 0.2em 0.25em; } .automation-editor--info-box--tag>button { @@ -47,13 +69,13 @@ width: 100%; display: flex; justify-content: center; - padding: 0.5em 0; - margin: 0 30%; + padding: 0; } .automation-editor--info-box--tag--error.hide { height: 0; opacity: 0; + display: none; } .automation-editor--info-box--tag--error.show { diff --git a/webapp/src/components/AutomationEditor/AutoInfoBox.stories.tsx b/webapp/src/components/AutomationEditor/AutoInfoBox.stories.tsx index c78126d..69a529a 100644 --- a/webapp/src/components/AutomationEditor/AutoInfoBox.stories.tsx +++ b/webapp/src/components/AutomationEditor/AutoInfoBox.stories.tsx @@ -27,9 +27,7 @@ export const NoTags: ComponentStory = args => { } NoTags.args = { ...NoTags.args, - tags: { - - }, + tags: [], metadata: { id: "random", alias: "Random", diff --git a/webapp/src/components/AutomationEditor/AutoInfoBox.tsx b/webapp/src/components/AutomationEditor/AutoInfoBox.tsx index f84047c..b6f70d4 100644 --- a/webapp/src/components/AutomationEditor/AutoInfoBox.tsx +++ b/webapp/src/components/AutomationEditor/AutoInfoBox.tsx @@ -39,7 +39,7 @@ export const AutoInfoBox: FC = ({ }, tags); const validTagName = (newName: string): string | undefined => { if (newName.length <= 0) { - return "Must be at least 1 character long" + return "Name must be at least 1 character long" } for (const [name, _] of tags) { if (name.trim() === newName.trim()) { diff --git a/webapp/src/components/AutomationEditor/index.css b/webapp/src/components/AutomationEditor/index.css index 3546581..41f43ce 100644 --- a/webapp/src/components/AutomationEditor/index.css +++ b/webapp/src/components/AutomationEditor/index.css @@ -1,9 +1,10 @@ .automation-editor--info-icon { position: absolute; - right : 10px; - z-index : 10; + right: 1.5em; + bottom: 0.5em; + z-index: 10; } .automation-editor--info-box.hide>.automation-editor--info-icon { transform: rotate(180deg); -} +} \ No newline at end of file diff --git a/webapp/src/components/AutomationEditor/index.stories.tsx b/webapp/src/components/AutomationEditor/index.stories.tsx index f0d998f..9d9e8ec 100644 --- a/webapp/src/components/AutomationEditor/index.stories.tsx +++ b/webapp/src/components/AutomationEditor/index.stories.tsx @@ -38,7 +38,12 @@ export const Simple = Template.bind({}) Simple.args = { ...Simple.args, automation: { - tags: {}, + tags: { + "Room": "Bathroom", + "For": "Toliet", + "Type": "Smell", + "Use": "Flushing" + }, metadata: { id: "random", alias: "Random", diff --git a/webapp/src/components/AutomationEditor/index.tsx b/webapp/src/components/AutomationEditor/index.tsx index 53ed85b..b359446 100644 --- a/webapp/src/components/AutomationEditor/index.tsx +++ b/webapp/src/components/AutomationEditor/index.tsx @@ -14,6 +14,7 @@ import { Modal } from "components/Modal"; import { NodeEditor } from "components/NodeEditor"; import { ButtonIcon } from "components/Icons/ButtonIcons"; import { Button } from "components/Inputs/Button"; +import { useAutomatioEditorState } from "./state"; interface TriggerEditorModalState { trigger: AutomationTrigger; @@ -30,9 +31,14 @@ export const AutomationEditor: FC = ({ onUpdate: propsOnUpdate, }) => { // state - const [[autoChanged, automation], setAutomation] = useState<[ - 'unchanged' | 'changed' | 'loading', AutomationData] - >(['unchanged', propsAutos]); + const { + state, + updateSequence, + updateTrigger, + updateMetadata, + save, + } = useAutomatioEditorState(propsAutos, propsOnUpdate); + const [zoomLevel, setZoomLevel] = useState(40); const { ratioWbh } = useWindowSize(); const [closeInfo, setCloseInfo] = useState(false); @@ -40,26 +46,16 @@ export const AutomationEditor: FC = ({ const { classes } = useEditorStyles({ closeInfo, horizontalMode: ratioWbh < 0.75, - autoChanged, + autoChanged: state.status, }); - // alias - const updateSequence = (sequence: AutomationSequenceNode[]) => onUpdate({ - ...automation, - sequence, - }); - const updateTriggers = (trigger: AutomationTrigger[]) => onUpdate({ - ...automation, - trigger, - }); - const onUpdate = (a: AutomationData) => setAutomation(['changed', a]); - - // effects - useEffect(() => { - setAutomation(['unchanged', propsAutos]); - }, [propsAutos]) // render + if (state.status === 'loading') { + return
+ Loading... +
+ } return
{!!modalState && = ({ onUpdate({ ...automation, metadata })} + metadata={state.data.metadata} + tags={state.data.tags} + onUpdate={updateMetadata} > = ({
@@ -113,15 +105,15 @@ export const AutomationEditor: FC = ({ zoomLevel={zoomLevel * 200 / 100 + 50} startPoint={[2, 0.5]} dims={dims} - sequence={automation.sequence} + sequence={state.data.sequence} onChange={updateSequence} additionalElements={() => chain([ computeExtraField([1, 0.5], [2, 0.5]), computeTriggerPos( [0.5, 0.5], - automation.trigger, + state.data.trigger, [1.5, 0.5], - updateTriggers, + updateTrigger, () => { setModalState({ trigger: { @@ -132,7 +124,7 @@ export const AutomationEditor: FC = ({ type: "", subtype: "" }, - onSave: (t) => updateTriggers([...automation.trigger, t]) + onSave: (t) => updateTrigger([...automation.trigger, t]) }); }, (trigger, onSave) => setModalState({ diff --git a/webapp/src/components/AutomationEditor/state.ts b/webapp/src/components/AutomationEditor/state.ts new file mode 100644 index 0000000..68df434 --- /dev/null +++ b/webapp/src/components/AutomationEditor/state.ts @@ -0,0 +1,91 @@ +import { useState, useEffect } from 'react'; +import { AutomationData, AutomationMetadata } from "types/automations"; + + +export type EditorData = ReturnType; +export type EditorState = { + status: 'unchanged' | 'changed', + data: EditorData +} | { + status: 'loading' +} +export type EditorStatus = EditorState[0]; + +export const useAutomatioEditorState = ( + automation: AutomationData, + onSave: (a: AutomationData) => void, +) => { + const [state, update] = useState({ + status: "loading", + }); + + const makeUpdate = (name: K) => ( + data: D + ) => state.status !== 'loading' && update({ + status: 'changed', + data: { + ...state.data, + [name]: data, + } + }) + + useEffect(() => { + update({ + status: 'unchanged', + data: genEditorData(automation), + }) + }, [automation]); + + return { + updateTrigger: makeUpdate('trigger'), + updateSequence: makeUpdate('sequence'), + updateMetadata: (metadata: AutomationMetadata, tags: Array<[string, string]>) => { + if (state.status !== 'loading') { + update({ + status: 'changed', + data: { + ...state.data, + tags, + metadata, + } + }) + } + }, + save: () => { + if (state.status !== 'loading') { + const data = { + ...state.data, + tags: state.data.tags.reduce((all, [tagName, tagValue]) => ({ + ...all, + [tagName]: tagValue, + }), {}) + }; + update({ + status: "loading", + }) + onSave(data); + } + }, + get state() { + return state; + } + } +} + + +const genEditorData = (automation: AutomationData) => { + const { + metadata, + trigger, + sequence, + } = automation; + + const tags: [string, string][] = Object.keys(automation.tags).map(tagName => [tagName, automation.tags[tagName]]); + + return { + metadata, + trigger, + sequence, + tags, + } +} \ No newline at end of file diff --git a/webapp/src/components/Inputs/styles.ts b/webapp/src/components/Inputs/styles.ts index 6c4e19e..f151dc6 100644 --- a/webapp/src/components/Inputs/styles.ts +++ b/webapp/src/components/Inputs/styles.ts @@ -9,9 +9,7 @@ const commonInputStyle = (theme: AppTheme) => ({ borderStyle: 'solid', borderWidth: 1, borderRadius: 5, - padding: 15, - marginLeft: 5, - marginRight: 5, + padding: '1em', fontSize: 16, "&:hover": {