fixed minor socket connection issues

This commit is contained in:
ari committed 2022-07-29 10:45:42 -04:00
1 parent 9a26fc46d5
commit ea86f7f763
4 files changed
+76 -10

No files matched your search

@@ -20,7 +20,7 @@ const { make, componentMeta } = makeStory({
api={useMockApiService(args.automations ?? [], !args.automations)}
haEntities={useMockHAEntities(haEntities)}
onAutomationStateChange={(eid, on) => {
if (!haEntities.loading) {
if (!haEntities.loading && !haEntities.error) {
setHAE({
...haEntities,
entities: {
@@ -49,7 +49,7 @@ const { make, componentMeta } = makeStory({
export default componentMeta;
export const Loading = make({});
export const Errored = make({
export const ErroredByAPI = make({
automations: undefined,
haProps: {
loading: false,
@@ -57,6 +57,16 @@ export const Errored = make({
entitySource: {},
},
});
export const ErroredByWS = make({
automations: [],
haProps: {
loading: false,
error: {
entities: { originalError: "1", message: "Failed to connect" },
entitySource: { originalError: "1", message: "Failed to connect" },
},
},
});
export const Empty = make({
automations: [],
haProps: {
@@ -25,6 +25,19 @@ export const AutomationManager: FC<AutomationManagerProps> = ({
const [isSaving, setIsSaving] = useState(false);
// handle bad states
if (!haEntities.ready && haEntities.error) {
return (
<div className="automation-manager error">
<Alert color="error">
Failed to connect to websocket, please check that your access token is
properly setup
</Alert>{" "}
<br />
<code>{JSON.stringify(haEntities.error)}</code>
</div>
);
}
if (!haEntities.ready || !api.state.automations.ready) {
return (
<div className="automation-manager loading">
+22 -1
View File
@@ -8,8 +8,16 @@ export type MockHAEntitiesProps =
}
| {
loading: false;
error?: false;
entities: HassEntities;
entitySource: Record<string, EntitySource>;
}
| {
loading: false;
error: {
entities: any;
entitySource: any;
};
};
export const useMockHAEntities = (args: MockHAEntitiesProps) => {
if (args.loading) {
@@ -23,7 +31,7 @@ export const useMockHAEntities = (args: MockHAEntitiesProps) => {
ready: false,
}
);
} else {
} else if (!args.error) {
return createHAEntitiesState(
{
additional: {},
@@ -36,5 +44,18 @@ export const useMockHAEntities = (args: MockHAEntitiesProps) => {
collection: args.entitySource,
}
);
} else {
return createHAEntitiesState(
{
additional: {},
ready: false,
error: args.error.entities,
},
{
additional: {},
ready: false,
error: args.error.entitySource,
}
);
}
};
+29 -7
View File
@@ -6,6 +6,7 @@ type _HACollectionState<T> =
| {
ready: false;
collection?: undefined;
error?: string;
}
| {
ready: true;
@@ -26,19 +27,23 @@ export const useHassCollection = <T, D extends {} = {}>(
if (conn.status === "loaded") {
return getHACollection(conn.connection);
}
}, [conn.status]);
}, [conn, getHACollection]);
useEffect(() => {
if (collection) {
setState({
ready: true,
collection: collection.state,
});
const unsub = collection.subscribe((d) => {
if (collection.state) {
setState({
ready: true,
collection: d,
collection: collection.state,
});
}
const unsub = collection.subscribe((d) => {
if (d) {
setState({
ready: true,
collection: d,
});
}
});
return () => {
unsub();
@@ -46,6 +51,23 @@ export const useHassCollection = <T, D extends {} = {}>(
}
}, [collection, state.ready]);
useEffect(() => {
if (conn.status === "error") {
if (!state.ready && conn.error !== state.error) {
setState({
ready: false,
error: conn.error,
});
}
}
}, [conn]);
if (state.ready) {
if (!state.collection) {
throw Error("What?");
}
}
return {
...state,
get additional(): Partial<D> {