templatized storymaking
This commit is contained in:
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 ".";
|
||||
|
||||
export default {
|
||||
title: "App/AutomationList/ListBoxGroup",
|
||||
component: ListBoxGroup,
|
||||
parameters: { actions: { argTypesRegex: "^on.*" } },
|
||||
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",
|
||||
},
|
||||
},
|
||||
],
|
||||
import { makeStory } from "devUtils";
|
||||
import { useTagDB } from "../TagDB";
|
||||
|
||||
const { make, componentMeta } = makeStory({
|
||||
Component: ListBoxGroup,
|
||||
BaseTemplate: (args) => {
|
||||
const tagsDB = useTagDB([], () => {});
|
||||
return <ListBoxGroup {...args} tagsDB={tagsDB} />;
|
||||
},
|
||||
} 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) => {
|
||||
return (
|
||||
<Page>
|
||||
<ListBoxGroup {...args} />
|
||||
</Page>
|
||||
);
|
||||
};
|
||||
export default componentMeta;
|
||||
|
||||
export const Basic = Template.bind({});
|
||||
export const Nested = Template.bind({});
|
||||
Nested.args = {
|
||||
export const Basic = make({});
|
||||
export const Nested = make({
|
||||
title: "Top Group",
|
||||
type: "group",
|
||||
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 <></>;
|
||||
};
|
||||
File renamed without changes.
@@ -1,59 +1,44 @@
|
||||
import "styles/root.css";
|
||||
import { ComponentMeta, ComponentStory } from "@storybook/react";
|
||||
import { Page } from "components/Page";
|
||||
import { MetadataBox } from ".";
|
||||
import { useTagDB } from "../TagDB";
|
||||
import { useState } from "react";
|
||||
|
||||
export default {
|
||||
title: "App/AutomationList/MetadataBox",
|
||||
component: MetadataBox,
|
||||
parameters: { actions: { argTypesRegex: "^on.*" } },
|
||||
args: {
|
||||
id: "dummy",
|
||||
title: "Example",
|
||||
description: "Cool!",
|
||||
state: "on",
|
||||
isSelected: false,
|
||||
tags: {
|
||||
room: "main bedroom",
|
||||
routine: "night time",
|
||||
import { makeStory } from "devUtils";
|
||||
|
||||
const { make, componentMeta } = makeStory({
|
||||
Component: MetadataBox,
|
||||
meta: {
|
||||
title: "App/AutomationManager/MetadataBox",
|
||||
args: {
|
||||
id: "dummy",
|
||||
title: "Example",
|
||||
description: "Cool!",
|
||||
state: "on",
|
||||
isSelected: false,
|
||||
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) => {
|
||||
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 default componentMeta;
|
||||
|
||||
export const Basic = Template.bind({});
|
||||
export const IsSelected = Template.bind({});
|
||||
IsSelected.args = {
|
||||
...IsSelected.args,
|
||||
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",
|
||||
};
|
||||
export const Basic = make({});
|
||||
export const IsSelected = make({ isSelected: true });
|
||||
export const InvalidState = make({ state: "unavailable" });
|
||||
export const WithIssue = make({ 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 { useTagDB } from "../TagDB";
|
||||
import { useState } from "react";
|
||||
import { AutomationListAuto } from "../types";
|
||||
|
||||
export default {
|
||||
title: "App/AutomationList/Sidebar",
|
||||
component: AutomationListSidebar,
|
||||
parameters: { actions: { argTypesRegex: "^on.*" } },
|
||||
args: {},
|
||||
} as ComponentMeta<typeof AutomationListSidebar>;
|
||||
import { makeStory } from "devUtils";
|
||||
|
||||
const Template: ComponentStory<typeof AutomationListSidebar> = (args) => {
|
||||
const [automations, setAutomations] = useState(args.automations);
|
||||
const [selectedAutomationId, setSelAutoId] = useState(
|
||||
args.selectedAutomationId
|
||||
);
|
||||
const onTagUpdate = (tags: Record<string, string>, aid: string) => {
|
||||
const index = automations.findIndex(({ id }) => id === aid);
|
||||
if (index >= 0) {
|
||||
setAutomations([
|
||||
...automations.slice(0, index),
|
||||
{
|
||||
...automations[index],
|
||||
tags,
|
||||
},
|
||||
...automations.slice(index + 1),
|
||||
]);
|
||||
}
|
||||
};
|
||||
return (
|
||||
<Page>
|
||||
const story = makeStory({
|
||||
meta: {
|
||||
title: "App/AutomationManager/Sidebar",
|
||||
},
|
||||
Component: AutomationListSidebar,
|
||||
BaseTemplate: (args) => {
|
||||
const [automations, setAutomations] = useState(args.automations);
|
||||
const [selectedAutomationId, setSelAutoId] = useState(
|
||||
args.selectedAutomationId
|
||||
);
|
||||
const onTagUpdate = (tags: Record<string, string>, aid: string) => {
|
||||
const index = automations.findIndex(({ id }) => id === aid);
|
||||
if (index >= 0) {
|
||||
setAutomations([
|
||||
...automations.slice(0, index),
|
||||
{
|
||||
...automations[index],
|
||||
tags,
|
||||
},
|
||||
...automations.slice(index + 1),
|
||||
]);
|
||||
}
|
||||
};
|
||||
return (
|
||||
<AutomationListSidebar
|
||||
selectedAutomationId={selectedAutomationId}
|
||||
tagsDB={useTagDB(automations, (a, t) => onTagUpdate(t, a))}
|
||||
@@ -76,18 +72,16 @@ const Template: ComponentStory<typeof AutomationListSidebar> = (args) => {
|
||||
setSelAutoId(id);
|
||||
}}
|
||||
/>
|
||||
</Page>
|
||||
);
|
||||
};
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
const make = (automations: AutomationListAuto[]) => {
|
||||
const Basic = Template.bind({});
|
||||
Basic.args = {
|
||||
...Basic.args,
|
||||
export default story.componentMeta;
|
||||
|
||||
const make = (automations: AutomationListAuto[]) =>
|
||||
story.make({
|
||||
automations,
|
||||
};
|
||||
return Basic;
|
||||
};
|
||||
});
|
||||
const makeAuto = (data: Partial<AutomationListAuto>): AutomationListAuto => {
|
||||
const id = `${Date.now()}_${Math.random().toString(26).slice(3, 6)}`;
|
||||
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 { FC } from "react";
|
||||
import { AutomationData } from "types/automations";
|
||||
import { consolidateAutomations } from "./helpers";
|
||||
import { AutomationListSidebar } from "./Sidebar";
|
||||
import { useAutomationManagerState } from "./state";
|
||||
import { useTagDB } from "./TagDB";
|
||||
import { FC, useState } from "react";
|
||||
import { AutomationManagerLoaded } from "./Loaded";
|
||||
import LinearProgress from "@mui/material/LinearProgress";
|
||||
|
||||
export type AutomationListProps = {
|
||||
export type AutomationManagerProps = {
|
||||
haEntites: HAEntitiesState;
|
||||
configAutomations: ListData<AutomationData>;
|
||||
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;
|
||||
api: ApiService;
|
||||
};
|
||||
|
||||
export const AutomationList: FC<AutomationListProps> = (props) => {
|
||||
const state = useAutomationManagerState();
|
||||
export const AutomationManager: FC<AutomationManagerProps> = ({
|
||||
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>;
|
||||
}
|
||||
|
||||
const tagsDB = useTagDB(
|
||||
props.configAutomations.data.map(({ metadata: { id }, tags }) => ({
|
||||
id,
|
||||
tags,
|
||||
})),
|
||||
props.onUpdateTags
|
||||
);
|
||||
const automations = consolidateAutomations(
|
||||
props.haEntites.collection.state,
|
||||
props.configAutomations.data.map(({ metadata }) => metadata),
|
||||
tagsDB
|
||||
);
|
||||
if (!api.state.automations.ok) {
|
||||
return (
|
||||
<div className="automation-manager error">
|
||||
<h1>Failed to load</h1>
|
||||
{api.state.automations.error}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// alias
|
||||
const configAutomations = api.state.automations.data.data;
|
||||
const hassEntities = haEntites.collection.state;
|
||||
return (
|
||||
<div className="automation-manager">
|
||||
{/* <AutomationListSidebar
|
||||
automations={automations}
|
||||
tagsDB={tagsDB}
|
||||
selectedAutomationId={state.current}
|
||||
onTagUpdate
|
||||
/> */}
|
||||
{isSaving && <LinearProgress />}
|
||||
<AutomationManagerLoaded
|
||||
configAutomations={configAutomations}
|
||||
hassEntities={hassEntities}
|
||||
onUpdateTags={async (aid, t) => {
|
||||
setIsSaving(true);
|
||||
await api.updateTags({
|
||||
automation_id: aid,
|
||||
tags: t,
|
||||
});
|
||||
setIsSaving(false);
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -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({});
|
||||
@@ -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,
|
||||
};
|
||||
};
|
||||
Reference in new issue
Block a user