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)} api={useMockApiService(args.automations ?? [], !args.automations)}
haEntities={useMockHAEntities(haEntities)} haEntities={useMockHAEntities(haEntities)}
onAutomationStateChange={(eid, on) => { onAutomationStateChange={(eid, on) => {
if (!haEntities.loading) { if (!haEntities.loading && !haEntities.error) {
setHAE({ setHAE({
...haEntities, ...haEntities,
entities: { entities: {
@@ -49,7 +49,7 @@ const { make, componentMeta } = makeStory({
export default componentMeta; export default componentMeta;
export const Loading = make({}); export const Loading = make({});
export const Errored = make({ export const ErroredByAPI = make({
automations: undefined, automations: undefined,
haProps: { haProps: {
loading: false, loading: false,
@@ -57,6 +57,16 @@ export const Errored = make({
entitySource: {}, 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({ export const Empty = make({
automations: [], automations: [],
haProps: { haProps: {
@@ -25,6 +25,19 @@ export const AutomationManager: FC<AutomationManagerProps> = ({
const [isSaving, setIsSaving] = useState(false); const [isSaving, setIsSaving] = useState(false);
// handle bad states // 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) { if (!haEntities.ready || !api.state.automations.ready) {
return ( return (
<div className="automation-manager loading"> <div className="automation-manager loading">
+22 -1
View File
@@ -8,8 +8,16 @@ export type MockHAEntitiesProps =
} }
| { | {
loading: false; loading: false;
error?: false;
entities: HassEntities; entities: HassEntities;
entitySource: Record<string, EntitySource>; entitySource: Record<string, EntitySource>;
}
| {
loading: false;
error: {
entities: any;
entitySource: any;
};
}; };
export const useMockHAEntities = (args: MockHAEntitiesProps) => { export const useMockHAEntities = (args: MockHAEntitiesProps) => {
if (args.loading) { if (args.loading) {
@@ -23,7 +31,7 @@ export const useMockHAEntities = (args: MockHAEntitiesProps) => {
ready: false, ready: false,
} }
); );
} else { } else if (!args.error) {
return createHAEntitiesState( return createHAEntitiesState(
{ {
additional: {}, additional: {},
@@ -36,5 +44,18 @@ export const useMockHAEntities = (args: MockHAEntitiesProps) => {
collection: args.entitySource, 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; ready: false;
collection?: undefined; collection?: undefined;
error?: string;
} }
| { | {
ready: true; ready: true;
@@ -26,19 +27,23 @@ export const useHassCollection = <T, D extends {} = {}>(
if (conn.status === "loaded") { if (conn.status === "loaded") {
return getHACollection(conn.connection); return getHACollection(conn.connection);
} }
}, [conn.status]); }, [conn, getHACollection]);
useEffect(() => { useEffect(() => {
if (collection) { if (collection) {
setState({ if (collection.state) {
ready: true,
collection: collection.state,
});
const unsub = collection.subscribe((d) => {
setState({ setState({
ready: true, ready: true,
collection: d, collection: collection.state,
}); });
}
const unsub = collection.subscribe((d) => {
if (d) {
setState({
ready: true,
collection: d,
});
}
}); });
return () => { return () => {
unsub(); unsub();
@@ -46,6 +51,23 @@ export const useHassCollection = <T, D extends {} = {}>(
} }
}, [collection, state.ready]); }, [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 { return {
...state, ...state,
get additional(): Partial<D> { get additional(): Partial<D> {