Skip to content

Commit

Permalink
Do not show connection error before it has been requested
Browse files Browse the repository at this point in the history
  • Loading branch information
tomi committed Feb 6, 2025
1 parent 9970a9c commit 0d3b8a0
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
17 changes: 15 additions & 2 deletions packages/editor-ui/src/components/PushConnectionTracker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import { createTestingPinia } from '@pinia/testing';
import { setActivePinia } from 'pinia';

let isConnected = true;
let isConnectionRequested = true;

vi.mock('@/stores/pushConnection.store', () => {
return {
usePushConnectionStore: vi.fn(() => ({
isConnected,
isConnectionRequested,
})),
};
});
Expand All @@ -21,6 +23,7 @@ describe('PushConnectionTracker', () => {
initialState: {
[STORES.PUSH]: {
isConnected,
isConnectionRequested,
},
},
});
Expand All @@ -29,15 +32,25 @@ describe('PushConnectionTracker', () => {
return createComponentRenderer(PushConnectionTracker)();
};

it('should render when connected', () => {
it('should not render error when connected and connection requested', () => {
isConnected = true;
isConnectionRequested = true;
const { container } = render();

expect(container).toMatchSnapshot();
});

it('should render when disconnected', () => {
it('should render error when disconnected and connection requested', () => {
isConnected = false;
isConnectionRequested = true;
const { container } = render();

expect(container).toMatchSnapshot();
});

it('should not render error when connected and connection not requested', () => {
isConnected = true;
isConnectionRequested = false;
const { container } = render();

expect(container).toMatchSnapshot();
Expand Down
11 changes: 10 additions & 1 deletion packages/editor-ui/src/components/PushConnectionTracker.vue
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
<script setup lang="ts">
import { usePushConnectionStore } from '@/stores/pushConnection.store';
import { useI18n } from '@/composables/useI18n';
import { computed } from 'vue';
const pushConnectionStore = usePushConnectionStore();
const i18n = useI18n();
const showConnectionLostError = computed(() => {
// Only show the connection lost error if the connection has been requested
// and the connection is not currently connected. This is to prevent the
// connection error from being shown e.g. when user navigates directly to
// the workflow executions list, which doesn't open the connection.
return pushConnectionStore.isConnectionRequested && !pushConnectionStore.isConnected;
});
</script>

<template>
<span>
<div v-if="!pushConnectionStore.isConnected" class="push-connection-lost primary-color">
<div v-if="showConnectionLostError" class="push-connection-lost primary-color">
<n8n-tooltip placement="bottom-end">
<template #content>
<div v-n8n-html="i18n.baseText('pushConnectionTracker.cannotConnectToServer')"></div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`PushConnectionTracker > should render when connected 1`] = `
exports[`PushConnectionTracker > should not render error when connected and connection not requested 1`] = `
<div>
<span>
Expand All @@ -9,7 +9,16 @@ exports[`PushConnectionTracker > should render when connected 1`] = `
</div>
`;

exports[`PushConnectionTracker > should render when disconnected 1`] = `
exports[`PushConnectionTracker > should not render error when connected and connection requested 1`] = `
<div>
<span>
</span>
</div>
`;

exports[`PushConnectionTracker > should render error when disconnected and connection requested 1`] = `
<div>
<span>
<div
Expand Down
18 changes: 16 additions & 2 deletions packages/editor-ui/src/stores/pushConnection.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export const usePushConnectionStore = defineStore(STORES.PUSH, () => {
*/
const outgoingQueue = ref<unknown[]>([]);

/** Whether the connection has been requested */
const isConnectionRequested = ref(false);

const onMessageReceivedHandlers = ref<OnPushMessageHandler[]>([]);

const addEventListener = (handler: OnPushMessageHandler) => {
Expand Down Expand Up @@ -77,6 +80,16 @@ export const usePushConnectionStore = defineStore(STORES.PUSH, () => {
client.sendMessage(JSON.stringify(message));
}

const pushConnect = () => {
isConnectionRequested.value = true;
client.connect();
};

const pushDisconnect = () => {
isConnectionRequested.value = false;
client.disconnect();
};

watch(client.isConnected, (didConnect) => {
if (!didConnect) {
return;
Expand All @@ -100,10 +113,11 @@ export const usePushConnectionStore = defineStore(STORES.PUSH, () => {

return {
isConnected,
isConnectionRequested,
onMessageReceivedHandlers,
addEventListener,
pushConnect: client.connect,
pushDisconnect: client.disconnect,
pushConnect,
pushDisconnect,
send: serializeAndSend,
clearQueue,
};
Expand Down

0 comments on commit 0d3b8a0

Please sign in to comment.