upping limits of display

This commit is contained in:
ari committed 2022-07-15 13:25:43 -04:00
1 parent e769a8e798
commit 6b465219a9
6 files changed
+243 -156

No files matched your search

+17 -19
View File
@@ -1,42 +1,40 @@
import { useState } from "react"; import { useState } from "react";
import { ApiState } from "./types"; import { ApiState } from "./types";
import { AutomationAPI } from './automations'; import { AutomationAPI } from "./automations";
export const useDefaultApiState = () =>
useState<ApiState>({
export const useDefaultApiState = () => useState<ApiState>({ automations: { ready: false },
automations: { ready: false }, });
});
export const makeReloadAutomations = ( export const makeReloadAutomations = (
automationAPI: AutomationAPI, automationAPI: AutomationAPI,
state: ApiState, state: ApiState,
setState: (s: ApiState) => void, setState: (s: ApiState) => void
) => { ) => {
const reload = async () => { const reload = async () => {
let params = { offset: 0, limit: 100 }; let params = { offset: 0, limit: 10000 };
if (state.automations.ready && state.automations.ok) { if (state.automations.ready && state.automations.ok) {
params = state.automations.data.params params = state.automations.data.params;
} }
const resp = await automationAPI.list(params); const resp = await automationAPI.list(params);
setState({ setState({
...state, automations: { ...state,
automations: {
ready: true, ready: true,
...resp, ...resp,
} },
}); });
} };
const wrapCall = < const wrapCall = <Call extends (a: any) => Promise<any>>(fcn: Call) => {
Call extends (a: any) => Promise<any>
>(fcn: Call) => {
return async (args: Parameters<Call>[0]): Promise<ReturnType<Call>> => { return async (args: Parameters<Call>[0]): Promise<ReturnType<Call>> => {
const resp = await fcn(args); const resp = await fcn(args);
await reload(); await reload();
return resp; return resp;
} };
} };
return { return {
reload, reload,
wrapCall, wrapCall,
} };
} };
@@ -5,6 +5,7 @@ import { AutomationList } from ".";
import { bigMockAutoList } from "utils/mocks"; import { bigMockAutoList } from "utils/mocks";
import { Page } from "components/Page"; import { Page } from "components/Page";
import { DEFAULT_DIMS } from "components/DAGGraph/elements/constants"; import { DEFAULT_DIMS } from "components/DAGGraph/elements/constants";
import { AutomationData } from "types/automations";
export default { export default {
title: "App/AutomationList", title: "App/AutomationList",
@@ -16,12 +17,15 @@ export default {
} as ComponentMeta<typeof AutomationList>; } as ComponentMeta<typeof AutomationList>;
const Template: ComponentStory<typeof AutomationList> = (args) => { const Template: ComponentStory<typeof AutomationList> = (args) => {
const [autos, setAutos] = useState(args.automations); const [autos, setAutos] = useState(args.automations.data);
return ( return (
<Page> <Page>
<AutomationList <AutomationList
{...args} {...args}
automations={autos} automations={{
...args.automations,
data: autos,
}}
onAdd={(a) => { onAdd={(a) => {
args.onAdd(a); args.onAdd(a);
setAutos([...autos, a]); setAutos([...autos, a]);
@@ -39,107 +43,115 @@ const Template: ComponentStory<typeof AutomationList> = (args) => {
); );
}; };
export const EmptyStart = Template.bind({}); const make = (autos: AutomationData[]) => {
EmptyStart.args = { const NewTemp = Template.bind({});
...EmptyStart.args, NewTemp.args = {
automations: [], ...NewTemp.args,
automations: {
data: autos,
params: {
offset: 0,
limit: 100,
},
totalItems: autos.length,
},
};
return NewTemp;
}; };
export const FewAutos = Template.bind({}); export const EmptyStart = make([]);
FewAutos.args = {
...FewAutos.args,
automations: bigMockAutoList,
};
export const BadAutmations = Template.bind({}); export const FewAutos = make(bigMockAutoList);
BadAutmations.args = {
...BadAutmations.args, export const BadAutmations = make([
automations: [ {
{ metadata: {
metadata: { alias: "Set Thermostat to 76 at 8am",
alias: "Set Thermostat to 76 at 8am", description: "",
description: "", mode: "single",
mode: "single", } as any,
} as any, trigger: [
trigger: [ {
{ at: "08:00:00",
at: "08:00:00", platform: "time",
platform: "time", },
],
condition: [
{
condition: "state",
entity_id: "climate.thermostat",
state: "cool",
},
],
sequence: [
{
data: {
temperature: 76,
}, },
], service: "climate.set_temperature",
condition: [ target: {
{
condition: "state",
entity_id: "climate.thermostat", entity_id: "climate.thermostat",
state: "cool",
}, },
],
sequence: [
{
data: {
temperature: 76,
},
service: "climate.set_temperature",
target: {
entity_id: "climate.thermostat",
},
},
],
tags: {},
},
{
condition: [],
tags: {},
metadata: {
id: "Bad Choose Sequence",
alias: "Bad Choose",
description: "Example Metadata",
trigger_variables: {
wowo: "!",
},
mode: "single",
}, },
trigger: [], ],
sequence: [ tags: {},
{ },
choose: {}, {
}, condition: [],
] as any, tags: {},
}, metadata: {
{ id: "Bad Choose Sequence",
condition: [], alias: "Bad Choose",
tags: {}, description: "Example Metadata",
metadata: { trigger_variables: {
id: "random", wowo: "!",
alias: "Bad Trigger",
description: "Example Metadata",
trigger_variables: {
wowo: "!",
},
mode: "single",
}, },
trigger: ["haha I am a string"] as any, mode: "single",
sequence: [],
}, },
{ trigger: [],
condition: [], sequence: [
tags: {}, {
metadata: {} as any, choose: {},
trigger: [], },
sequence: [], ] as any,
},
{
condition: [],
tags: {},
metadata: {
id: "random",
alias: "Bad Trigger",
description: "Example Metadata",
trigger_variables: {
wowo: "!",
},
mode: "single",
}, },
], trigger: ["haha I am a string"] as any,
}; sequence: [],
},
{
condition: [],
tags: {},
metadata: {} as any,
trigger: [],
sequence: [],
},
]);
export const FewAutosWithSlowLoader: ComponentStory<typeof AutomationList> = ( export const FewAutosWithSlowLoader: ComponentStory<typeof AutomationList> = (
args args
) => { ) => {
const [autos, setAutos] = useState(args.automations); const [autos, setAutos] = useState(args.automations.data);
return ( return (
<Page> <Page>
<AutomationList <AutomationList
{...args} {...args}
automations={autos} automations={{
...args.automations,
data: autos,
}}
onAdd={(a) => { onAdd={(a) => {
window.setTimeout(() => { window.setTimeout(() => {
args.onAdd(a); args.onAdd(a);
@@ -164,5 +176,12 @@ export const FewAutosWithSlowLoader: ComponentStory<typeof AutomationList> = (
}; };
FewAutosWithSlowLoader.args = { FewAutosWithSlowLoader.args = {
...FewAutosWithSlowLoader.args, ...FewAutosWithSlowLoader.args,
automations: bigMockAutoList, automations: {
data: bigMockAutoList,
params: {
limit: 100,
offset: 0,
},
totalItems: 100,
},
}; };
@@ -65,8 +65,20 @@
align-items: center; align-items: center;
} }
.automation-list-box--bottom>button { .automation-list-box--bottom>.automation-list-box--bottom--add {
background-color: #070; background-color: var(--mui-primary-main);
color: var(--mui-grey-400); color: var(--mui-primary-contrastText);
padding: 0.5em 2em; padding: 0.5em 2em;
}
.automation-list-box--bottom>.automation-list-box--bottom--add:hover {
color: var(--mui-primary-light);
}
.automation-list-box--bottom--load svg {
color: var(--mui-primary-main);
}
.automation-list-box--bottom--load.less svg {
transform: rotate(180deg);
} }
@@ -5,6 +5,7 @@ import { createMockAuto } from "utils/mocks";
import { Page } from "components/Page"; import { Page } from "components/Page";
import { CookiesProvider } from "react-cookie"; import { CookiesProvider } from "react-cookie";
import { makeTagDB } from "./TagDB"; import { makeTagDB } from "./TagDB";
import { AutomationData } from "types/automations";
export default { export default {
title: "App/AutomationList/Box", title: "App/AutomationList/Box",
@@ -20,7 +21,7 @@ const Template: ComponentStory<typeof AutomationListBox> = (args) => {
<CookiesProvider> <CookiesProvider>
<AutomationListBox <AutomationListBox
{...args} {...args}
tagsDB={makeTagDB(args.automations)} tagsDB={makeTagDB(args.automations.data)}
automations={args.automations} automations={args.automations}
selected={selected} selected={selected}
onSelectAutomation={(i) => { onSelectAutomation={(i) => {
@@ -33,41 +34,46 @@ const Template: ComponentStory<typeof AutomationListBox> = (args) => {
); );
}; };
export const EmptyStart = Template.bind({}); const make = (autos: AutomationData[]) => {
EmptyStart.args = { const NewTemp = Template.bind({});
...EmptyStart.args, NewTemp.args = {
automations: [], ...NewTemp.args,
automations: {
data: autos,
params: {
offset: 0,
limit: 100,
},
totalItems: autos.length,
},
};
return NewTemp;
}; };
export const FewAutos = Template.bind({}); export const EmptyStart = make([]);
FewAutos.args = {
...FewAutos.args,
automations: [
createMockAuto({
Room: "Bathroom",
Type: "Lights",
NewDevice: "Yes",
Remote: "No",
Floor: "1",
}),
createMockAuto({ Room: "Bathroom", Type: "Climate" }),
createMockAuto({ Room: "Living Room", Type: "Climate" }),
createMockAuto({ Type: "Climate" }),
createMockAuto({ Type: "Lights", Routine: "Myself" }),
createMockAuto({ Routine: "DNS", Type: "SSL" }),
createMockAuto({ Room: "Bedroom", Type: "Lights", Routine: "Baby" }),
createMockAuto({ Room: "Office", Type: "Lights" }),
createMockAuto({ Room: "Office" }),
createMockAuto(),
],
};
export const OneAuto = Template.bind({}); export const FewAutos = make([
OneAuto.args = { createMockAuto({
...OneAuto.args, Room: "Bathroom",
automations: [ Type: "Lights",
createMockAuto({ NewDevice: "Yes",
Type: "Climate", Remote: "No",
}), Floor: "1",
], }),
}; createMockAuto({ Room: "Bathroom", Type: "Climate" }),
createMockAuto({ Room: "Living Room", Type: "Climate" }),
createMockAuto({ Type: "Climate" }),
createMockAuto({ Type: "Lights", Routine: "Myself" }),
createMockAuto({ Routine: "DNS", Type: "SSL" }),
createMockAuto({ Room: "Bedroom", Type: "Lights", Routine: "Baby" }),
createMockAuto({ Room: "Office", Type: "Lights" }),
createMockAuto({ Room: "Office" }),
createMockAuto(),
]);
export const OneAuto = make([
createMockAuto({
Type: "Climate",
}),
]);
@@ -8,14 +8,20 @@ import { makeGrouping } from "./automationGrouper";
import { AutomationListBoxGroup } from "./AutomationListBoxGroup"; import { AutomationListBoxGroup } from "./AutomationListBoxGroup";
import { useCookies } from "react-cookie"; import { useCookies } from "react-cookie";
import { TagDB } from "components/AutomationList/TagDB"; import { TagDB } from "components/AutomationList/TagDB";
// import { ButtonIcon } from "components/Icons/ButtonIcons";
// import ReadMoreOutlinedIcon from "@mui/icons-material/ReadMoreOutlined";
import { ListData, ListParams } from "apiService/types";
import Chip from "@mui/material/Chip";
import InfoOutlinedIcon from "@mui/icons-material/InfoOutlined";
export type AutomationListBoxProps = { export type AutomationListBoxProps = {
automations: AutomationData[]; automations: ListData<AutomationData>;
selected: number; selected: number;
onSelectAutomation: (i: number) => void; onSelectAutomation: (i: number) => void;
onRemove: (i: number) => void; onRemove: (i: number) => void;
onAdd: () => void; onAdd: () => void;
onUpdate: (a: AutomationData, i: number) => void; onUpdate: (a: AutomationData, i: number) => void;
onLoadMore: (p: ListParams) => void;
tagsDB: TagDB; tagsDB: TagDB;
}; };
@@ -54,11 +60,16 @@ const useAutomationListBoxState = () => {
}; };
export const AutomationListBox: FC<AutomationListBoxProps> = ({ export const AutomationListBox: FC<AutomationListBoxProps> = ({
automations, automations: {
data: automations,
params: autoParams,
totalItems: totalAutomations,
},
selected, selected,
onSelectAutomation, onSelectAutomation,
onAdd, onAdd,
onRemove, onRemove,
onLoadMore,
tagsDB, tagsDB,
onUpdate, onUpdate,
}) => { }) => {
@@ -101,11 +112,44 @@ export const AutomationListBox: FC<AutomationListBoxProps> = ({
/> />
</div> </div>
<div className="automation-list-box--bottom"> <div className="automation-list-box--bottom">
<Button onClick={onAdd}>Add</Button> <Button className="automation-list-box--bottom--add" onClick={onAdd}>
<span> Add
Showing Automations: {filteredAutomations.length} out of{" "} </Button>
{automations.length} {automations.length < totalAutomations ? (
</span> <>
<Chip
label={`Only loaded ${automations.length} out of ${totalAutomations}`}
variant="outlined"
color="warning"
icon={<InfoOutlinedIcon />}
onClick={() =>
window.open(
"https://github.com/asosnovsky/Shortumation/issues/78",
"_blank"
)
}
/>
<small>support for more is not yet implemented</small>
</>
) : (
""
)}
{/* {autoParams.offset > 0 && (
<ButtonIcon
borderless
className="automation-list-box--bottom--load less"
icon={<ReadMoreOutlinedIcon />}
onClick={() => onLoadMore()}
/>
)} */}
{/* {filteredAutomations.length} out of {totalAutomations} */}
{/* {autoParams.offset + automations.length < totalAutomations && (
<ButtonIcon
borderless
className="automation-list-box--bottom--load more"
icon={<ReadMoreOutlinedIcon />}
/>
)} */}
</div> </div>
</div> </div>
); );
+13 -5
View File
@@ -16,12 +16,14 @@ import { useHA } from "haService";
import { ArrowBack } from "@mui/icons-material"; import { ArrowBack } from "@mui/icons-material";
import { useSnackbar } from "notistack"; import { useSnackbar } from "notistack";
import { DAGDims } from "components/DAGGraph/elements/types"; import { DAGDims } from "components/DAGGraph/elements/types";
import { ListData, ListParams } from "apiService/types";
interface AutomationListParams { interface AutomationListParams {
automations: AutomationData[]; automations: ListData<AutomationData>;
onUpdate: (i: number, auto: AutomationData) => void; onUpdate: (i: number, auto: AutomationData) => void;
onAdd: (auto: AutomationData) => void; onAdd: (auto: AutomationData) => void;
onRemove: (i: number) => void; onRemove: (i: number) => void;
onLoadMore: (p: ListParams) => void;
dims: DAGDims; dims: DAGDims;
children?: ReactNode; children?: ReactNode;
} }
@@ -54,6 +56,7 @@ export const useAutomationListState = () => {
export const AutomationList: FC<AutomationListParams> = ({ export const AutomationList: FC<AutomationListParams> = ({
automations, automations,
onLoadMore,
onUpdate, onUpdate,
onAdd, onAdd,
onRemove, onRemove,
@@ -67,8 +70,11 @@ export const AutomationList: FC<AutomationListParams> = ({
const { isMobile } = useWindowSize(); const { isMobile } = useWindowSize();
const firstRender = useRef(true); const firstRender = useRef(true);
// alias // alias
const currentAuto = automations.length > 0 ? automations[current] : null; const currentAuto =
const tagDB = makeTagDB(automations); automations.data.length > 0
? automations.data[current - automations.params.offset]
: null;
const tagDB = makeTagDB(automations.data);
// effect // effect
useEffect(() => { useEffect(() => {
if (firstRender.current) { if (firstRender.current) {
@@ -92,7 +98,7 @@ export const AutomationList: FC<AutomationListParams> = ({
onUpdate={(a, i) => onUpdate(i, a)} onUpdate={(a, i) => onUpdate(i, a)}
onAdd={() => { onAdd={() => {
onAdd(defaultAutomation(String(Date.now()))); onAdd(defaultAutomation(String(Date.now())));
setCurrent(automations.length); setCurrent(automations.data.length);
if (isMobile) { if (isMobile) {
setHideList(true); setHideList(true);
} }
@@ -103,6 +109,7 @@ export const AutomationList: FC<AutomationListParams> = ({
setHideList(true); setHideList(true);
} }
}} }}
onLoadMore={onLoadMore}
selected={current} selected={current}
onRemove={onRemove} onRemove={onRemove}
tagsDB={tagDB} tagsDB={tagDB}
@@ -171,7 +178,8 @@ export const ConnectedAutomationList: FC<ConnectedAutomationListParams> = ({
return ( return (
<AutomationList <AutomationList
dims={dims} dims={dims}
automations={automations.data.data} automations={automations.data}
onLoadMore={() => {}}
onAdd={(auto) => onAdd={(auto) =>
updateAutomation({ auto, index: automations.data.totalItems + 1 }) updateAutomation({ auto, index: automations.data.totalItems + 1 })
} }