From 51cb930b6a6b12e85e0d53553a7d4a9e8fa5d29f Mon Sep 17 00:00:00 2001 From: Its-treason <39559178+Its-treason@users.noreply.github.com> Date: Thu, 5 Oct 2023 01:57:02 +0200 Subject: [PATCH 1/2] feat(#111): Automaticly open a newly created request --- .../providers/App/useCollectionTreeSync.js | 37 +++++++++++++++++-- .../ReduxStore/slices/collections/actions.js | 7 +++- .../ReduxStore/slices/collections/index.js | 8 +++- 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/packages/bruno-app/src/providers/App/useCollectionTreeSync.js b/packages/bruno-app/src/providers/App/useCollectionTreeSync.js index caf057d5b..89fdaabb5 100644 --- a/packages/bruno-app/src/providers/App/useCollectionTreeSync.js +++ b/packages/bruno-app/src/providers/App/useCollectionTreeSync.js @@ -1,5 +1,5 @@ import { useEffect } from 'react'; -import { useDispatch } from 'react-redux'; +import { useDispatch, useSelector } from 'react-redux'; import { collectionAddDirectoryEvent, collectionAddFileEvent, @@ -17,9 +17,15 @@ import { import toast from 'react-hot-toast'; import { openCollectionEvent, collectionAddEnvFileEvent } from 'providers/ReduxStore/slices/collections/actions'; import { isElectron } from 'utils/common/platform'; +import { updateNewRequest } from 'providers/ReduxStore/slices/collections/index'; +import { addTab } from 'providers/ReduxStore/slices/tabs'; +import { getDefaultRequestPaneTab } from 'utils/collections/index'; +import { hideHomePage } from 'providers/ReduxStore/slices/app'; const useCollectionTreeSync = () => { const dispatch = useDispatch(); + const tabs = useSelector((state) => state.tabs.tabs); + const newRequestName = useSelector((state) => state.collections.newRequestName); useEffect(() => { if (!isElectron()) { @@ -50,6 +56,22 @@ const useCollectionTreeSync = () => { file: val }) ); + + // Remove the newRequestName so no random stuff happens + dispatch( + updateNewRequest({ newRequestName: null }) + ); + // When the request was just created open it in a new tab + if (newRequestName === val.data.name) { + dispatch( + addTab({ + uid: val.data.uid, + collectionUid: val.meta.collectionUid, + requestPaneTab: getDefaultRequestPaneTab(val.data) + }) + ); + dispatch(hideHomePage()); + } } if (type === 'change') { dispatch( @@ -115,8 +137,6 @@ const useCollectionTreeSync = () => { dispatch(runRequestEvent(val)); }; - ipcRenderer.invoke('renderer:ready'); - const removeListener1 = ipcRenderer.on('main:collection-opened', _openCollection); const removeListener2 = ipcRenderer.on('main:collection-tree-updated', _collectionTreeUpdated); const removeListener3 = ipcRenderer.on('main:collection-already-opened', _collectionAlreadyOpened); @@ -144,7 +164,16 @@ const useCollectionTreeSync = () => { removeListener10(); removeListener11(); }; - }, [isElectron]); + }, [isElectron, tabs, newRequestName]); + + useEffect(() => { + if (!isElectron()) { + return () => {}; + } + + const { ipcRenderer } = window; + ipcRenderer.invoke('renderer:ready'); + }, []); }; export default useCollectionTreeSync; diff --git a/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js b/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js index ccdd7fe1a..3ad39e676 100644 --- a/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js +++ b/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js @@ -39,7 +39,8 @@ import { renameCollection as _renameCollection, removeCollection as _removeCollection, sortCollections as _sortCollections, - collectionAddEnvFileEvent as _collectionAddEnvFileEvent + collectionAddEnvFileEvent as _collectionAddEnvFileEvent, + updateNewRequest } from './index'; import { closeAllCollectionTabs } from 'providers/ReduxStore/slices/tabs'; @@ -595,6 +596,8 @@ export const newHttpRequest = (params) => (dispatch, getState) => { const { ipcRenderer } = window; ipcRenderer.invoke('renderer:new-request', fullName, item).then(resolve).catch(reject); + // Add the new request name here so it can be opened in a new tab in useCollectionTreeSync.js + dispatch(updateNewRequest({ newRequestName: item.name })) } else { return reject(new Error('Duplicate request names are not allowed under the same folder')); } @@ -612,6 +615,8 @@ export const newHttpRequest = (params) => (dispatch, getState) => { const { ipcRenderer } = window; ipcRenderer.invoke('renderer:new-request', fullName, item).then(resolve).catch(reject); + // Add the new request name here so it can be opened in a new tab in useCollectionTreeSync.js + dispatch(updateNewRequest({ newRequestName: item.name })) } else { return reject(new Error('Duplicate request names are not allowed under the same folder')); } diff --git a/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js b/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js index 2cb1bdea5..d7ff34391 100644 --- a/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js +++ b/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js @@ -29,7 +29,8 @@ const PATH_SEPARATOR = path.sep; const initialState = { collections: [], - collectionSortOrder: 'default' + collectionSortOrder: 'default', + newRequestName: null }; export const collectionsSlice = createSlice({ @@ -129,6 +130,10 @@ export const collectionsSlice = createSlice({ } } }, + updateNewRequest: (state, action) => { + const { newRequestName } = action.payload; + state.newRequestName = newRequestName; + }, newItem: (state, action) => { const collection = findCollectionByUid(state.collections, action.payload.collectionUid); @@ -1200,6 +1205,7 @@ export const { collectionUnlinkEnvFileEvent, saveEnvironment, selectEnvironment, + updateNewRequest, newItem, deleteItem, renameItem, From 8a19189dcd997d72f28f8d63d04eba493f46783d Mon Sep 17 00:00:00 2001 From: Its-treason <39559178+Its-treason@users.noreply.github.com> Date: Thu, 5 Oct 2023 17:43:17 +0200 Subject: [PATCH 2/2] feat(#111): Use existing last actions for auto open tabs --- .../providers/App/useCollectionTreeSync.js | 37 ++++++++++--------- .../ReduxStore/slices/collections/actions.js | 4 +- .../ReduxStore/slices/collections/index.js | 7 +--- 3 files changed, 23 insertions(+), 25 deletions(-) diff --git a/packages/bruno-app/src/providers/App/useCollectionTreeSync.js b/packages/bruno-app/src/providers/App/useCollectionTreeSync.js index 89fdaabb5..ba7193f31 100644 --- a/packages/bruno-app/src/providers/App/useCollectionTreeSync.js +++ b/packages/bruno-app/src/providers/App/useCollectionTreeSync.js @@ -17,15 +17,15 @@ import { import toast from 'react-hot-toast'; import { openCollectionEvent, collectionAddEnvFileEvent } from 'providers/ReduxStore/slices/collections/actions'; import { isElectron } from 'utils/common/platform'; -import { updateNewRequest } from 'providers/ReduxStore/slices/collections/index'; import { addTab } from 'providers/ReduxStore/slices/tabs'; -import { getDefaultRequestPaneTab } from 'utils/collections/index'; +import { findCollectionByUid, getDefaultRequestPaneTab } from 'utils/collections/index'; import { hideHomePage } from 'providers/ReduxStore/slices/app'; +import { updateLastAction } from 'providers/ReduxStore/slices/collections/index'; const useCollectionTreeSync = () => { const dispatch = useDispatch(); const tabs = useSelector((state) => state.tabs.tabs); - const newRequestName = useSelector((state) => state.collections.newRequestName); + const collections = useSelector((state) => state.collections.collections); useEffect(() => { if (!isElectron()) { @@ -57,20 +57,23 @@ const useCollectionTreeSync = () => { }) ); - // Remove the newRequestName so no random stuff happens - dispatch( - updateNewRequest({ newRequestName: null }) - ); + const collectionUid = val.meta.collectionUid; + const lastAction = findCollectionByUid(collections, collectionUid)?.lastAction; + // When the request was just created open it in a new tab - if (newRequestName === val.data.name) { - dispatch( - addTab({ - uid: val.data.uid, - collectionUid: val.meta.collectionUid, - requestPaneTab: getDefaultRequestPaneTab(val.data) - }) - ); - dispatch(hideHomePage()); + if (lastAction && lastAction.type === 'ADD_REQUEST') { + dispatch(updateLastAction({ lastAction: null, collectionUid })); + + if (lastAction.payload === val.data.name) { + dispatch( + addTab({ + uid: val.data.uid, + collectionUid: collectionUid, + requestPaneTab: getDefaultRequestPaneTab(val.data) + }) + ); + dispatch(hideHomePage()); + } } } if (type === 'change') { @@ -164,7 +167,7 @@ const useCollectionTreeSync = () => { removeListener10(); removeListener11(); }; - }, [isElectron, tabs, newRequestName]); + }, [isElectron, tabs, collections]); useEffect(() => { if (!isElectron()) { diff --git a/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js b/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js index 3ad39e676..eecb75263 100644 --- a/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js +++ b/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js @@ -597,7 +597,7 @@ export const newHttpRequest = (params) => (dispatch, getState) => { ipcRenderer.invoke('renderer:new-request', fullName, item).then(resolve).catch(reject); // Add the new request name here so it can be opened in a new tab in useCollectionTreeSync.js - dispatch(updateNewRequest({ newRequestName: item.name })) + dispatch(updateLastAction({ lastAction: { type: 'ADD_REQUEST', payload: item.name }, collectionUid })); } else { return reject(new Error('Duplicate request names are not allowed under the same folder')); } @@ -616,7 +616,7 @@ export const newHttpRequest = (params) => (dispatch, getState) => { ipcRenderer.invoke('renderer:new-request', fullName, item).then(resolve).catch(reject); // Add the new request name here so it can be opened in a new tab in useCollectionTreeSync.js - dispatch(updateNewRequest({ newRequestName: item.name })) + dispatch(updateLastAction({ lastAction: { type: 'ADD_REQUEST', payload: item.name }, collectionUid })); } else { return reject(new Error('Duplicate request names are not allowed under the same folder')); } diff --git a/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js b/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js index d7ff34391..ec89bb85d 100644 --- a/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js +++ b/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js @@ -29,8 +29,7 @@ const PATH_SEPARATOR = path.sep; const initialState = { collections: [], - collectionSortOrder: 'default', - newRequestName: null + collectionSortOrder: 'default' }; export const collectionsSlice = createSlice({ @@ -130,10 +129,6 @@ export const collectionsSlice = createSlice({ } } }, - updateNewRequest: (state, action) => { - const { newRequestName } = action.payload; - state.newRequestName = newRequestName; - }, newItem: (state, action) => { const collection = findCollectionByUid(state.collections, action.payload.collectionUid);