From cd12a5990a13d43d1e199f718177f0aed319c7bb Mon Sep 17 00:00:00 2001 From: Csaba Tuncsik Date: Thu, 5 Oct 2023 15:49:59 +0200 Subject: [PATCH] feat(editor): Workflow history [WIP]- Improve switching between workflow history and editor (no-changelog) (#7353) --- .../components/MainHeader/WorkflowDetails.vue | 17 ++++++++++- .../utils/workflowHistoryTestUtils.ts | 11 ++++---- .../__tests__/workflowHistory.store.test.ts | 27 ++---------------- .../src/stores/workflowHistory.store.ts | 20 +------------ .../editor-ui/src/types/workflowHistory.ts | 10 ++++--- .../editor-ui/src/views/WorkflowHistory.vue | 28 +++++++++---------- .../views/__tests__/WorkflowHistory.test.ts | 6 ++-- 7 files changed, 48 insertions(+), 71 deletions(-) diff --git a/packages/editor-ui/src/components/MainHeader/WorkflowDetails.vue b/packages/editor-ui/src/components/MainHeader/WorkflowDetails.vue index f746cd62c..3852d65c2 100644 --- a/packages/editor-ui/src/components/MainHeader/WorkflowDetails.vue +++ b/packages/editor-ui/src/components/MainHeader/WorkflowDetails.vue @@ -106,7 +106,13 @@ :to="workflowHistoryRoute" :class="$style.workflowHistoryButton" > - +
diff --git a/packages/editor-ui/src/stores/__tests__/utils/workflowHistoryTestUtils.ts b/packages/editor-ui/src/stores/__tests__/utils/workflowHistoryTestUtils.ts index b4e8a7289..7e62e20cb 100644 --- a/packages/editor-ui/src/stores/__tests__/utils/workflowHistoryTestUtils.ts +++ b/packages/editor-ui/src/stores/__tests__/utils/workflowHistoryTestUtils.ts @@ -1,17 +1,18 @@ import { faker } from '@faker-js/faker'; -import type { WorkflowHistory } from '@/types/workflowHistory'; +import type { WorkflowHistory, WorkflowVersion } from '@/types/workflowHistory'; export const workflowHistoryDataFactory: () => WorkflowHistory = () => ({ versionId: faker.string.nanoid(), createdAt: faker.date.past().toDateString(), + updatedAt: faker.date.past().toDateString(), authors: Array.from({ length: faker.number.int({ min: 2, max: 5 }) }, faker.person.fullName).join( ', ', ), }); -export const workflowVersionDataFactory: () => WorkflowHistory = () => ({ +export const workflowVersionDataFactory: () => WorkflowVersion = () => ({ ...workflowHistoryDataFactory(), - workflow: { - name: faker.lorem.words(3), - }, + workflowId: faker.string.nanoid(), + connections: {}, + nodes: [], }); diff --git a/packages/editor-ui/src/stores/__tests__/workflowHistory.store.test.ts b/packages/editor-ui/src/stores/__tests__/workflowHistory.store.test.ts index 9386f2f03..e4b254f47 100644 --- a/packages/editor-ui/src/stores/__tests__/workflowHistory.store.test.ts +++ b/packages/editor-ui/src/stores/__tests__/workflowHistory.store.test.ts @@ -1,36 +1,13 @@ import { createPinia, setActivePinia } from 'pinia'; +import type { IN8nUISettings } from 'n8n-workflow'; import { useWorkflowHistoryStore } from '@/stores/workflowHistory.store'; import { useSettingsStore } from '@/stores/settings.store'; -import { - workflowHistoryDataFactory, - workflowVersionDataFactory, -} from '@/stores/__tests__/utils/workflowHistoryTestUtils'; - -const historyData = Array.from({ length: 5 }, workflowHistoryDataFactory); -const versionData = { - ...workflowVersionDataFactory(), - ...historyData[0], -}; describe('Workflow history store', () => { beforeEach(() => { setActivePinia(createPinia()); }); - it('should reset data', () => { - const workflowHistoryStore = useWorkflowHistoryStore(); - - workflowHistoryStore.addWorkflowHistory(historyData); - workflowHistoryStore.setActiveWorkflowVersion(versionData); - - expect(workflowHistoryStore.workflowHistory).toEqual(historyData); - expect(workflowHistoryStore.activeWorkflowVersion).toEqual(versionData); - - workflowHistoryStore.reset(); - expect(workflowHistoryStore.workflowHistory).toEqual([]); - expect(workflowHistoryStore.activeWorkflowVersion).toEqual(null); - }); - test.each([ [true, 1, 1], [true, 2, 2], @@ -49,7 +26,7 @@ describe('Workflow history store', () => { pruneTime, licensePruneTime, }, - }; + } as IN8nUISettings; expect(workflowHistoryStore.shouldUpgrade).toBe(shouldUpgrade); }, diff --git a/packages/editor-ui/src/stores/workflowHistory.store.ts b/packages/editor-ui/src/stores/workflowHistory.store.ts index 079f2d86f..6de3eb352 100644 --- a/packages/editor-ui/src/stores/workflowHistory.store.ts +++ b/packages/editor-ui/src/stores/workflowHistory.store.ts @@ -1,4 +1,4 @@ -import { ref, computed } from 'vue'; +import { computed } from 'vue'; import { defineStore } from 'pinia'; import * as whApi from '@/api/workflowHistory'; import { useRootStore } from '@/stores/n8nRoot.store'; @@ -13,8 +13,6 @@ export const useWorkflowHistoryStore = defineStore('workflowHistory', () => { const rootStore = useRootStore(); const settingsStore = useSettingsStore(); - const workflowHistory = ref([]); - const activeWorkflowVersion = ref(null); const licensePruneTime = computed(() => settingsStore.settings.workflowHistory.licensePruneTime); const pruneTime = computed(() => settingsStore.settings.workflowHistory.pruneTime); const evaluatedPruneTime = computed(() => Math.min(pruneTime.value, licensePruneTime.value)); @@ -22,11 +20,6 @@ export const useWorkflowHistoryStore = defineStore('workflowHistory', () => { () => licensePruneTime.value !== -1 && licensePruneTime.value === pruneTime.value, ); - const reset = () => { - workflowHistory.value = []; - activeWorkflowVersion.value = null; - }; - const getWorkflowHistory = async ( workflowId: string, queryParams: WorkflowHistoryRequestParams, @@ -37,9 +30,6 @@ export const useWorkflowHistoryStore = defineStore('workflowHistory', () => { console.error(error); return [] as WorkflowHistory[]; }); - const addWorkflowHistory = (history: WorkflowHistory[]) => { - workflowHistory.value = workflowHistory.value.concat(history); - }; const getWorkflowVersion = async ( workflowId: string, @@ -49,18 +39,10 @@ export const useWorkflowHistoryStore = defineStore('workflowHistory', () => { console.error(error); return null; }); - const setActiveWorkflowVersion = (version: WorkflowVersion | null) => { - activeWorkflowVersion.value = version; - }; return { - reset, getWorkflowHistory, - addWorkflowHistory, getWorkflowVersion, - setActiveWorkflowVersion, - workflowHistory, - activeWorkflowVersion, evaluatedPruneTime, shouldUpgrade, }; diff --git a/packages/editor-ui/src/types/workflowHistory.ts b/packages/editor-ui/src/types/workflowHistory.ts index ce4d752ff..0c6698715 100644 --- a/packages/editor-ui/src/types/workflowHistory.ts +++ b/packages/editor-ui/src/types/workflowHistory.ts @@ -1,17 +1,19 @@ -import type { IWorkflowDb } from '@/Interface'; +import type { IConnections } from 'n8n-workflow'; +import type { INodeUi } from '@/Interface'; export type WorkflowHistory = { versionId: string; authors: string; createdAt: string; + updatedAt: string; }; export type WorkflowVersionId = WorkflowHistory['versionId']; export type WorkflowVersion = WorkflowHistory & { - nodes: IWorkflowDb['nodes']; - connection: IWorkflowDb['connections']; - workflow: IWorkflowDb; + workflowId: string; + nodes: INodeUi[]; + connections: IConnections; }; export type WorkflowHistoryActionTypes = ['restore', 'clone', 'open', 'download']; diff --git a/packages/editor-ui/src/views/WorkflowHistory.vue b/packages/editor-ui/src/views/WorkflowHistory.vue index d7198d461..4d303ed6f 100644 --- a/packages/editor-ui/src/views/WorkflowHistory.vue +++ b/packages/editor-ui/src/views/WorkflowHistory.vue @@ -1,6 +1,6 @@ @@ -187,9 +187,9 @@ watchEffect(async () => {
{ const params = {}; @@ -34,7 +35,7 @@ vi.mock('vue-router', () => { const workflowId = faker.string.nanoid(); const historyData = Array.from({ length: 5 }, workflowHistoryDataFactory); -const versionData = { +const versionData: WorkflowVersion = { ...workflowVersionDataFactory(), ...historyData[0], }; @@ -76,8 +77,7 @@ describe('WorkflowHistory', () => { router = useRouter(); vi.spyOn(workflowHistoryStore, 'getWorkflowHistory').mockResolvedValue(historyData); - vi.spyOn(workflowHistoryStore, 'workflowHistory', 'get').mockReturnValue(historyData); - vi.spyOn(workflowHistoryStore, 'activeWorkflowVersion', 'get').mockReturnValue(versionData); + vi.spyOn(workflowHistoryStore, 'getWorkflowVersion').mockResolvedValue(versionData); windowOpenSpy = vi.spyOn(window, 'open').mockImplementation(() => null); });