templatized storymaking

This commit is contained in:
ari committed 2022-07-28 14:48:05 -04:00
1 parent 02ec81c16a
commit a9c3e9c890
9 files changed
+255 -172

No files matched your search

@@ -1,53 +1,51 @@
import "styles/root.css";
import { ComponentMeta, ComponentStory } from "@storybook/react";
import { Page } from "components/Page";
import { ListBoxGroup } from "."; import { ListBoxGroup } from ".";
export default { import { makeStory } from "devUtils";
title: "App/AutomationList/ListBoxGroup", import { useTagDB } from "../TagDB";
component: ListBoxGroup,
parameters: { actions: { argTypesRegex: "^on.*" } }, const { make, componentMeta } = makeStory({
args: { Component: ListBoxGroup,
title: "Example", BaseTemplate: (args) => {
type: "items", const tagsDB = useTagDB([], () => {});
isSelected: false, return <ListBoxGroup {...args} tagsDB={tagsDB} />;
data: [
{
id: "1",
title: "Example",
entityId: "automation.1",
description: "Amazing automation!",
tags: {},
state: "on",
isSelected: false,
},
{
id: "2",
title: "Example 2",
entityId: "automation.2",
description: "Cool!",
state: "on",
isSelected: false,
tags: {
room: "main bedroom",
routine: "night time",
},
},
],
}, },
} as ComponentMeta<typeof ListBoxGroup>; meta: {
title: "App/AutomationManager/ListBoxGroup",
args: {
title: "Example",
type: "items",
isSelected: false,
data: [
{
id: "1",
title: "Example",
entityId: "automation.1",
description: "Amazing automation!",
tags: {},
state: "on",
isSelected: false,
},
{
id: "2",
title: "Example 2",
entityId: "automation.2",
description: "Cool!",
state: "on",
isSelected: false,
tags: {
room: "main bedroom",
routine: "night time",
},
},
],
},
},
});
const Template: ComponentStory<typeof ListBoxGroup> = (args) => { export default componentMeta;
return (
<Page>
<ListBoxGroup {...args} />
</Page>
);
};
export const Basic = Template.bind({}); export const Basic = make({});
export const Nested = Template.bind({}); export const Nested = make({
Nested.args = {
title: "Top Group", title: "Top Group",
type: "group", type: "group",
isSelected: false, isSelected: false,
@@ -120,4 +118,4 @@ Nested.args = {
], ],
}, },
], ],
}; });
@@ -0,0 +1,34 @@
import { HassEntities } from "home-assistant-js-websocket";
import { FC } from "react";
import { AutomationData } from "types/automations";
import { consolidateAutomations } from "../helpers";
import { useTagDB } from "../TagDB";
import { useAutomationManagerState } from "./state";
export type AutomationManagerLoadedProps = {
configAutomations: AutomationData[];
hassEntities: HassEntities;
onUpdateTags: (aid: string, tags: Record<string, string>) => void;
};
export const AutomationManagerLoaded: FC<AutomationManagerLoadedProps> = ({
configAutomations,
onUpdateTags,
hassEntities,
}) => {
const state = useAutomationManagerState();
const tagsDB = useTagDB(
configAutomations.map(({ metadata: { id }, tags }) => ({
id,
tags,
})),
onUpdateTags
);
const automations = consolidateAutomations(
hassEntities,
configAutomations.map(({ metadata }) => metadata),
tagsDB
);
return <></>;
};
@@ -1,59 +1,44 @@
import "styles/root.css";
import { ComponentMeta, ComponentStory } from "@storybook/react";
import { Page } from "components/Page";
import { MetadataBox } from "."; import { MetadataBox } from ".";
import { useTagDB } from "../TagDB"; import { useTagDB } from "../TagDB";
import { useState } from "react"; import { useState } from "react";
export default { import { makeStory } from "devUtils";
title: "App/AutomationList/MetadataBox",
component: MetadataBox, const { make, componentMeta } = makeStory({
parameters: { actions: { argTypesRegex: "^on.*" } }, Component: MetadataBox,
args: { meta: {
id: "dummy", title: "App/AutomationManager/MetadataBox",
title: "Example", args: {
description: "Cool!", id: "dummy",
state: "on", title: "Example",
isSelected: false, description: "Cool!",
tags: { state: "on",
room: "main bedroom", isSelected: false,
routine: "night time", tags: {
room: "main bedroom",
routine: "night time",
},
}, },
}, },
} as ComponentMeta<typeof MetadataBox>; BaseTemplate: (args) => {
const [autoTags, setTags] = useState([{ id: args.id, tags: args.tags }]);
const tagsDB = useTagDB(autoTags, (aid, t) =>
setTags(
autoTags
.filter(({ id }) => id !== aid)
.concat({
id: aid,
tags: t,
})
)
);
return <MetadataBox {...args} tagsDB={tagsDB} />;
},
});
const Template: ComponentStory<typeof MetadataBox> = (args) => { export default componentMeta;
const [autoTags, setTags] = useState([{ id: args.id, tags: args.tags }]);
const tagsDB = useTagDB(autoTags, (aid, t) =>
setTags(
autoTags
.filter(({ id }) => id !== aid)
.concat({
id: aid,
tags: t,
})
)
);
return (
<Page>
<MetadataBox {...args} tagsDB={tagsDB} />
</Page>
);
};
export const Basic = Template.bind({}); export const Basic = make({});
export const IsSelected = Template.bind({}); export const IsSelected = make({ isSelected: true });
IsSelected.args = { export const InvalidState = make({ state: "unavailable" });
...IsSelected.args, export const WithIssue = make({ issue: "look at me" });
isSelected: true,
};
export const InvalidState = Template.bind({});
InvalidState.args = {
...InvalidState.args,
state: "unavailable",
};
export const WithIssue = Template.bind({});
WithIssue.args = {
...WithIssue.args,
issue: "look at me",
};
@@ -1,38 +1,34 @@
import "styles/root.css";
import { ComponentMeta, ComponentStory } from "@storybook/react";
import { Page } from "components/Page";
import { AutomationListSidebar } from "."; import { AutomationListSidebar } from ".";
import { useTagDB } from "../TagDB"; import { useTagDB } from "../TagDB";
import { useState } from "react"; import { useState } from "react";
import { AutomationListAuto } from "../types"; import { AutomationListAuto } from "../types";
export default { import { makeStory } from "devUtils";
title: "App/AutomationList/Sidebar",
component: AutomationListSidebar,
parameters: { actions: { argTypesRegex: "^on.*" } },
args: {},
} as ComponentMeta<typeof AutomationListSidebar>;
const Template: ComponentStory<typeof AutomationListSidebar> = (args) => { const story = makeStory({
const [automations, setAutomations] = useState(args.automations); meta: {
const [selectedAutomationId, setSelAutoId] = useState( title: "App/AutomationManager/Sidebar",
args.selectedAutomationId },
); Component: AutomationListSidebar,
const onTagUpdate = (tags: Record<string, string>, aid: string) => { BaseTemplate: (args) => {
const index = automations.findIndex(({ id }) => id === aid); const [automations, setAutomations] = useState(args.automations);
if (index >= 0) { const [selectedAutomationId, setSelAutoId] = useState(
setAutomations([ args.selectedAutomationId
...automations.slice(0, index), );
{ const onTagUpdate = (tags: Record<string, string>, aid: string) => {
...automations[index], const index = automations.findIndex(({ id }) => id === aid);
tags, if (index >= 0) {
}, setAutomations([
...automations.slice(index + 1), ...automations.slice(0, index),
]); {
} ...automations[index],
}; tags,
return ( },
<Page> ...automations.slice(index + 1),
]);
}
};
return (
<AutomationListSidebar <AutomationListSidebar
selectedAutomationId={selectedAutomationId} selectedAutomationId={selectedAutomationId}
tagsDB={useTagDB(automations, (a, t) => onTagUpdate(t, a))} tagsDB={useTagDB(automations, (a, t) => onTagUpdate(t, a))}
@@ -76,18 +72,16 @@ const Template: ComponentStory<typeof AutomationListSidebar> = (args) => {
setSelAutoId(id); setSelAutoId(id);
}} }}
/> />
</Page> );
); },
}; });
const make = (automations: AutomationListAuto[]) => { export default story.componentMeta;
const Basic = Template.bind({});
Basic.args = { const make = (automations: AutomationListAuto[]) =>
...Basic.args, story.make({
automations, automations,
}; });
return Basic;
};
const makeAuto = (data: Partial<AutomationListAuto>): AutomationListAuto => { const makeAuto = (data: Partial<AutomationListAuto>): AutomationListAuto => {
const id = `${Date.now()}_${Math.random().toString(26).slice(3, 6)}`; const id = `${Date.now()}_${Math.random().toString(26).slice(3, 6)}`;
return { return {
@@ -0,0 +1,12 @@
import { makeStory } from "devUtils";
import { AutomationManager } from ".";
const { make, componentMeta } = makeStory({
Component: AutomationManager,
meta: {
title: "App/AutomationManager",
},
});
export default componentMeta;
export const Basic = make({});
@@ -1,50 +1,52 @@
import { ListData } from "apiService/types"; import { ApiService } from "apiService/core";
import { HAEntitiesState } from "haService/HAEntities"; import { HAEntitiesState } from "haService/HAEntities";
import { FC } from "react"; import { FC, useState } from "react";
import { AutomationData } from "types/automations"; import { AutomationManagerLoaded } from "./Loaded";
import { consolidateAutomations } from "./helpers"; import LinearProgress from "@mui/material/LinearProgress";
import { AutomationListSidebar } from "./Sidebar";
import { useAutomationManagerState } from "./state";
import { useTagDB } from "./TagDB";
export type AutomationListProps = { export type AutomationManagerProps = {
haEntites: HAEntitiesState; haEntites: HAEntitiesState;
configAutomations: ListData<AutomationData>; api: ApiService;
onUpdate: (i: number, auto: AutomationData) => void;
onUpdateTags: (id: string, tags: Record<string, string>) => void;
onUpdateState: (id: string, state: string) => void;
onTrigger: (id: string, state: string) => void;
onAdd: () => void;
onRemove: (i: number) => void;
}; };
export const AutomationList: FC<AutomationListProps> = (props) => { export const AutomationManager: FC<AutomationManagerProps> = ({
const state = useAutomationManagerState(); api,
haEntites,
}) => {
const [isSaving, setIsSaving] = useState(false);
if (!props.haEntites.ready) { // handle bad states
if (!haEntites.ready || !api.state.automations.ready) {
return <div className="automation-manager loading">...</div>; return <div className="automation-manager loading">...</div>;
} }
const tagsDB = useTagDB( if (!api.state.automations.ok) {
props.configAutomations.data.map(({ metadata: { id }, tags }) => ({ return (
id, <div className="automation-manager error">
tags, <h1>Failed to load</h1>
})), {api.state.automations.error}
props.onUpdateTags </div>
); );
const automations = consolidateAutomations( }
props.haEntites.collection.state,
props.configAutomations.data.map(({ metadata }) => metadata), // alias
tagsDB const configAutomations = api.state.automations.data.data;
); const hassEntities = haEntites.collection.state;
return ( return (
<div className="automation-manager"> <div className="automation-manager">
{/* <AutomationListSidebar {isSaving && <LinearProgress />}
automations={automations} <AutomationManagerLoaded
tagsDB={tagsDB} configAutomations={configAutomations}
selectedAutomationId={state.current} hassEntities={hassEntities}
onTagUpdate onUpdateTags={async (aid, t) => {
/> */} setIsSaving(true);
await api.updateTags({
automation_id: aid,
tags: t,
});
setIsSaving(false);
}}
/>
</div> </div>
); );
}; };
+11
View File
@@ -0,0 +1,11 @@
import { makeStory } from "devUtils";
const { make, componentMeta } = makeStory({
Component: () => <div>Example</div>,
meta: {
title: "DevUtil",
},
});
export default componentMeta;
export const Example = make({});
+47
View File
@@ -0,0 +1,47 @@
import "styles/root.css";
import { ComponentMeta, ComponentStory } from "@storybook/react";
import { Page } from "components/Page";
import { ComponentProps, JSXElementConstructor } from "react";
export type MakeStoryArgs<
T extends keyof JSX.IntrinsicElements | JSXElementConstructor<any>
> = {
Component: T;
BaseTemplate?: ComponentStory<T>;
meta: Partial<ComponentMeta<T>> & { title: string };
};
export const makeStory = <
T extends keyof JSX.IntrinsicElements | JSXElementConstructor<any>
>({
BaseTemplate,
Component,
meta,
}: MakeStoryArgs<T>) => {
const componentMeta: ComponentMeta<T> = {
component: Component as any,
parameters: { actions: { argTypesRegex: "^on.*" } },
args: {},
...meta,
};
const Template: ComponentStory<T> = (args) => {
return (
<Page>
{BaseTemplate ? <BaseTemplate {...args} /> : <Component {...args} />}
</Page>
);
};
const make = (args: Partial<ComponentProps<T>>) => {
const Basic = Template.bind({});
Basic.args = {
...Basic.args,
...args,
};
return Basic;
};
return {
make,
componentMeta,
};
};