fixred tag scrolling

This commit is contained in:
ari committed 2022-04-26 21:49:39 -04:00
1 parent 19ea12a70e
commit 3e28974abf
8 files changed
+155 -48

No files matched your search

@@ -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 {
@@ -27,9 +27,7 @@ export const NoTags: ComponentStory<typeof AutoInfoBox> = args => {
}
NoTags.args = {
...NoTags.args,
tags: {
},
tags: [],
metadata: {
id: "random",
alias: "Random",
@@ -39,7 +39,7 @@ export const AutoInfoBox: FC<Props> = ({
}, 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()) {
@@ -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);
}
}
@@ -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",
@@ -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<Props> = ({
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<Props> = ({
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 <div className={classes.root}>
Loading...
</div>
}
return <div className={classes.root}>
<Modal open={!!modalState}>
{!!modalState && <NodeEditor
@@ -74,8 +70,9 @@ export const AutomationEditor: FC<Props> = ({
</Modal>
<AutoInfoBox
className={closeInfo ? "hide" : "show"}
metadata={automation.metadata}
onUpdate={metadata => onUpdate({ ...automation, metadata })}
metadata={state.data.metadata}
tags={state.data.tags}
onUpdate={updateMetadata}
>
<ButtonIcon
className={`${classes.infoIcon} automation-editor--info-icon`}
@@ -98,13 +95,8 @@ export const AutomationEditor: FC<Props> = ({
</div>
<Button
className={classes.saveBtn}
onClick={() => {
if (autoChanged === 'changed') {
setAutomation(['loading', automation]);
propsOnUpdate(automation);
}
}}
disabled={autoChanged !== 'changed'}
onClick={save}
disabled={state.status !== 'changed'}
>
Save <CheckMarkIcon color="#bf4" />
</Button>
@@ -113,15 +105,15 @@ export const AutomationEditor: FC<Props> = ({
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<Props> = ({
type: "",
subtype: ""
},
onSave: (t) => updateTriggers([...automation.trigger, t])
onSave: (t) => updateTrigger([...automation.trigger, t])
});
},
(trigger, onSave) => setModalState({
@@ -0,0 +1,91 @@
import { useState, useEffect } from 'react';
import { AutomationData, AutomationMetadata } from "types/automations";
export type EditorData = ReturnType<typeof genEditorData>;
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<EditorState>({
status: "loading",
});
const makeUpdate = <K extends keyof EditorData, D extends EditorData[K]>(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,
}
}