mirror of
https://github.com/usebruno/bruno.git
synced 2026-06-25 13:45:52 +00:00
Merge pull request #373 from Its-treason/feature/auto-open-new-request
feat(#111): Automaticly open a newly created request
This commit is contained in:
@@ -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 { addTab } from 'providers/ReduxStore/slices/tabs';
|
||||
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 collections = useSelector((state) => state.collections.collections);
|
||||
|
||||
useEffect(() => {
|
||||
if (!isElectron()) {
|
||||
@@ -50,6 +56,25 @@ const useCollectionTreeSync = () => {
|
||||
file: val
|
||||
})
|
||||
);
|
||||
|
||||
const collectionUid = val.meta.collectionUid;
|
||||
const lastAction = findCollectionByUid(collections, collectionUid)?.lastAction;
|
||||
|
||||
// When the request was just created open it in a new tab
|
||||
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') {
|
||||
dispatch(
|
||||
@@ -115,8 +140,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 +167,16 @@ const useCollectionTreeSync = () => {
|
||||
removeListener10();
|
||||
removeListener11();
|
||||
};
|
||||
}, [isElectron]);
|
||||
}, [isElectron, tabs, collections]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!isElectron()) {
|
||||
return () => {};
|
||||
}
|
||||
|
||||
const { ipcRenderer } = window;
|
||||
ipcRenderer.invoke('renderer:ready');
|
||||
}, []);
|
||||
};
|
||||
|
||||
export default useCollectionTreeSync;
|
||||
|
||||
@@ -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(updateLastAction({ lastAction: { type: 'ADD_REQUEST', payload: item.name }, collectionUid }));
|
||||
} 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(updateLastAction({ lastAction: { type: 'ADD_REQUEST', payload: item.name }, collectionUid }));
|
||||
} else {
|
||||
return reject(new Error('Duplicate request names are not allowed under the same folder'));
|
||||
}
|
||||
|
||||
@@ -1200,6 +1200,7 @@ export const {
|
||||
collectionUnlinkEnvFileEvent,
|
||||
saveEnvironment,
|
||||
selectEnvironment,
|
||||
updateNewRequest,
|
||||
newItem,
|
||||
deleteItem,
|
||||
renameItem,
|
||||
|
||||
Reference in New Issue
Block a user